tor-browser

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

test_ext_settings_overrides_shutdown.js (3523B)


      1 /* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
      2 /* vim: set sts=2 sw=2 et tw=80: */
      3 
      4 "use strict";
      5 
      6 const { AddonTestUtils } = ChromeUtils.importESModule(
      7  "resource://testing-common/AddonTestUtils.sys.mjs"
      8 );
      9 // Lazily import ExtensionParent to allow AddonTestUtils.createAppInfo to
     10 // override Services.appinfo.
     11 ChromeUtils.defineESModuleGetters(this, {
     12  ExtensionParent: "resource://gre/modules/ExtensionParent.sys.mjs",
     13 });
     14 
     15 AddonTestUtils.init(this);
     16 AddonTestUtils.overrideCertDB();
     17 AddonTestUtils.createAppInfo(
     18  "xpcshell@tests.mozilla.org",
     19  "XPCShell",
     20  "42",
     21  "42"
     22 );
     23 
     24 add_task(async function shutdown_during_search_provider_startup() {
     25  await AddonTestUtils.promiseStartupManager();
     26 
     27  let extension = ExtensionTestUtils.loadExtension({
     28    useAddonManager: "permanent",
     29    manifest: {
     30      chrome_settings_overrides: {
     31        search_provider: {
     32          is_default: true,
     33          name: "dummy name",
     34          search_url: "https://example.com/",
     35        },
     36      },
     37    },
     38  });
     39 
     40  info("Starting up search extension");
     41  await extension.startup();
     42  let extStartPromise = AddonTestUtils.waitForSearchProviderStartup(extension, {
     43    // Search provider registration is expected to be pending because the search
     44    // service has not been initialized yet.
     45    expectPending: true,
     46  });
     47 
     48  let initialized = false;
     49  Services.search.promiseInitialized.then(() => {
     50    initialized = true;
     51  });
     52 
     53  await extension.addon.disable();
     54 
     55  info("Extension managed to shut down despite the uninitialized search");
     56  // Initialize search after extension shutdown to check that it does not cause
     57  // any problems, and that the test can continue to test uninstall behavior.
     58  Assert.ok(!initialized, "Search service should not have been initialized");
     59 
     60  extension.addon.enable();
     61  await extension.awaitStartup();
     62 
     63  // Check that uninstall is blocked until the search registration at startup
     64  // has finished. This registration only finished once the search service is
     65  // initialized.
     66  let uninstallingPromise = new Promise(resolve => {
     67    let Management = ExtensionParent.apiManager;
     68    Management.on("uninstall", function listener(eventName, { id }) {
     69      Management.off("uninstall", listener);
     70      Assert.equal(id, extension.id, "Expected extension");
     71      resolve();
     72    });
     73  });
     74 
     75  let extRestartPromise = AddonTestUtils.waitForSearchProviderStartup(
     76    extension,
     77    {
     78      // Search provider registration is expected to be pending again,
     79      // because the search service has still not been initialized yet.
     80      expectPending: true,
     81    }
     82  );
     83 
     84  let uninstalledPromise = extension.addon.uninstall();
     85  let uninstalled = false;
     86  uninstalledPromise.then(() => {
     87    uninstalled = true;
     88  });
     89 
     90  await uninstallingPromise;
     91  Assert.ok(!uninstalled, "Uninstall should not be finished yet");
     92  Assert.ok(!initialized, "Search service should still be uninitialized");
     93  await Services.search.init();
     94  Assert.ok(initialized, "Search service should be initialized");
     95 
     96  // After initializing the search service, the search provider registration
     97  // promises should settle eventually.
     98 
     99  // Despite the interrupted startup, the promise should still resolve without
    100  // an error.
    101  await extStartPromise;
    102  // The extension that is still active. The promise should just resolve.
    103  await extRestartPromise;
    104 
    105  // After initializing the search service, uninstall should eventually finish.
    106  await uninstalledPromise;
    107 
    108  await AddonTestUtils.promiseShutdownManager();
    109 });