Bug 1503617 - Fix intermittent failures in tests with permission prompts r=rpl
authorRob Wu <rob@robwu.nl>
Wed, 20 Feb 2019 21:48:21 +0000
changeset 460256 be7d037f26bfb212c3c646d3aa5ec519264a34bf
parent 460255 0c48c658c7cfcc8b49e69b4805d8eb64b3f1cb48
child 460257 f362f67dfd3759135876b1fd2f4d900050bfc6cd
push id35590
push userrgurzau@mozilla.com
push dateFri, 22 Feb 2019 05:26:22 +0000
treeherdermozilla-central@cd28688c1642 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersrpl
bugs1503617
milestone67.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 1503617 - Fix intermittent failures in tests with permission prompts r=rpl Differential Revision: https://phabricator.services.mozilla.com/D19492
browser/components/extensions/test/browser/browser_ext_user_events.js
toolkit/mozapps/extensions/test/browser/browser_webapi_install.js
--- a/browser/components/extensions/test/browser/browser_ext_user_events.js
+++ b/browser/components/extensions/test/browser/browser_ext_user_events.js
@@ -31,19 +31,19 @@ function promisePopupNotificationShown(n
 add_task(async function testSources() {
   let extension = ExtensionTestUtils.loadExtension({
     async background() {
       async function request(perm) {
         try {
           let result = await browser.permissions.request({
             permissions: [perm],
           });
-          browser.test.sendMessage("request", {success: true, result});
+          browser.test.sendMessage("request", {success: true, result, perm});
         } catch (err) {
-          browser.test.sendMessage("request", {success: false, errmsg: err.message});
+          browser.test.sendMessage("request", {success: false, errmsg: err.message, perm});
         }
       }
 
       let tabs = await browser.tabs.query({active: true, currentWindow: true});
       await browser.pageAction.show(tabs[0].id);
 
       browser.pageAction.onClicked.addListener(() => request("bookmarks"));
       browser.browserAction.onClicked.addListener(() => request("tabs"));
@@ -91,23 +91,24 @@ add_task(async function testSources() {
         addEventListener("load", async () => {
           let link = document.getElementById("link");
           link.onclick = async event => {
             link.onclick = null;
             event.preventDefault();
 
             browser.test.log("Calling permission.request from options page.");
 
+            let perm = "history";
             try {
               let result = await browser.permissions.request({
-                permissions: ["webRequest"],
+                permissions: [perm],
               });
-              browser.test.sendMessage("request", {success: true, result});
+              browser.test.sendMessage("request", {success: true, result, perm});
             } catch (err) {
-              browser.test.sendMessage("request", {success: false, errmsg: err.message});
+              browser.test.sendMessage("request", {success: false, errmsg: err.message, perm});
             }
           };
 
           // Make a few trips through the event loop to make sure the
           // options browser is fully visible. This is a bit dodgy, but
           // we don't really have a reliable way to detect this from the
           // options page side, and synthetic click events won't work
           // until it is.
@@ -119,90 +120,114 @@ add_task(async function testSources() {
         });
       },
     },
 
     manifest: {
       browser_action: {default_title: "test"},
       page_action: {default_title: "test"},
       permissions: ["contextMenus"],
-      optional_permissions: ["bookmarks", "tabs", "webNavigation", "webRequest",
+      optional_permissions: ["bookmarks", "tabs", "webNavigation", "history",
                              "cookies", "downloads"],
       options_ui: {page: "options.html"},
       content_security_policy: "script-src 'self' https://example.com; object-src 'none';",
       commands: {
         command: {
           suggested_key: {
             "default": "Alt+Shift+J",
           },
         },
       },
     },
 
     useAddonManager: "temporary",
   });
 
-  async function check(what) {
+  async function testPermissionRequest({requestPermission, expectPrompt, perm}, what) {
+    info(`check request permission from '${what}'`);
+
+    let promptPromise = null;
+    if (expectPrompt) {
+      promptPromise = promisePopupNotificationShown("addon-webext-permissions")
+        .then(panel => {
+          panel.button.click();
+        });
+    }
+
+    await requestPermission();
+    await promptPromise;
+
     let result = await extension.awaitMessage("request");
     ok(result.success, `request() did not throw when called from ${what}`);
     is(result.result, true, `request() succeeded when called from ${what}`);
+    is(result.perm, perm, `requested permission ${what}`);
+    await promptPromise;
   }
 
   // Remove Sidebar button to prevent pushing extension button to overflow menu
   CustomizableUI.removeWidgetFromArea("sidebar-button");
 
   await extension.startup();
   await extension.awaitMessage("actions-ready");
 
-  promisePopupNotificationShown("addon-webext-permissions").then(panel => {
-    panel.button.click();
-  });
-
-  clickPageAction(extension);
-  await check("page action click");
+  await testPermissionRequest({
+    requestPermission: () => clickPageAction(extension),
+    expectPrompt: true,
+    perm: "bookmarks",
+  }, "page action click");
 
-  promisePopupNotificationShown("addon-webext-permissions").then(panel => {
-    panel.button.click();
-  });
-
-  clickBrowserAction(extension);
-  await check("browser action click");
-
-  promisePopupNotificationShown("addon-webext-permissions").then(panel => {
-    panel.button.click();
-  });
+  await testPermissionRequest({
+    requestPermission: () => clickBrowserAction(extension),
+    expectPrompt: true,
+    perm: "tabs",
+  }, "browser action click");
 
   let tab = await BrowserTestUtils.openNewForegroundTab(gBrowser);
   gBrowser.selectedTab = tab;
 
-  let menu = await openContextMenu("body");
-  let items = menu.getElementsByAttribute("label", "test user events in onclick");
-  is(items.length, 1, "Found context menu item");
-  EventUtils.synthesizeMouseAtCenter(items[0], {});
-  await check("context menu in onclick");
+  await testPermissionRequest({
+    requestPermission: async () => {
+      let menu = await openContextMenu("body");
+      let items = menu.getElementsByAttribute("label", "test user events in onclick");
+      is(items.length, 1, "Found context menu item");
+      EventUtils.synthesizeMouseAtCenter(items[0], {});
+    },
+    expectPrompt: false, // cookies permission has no prompt.
+    perm: "cookies",
+  }, "context menu in onclick");
 
   extension.sendMessage("contextMenus.update");
   await extension.awaitMessage("contextMenus.update-done");
-  menu = await openContextMenu("body");
-  items = menu.getElementsByAttribute("label", "test user events in onClicked");
-  is(items.length, 1, "Found context menu item again");
-  EventUtils.synthesizeMouseAtCenter(items[0], {});
-  await check("context menu in onClicked");
+
+  await testPermissionRequest({
+    requestPermission: async () => {
+      let menu = await openContextMenu("body");
+      let items = menu.getElementsByAttribute("label", "test user events in onClicked");
+      is(items.length, 1, "Found context menu item again");
+      EventUtils.synthesizeMouseAtCenter(items[0], {});
+    },
+    expectPrompt: true,
+    perm: "webNavigation",
+  }, "context menu in onClicked");
 
-  EventUtils.synthesizeKey("j", {altKey: true, shiftKey: true});
-  promisePopupNotificationShown("addon-webext-permissions").then(panel => {
-    panel.button.click();
-  });
-  await check("commands shortcut");
+  await testPermissionRequest({
+    requestPermission: () => {
+      EventUtils.synthesizeKey("j", {altKey: true, shiftKey: true});
+    },
+    expectPrompt: true,
+    perm: "downloads",
+  }, "commands shortcut");
 
-  promisePopupNotificationShown("addon-webext-permissions").then(panel => {
-    panel.button.click();
-  });
-  extension.sendMessage("openOptionsPage");
-  await check("options page link click");
+  await testPermissionRequest({
+    requestPermission: () => {
+      extension.sendMessage("openOptionsPage");
+    },
+    expectPrompt: true,
+    perm: "history",
+  }, "options page link click");
 
   await BrowserTestUtils.removeTab(gBrowser.selectedTab);
   await BrowserTestUtils.removeTab(tab);
 
   await extension.unload();
 
   registerCleanupFunction(() => CustomizableUI.reset());
 });
--- a/toolkit/mozapps/extensions/test/browser/browser_webapi_install.js
+++ b/toolkit/mozapps/extensions/test/browser/browser_webapi_install.js
@@ -34,20 +34,16 @@ add_task(async function setup() {
 // Wrapper around a common task to run in the content process to test
 // the mozAddonManager API.  Takes a URL for the XPI to install and an
 // array of steps, each of which can either be an action to take
 // (i.e., start or cancel the install) or an install event to wait for.
 // Steps that look for a specific event may also include a "props" property
 // with properties that the AddonInstall object is expected to have when
 // that event is triggered.
 async function testInstall(browser, args, steps, description) {
-  promisePopupNotificationShown("addon-webext-permissions").then(panel => {
-    panel.button.click();
-  });
-
   let success = await ContentTask.spawn(browser, {args, steps}, async function(opts) {
     let { args, steps } = opts;
     let install = await content.navigator.mozAddonManager.createInstall(args);
     if (!install) {
       await Promise.reject("createInstall() did not return an install object");
     }
 
     // Check that the initial state of the AddonInstall is sane.
@@ -197,20 +193,27 @@ function makeRegularTest(options, what) 
         props: {state: "STATE_INSTALLING"},
       },
       {
         event: "onInstallEnded",
         props: {state: "STATE_INSTALLED"},
       },
     ];
 
+    let installPromptPromise =
+      promisePopupNotificationShown("addon-webext-permissions").then(panel => {
+        panel.button.click();
+      });
+
     let promptPromise = acceptAppMenuNotificationWhenShown("addon-installed");
 
     await testInstall(browser, options, steps, what);
 
+    await installPromptPromise;
+
     await promptPromise;
 
     // Sanity check to ensure that the test in makeInstallTest() that
     // installs.size == 0 means we actually did clean up.
     ok(AddonManager.webAPI.installs.size > 0, "webAPI is tracking the AddonInstall");
 
     let addon = await promiseAddonByID(ID);
     isnot(addon, null, "Found the addon");