diff --git a/browser/base/content/test/webextensions/browser_extension_update_background.js b/browser/base/content/test/webextensions/browser_extension_update_background.js index d57d9efa38cc..958e53bef212 100644 --- a/browser/base/content/test/webextensions/browser_extension_update_background.js +++ b/browser/base/content/test/webextensions/browser_extension_update_background.js @@ -151,7 +151,7 @@ function* backgroundUpdateTest(url, id, checkIconFn) { is(win.gViewController.currentViewId, VIEW, "about:addons is at extensions list"); // Wait for the permission prompt and accept it this time - updatePromise = promiseInstallEvent(addon, "onInstallEnded"); + updatePromise = waitForUpdate(addon); panel = yield popupPromise; panel.button.click(); @@ -176,7 +176,6 @@ function checkDefaultIcon(icon) { add_task(() => backgroundUpdateTest(`${BASE}/browser_webext_update1.xpi`, ID, checkDefaultIcon)); - function checkNonDefaultIcon(icon) { // The icon should come from the extension, don't bother with the precise // path, just make sure we've got a jar url pointing to the right path @@ -209,7 +208,7 @@ async function testNoPrompt(origUrl, id) { {once: true}); // Trigger an update check and wait for the update to be applied. - let updatePromise = promiseInstallEvent(addon, "onInstallEnded"); + let updatePromise = waitForUpdate(addon); AddonManagerPrivate.backgroundUpdateCheck(); await updatePromise; @@ -239,4 +238,3 @@ add_task(() => testNoPrompt(`${BASE}/browser_webext_update_perms1.xpi`, // doesn't show a prompt even when the webextension uses // promptable required permissions. add_task(() => testNoPrompt(`${BASE}/browser_legacy.xpi`, ID_LEGACY)); - diff --git a/browser/base/content/test/webextensions/browser_update_interactive_noprompt.js b/browser/base/content/test/webextensions/browser_update_interactive_noprompt.js index 56e7974b203a..f2d4497b6674 100644 --- a/browser/base/content/test/webextensions/browser_update_interactive_noprompt.js +++ b/browser/base/content/test/webextensions/browser_update_interactive_noprompt.js @@ -34,7 +34,7 @@ async function testUpdateNoPrompt(filename, id, PopupNotifications.panel.addEventListener("popupshown", popupListener); // Trigger an update check, we should see the update get applied - let updatePromise = promiseInstallEvent(addon, "onInstallEnded"); + let updatePromise = waitForUpdate(addon); win.gViewController.doCommand("cmd_findAllUpdates"); await updatePromise; diff --git a/browser/base/content/test/webextensions/head.js b/browser/base/content/test/webextensions/head.js index c6a7ac319e74..07f60378082e 100644 --- a/browser/base/content/test/webextensions/head.js +++ b/browser/base/content/test/webextensions/head.js @@ -3,6 +3,11 @@ const BASE = getRootDirectory(gTestPath) .replace("chrome://mochitests/content/", "https://example.com/"); Cu.import("resource:///modules/ExtensionsUI.jsm"); +XPCOMUtils.defineLazyGetter(this, "Management", () => { + const {Management} = Components.utils.import("resource://gre/modules/Extension.jsm", {}); + return Management; +}); + /** * Wait for the given PopupNotification to display @@ -65,19 +70,58 @@ function promiseInstallEvent(addon, event) { * Resolves when the extension has been installed with the Addon * object as the resolution value. */ -function promiseInstallAddon(url) { - return AddonManager.getInstallForURL(url, null, "application/x-xpinstall") - .then(install => { - ok(install, "Created install"); - return new Promise(resolve => { - install.addListener({ - onInstallEnded(_install, addon) { - resolve(addon); - }, - }); - install.install(); - }); - }); +async function promiseInstallAddon(url) { + let install = await AddonManager.getInstallForURL(url, null, "application/x-xpinstall"); + install.install(); + + let addon = await new Promise(resolve => { + install.addListener({ + onInstallEnded(_install, _addon) { + resolve(_addon); + }, + }); + }); + + if (addon.isWebExtension) { + await new Promise(resolve => { + function listener(event, extension) { + if (extension.id == addon.id) { + Management.off("ready", listener); + resolve(); + } + } + Management.on("ready", listener); + }); + } + + return addon; +} + +/** + * Wait for an update to the given webextension to complete. + * (This does not actually perform an update, it just watches for + * the events that occur as a result of an update.) + * + * @param {AddonWrapper} addon + * The addon to be updated. + * + * @returns {Promise} + * Resolves when the extension has ben updated. + */ +async function waitForUpdate(addon) { + let installPromise = promiseInstallEvent(addon, "onInstallEnded"); + let readyPromise = new Promise(resolve => { + function listener(event, extension) { + if (extension.id == addon.id) { + Management.off("ready", listener); + resolve(); + } + } + Management.on("ready", listener); + }); + + let [newAddon, ] = await Promise.all([installPromise, readyPromise]); + return newAddon; } function isDefaultIcon(icon) {