Bug 1411123 - Enable settings when set in ExtensionSettingsStore r?aswan
MozReview-Commit-ID: 64LaEoe7V42
--- a/toolkit/components/extensions/ExtensionSettingsStore.jsm
+++ b/toolkit/components/extensions/ExtensionSettingsStore.jsm
@@ -300,17 +300,20 @@ this.ExtensionSettingsStore = {
let foundIndex = keyInfo.precedenceList.findIndex(item => item.id == id);
if (foundIndex === -1) {
// No item for this extension, so add a new one.
let addon = await AddonManager.getAddonByID(id);
keyInfo.precedenceList.push(
{id, installDate: addon.installDate.valueOf(), value, enabled: true});
} else {
// Item already exists or this extension, so update it.
- keyInfo.precedenceList[foundIndex].value = value;
+ let item = keyInfo.precedenceList[foundIndex];
+ item.value = value;
+ // Ensure the item is enabled.
+ item.enabled = true;
}
// Sort the list.
keyInfo.precedenceList.sort(precedenceComparator);
_store.saveSoon();
// Check whether this is currently the top item.
--- a/toolkit/components/extensions/test/xpcshell/test_ext_extensionPreferencesManager.js
+++ b/toolkit/components/extensions/test/xpcshell/test_ext_extensionPreferencesManager.js
@@ -275,8 +275,53 @@ add_task(async function test_preference_
"getLevelOfControl returns correct levelOfControl when a pref is locked.");
for (let extension of testExtensions) {
await extension.unload();
}
await promiseShutdownManager();
});
+
+add_task(async function test_preference_manager_set_when_disabled() {
+ await promiseStartupManager();
+
+ let id = "@set-disabled-pref";
+ let extension = ExtensionTestUtils.loadExtension({
+ useAddonManager: "temporary",
+ manifest: {
+ applications: {gecko: {id}},
+ },
+ });
+
+ await extension.startup();
+
+ await ExtensionSettingsStore.initialize();
+ ExtensionPreferencesManager.addSetting("some-pref", {
+ pref_names: ["foo"],
+ setCallback(value) {
+ return {foo: value};
+ },
+ });
+
+ // We want to test that ExtensionPreferencesManager.setSetting() will enable a
+ // disabled setting so we will manually add and disable it in
+ // ExtensionSettingsStore.
+ await ExtensionSettingsStore.addSetting(
+ id, "prefs", "some-pref", "my value", () => "default");
+
+ let item = ExtensionSettingsStore.getSetting("prefs", "some-pref");
+ equal(item.value, "my value", "The value is set");
+
+ ExtensionSettingsStore.disable(id, "prefs", "some-pref");
+
+ item = ExtensionSettingsStore.getSetting("prefs", "some-pref");
+ equal(item.initialValue, "default", "The value is back to default");
+
+ await ExtensionPreferencesManager.setSetting(id, "some-pref", "new value");
+
+ item = ExtensionSettingsStore.getSetting("prefs", "some-pref");
+ equal(item.value, "new value", "The value is set again");
+
+ await extension.unload();
+
+ await promiseShutdownManager();
+});
--- a/toolkit/components/extensions/test/xpcshell/test_ext_extensionSettingsStore.js
+++ b/toolkit/components/extensions/test/xpcshell/test_ext_extensionSettingsStore.js
@@ -531,16 +531,51 @@ add_task(async function test_settings_st
for (let extension of testExtensions) {
await extension.unload();
}
await promiseShutdownManager();
});
+add_task(async function test_settings_store_add_disabled() {
+ await promiseStartupManager();
+
+ let id = "@add-on-disable";
+ let extension = ExtensionTestUtils.loadExtension({
+ useAddonManager: "temporary",
+ manifest: {
+ applications: {gecko: {id}},
+ },
+ });
+
+ await extension.startup();
+ await ExtensionSettingsStore.initialize();
+
+ await ExtensionSettingsStore.addSetting(id, "foo", "bar", "set", () => "not set");
+
+ let item = ExtensionSettingsStore.getSetting("foo", "bar");
+ equal(item.id, id, "The add-on is in control");
+ equal(item.value, "set", "The value is set");
+
+ ExtensionSettingsStore.disable(id, "foo", "bar");
+ item = ExtensionSettingsStore.getSetting("foo", "bar");
+ equal(item.id, undefined, "The add-on is not in control");
+ equal(item.initialValue, "not set", "The value is not set");
+
+ await ExtensionSettingsStore.addSetting(id, "foo", "bar", "set", () => "not set");
+ item = ExtensionSettingsStore.getSetting("foo", "bar");
+ equal(item.id, id, "The add-on is in control");
+ equal(item.value, "set", "The value is set");
+
+ await extension.unload();
+
+ await promiseShutdownManager();
+});
+
add_task(async function test_exceptions() {
await ExtensionSettingsStore.initialize();
await Assert.rejects(
ExtensionSettingsStore.addSetting(
1, TEST_TYPE, "key_not_a_function", "val1", "not a function"),
/initialValueCallback must be a function/,
"addSetting rejects with a callback that is not a function.");