Bug 1279491 - Fix timing issue when waiting for captive portal tab in new window in browser_CaptivePortalWatcher.js. r=MattN draft
authorNihanth Subramanya <nhnt11@gmail.com>
Fri, 28 Oct 2016 10:54:17 +0530
changeset 430799 599f77501ae6cecdbeb5aa9130a2911feec15115
parent 430781 b52a42f1ecd3202165da13afedc38a4835ebff42
child 430800 49108df8680ff6bee95a98ad61bdc1635aaa4a32
push id33897
push usernhnt11@gmail.com
push dateFri, 28 Oct 2016 05:35:41 +0000
reviewersMattN
bugs1279491
milestone52.0a1
Bug 1279491 - Fix timing issue when waiting for captive portal tab in new window in browser_CaptivePortalWatcher.js. r=MattN MozReview-Commit-ID: llfUczB3y9
browser/modules/CaptivePortalWatcher.jsm
browser/modules/test/browser_CaptivePortalWatcher.js
--- a/browser/modules/CaptivePortalWatcher.jsm
+++ b/browser/modules/CaptivePortalWatcher.jsm
@@ -92,17 +92,17 @@ this.CaptivePortalWatcher = {
       return;
     }
 
     let win = RecentWindow.getMostRecentBrowserWindow();
     // If there's no browser window or none have focus, open and show the
     // tab when we regain focus. This is so that if a different application was
     // focused, when the user (re-)focuses a browser window, we open the tab
     // immediately in that window so they can login before continuing to browse.
-    if (!win || !win.document.hasFocus()) {
+    if (!win || win != Services.ww.activeWindow) {
       this._waitingToAddTab = true;
       Services.obs.addObserver(this, "xul-window-visible", false);
       return;
     }
 
     // The browser is in use - show a notification and add the tab without
     // selecting it, unless the caller specifically requested selection.
     this._ensureCaptivePortalTab(win);
--- a/browser/modules/test/browser_CaptivePortalWatcher.js
+++ b/browser/modules/test/browser_CaptivePortalWatcher.js
@@ -23,20 +23,35 @@ function* portalDetectedNoBrowserWindow(
   let getMostRecentBrowserWindow = RecentWindow.getMostRecentBrowserWindow;
   RecentWindow.getMostRecentBrowserWindow = () => {};
   Services.obs.notifyObservers(null, "captive-portal-login", null);
   RecentWindow.getMostRecentBrowserWindow = getMostRecentBrowserWindow;
 }
 
 function* openWindowAndWaitForPortalTabAndNotification() {
   let win = yield BrowserTestUtils.openNewBrowserWindow();
-  let [notification, tab] = yield Promise.all([
-    BrowserTestUtils.waitForGlobalNotificationBar(win, PORTAL_NOTIFICATION_VALUE),
-    BrowserTestUtils.waitForNewTab(win.gBrowser, CANONICAL_URL)
-  ]);
+  // Thanks to things being async, at this point we now have a new browser window
+  // but the portal notification and tab may or may not have opened. So first we
+  // check if there's already a portal notification, and if not, wait.
+  let notification = win.document.getElementById("high-priority-global-notificationbox")
+                        .getNotificationWithValue(PORTAL_NOTIFICATION_VALUE);
+  if (!notification) {
+    notification =
+      yield BrowserTestUtils.waitForGlobalNotificationBar(win, PORTAL_NOTIFICATION_VALUE);
+  }
+  // Then we see if there's already a portal tab. If it's open, it'll be the second one.
+  let tab = win.gBrowser.tabs[1];
+  if (!tab || tab.linkedBrowser.currentURI.spec != CANONICAL_URL) {
+    // The tab either hasn't been opened yet or it hasn't loaded the portal URL.
+    // Waiting for a location change in the tabbrowser covers both cases.
+    yield BrowserTestUtils.waitForLocationChange(win.gBrowser, CANONICAL_URL);
+    // At this point the portal tab should be the second tab. If there is still
+    // no second tab, something is wrong, and the selectedTab test below will fail.
+    tab = win.gBrowser.tabs[1];
+  }
   is(win.gBrowser.selectedTab, tab,
     "The captive portal tab should be open and selected in the new window.");
   testShowLoginPageButtonVisibility(notification, "hidden");
   return win;
 }
 
 function freePortal(aSuccess) {
   Services.obs.notifyObservers(null,