Bug 1411123 - Enable settings when set in ExtensionSettingsStore r?aswan draft
authorMark Striemer <mstriemer@mozilla.com>
Thu, 25 Jan 2018 17:17:13 -0600
changeset 747381 bc7ce64dd04fae0659690fbdbc5dc1c2724658a8
parent 747380 5decaeffb7c0a5af6ec788809b21b83009cbc8f9
push id96892
push userbmo:mstriemer@mozilla.com
push dateThu, 25 Jan 2018 23:17:55 +0000
reviewersaswan
bugs1411123
milestone60.0a1
Bug 1411123 - Enable settings when set in ExtensionSettingsStore r?aswan MozReview-Commit-ID: 64LaEoe7V42
toolkit/components/extensions/ExtensionSettingsStore.jsm
toolkit/components/extensions/test/xpcshell/test_ext_extensionPreferencesManager.js
toolkit/components/extensions/test/xpcshell/test_ext_extensionSettingsStore.js
--- 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.");