Bug 1644519 - Wait for permission change to settle in browser_doorhanger_installs.js. r=mixedpuppy
authorAgi Sferro <agi@sferro.dev>
Fri, 12 Jun 2020 17:13:37 +0000
changeset 535514 0b4d785bd7a16c1ccbac06957ee784d3f5c49750
parent 535513 033ba8f1c3a2d6c0cb8929efe4a447e8148afa8b
child 535515 c75b282599717d372f1861ea95eb3cf68a3bfe43
push id37501
push usernbeleuzu@mozilla.com
push dateSat, 13 Jun 2020 03:21:52 +0000
treeherdermozilla-central@80b6f21783a3 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmixedpuppy
bugs1644519
milestone79.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 1644519 - Wait for permission change to settle in browser_doorhanger_installs.js. r=mixedpuppy Differential Revision: https://phabricator.services.mozilla.com/D79382
toolkit/mozapps/extensions/test/browser/browser_webext_incognito.js
toolkit/mozapps/extensions/test/xpinstall/browser_doorhanger_installs.js
--- a/toolkit/mozapps/extensions/test/browser/browser_webext_incognito.js
+++ b/toolkit/mozapps/extensions/test/browser/browser_webext_incognito.js
@@ -58,22 +58,20 @@ function getPrivateBrowsingValue() {
 
 async function setPrivateBrowsingValue(value, id) {
   let changePromise = new Promise(resolve => {
     const listener = (type, { extensionId, added, removed }) => {
       if (extensionId == id) {
         // Let's make sure we received the right message
         let { permissions } = value == "0" ? removed : added;
         ok(permissions.includes("internal:privateBrowsingAllowed"));
-
-        Management.off("change-permissions", listener);
         resolve();
       }
     };
-    Management.on("change-permissions", listener);
+    Management.once("change-permissions", listener);
   });
   let radio = getHtmlElem(
     `input[type="radio"][name="private-browsing"][value="${value}"]`
   );
   EventUtils.synthesizeMouseAtCenter(
     radio,
     { clickCount: 1 },
     radio.ownerGlobal
--- a/toolkit/mozapps/extensions/test/xpinstall/browser_doorhanger_installs.js
+++ b/toolkit/mozapps/extensions/test/xpinstall/browser_doorhanger_installs.js
@@ -3,16 +3,19 @@
  */
 
 const { AddonTestUtils } = ChromeUtils.import(
   "resource://testing-common/AddonTestUtils.jsm"
 );
 const { ExtensionPermissions } = ChromeUtils.import(
   "resource://gre/modules/ExtensionPermissions.jsm"
 );
+const { Management } = ChromeUtils.import(
+  "resource://gre/modules/Extension.jsm"
+);
 
 const SECUREROOT =
   "https://example.com/browser/toolkit/mozapps/extensions/test/xpinstall/";
 const PROGRESS_NOTIFICATION = "addon-progress";
 
 const CHROMEROOT = extractChromeRoot(gTestPath);
 
 AddonTestUtils.initMochitest(this);
@@ -118,33 +121,35 @@ async function waitForProgressNotificati
     );
   }
 
   return PopupNotifications.panel;
 }
 
 function acceptAppMenuNotificationWhenShown(
   id,
+  extensionId,
   {
     dismiss = false,
     checkIncognito = false,
     incognitoChecked = false,
     global = window,
   } = {}
 ) {
   const { AppMenuNotifications, PanelUI, document } = global;
   return new Promise(resolve => {
+    let permissionChangePromise = null;
     function appMenuPopupHidden() {
       PanelUI.panel.removeEventListener("popuphidden", appMenuPopupHidden);
       is(
         PanelUI.menuButton.getAttribute("badge-status"),
         false,
         "badge is not set after addon-installed"
       );
-      resolve();
+      resolve(permissionChangePromise);
     }
     function appMenuPopupShown() {
       PanelUI.panel.removeEventListener("popupshown", appMenuPopupShown);
       PanelUI.menuButton.click();
     }
     function popupshown() {
       let notification = AppMenuNotifications.activeNotification;
       if (!notification) {
@@ -162,32 +167,52 @@ function acceptAppMenuNotificationWhenSh
       );
       let checkbox = document.getElementById("addon-incognito-checkbox");
       is(
         checkbox.hidden,
         allowPrivate && !checkIncognito,
         "checkbox visibility is correct"
       );
       is(checkbox.checked, incognitoChecked, "checkbox is marked as expected");
+
+      // If we're unchecking or checking the incognito property, this will
+      // trigger an update in ExtensionPermission, let's wait for it before
+      // returning from this promise.
+      if (incognitoChecked != checkIncognito) {
+        permissionChangePromise = new Promise(resolve => {
+          const listener = (type, change) => {
+            if (extensionId == change.extensionId) {
+              // Let's make sure we received the right message
+              let { permissions } = checkIncognito
+                ? change.added
+                : change.removed;
+              ok(permissions.includes("internal:privateBrowsingAllowed"));
+              resolve();
+            }
+          };
+          Management.once("change-permissions", listener);
+        });
+      }
+
       checkbox.checked = checkIncognito;
 
       if (dismiss) {
         // Dismiss the panel by clicking on the appMenu button.
         PanelUI.panel.addEventListener("popupshown", appMenuPopupShown);
         PanelUI.panel.addEventListener("popuphidden", appMenuPopupHidden);
         PanelUI.menuButton.click();
         return;
       }
 
       // Dismiss the panel by clicking the primary button.
       let popupnotificationID = PanelUI._getPopupId(notification);
       let popupnotification = document.getElementById(popupnotificationID);
 
       popupnotification.button.click();
-      resolve();
+      resolve(permissionChangePromise);
     }
     PanelUI.notificationPanel.addEventListener("popupshown", popupshown);
   });
 }
 
 async function waitForNotification(aId, aExpectedCount = 1) {
   info("Waiting for " + aId + " notification");
 
@@ -385,17 +410,21 @@ var TESTS = [
     is(
       notification.id,
       "addon-progress-notification",
       "Should have seen the progress notification"
     );
 
     let installDialog = await dialogPromise;
 
-    notificationPromise = acceptAppMenuNotificationWhenShown("addon-installed");
+    notificationPromise = acceptAppMenuNotificationWhenShown(
+      "addon-installed",
+      "amosigned-xpi@tests.mozilla.org"
+    );
+
     installDialog.button.click();
     await notificationPromise;
 
     let installs = await AddonManager.getAllInstalls();
     is(installs.length, 0, "Should be no pending installs");
 
     let addon = await AddonManager.getAddonByID(
       "amosigned-xpi@tests.mozilla.org"
@@ -509,16 +538,17 @@ var TESTS = [
     is(
       gBrowser.selectedTab,
       tab,
       "tab selected in response to the addon-install-confirmation notification"
     );
 
     let notificationPromise = acceptAppMenuNotificationWhenShown(
       "addon-installed",
+      "amosigned-xpi@tests.mozilla.org",
       { dismiss: true }
     );
     acceptInstallDialog(installDialog);
     await notificationPromise;
 
     let installs = await AddonManager.getAllInstalls();
     is(installs.length, 0, "Should be no pending installs");
 
@@ -698,16 +728,17 @@ var TESTS = [
     gURLBar.focus();
     EventUtils.synthesizeKey("KEY_Enter");
 
     await progressPromise;
     let installDialog = await dialogPromise;
 
     let notificationPromise = acceptAppMenuNotificationWhenShown(
       "addon-installed",
+      "amosigned-xpi@tests.mozilla.org",
       { checkIncognito: true }
     );
     installDialog.button.click();
     await notificationPromise;
 
     let installs = await AddonManager.getAllInstalls();
     is(installs.length, 0, "Should be no pending installs");
 
@@ -970,16 +1001,17 @@ var TESTS = [
     gURLBar.focus();
     EventUtils.synthesizeKey("KEY_Enter");
 
     await progressPromise;
     let installDialog = await dialogPromise;
 
     let notificationPromise = acceptAppMenuNotificationWhenShown(
       "addon-installed",
+      "amosigned-xpi@tests.mozilla.org",
       { incognitoChecked: true }
     );
     installDialog.button.click();
     await notificationPromise;
 
     let installs = await AddonManager.getAllInstalls();
     is(installs.length, 0, "Should be no pending installs");
 
@@ -1067,16 +1099,17 @@ var TESTS = [
     await panelEventPromise;
     await waitForTick();
 
     let panel = win.PopupNotifications.panel;
     let installDialog = panel.childNodes[0];
 
     let notificationPromise = acceptAppMenuNotificationWhenShown(
       "addon-installed",
+      "amosigned-xpi@tests.mozilla.org",
       { incognitoChecked: true, global: win }
     );
     acceptInstallDialog(installDialog);
     await notificationPromise;
 
     let installs = await AddonManager.getAllInstalls();
     is(installs.length, 0, "Should be no pending installs");