tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_extension_storage_actor_upgrade.js (3396B)


      1 /* Any copyright is dedicated to the Public Domain.
      2   http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 /**
      7 * Note: this test used to be in test_extension_storage_actor.js, but seems to
      8 * fail frequently as soon as we start auto-attaching targets.
      9 * See Bug 1618059.
     10 */
     11 
     12 const { ExtensionTestUtils } = ChromeUtils.importESModule(
     13  "resource://testing-common/ExtensionXPCShellUtils.sys.mjs"
     14 );
     15 
     16 const {
     17  createMissingIndexedDBDirs,
     18  extensionScriptWithMessageListener,
     19  getExtensionConfig,
     20  openAddonStoragePanel,
     21  shutdown,
     22  startupExtension,
     23 } = require("resource://test/webextension-helpers.js");
     24 
     25 const l10n = new Localization(["devtools/client/storage.ftl"], true);
     26 const sessionString = l10n.formatValueSync("storage-expires-session");
     27 
     28 // Ignore rejection related to the storage.onChanged listener being removed while the extension context is being closed.
     29 const { PromiseTestUtils } = ChromeUtils.importESModule(
     30  "resource://testing-common/PromiseTestUtils.sys.mjs"
     31 );
     32 PromiseTestUtils.allowMatchingRejectionsGlobally(
     33  /Message manager disconnected/
     34 );
     35 
     36 const { createAppInfo, promiseStartupManager } = AddonTestUtils;
     37 
     38 AddonTestUtils.init(this);
     39 createAppInfo("xpcshell@tests.mozilla.org", "XPCShell", "1", "42");
     40 
     41 ExtensionTestUtils.init(this);
     42 
     43 add_task(async function setup() {
     44  await promiseStartupManager();
     45  const dir = createMissingIndexedDBDirs();
     46 
     47  Assert.ok(
     48    dir.exists(),
     49    "Should have a 'storage/permanent' dir in the profile dir"
     50  );
     51 });
     52 
     53 /**
     54 * Test case: Bg page adds an item to storage. With storage panel open, reload extension.
     55 * - Load extension with background page that adds a storage item on message.
     56 * - Open the add-on storage panel.
     57 * - With the storage panel still open, reload the extension.
     58 * - The data in the storage panel should match the item added prior to reloading.
     59 */
     60 add_task(async function test_panel_live_reload() {
     61  const EXTENSION_ID = "test_panel_live_reload@xpcshell.mozilla.org";
     62  let manifest = {
     63    version: "1.0",
     64    browser_specific_settings: {
     65      gecko: {
     66        id: EXTENSION_ID,
     67      },
     68    },
     69  };
     70 
     71  info("Loading extension version 1.0");
     72  const extension = await startupExtension(
     73    getExtensionConfig({
     74      manifest,
     75      background: extensionScriptWithMessageListener,
     76    })
     77  );
     78 
     79  info("Waiting for message from test extension");
     80  const host = await extension.awaitMessage("extension-origin");
     81 
     82  info("Adding storage item");
     83  extension.sendMessage("storage-local-set", { a: 123 });
     84  await extension.awaitMessage("storage-local-set:done");
     85 
     86  const { commands, extensionStorage } = await openAddonStoragePanel(
     87    extension.id
     88  );
     89 
     90  manifest = {
     91    ...manifest,
     92    version: "2.0",
     93  };
     94  // "Reload" is most similar to an upgrade, as e.g. storage data is preserved
     95  info("Update to version 2.0");
     96 
     97  await extension.upgrade(
     98    getExtensionConfig({
     99      manifest,
    100      background: extensionScriptWithMessageListener,
    101    })
    102  );
    103 
    104  await extension.awaitMessage("extension-origin");
    105 
    106  const { data } = await extensionStorage.getStoreObjects(host, null, {
    107    sessionString,
    108  });
    109  Assert.deepEqual(
    110    data,
    111    [
    112      {
    113        area: "local",
    114        name: "a",
    115        value: { str: "123" },
    116        isValueEditable: true,
    117      },
    118    ],
    119    "Got the expected results on populated storage.local"
    120  );
    121 
    122  await shutdown(extension, commands);
    123 });