tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

browser_CaptivePortalWatcher.js (4764B)


      1 /* eslint-disable mozilla/no-arbitrary-setTimeout */
      2 "use strict";
      3 
      4 // Bug 1318389 - This test does a lot of window and tab manipulation,
      5 //               causing it to take a long time on debug.
      6 requestLongerTimeout(2);
      7 
      8 add_task(setupPrefsAndRecentWindowBehavior);
      9 
     10 // Each of the test cases below is run twice: once for login-success and once
     11 // for login-abort (aSuccess set to true and false respectively).
     12 let testCasesForBothSuccessAndAbort = [
     13  /**
     14   * A portal is detected when there's no browser window, then a browser
     15   * window is opened, then the portal is freed.
     16   * The portal tab should be added and focused when the window is
     17   * opened, and closed automatically when the success event is fired.
     18   * The captive portal notification should be shown when the window is
     19   * opened, and closed automatically when the success event is fired.
     20   */
     21  async function test_detectedWithNoBrowserWindow_Open(aSuccess) {
     22    await portalDetected();
     23    let win = await focusWindowAndWaitForPortalUI();
     24    await freePortal(aSuccess);
     25    ensureNoPortalTab(win);
     26    ensureNoPortalNotification(win);
     27    await closeWindowAndWaitForWindowActivate(win);
     28  },
     29 
     30  /**
     31   * A portal is detected when multiple browser windows are open but none
     32   * have focus. A browser window is focused, then the portal is freed.
     33   * The portal tab should be added and focused when the window is
     34   * focused, and closed automatically when the success event is fired.
     35   * The captive portal notification should be shown in all windows upon
     36   * detection, and closed automatically when the success event is fired.
     37   */
     38  async function test_detectedWithNoBrowserWindow_Focused(aSuccess) {
     39    let win1 = await openWindowAndWaitForFocus();
     40    let win2 = await openWindowAndWaitForFocus();
     41    // Defocus both windows.
     42    await SimpleTest.promiseFocus(window);
     43 
     44    await portalDetected();
     45 
     46    // Notification should be shown in both windows.
     47    await ensurePortalNotification(win1);
     48    ensureNoPortalTab(win1);
     49    await ensurePortalNotification(win2);
     50    ensureNoPortalTab(win2);
     51 
     52    await focusWindowAndWaitForPortalUI(false, win2);
     53 
     54    await freePortal(aSuccess);
     55 
     56    ensureNoPortalNotification(win1);
     57    ensureNoPortalTab(win2);
     58    ensureNoPortalNotification(win2);
     59 
     60    await closeWindowAndWaitForWindowActivate(win2);
     61    // No need to wait for xul-window-visible: after win2 is closed, focus
     62    // is restored to the default window and win1 remains in the background.
     63    await BrowserTestUtils.closeWindow(win1);
     64  },
     65 
     66  /**
     67   * A portal is detected when there's no browser window, then a browser
     68   * window is opened, then the portal is freed.
     69   * The recheck triggered when the browser window is opened takes a
     70   * long time. No portal tab should be added.
     71   * The captive portal notification should be shown when the window is
     72   * opened, and closed automatically when the success event is fired.
     73   */
     74  async function test_detectedWithNoBrowserWindow_LongRecheck(aSuccess) {
     75    await portalDetected();
     76    let win = await focusWindowAndWaitForPortalUI(true);
     77    await freePortal(aSuccess);
     78    ensureNoPortalTab(win);
     79    ensureNoPortalNotification(win);
     80    await closeWindowAndWaitForWindowActivate(win);
     81  },
     82 
     83  /**
     84   * A portal is detected when there's no browser window, and the
     85   * portal is freed before a browser window is opened. No portal
     86   * UI should be shown when a browser window is opened.
     87   */
     88  async function test_detectedWithNoBrowserWindow_GoneBeforeOpen(aSuccess) {
     89    await portalDetected();
     90    await freePortal(aSuccess);
     91    let win = await openWindowAndWaitForFocus();
     92    // Wait for a while to make sure no UI is shown.
     93    await new Promise(resolve => {
     94      setTimeout(resolve, 1000);
     95    });
     96    ensureNoPortalTab(win);
     97    ensureNoPortalNotification(win);
     98    await closeWindowAndWaitForWindowActivate(win);
     99  },
    100 
    101  /**
    102   * A portal is detected when a browser window has focus. No portal tab should
    103   * be opened. A notification bar should be displayed in all browser windows.
    104   */
    105  async function test_detectedWithFocus(aSuccess) {
    106    let win1 = await openWindowAndWaitForFocus();
    107    let win2 = await openWindowAndWaitForFocus();
    108    await portalDetected();
    109    ensureNoPortalTab(win1);
    110    ensureNoPortalTab(win2);
    111    await ensurePortalNotification(win1);
    112    await ensurePortalNotification(win2);
    113    await freePortal(aSuccess);
    114    ensureNoPortalNotification(win1);
    115    ensureNoPortalNotification(win2);
    116    await BrowserTestUtils.closeWindow(win2);
    117    await BrowserTestUtils.closeWindow(win1);
    118    await waitForBrowserWindowActive(window);
    119  },
    120 ];
    121 
    122 for (let testcase of testCasesForBothSuccessAndAbort) {
    123  add_task(testcase.bind(null, true));
    124  add_task(testcase.bind(null, false));
    125 }