Bug 1029097 - ViewHelpers.Prefs should have a way of clearing the cached preferences, r=rcampbell

This commit is contained in:
Victor Porof 2014-06-24 21:45:47 -04:00
Родитель 31c474a34c
Коммит 285f9c80c1
4 изменённых файлов: 53 добавлений и 8 удалений

Просмотреть файл

@ -35,7 +35,7 @@ function test() {
function testPrefs() {
let { Prefs } = aMonitor.panelWin;
is(Prefs.root, "devtools.netmonitor",
is(Prefs._root, "devtools.netmonitor",
"The preferences object should have a correct root path.");
is(Prefs.networkDetailsWidth,

Просмотреть файл

@ -31,6 +31,7 @@ support-files =
[browser_layoutHelpers-getBoxQuads.js]
[browser_observableobject.js]
[browser_outputparser.js]
[browser_prefs.js]
[browser_require_basic.js]
[browser_spectrum.js]
[browser_tableWidget_basic.js]

Просмотреть файл

@ -0,0 +1,33 @@
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
// Tests that ViewHelpers.Prefs work properly.
let {ViewHelpers} = Cu.import("resource:///modules/devtools/ViewHelpers.jsm", {});
function test() {
let Prefs = new ViewHelpers.Prefs("devtools.debugger", {
"foo": ["Bool", "enabled"]
});
let originalPrefValue = Services.prefs.getBoolPref("devtools.debugger.enabled");
is(Prefs.foo, originalPrefValue, "The pref value was correctly fetched.");
Prefs.foo = !originalPrefValue;
is(Prefs.foo, !originalPrefValue,
"The pref was was correctly changed (1).");
is(Services.prefs.getBoolPref("devtools.debugger.enabled"), !originalPrefValue,
"The pref was was correctly changed (2).");
Services.prefs.setBoolPref("devtools.debugger.enabled", originalPrefValue);
info("The pref value was reset.");
is(Prefs.foo, !originalPrefValue,
"The cached pref value hasn't changed yet.");
Prefs.refresh();
is(Prefs.foo, originalPrefValue,
"The cached pref value has changed now.");
finish();
}

Просмотреть файл

@ -394,7 +394,8 @@ ViewHelpers.L10N.prototype = {
* An object containing { accessorName: [prefType, prefName] } keys.
*/
ViewHelpers.Prefs = function(aPrefsRoot = "", aPrefsObject = {}) {
this.root = aPrefsRoot;
this._root = aPrefsRoot;
this._cache = new Map();
for (let accessorName in aPrefsObject) {
let [prefType, prefName] = aPrefsObject[accessorName];
@ -411,10 +412,13 @@ ViewHelpers.Prefs.prototype = {
* @return any
*/
_get: function(aType, aPrefName) {
if (this[aPrefName] === undefined) {
this[aPrefName] = Services.prefs["get" + aType + "Pref"](aPrefName);
let cachedPref = this._cache.get(aPrefName);
if (cachedPref !== undefined) {
return cachedPref;
}
return this[aPrefName];
let value = Services.prefs["get" + aType + "Pref"](aPrefName);
this._cache.set(aPrefName, value);
return value;
},
/**
@ -426,7 +430,7 @@ ViewHelpers.Prefs.prototype = {
*/
_set: function(aType, aPrefName, aValue) {
Services.prefs["set" + aType + "Pref"](aPrefName, aValue);
this[aPrefName] = aValue;
this._cache.set(aPrefName, aValue);
},
/**
@ -446,9 +450,16 @@ ViewHelpers.Prefs.prototype = {
}
Object.defineProperty(this, aAccessorName, {
get: () => aSerializer.in(this._get(aType, [this.root, aPrefName].join("."))),
set: (e) => this._set(aType, [this.root, aPrefName].join("."), aSerializer.out(e))
get: () => aSerializer.in(this._get(aType, [this._root, aPrefName].join("."))),
set: (e) => this._set(aType, [this._root, aPrefName].join("."), aSerializer.out(e))
});
},
/**
* Clears all the cached preferences' values.
*/
refresh: function() {
this._cache.clear();
}
};