Bug 897943 - Fix focus issues and wait for delayed startup in browser_locationBarCommand.js; f=margaret r=gavin
authorTim Taubert <ttaubert@mozilla.com>
Mon, 29 Jul 2013 12:55:09 -0700
changeset 140385 91ee26cefa71efec217b3a2b5377240825c80265
parent 140384 1db0b845ce131314567302e5c88fcc814364ec89
child 140386 398c7364058619c1de658f6581d630d861d93131
push id25028
push userryanvm@gmail.com
push dateMon, 29 Jul 2013 22:42:06 +0000
treeherdermozilla-central@3d40d270c031 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersgavin
bugs897943
milestone25.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 897943 - Fix focus issues and wait for delayed startup in browser_locationBarCommand.js; f=margaret r=gavin
browser/base/content/test/browser_locationBarCommand.js
--- a/browser/base/content/test/browser_locationBarCommand.js
+++ b/browser/base/content/test/browser_locationBarCommand.js
@@ -27,28 +27,30 @@ saveURL = function() {
   runShiftLeftClickTest();
 }
 function runAltLeftClickTest() {
   info("Running test: Alt left click");
   triggerCommand(true, { altKey: true });
 }
 
 function runShiftLeftClickTest() {
-  let listener = new WindowListener(getBrowserURL(), function(aWindow) {
+  let listener = new BrowserWindowListener(getBrowserURL(), function(aWindow) {
     Services.wm.removeListener(listener);
     addPageShowListener(aWindow.gBrowser.selectedBrowser, function() {
       executeSoon(function () {
         info("URL should be loaded in a new window");
         is(gURLBar.value, "", "Urlbar reverted to original value");       
         is(gFocusManager.focusedElement, null, "There should be no focused element");
         is(gFocusManager.focusedWindow, aWindow.gBrowser.contentWindow, "Content window should be focused");
         is(aWindow.gURLBar.value, TEST_VALUE, "New URL is loaded in new window");
 
         aWindow.close();
-        runNextTest();
+
+        // Continue testing when the original window has focus again.
+        whenWindowActivated(window, runNextTest);
       });
     }, "http://example.com/");
   });
   Services.wm.addListener(listener);
 
   info("Running test: Shift left click");
   triggerCommand(true, { shiftKey: true });
 }
@@ -170,32 +172,42 @@ function addPageShowListener(browser, cb
     info("pageshow: " + browser.currentURI.spec);
     if (expectedURL && browser.currentURI.spec != expectedURL)
       return; // ignore pageshows for non-expected URLs
     browser.removeEventListener("pageshow", pageShowListener, false);
     cb();
   });
 }
 
-function WindowListener(aURL, aCallback) {
+function whenWindowActivated(win, cb) {
+  if (Services.focus.activeWindow == win) {
+    executeSoon(cb);
+    return;
+  }
+
+  win.addEventListener("activate", function onActivate() {
+    win.removeEventListener("activate", onActivate);
+    executeSoon(cb);
+  });
+}
+
+function BrowserWindowListener(aURL, aCallback) {
   this.callback = aCallback;
   this.url = aURL;
 }
-WindowListener.prototype = {
+BrowserWindowListener.prototype = {
   onOpenWindow: function(aXULWindow) {
-    var domwindow = aXULWindow.QueryInterface(Ci.nsIInterfaceRequestor)
+    let cb = () => this.callback(domwindow);
+    let domwindow = aXULWindow.QueryInterface(Ci.nsIInterfaceRequestor)
                               .getInterface(Ci.nsIDOMWindow);
-    var self = this;
-    domwindow.addEventListener("load", function() {
-      domwindow.removeEventListener("load", arguments.callee, false);
 
-      if (domwindow.document.location.href != self.url)
-        return;
+    let numWait = 2;
+    function maybeRunCallback() {
+      if (--numWait == 0)
+        cb();
+    }
 
-      // Allow other window load listeners to execute before passing to callback
-      executeSoon(function() {
-        self.callback(domwindow);
-      });
-    }, false);
+    whenWindowActivated(domwindow, maybeRunCallback);
+    whenDelayedStartupFinished(domwindow, maybeRunCallback);
   },
   onCloseWindow: function(aXULWindow) {},
   onWindowTitleChange: function(aXULWindow, aNewTitle) {}
 }