Bug 1159497 - Fix intermittent browser_bug427559.js failures r=MattN
authorTim Taubert <ttaubert@mozilla.com>
Thu, 30 Apr 2015 19:09:42 +0200
changeset 273576 25259bc37ac5ddbdd053064f0a9d511489ce57bf
parent 273575 87cfa9c94081a2079a03c4abbf99aea2ffa045de
child 273577 97ba42d0b930d7fa6d919b20c64306df8cd4eb3d
push id863
push userraliiev@mozilla.com
push dateMon, 03 Aug 2015 13:22:43 +0000
treeherdermozilla-release@f6321b14228d [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersMattN
bugs1159497, 427559
milestone40.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 1159497 - Fix intermittent browser_bug427559.js failures r=MattN
browser/base/content/test/general/browser_bug427559.js
--- a/browser/base/content/test/general/browser_bug427559.js
+++ b/browser/base/content/test/general/browser_bug427559.js
@@ -1,49 +1,53 @@
-/* This Source Code Form is subject to the terms of the Mozilla Public
- * License, v. 2.0. If a copy of the MPL was not distributed with this
- * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
 
 /*
  * Test bug 427559 to make sure focused elements that are no longer on the page
  * will have focus transferred to the window when changing tabs back to that
  * tab with the now-gone element.
  */
 
-// Default focus on a button and have it kill itself on blur
-let testPage = 'data:text/html,<body><button onblur="this.parentNode.removeChild(this);"><script>document.body.firstChild.focus();</script></body>';
-
-function test() {
-  waitForExplicitFinish();
-
-  gBrowser.selectedTab = gBrowser.addTab();
-  var browser = gBrowser.selectedBrowser;
+// Default focus on a button and have it kill itself on blur.
+const URL = 'data:text/html;charset=utf-8,' +
+            '<body><button onblur="this.remove()">' +
+            '<script>document.body.firstChild.focus()</script></body>';
 
-  browser.addEventListener("load", function () {
-    browser.removeEventListener("load", arguments.callee, true);
-    executeSoon(function () {
-      var testPageWin = content;
+function getFocusedLocalName(browser) {
+  return ContentTask.spawn(browser, null, function* () {
+    return content.document.activeElement.localName;
+  });
+}
 
-      is(browser.contentDocumentAsCPOW.activeElement.localName, "button", "button is focused");
+add_task(function* () {
+  gBrowser.selectedTab = gBrowser.addTab(URL);
+  let browser = gBrowser.selectedBrowser;
+  yield BrowserTestUtils.browserLoaded(browser);
 
-      addEventListener("focus", function focusedWindow(event) {
-        if (!String(event.target.location).startsWith("data:"))
-          return;
+  is((yield getFocusedLocalName(browser)), "button", "button is focused");
 
-        removeEventListener("focus", focusedWindow, true);
-
-        // Make sure focus is given to the window because the element is now gone
-        is(browser.contentDocumentAsCPOW.activeElement.localName, "body", "body is focused");
-
-        gBrowser.removeCurrentTab();
-        finish();
-      }, true);
+  let promiseFocused = ContentTask.spawn(browser, null, function* () {
+    return new Promise(resolve => {
+      content.addEventListener("focus", function onFocus({target}) {
+        if (String(target.location).startsWith("data:")) {
+          content.removeEventListener("focus", onFocus);
+          resolve();
+        }
+      });
+    });
+  });
 
-      // The test page loaded, so open an empty tab, select it, then restore
-      // the test tab. This causes the test page's focused element to be removed
-      // from its document.
-      gBrowser.selectedTab = gBrowser.addTab();
-      gBrowser.removeCurrentTab();
-    });
-  }, true);
+  // The test page loaded, so open an empty tab, select it, then restore
+  // the test tab. This causes the test page's focused element to be removed
+  // from its document.
+  gBrowser.selectedTab = gBrowser.addTab("about:blank");
+  yield BrowserTestUtils.browserLoaded(gBrowser.selectedBrowser);
+  gBrowser.removeCurrentTab();
 
-  content.location = testPage;
-}
+  // Wait until the original tab is focused again.
+  yield promiseFocused;
+
+  // Make sure focus is given to the window because the element is now gone.
+  is((yield getFocusedLocalName(browser)), "body", "body is focused");
+
+  // Cleanup.
+  gBrowser.removeCurrentTab();
+});