1 /* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
2 /* vim: set sts=2 sw=2 et tw=80: */
6 const { AddonTestUtils } = ChromeUtils.importESModule(
7 "resource://testing-common/AddonTestUtils.sys.mjs"
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",
15 AddonTestUtils.init(this);
16 AddonTestUtils.overrideCertDB();
17 AddonTestUtils.createAppInfo(
18 "xpcshell@tests.mozilla.org",
24 add_task(async function shutdown_during_search_provider_startup() {
25 await AddonTestUtils.promiseStartupManager();
27 let extension = ExtensionTestUtils.loadExtension({
28 useAddonManager: "permanent",
30 chrome_settings_overrides: {
34 search_url: "https://example.com/",
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.
48 let initialized = false;
49 ExtensionParent.apiManager.global.searchInitialized.then(() => {
53 await extension.addon.disable();
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");
60 extension.addon.enable();
61 await extension.awaitStartup();
63 // Check that uninstall is blocked until the search registration at startup
64 // has finished. This registration only finished once the search service is
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");
75 let extRestartPromise = AddonTestUtils.waitForSearchProviderStartup(
78 // Search provider registration is expected to be pending again,
79 // because the search service has still not been initialized yet.
84 let uninstalledPromise = extension.addon.uninstall();
85 let uninstalled = false;
86 uninstalledPromise.then(() => {
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");
96 // After initializing the search service, the search provider registration
97 // promises should settle eventually.
99 // Despite the interrupted startup, the promise should still resolve without
101 await extStartPromise;
102 // The extension that is still active. The promise should just resolve.
103 await extRestartPromise;
105 // After initializing the search service, uninstall should eventually finish.
106 await uninstalledPromise;
108 await AddonTestUtils.promiseShutdownManager();