Bug 1406181 - Test storage.local JSONFile and IndexedDB backends. r=kmag
authorLuca Greco <lgreco@mozilla.com>
Thu, 19 Oct 2017 15:15:31 +0200
changeset 421615 85a31c13073a82398f2b41dd047fe899d60c372b
parent 421614 0f5748ff685db5a0ef2b58ccf81f6a994e85df68
child 421616 645c311bf4ca9057572d2cd947eb7dd0c23069c3
push id34099
push userncsoregi@mozilla.com
push dateWed, 06 Jun 2018 22:00:08 +0000
treeherdermozilla-central@1ab062fd31db [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerskmag
bugs1406181
milestone62.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1406181 - Test storage.local JSONFile and IndexedDB backends. r=kmag MozReview-Commit-ID: CU9CVdIp8l8
toolkit/components/extensions/test/xpcshell/head.js
toolkit/components/extensions/test/xpcshell/test_ext_storage.js
--- a/toolkit/components/extensions/test/xpcshell/head.js
+++ b/toolkit/components/extensions/test/xpcshell/head.js
@@ -1,11 +1,12 @@
 "use strict";
 
-/* exported createHttpServer, promiseConsoleOutput, cleanupDir, clearCache, testEnv */
+/* exported createHttpServer, promiseConsoleOutput, cleanupDir, clearCache, testEnv
+            runWithPrefs */
 
 ChromeUtils.import("resource://gre/modules/AppConstants.jsm");
 ChromeUtils.import("resource://gre/modules/Services.jsm");
 ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm");
 ChromeUtils.import("resource://gre/modules/Timer.jsm");
 ChromeUtils.import("resource://testing-common/AddonTestUtils.jsm");
 
 // eslint-disable-next-line no-unused-vars
@@ -105,8 +106,37 @@ function cleanupDir(dir) {
       if (count >= 25) {
         return reject(`Failed to cleanup directory: ${dir}`);
       }
       setTimeout(tryToRemoveDir, 100);
     }
     tryToRemoveDir();
   });
 }
+
+// Run a test with the specified preferences and then clear them
+// right after the test function run (whether it passes or fails).
+async function runWithPrefs(prefsToSet, testFn) {
+  try {
+    for (let [pref, value] of prefsToSet) {
+      info(`Setting pref "${pref}": ${value}`);
+      switch (typeof(value)) {
+        case "boolean":
+          Services.prefs.setBoolPref(pref, value);
+          break;
+        case "number":
+          Services.prefs.setIntPref(pref, value);
+          break;
+        case "string":
+          Services.prefs.setStringPref(pref, value);
+          break;
+        default:
+          throw new Error("runWithPrefs doesn't support this pref type yet");
+      }
+    }
+    await testFn();
+  } finally {
+    for (let [prefName] of prefsToSet) {
+      info(`Clearing pref "${prefName}"`);
+      Services.prefs.clearUserPref(prefName);
+    }
+  }
+}
--- a/toolkit/components/extensions/test/xpcshell/test_ext_storage.js
+++ b/toolkit/components/extensions/test/xpcshell/test_ext_storage.js
@@ -1,14 +1,16 @@
 /* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
 /* vim: set sts=2 sw=2 et tw=80: */
 "use strict";
 
+ChromeUtils.defineModuleGetter(this, "ExtensionStorageIDB",
+                               "resource://gre/modules/ExtensionStorageIDB.jsm");
+
 const STORAGE_SYNC_PREF = "webextensions.storage.sync.enabled";
