Bug 1075160 - Support action: reset a pref to the default setting r=gfritzsche, r=bholley

This commit is contained in:
André Reinald 2015-04-16 17:00:14 +02:00
parent 820a3124ba
commit fd83067a9c
5 changed files with 70 additions and 0 deletions

View File

@ -7,6 +7,7 @@
const {classes: Cc, interfaces: Ci, utils: Cu} = Components;
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource://gre/modules/Services.jsm");
const policy = Cc["@mozilla.org/datareporting/service;1"]
.getService(Ci.nsISupports)
@ -63,6 +64,10 @@ MozSelfSupportInterface.prototype = {
}
}.bind(this));
},
resetPref: function(name) {
Services.prefs.clearUserPref(name);
},
}
this.NSGetFactory = XPCOMUtils.generateNSGetFactory([MozSelfSupportInterface]);

View File

@ -8,3 +8,7 @@ EXTRA_COMPONENTS += [
'SelfSupportService.js',
'SelfSupportService.manifest',
]
BROWSER_CHROME_MANIFESTS += [
'test/browser.ini',
]

View File

@ -0,0 +1,3 @@
[DEFAULT]
[browser_selfsupportAPI.js]

View File

@ -0,0 +1,47 @@
Cu.import("resource://gre/modules/Preferences.jsm");
function test() {
const prefNewName = "browser.newpref.fake";
Assert.ok(!Preferences.has(prefNewName), "pref should not exist");
const prefExistingName = "extensions.hotfix.id";
Assert.ok(Preferences.has(prefExistingName), "pref should exist");
Assert.ok(!Preferences.isSet(prefExistingName), "pref should not be user-set");
let prefExistingOriginalValue = Preferences.get(prefExistingName);
registerCleanupFunction(function() {
Preferences.set(prefExistingName, prefExistingOriginalValue);
Services.prefs.deleteBranch(prefNewName);
});
// 1. do nothing on an inexistent pref
MozSelfSupport.resetPref(prefNewName);
Assert.ok(!Preferences.has(prefNewName), "pref should still not exist");
// 2. creation of a new pref
Preferences.set(prefNewName, 10);
Assert.ok(Preferences.has(prefNewName), "pref should exist");
Assert.equal(Preferences.get(prefNewName), 10, "pref value should be 10");
MozSelfSupport.resetPref(prefNewName);
Assert.ok(!Preferences.has(prefNewName), "pref should not exist any more");
// 3. do nothing on an unchanged existing pref
MozSelfSupport.resetPref(prefExistingName);
Assert.ok(Preferences.has(prefExistingName), "pref should still exist");
Assert.equal(Preferences.get(prefExistingName), prefExistingOriginalValue, "pref value should be the same as original");
// 4. change the value of an existing pref
Preferences.set(prefExistingName, "anyone@mozilla.org");
Assert.ok(Preferences.has(prefExistingName), "pref should exist");
Assert.equal(Preferences.get(prefExistingName), "anyone@mozilla.org", "pref value should have changed");
MozSelfSupport.resetPref(prefExistingName);
Assert.ok(Preferences.has(prefExistingName), "pref should still exist");
Assert.equal(Preferences.get(prefExistingName), prefExistingOriginalValue, "pref value should be the same as original");
// 5. delete an existing pref
// deleteBranch is implemented in such a way that
// clearUserPref can't undo its action
// see discussion in bug 1075160
}

View File

@ -39,4 +39,15 @@ interface MozSelfSupport
* Resolved when the FHR payload data has been collected.
*/
Promise<object> getHealthReportPayload();
/**
* Resets a named pref:
* - if there is a default value, then change the value back to default,
* - if there's no default value, then delete the pref,
* - no-op otherwise.
*
* @param DOMString
* The name of the pref to reset.
*/
void resetPref(DOMString name);
};