tor-browser

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

browser_CaptivePortalWatcher_1.js (3770B)


      1 "use strict";
      2 
      3 add_task(setupPrefsAndRecentWindowBehavior);
      4 
      5 let testcases = [
      6  /**
      7   * A portal is detected when there's no browser window,
      8   * then a browser window is opened, and the portal is logged into
      9   * and redirects to a different page. The portal tab should be added
     10   * and focused when the window is opened, and left open after login
     11   * since it redirected.
     12   */
     13  async function test_detectedWithNoBrowserWindow_Redirect() {
     14    await portalDetected();
     15    let win = await focusWindowAndWaitForPortalUI();
     16    let browser = win.gBrowser.selectedTab.linkedBrowser;
     17    let loadPromise = BrowserTestUtils.browserLoaded(
     18      browser,
     19      false,
     20      CANONICAL_URL_REDIRECTED
     21    );
     22    BrowserTestUtils.startLoadingURIString(browser, CANONICAL_URL_REDIRECTED);
     23    await loadPromise;
     24    await freePortal(true);
     25    ensurePortalTab(win);
     26    ensureNoPortalNotification(win);
     27    await closeWindowAndWaitForWindowActivate(win);
     28  },
     29 
     30  /**
     31   * Test the various expected behaviors of the "Show Login Page" button
     32   * in the captive portal notification. The button should be visible for
     33   * all tabs except the captive portal tab, and when clicked, should
     34   * ensure a captive portal tab is open and select it.
     35   */
     36  async function test_showLoginPageButton() {
     37    let win = await openWindowAndWaitForFocus();
     38    await portalDetected();
     39    let notification = await ensurePortalNotification(win);
     40    await testShowLoginPageButtonVisibility(notification, "visible");
     41 
     42    async function testPortalTabSelectedAndButtonNotVisible() {
     43      is(
     44        win.gBrowser.selectedTab,
     45        tab,
     46        "The captive portal tab should be selected."
     47      );
     48      await testShowLoginPageButtonVisibility(notification, "hidden");
     49    }
     50 
     51    let button = notification.buttonContainer.querySelector(
     52      "button.notification-button"
     53    );
     54    async function clickButtonAndExpectNewPortalTab() {
     55      let p = BrowserTestUtils.waitForNewTab(win.gBrowser, CANONICAL_URL);
     56      button.click();
     57      let tab = await p;
     58      is(
     59        win.gBrowser.selectedTab,
     60        tab,
     61        "The captive portal tab should be selected."
     62      );
     63      return tab;
     64    }
     65 
     66    // Simulate clicking the button. The portal tab should be opened and
     67    // selected and the button should hide.
     68    let tab = await clickButtonAndExpectNewPortalTab();
     69    await testPortalTabSelectedAndButtonNotVisible();
     70 
     71    // Close the tab. The button should become visible.
     72    BrowserTestUtils.removeTab(tab);
     73    ensureNoPortalTab(win);
     74    await testShowLoginPageButtonVisibility(notification, "visible");
     75 
     76    // When the button is clicked, a new portal tab should be opened and
     77    // selected.
     78    tab = await clickButtonAndExpectNewPortalTab();
     79 
     80    // Open another arbitrary tab. The button should become visible. When it's clicked,
     81    // the portal tab should be selected.
     82    let anotherTab = await BrowserTestUtils.openNewForegroundTab(win.gBrowser);
     83    await testShowLoginPageButtonVisibility(notification, "visible");
     84    button.click();
     85    is(
     86      win.gBrowser.selectedTab,
     87      tab,
     88      "The captive portal tab should be selected."
     89    );
     90 
     91    // Close the portal tab and select the arbitrary tab. The button should become
     92    // visible and when it's clicked, a new portal tab should be opened.
     93    BrowserTestUtils.removeTab(tab);
     94    win.gBrowser.selectedTab = anotherTab;
     95    await testShowLoginPageButtonVisibility(notification, "visible");
     96    tab = await clickButtonAndExpectNewPortalTab();
     97 
     98    BrowserTestUtils.removeTab(anotherTab);
     99    await freePortal(true);
    100    ensureNoPortalTab(win);
    101    ensureNoPortalNotification(win);
    102    await closeWindowAndWaitForWindowActivate(win);
    103  },
    104 ];
    105 
    106 for (let testcase of testcases) {
    107  add_task(testcase);
    108 }