-ChromeUtils.import("resource://gre/modules/Preferences.jsm");
 
 add_task(async function setup() {
   await ExtensionTestUtils.startAddonManager();
 });
 
 /**
  * Utility function to ensure that all supported APIs for getting are
  * tested.
@@ -99,91 +101,94 @@ add_task(async function test_single_init
     await extension.awaitFinish("initialize once");
     await extension.unload();
     equal(callCount, 1, "Initialized FirefoxAdapter connection and Kinto exactly once");
   } finally {
     FirefoxAdapter.openConnection = origOpenConnection;
   }
 });
 
-add_task(async function test_config_flag_needed() {
-  function background() {
-    let promises = [];
-    let apiTests = [
-      {method: "get", args: ["foo"]},
-      {method: "set", args: [{foo: "bar"}]},
-      {method: "remove", args: ["foo"]},
-      {method: "clear", args: []},
-    ];
-    apiTests.forEach(testDef => {
-      promises.push(browser.test.assertRejects(
-        browser.storage.sync[testDef.method](...testDef.args),
-        "Please set webextensions.storage.sync.enabled to true in about:config",
-        `storage.sync.${testDef.method} is behind a flag`));
-    });
-
-    Promise.all(promises).then(() => browser.test.notifyPass("flag needed"));
-  }
+add_task(function test_config_flag_needed() {
+  async function testFn() {
+    function background() {
+      let promises = [];
+      let apiTests = [
+        {method: "get", args: ["foo"]},
+        {method: "set", args: [{foo: "bar"}]},
+        {method: "remove", args: ["foo"]},
+        {method: "clear", args: []},
+      ];
+      apiTests.forEach(testDef => {
+        promises.push(browser.test.assertRejects(
+          browser.storage.sync[testDef.method](...testDef.args),
+          "Please set webextensions.storage.sync.enabled to true in about:config",
+          `storage.sync.${testDef.method} is behind a flag`));
+      });
 
-  Preferences.set(STORAGE_SYNC_PREF, false);
-  ok(!Preferences.get(STORAGE_SYNC_PREF));
-  let extension = ExtensionTestUtils.loadExtension({
-    manifest: {
-      permissions: ["storage"],
-    },
-    background: `(${background})(${checkGetImpl})`,
-  });
-
-  await extension.startup();
-  await extension.awaitFinish("flag needed");
-  await extension.unload();
-  Preferences.reset(STORAGE_SYNC_PREF);
-});
-
-add_task(async function test_reloading_extensions_works() {
-  // Just some random extension ID that we can re-use
-  const extensionId = "my-extension-id@1";
-
-  function loadExtension() {
-    function background() {
-      browser.storage.sync.set({"a": "b"}).then(() => {
-        browser.test.notifyPass("set-works");
-      });
+      Promise.all(promises).then(() => browser.test.notifyPass("flag needed"));
     }
 
-    return ExtensionTestUtils.loadExtension({
+    ok(!Services.prefs.getBoolPref(STORAGE_SYNC_PREF, false),
+       "The `${STORAGE_SYNC_PREF}` should be set to false");
+
+    let extension = ExtensionTestUtils.loadExtension({
       manifest: {
         permissions: ["storage"],
       },
-      background: `(${background})()`,
-    }, extensionId);
+      background: `(${background})(${checkGetImpl})`,
+    });
+
+    await extension.startup();
+    await extension.awaitFinish("flag needed");
+    await extension.unload();
   }
 
-  Preferences.set(STORAGE_SYNC_PREF, true);
-
-  let extension1 = loadExtension();
-
-  await extension1.startup();
-  await extension1.awaitFinish("set-works");
-  await extension1.unload();
-
-  let extension2 = loadExtension();
-
-  await extension2.startup();
-  await extension2.awaitFinish("set-works");
-  await extension2.unload();
-
-  Preferences.reset(STORAGE_SYNC_PREF);
+  return runWithPrefs([[STORAGE_SYNC_PREF, false]], testFn);
 });
 
-registerCleanupFunction(() => {
-  Preferences.reset(STORAGE_SYNC_PREF);
+add_task(function test_reloading_extensions_works() {
+  async function testFn() {
+    // Just some random extension ID that we can re-use
+    const extensionId = "my-extension-id@1";
+
+    function loadExtension() {
+      function background() {
+        browser.storage.sync.set({"a": "b"}).then(() => {
+          browser.test.notifyPass("set-works");
+        });
+      }
+
+      return ExtensionTestUtils.loadExtension({
+        manifest: {
+          permissions: ["storage"],
+        },
+        background: `(${background})()`,
+      }, extensionId);
+    }
+
+    ok(Services.prefs.getBoolPref(STORAGE_SYNC_PREF, false),
+       "The `${STORAGE_SYNC_PREF}` should be set to true");
+
+    let extension1 = loadExtension();
+
+    await extension1.startup();
+    await extension1.awaitFinish("set-works");
+    await extension1.unload();
+
+    let extension2 = loadExtension();
+
+    await extension2.startup();
+    await extension2.awaitFinish("set-works");
+    await extension2.unload();
+  }
+
+  return runWithPrefs([[STORAGE_SYNC_PREF, true]], testFn);
 });
 
-add_task(async function test_backgroundScript() {
+async function test_background_page_storage(testAreaName) {
   async function backgroundScript(checkGet) {
     let globalChanges, gResolve;
     function clearGlobalChanges() {
       globalChanges = new Promise(resolve => { gResolve = resolve; });
     }
     clearGlobalChanges();
     let expectedAreaName;
 
@@ -363,54 +368,64 @@ add_task(async function test_backgroundS
 
   let extensionData = {
     background: `(${backgroundScript})(${checkGetImpl})`,
     manifest: {
       permissions: ["storage"],
     },
   };
 
-  Preferences.set(STORAGE_SYNC_PREF, true);
-
   let extension = ExtensionTestUtils.loadExtension(extensionData);
   await extension.startup();
   await extension.awaitMessage("ready");
 
-  extension.sendMessage("test-local");
+  extension.sendMessage(`test-${testAreaName}`);
   await extension.awaitMessage("test-finished");
 
-  extension.sendMessage("test-sync");
-  await extension.awaitMessage("test-finished");
+  await extension.unload();
+}
+
+add_task(function test_storage_local_file_backend() {
+  return runWithPrefs([[ExtensionStorageIDB.BACKEND_ENABLED_PREF, false]],
+                      () => test_background_page_storage("local"));
+});
 
-  Preferences.reset(STORAGE_SYNC_PREF);
-  await extension.unload();
+add_task(function test_storage_local_idb_backend() {
+  return runWithPrefs([[ExtensionStorageIDB.BACKEND_ENABLED_PREF, true]],
+                      () => test_background_page_storage("local"));
+});
+
+add_task(function test_storage_sync() {
+  return runWithPrefs([[STORAGE_SYNC_PREF, true]],
+                      () => test_background_page_storage("sync"));
 });
 
-add_task(async function test_storage_requires_real_id() {
-  async function backgroundScript() {
-    const EXCEPTION_MESSAGE =
-          "The storage API is not available with a temporary addon ID. " +
-          "Please add an explicit addon ID to your manifest. " +
-          "For more information see https://bugzil.la/1323228.";
+add_task(function test_storage_sync_requires_real_id() {
+  async function testFn() {
+    async function backgroundScript() {
+      const EXCEPTION_MESSAGE =
+              "The storage API is not available with a temporary addon ID. " +
+              "Please add an explicit addon ID to your manifest. " +
+              "For more information see https://bugzil.la/1323228.";
+
+      await browser.test.assertRejects(browser.storage.sync.set({"foo": "bar"}),
+                                       EXCEPTION_MESSAGE);
+
+      browser.test.notifyPass("exception correct");
+    }
 
-    await browser.test.assertRejects(browser.storage.sync.set({"foo": "bar"}),
-                                     EXCEPTION_MESSAGE);
+    let extensionData = {
+      background: `(${backgroundScript})(${checkGetImpl})`,
+      manifest: {
+        permissions: ["storage"],
+      },
+      useAddonManager: "temporary",
+    };
 
-    browser.test.notifyPass("exception correct");
+    let extension = ExtensionTestUtils.loadExtension(extensionData);
+    await extension.startup();
+    await extension.awaitFinish("exception correct");
+
+    await extension.unload();
   }
 
-  let extensionData = {
-    background: `(${backgroundScript})(${checkGetImpl})`,
-    manifest: {
-      permissions: ["storage"],
-    },
-    useAddonManager: "temporary",
-  };
-
-  Preferences.set(STORAGE_SYNC_PREF, true);
-
-  let extension = ExtensionTestUtils.loadExtension(extensionData);
-  await extension.startup();
-  await extension.awaitFinish("exception correct");
-
-  Preferences.reset(STORAGE_SYNC_PREF);
-  await extension.unload();
+  return runWithPrefs([[STORAGE_SYNC_PREF, true]], testFn);
 });