Bug 1333075 - Ensure captive portal notification is displayed in all windows after portal is detected when none have focus. r=MattN, a=jcristau
authorNihanth Subramanya <nhnt11@gmail.com>
Tue, 24 Jan 2017 21:24:07 +0530
changeset 375680 55d70a7681c3ced07e5d5301f30a15a24b8f301e
parent 375679 44d3a67db0f5aec366b6b00dbdfec1caa577ae63
child 375681 602af8c8550cce482cd53260ccf98e0bc7337449
push id6996
push userjlorenzo@mozilla.com
push dateMon, 06 Mar 2017 20:48:21 +0000
treeherdermozilla-beta@d89512dab048 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersMattN, jcristau
bugs1333075
milestone53.0a2
Bug 1333075 - Ensure captive portal notification is displayed in all windows after portal is detected when none have focus. r=MattN, a=jcristau MozReview-Commit-ID: 9dvZp9Kn6kT
browser/base/content/browser-captivePortal.js
browser/base/content/test/captivePortal/browser_CaptivePortalWatcher.js
--- a/browser/base/content/browser-captivePortal.js
+++ b/browser/base/content/browser-captivePortal.js
@@ -101,17 +101,16 @@ var CaptivePortalWatcher = {
     let win = RecentWindow.getMostRecentBrowserWindow();
     // If no browser window has 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 log in before continuing to browse.
     if (win != Services.ww.activeWindow) {
       this._delayedCaptivePortalDetectedInProgress = true;
       Services.obs.addObserver(this, "xul-window-visible", false);
-      return;
     }
 
     this._showNotification();
   },
 
   /**
    * Called after we regain focus if we detect a portal while a browser window
    * doesn't have focus. Triggers a portal recheck to reaffirm state, and adds
@@ -145,17 +144,16 @@ var CaptivePortalWatcher = {
       let time = Date.now() - requestTime;
       Services.obs.removeObserver(observer, "captive-portal-check-complete");
       self._waitingForRecheck = false;
       if (cps.state != cps.LOCKED_PORTAL) {
         // We're free of the portal!
         return;
       }
 
-      self._showNotification();
       if (time <= self.PORTAL_RECHECK_DELAY_MS) {
         // The amount of time elapsed since we requested a recheck (i.e. since
         // the browser window was focused) was small enough that we can add and
         // focus a tab with the login page with no noticeable delay.
         self.ensureCaptivePortalTab();
       }
     }, "captive-portal-check-complete", false);
   },
--- a/browser/base/content/test/captivePortal/browser_CaptivePortalWatcher.js
+++ b/browser/base/content/test/captivePortal/browser_CaptivePortalWatcher.js
@@ -46,27 +46,32 @@ function* portalDetected() {
 function* freePortal(aSuccess) {
   Services.obs.notifyObservers(null,
     "captive-portal-login-" + (aSuccess ? "success" : "abort"), null);
   yield BrowserTestUtils.waitForCondition(() => {
     return cps.state != cps.LOCKED_PORTAL;
   }, "Waiting for Captive Portal Service to update state after portal freed.");
 }
 
-function* openWindowAndWaitForPortalUI(aLongRecheck) {
+// If a window is provided, it will be focused. Otherwise, a new window
+// will be opened and focused.
+function* focusWindowAndWaitForPortalUI(aLongRecheck, win) {
   // CaptivePortalWatcher triggers a recheck when a window gains focus. If
   // the time taken for the check to complete is under PORTAL_RECHECK_DELAY_MS,
   // a tab with the login page is opened and selected. If it took longer,
   // no tab is opened. It's not reliable to time things in an async test,
   // so use a delay threshold of -1 to simulate a long recheck (so that any
   // amount of time is considered excessive), and a very large threshold to
   // simulate a short recheck.
   Preferences.set("captivedetect.portalRecheckDelayMS", aLongRecheck ? -1 : 1000000);
 
-  let win = yield openWindowAndWaitForFocus();
+  if (!win) {
+    win = yield BrowserTestUtils.openNewBrowserWindow();
+  }
+  yield SimpleTest.promiseFocus(win);
 
   // After a new window is opened, CaptivePortalWatcher asks for a recheck, and
   // waits for it to complete. We need to manually tell it a recheck completed.
   yield BrowserTestUtils.waitForCondition(() => {
     return win.CaptivePortalWatcher._waitingForRecheck;
   }, "Waiting for CaptivePortalWatcher to trigger a recheck.");
   Services.obs.notifyObservers(null, "captive-portal-check-complete", null);
 
@@ -170,34 +175,70 @@ let testCasesForBothSuccessAndAbort = [
    * window is opened, then the portal is freed.
    * The portal tab should be added and focused when the window is
    * opened, and closed automatically when the success event is fired.
    * The captive portal notification should be shown when the window is
    * opened, and closed automatically when the success event is fired.
    */
   function* test_detectedWithNoBrowserWindow_Open(aSuccess) {
     yield portalDetected();
-    let win = yield openWindowAndWaitForPortalUI();
+    let win = yield focusWindowAndWaitForPortalUI();
     yield freePortal(aSuccess);
     ensureNoPortalTab(win);
     ensureNoPortalNotification(win);
     yield closeWindowAndWaitForXulWindowVisible(win);
   },
 
   /**
+   * A portal is detected when multiple browser windows are open but none
+   * have focus. A brower window is focused, then the portal is freed.
+   * The portal tab should be added and focused when the window is
+   * focused, and closed automatically when the success event is fired.
+   * The captive portal notification should be shown in all windows upon
+   * detection, and closed automatically when the success event is fired.
+   */
+  function* test_detectedWithNoBrowserWindow_Focused(aSuccess) {
+    let win1 = yield openWindowAndWaitForFocus();
+    let win2 = yield openWindowAndWaitForFocus();
+    // Defocus both windows.
+    yield SimpleTest.promiseFocus(window);
+
+    yield portalDetected();
+
+    // Notification should be shown in both windows.
+    ensurePortalNotification(win1);
+    ensureNoPortalTab(win1);
+    ensurePortalNotification(win2);
+    ensureNoPortalTab(win2);
+
+    yield focusWindowAndWaitForPortalUI(false, win2);
+
+    yield freePortal(aSuccess);
+
+    ensureNoPortalNotification(win1);
+    ensureNoPortalTab(win2);
+    ensureNoPortalNotification(win2);
+
+    yield closeWindowAndWaitForXulWindowVisible(win2);
+    // No need to wait for xul-window-visible: after win2 is closed, focus
+    // is restored to the default window and win1 remains in the background.
+    yield BrowserTestUtils.closeWindow(win1);
+  },
+
+  /**
    * A portal is detected when there's no browser window, then a browser
    * window is opened, then the portal is freed.
    * The recheck triggered when the browser window is opened takes a
    * long time. No portal tab should be added.
    * The captive portal notification should be shown when the window is
    * opened, and closed automatically when the success event is fired.
    */
   function* test_detectedWithNoBrowserWindow_LongRecheck(aSuccess) {
     yield portalDetected();
-    let win = yield openWindowAndWaitForPortalUI(true);
+    let win = yield focusWindowAndWaitForPortalUI(true);
     yield freePortal(aSuccess);
     ensureNoPortalTab(win);
     ensureNoPortalNotification(win);
     yield closeWindowAndWaitForXulWindowVisible(win);
   },
 
   /**
    * A portal is detected when there's no browser window, and the
@@ -242,17 +283,17 @@ let singleRunTestCases = [
    * A portal is detected when there's no browser window,
    * then a browser window is opened, and the portal is logged into
    * and redirects to a different page. The portal tab should be added
    * and focused when the window is opened, and left open after login
    * since it redirected.
    */
   function* test_detectedWithNoBrowserWindow_Redirect() {
     yield portalDetected();
-    let win = yield openWindowAndWaitForPortalUI();
+    let win = yield focusWindowAndWaitForPortalUI();
     let browser = win.gBrowser.selectedTab.linkedBrowser;
     let loadPromise =
       BrowserTestUtils.browserLoaded(browser, false, CANONICAL_URL_REDIRECTED);
     BrowserTestUtils.loadURI(browser, CANONICAL_URL_REDIRECTED);
     yield loadPromise;
     yield freePortal(true);
     ensurePortalTab(win);
     ensureNoPortalNotification(win);