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 498968 f624eab872101f974a3da00d4d19257a755ced1c
parent 498899 9e84f8d5b086020dba510678222a6c3ec568edf2
child 498969 33692f367564122728279498617718bbc235e542
push id1864
push userffxbld-merge
push dateMon, 03 Dec 2018 15:51:40 +0000
treeherdermozilla-release@f040763d99ad [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);