Bug 1279491 - Fix timing issue when waiting for captive portal tab in new window in browser_CaptivePortalWatcher.js. r=MattN
MozReview-Commit-ID: llfUczB3y9
--- 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,