Bug 1779305 - Fix intermittent browseraction keyclick test. r?rpl! draft
authorShane Caraveo <scaraveo@mozilla.com>
Thu, 29 Sep 2022 09:32:42 +0000
changeset 4682711 276e7a0131615858ab73b5acc576e4d9707af457
parent 4679962 170f356185eb469b153bc62d1a203e66a25a8035
child 4682712 bbb5d82a549be6f5abce3a840356dc87bf00ec4a
push id858557
push userreviewbot
push dateThu, 29 Sep 2022 09:33:02 +0000
treeherdertry@bbb5d82a549b [default view] [failures only]
reviewersrpl
bugs1779305
milestone107.0a1
Bug 1779305 - Fix intermittent browseraction keyclick test. r?rpl! Summary: Differential Revision: https://phabricator.services.mozilla.com/D153772 Co-authored-by: William Durand <will+git@drnd.me> Test Plan: Reviewers: rpl! Subscribers: Bug #: 1779305 Differential Diff: PHID-DIFF-aags4jjv27la77jgnoug
browser/components/extensions/test/browser/browser_ext_browserAction_keyclick.js
--- a/browser/components/extensions/test/browser/browser_ext_browserAction_keyclick.js
+++ b/browser/components/extensions/test/browser/browser_ext_browserAction_keyclick.js
@@ -2,62 +2,65 @@
 /* vim: set sts=2 sw=2 et tw=80: */
 "use strict";
 
 // Like focusButtonAndPressKey, but leaves time between keydown and keyup
 // rather than dispatching both synchronously. This allows time for the
 // element's `open` property to go back to being `false` if forced to true
 // synchronously in response to keydown.
 async function focusButtonAndPressKeyWithDelay(key, elem, modifiers) {
+  let focused = BrowserTestUtils.waitForEvent(elem, "focus", true);
   elem.setAttribute("tabindex", "-1");
   elem.focus();
   elem.removeAttribute("tabindex");
+  await focused;
 
   EventUtils.synthesizeKey(key, { type: "keydown", modifiers });
   await new Promise(executeSoon);
   EventUtils.synthesizeKey(key, { type: "keyup", modifiers });
+  let blurred = BrowserTestUtils.waitForEvent(elem, "blur", true);
   elem.blur();
+  await blurred;
 }
 
 // This test verifies that pressing enter while a page action is focused
 // triggers the action once and only once.
 add_task(async function testKeyBrowserAction() {
   let extension = ExtensionTestUtils.loadExtension({
     manifest: {
       browser_action: {},
-      background: {
-        persistent: false,
-        scripts: ["background.js"],
-      },
     },
 
-    files: {
-      "background.js": async function background() {
-        let counter = 0;
+    async background() {
+      let counter = 0;
 
-        function onClicked() {
-          dump("clicked\n");
-          counter++;
-        }
+      browser.browserAction.onClicked.addListener(() => {
+        counter++;
+      });
 
-        browser.browserAction.onClicked.addListener(onClicked);
+      browser.test.onMessage.addListener(async msg => {
+        browser.test.assertEq(
+          "checkCounter",
+          msg,
+          "expected check counter message"
+        );
+        browser.test.sendMessage("counter", counter);
+      });
 
-        browser.test.onMessage.addListener(async msg => {
-          browser.test.sendMessage("counter", counter);
-        });
-
-        browser.test.sendMessage("ready");
-      },
+      browser.test.sendMessage("ready");
     },
   });
 
   await extension.startup();
   await extension.awaitMessage("ready");
 
   let elem = getBrowserActionWidget(extension).forWindow(window).node;
+
+  await promiseAnimationFrame(window);
+  await showBrowserAction(extension, window);
   await focusButtonAndPressKeyWithDelay(" ", elem, {});
 
-  extension.sendMessage("checkCounter", {});
+  extension.sendMessage("checkCounter");
   let counter = await extension.awaitMessage("counter");
   is(counter, 1, "Key only triggered button once");
 
   await extension.unload();
 });