Bug 1497364 - allow browser_ext_themes_dynamic_getCurrent.js to work if window focus becomes more async, r=aswan
authorGijs Kruitbosch <gijskruitbosch@gmail.com>
Wed, 10 Oct 2018 12:14:16 +0000
changeset 440468 f624eab87210
parent 440467 9e84f8d5b086
child 440469 33692f367564
push id70681
push usergijskruitbosch@gmail.com
push dateWed, 10 Oct 2018 12:15:25 +0000
treeherderautoland@f624eab87210 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersaswan
bugs1497364
milestone64.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 1497364 - allow browser_ext_themes_dynamic_getCurrent.js to work if window focus becomes more async, r=aswan Differential Revision: https://phabricator.services.mozilla.com/D8078
toolkit/components/extensions/test/browser/browser_ext_themes_dynamic_getCurrent.js
--- a/toolkit/components/extensions/test/browser/browser_ext_themes_dynamic_getCurrent.js
+++ b/toolkit/components/extensions/test/browser/browser_ext_themes_dynamic_getCurrent.js
@@ -32,25 +32,35 @@ add_task(async function test_get_current
           "headerURL": "image2.png",
         },
         "colors": {
           "accentcolor": ACCENT_COLOR_2,
           "textcolor": TEXT_COLOR_2,
         },
       };
 
-      function promiseWindowChanged(winId) {
-        return new Promise(resolve => {
+      function ensureWindowFocused(winId) {
+        browser.test.log("Waiting for focused window to be " + winId);
+        return new Promise(async (resolve) => {
           let listener = windowId => {
             if (windowId === winId) {
               browser.windows.onFocusChanged.removeListener(listener);
               resolve();
             }
           };
+          // We first add a listener and then check whether the window is
+          // focused using .get(), because the .get() Promise resolving
+          // could race with the listener running, in which case we'd
+          // never be notified.
           browser.windows.onFocusChanged.addListener(listener);
+          let {focused} = await browser.windows.get(winId);
+          if (focused) {
+            browser.windows.onFocusChanged.removeListener(listener);
+            resolve();
+          }
         });
       }
 
       function testTheme1(returnedTheme) {
         browser.test.assertTrue(
           returnedTheme.images.headerURL.includes("image1.png"),
           "Theme 1 header URL should be applied");
         browser.test.assertEq(
@@ -84,45 +94,46 @@ add_task(async function test_get_current
 
       browser.test.log("Testing getCurrent() with after theme.update()");
       await browser.theme.update(theme1);
       testTheme1(await browser.theme.getCurrent());
       testTheme1(await browser.theme.getCurrent(firstWin.id));
 
       browser.test.log("Testing getCurrent() with after theme.update(windowId)");
       const secondWin = await browser.windows.create();
+      await ensureWindowFocused(secondWin.id);
       await browser.theme.update(secondWin.id, theme2);
       testTheme2(await browser.theme.getCurrent());
       testTheme1(await browser.theme.getCurrent(firstWin.id));
       testTheme2(await browser.theme.getCurrent(secondWin.id));
 
       browser.test.log("Testing getCurrent() after window focus change");
-      let focusChanged = promiseWindowChanged(firstWin.id);
+      let focusChanged = ensureWindowFocused(firstWin.id);
       await browser.windows.update(firstWin.id, {focused: true});
       await focusChanged;
       testTheme1(await browser.theme.getCurrent());
       testTheme1(await browser.theme.getCurrent(firstWin.id));
       testTheme2(await browser.theme.getCurrent(secondWin.id));
 
       browser.test.log("Testing getCurrent() after another window focus change");
-      focusChanged = promiseWindowChanged(secondWin.id);
+      focusChanged = ensureWindowFocused(secondWin.id);
       await browser.windows.update(secondWin.id, {focused: true});
       await focusChanged;
       testTheme2(await browser.theme.getCurrent());
       testTheme1(await browser.theme.getCurrent(firstWin.id));
       testTheme2(await browser.theme.getCurrent(secondWin.id));
 
       browser.test.log("Testing getCurrent() after theme.reset(windowId)");
       await browser.theme.reset(firstWin.id);
       testTheme2(await browser.theme.getCurrent());
       testEmptyTheme(await browser.theme.getCurrent(firstWin.id));
       testTheme2(await browser.theme.getCurrent(secondWin.id));
 
       browser.test.log("Testing getCurrent() after reset and window focus change");
-      focusChanged = promiseWindowChanged(firstWin.id);
+      focusChanged = ensureWindowFocused(firstWin.id);
       await browser.windows.update(firstWin.id, {focused: true});
       await focusChanged;
       testEmptyTheme(await browser.theme.getCurrent());
       testEmptyTheme(await browser.theme.getCurrent(firstWin.id));
       testTheme2(await browser.theme.getCurrent(secondWin.id));
 
       browser.test.log("Testing getCurrent() after theme.update(windowId)");
       await browser.theme.update(firstWin.id, theme1);