summaryrefslogtreecommitdiffstats
path: root/browser/base/content/test/captivePortal/browser_CaptivePortalWatcher.js
blob: aeafae21d81998434e5a78296dcd87473f1ad670 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/* eslint-disable mozilla/no-arbitrary-setTimeout */
"use strict";

// Bug 1318389 - This test does a lot of window and tab manipulation,
//               causing it to take a long time on debug.
requestLongerTimeout(2);

add_task(setupPrefsAndRecentWindowBehavior);

// Each of the test cases below is run twice: once for login-success and once
// for login-abort (aSuccess set to true and false respectively).
let testCasesForBothSuccessAndAbort = [
  /**
   * A portal is detected when there's no browser window, then a browser
   * 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.
   */
  async function test_detectedWithNoBrowserWindow_Open(aSuccess) {
    await portalDetected();
    let win = await focusWindowAndWaitForPortalUI();
    await freePortal(aSuccess);
    ensureNoPortalTab(win);
    ensureNoPortalNotification(win);
    await closeWindowAndWaitForWindowActivate(win);
  },

  /**
   * A portal is detected when multiple browser windows are open but none
   * have focus. A browser 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.
   */
  async function test_detectedWithNoBrowserWindow_Focused(aSuccess) {
    let win1 = await openWindowAndWaitForFocus();
    let win2 = await openWindowAndWaitForFocus();
    // Defocus both windows.
    await SimpleTest.promiseFocus(window);

    await portalDetected();

    // Notification should be shown in both windows.
    ensurePortalNotification(win1);
    ensureNoPortalTab(win1);
    ensurePortalNotification(win2);
    ensureNoPortalTab(win2);

    await focusWindowAndWaitForPortalUI(false, win2);

    await freePortal(aSuccess);

    ensureNoPortalNotification(win1);
    ensureNoPortalTab(win2);
    ensureNoPortalNotification(win2);

    await closeWindowAndWaitForWindowActivate(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.
    await 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.
   */
  async function test_detectedWithNoBrowserWindow_LongRecheck(aSuccess) {
    await portalDetected();
    let win = await focusWindowAndWaitForPortalUI(true);
    await freePortal(aSuccess);
    ensureNoPortalTab(win);
    ensureNoPortalNotification(win);
    await closeWindowAndWaitForWindowActivate(win);
  },

  /**
   * A portal is detected when there's no browser window, and the
   * portal is freed before a browser window is opened. No portal
   * UI should be shown when a browser window is opened.
   */
  async function test_detectedWithNoBrowserWindow_GoneBeforeOpen(aSuccess) {
    await portalDetected();
    await freePortal(aSuccess);
    let win = await openWindowAndWaitForFocus();
    // Wait for a while to make sure no UI is shown.
    await new Promise(resolve => {
      setTimeout(resolve, 1000);
    });
    ensureNoPortalTab(win);
    ensureNoPortalNotification(win);
    await closeWindowAndWaitForWindowActivate(win);
  },

  /**
   * A portal is detected when a browser window has focus. No portal tab should
   * be opened. A notification bar should be displayed in all browser windows.
   */
  async function test_detectedWithFocus(aSuccess) {
    let win1 = await openWindowAndWaitForFocus();
    let win2 = await openWindowAndWaitForFocus();
    await portalDetected();
    ensureNoPortalTab(win1);
    ensureNoPortalTab(win2);
    ensurePortalNotification(win1);
    ensurePortalNotification(win2);
    await freePortal(aSuccess);
    ensureNoPortalNotification(win1);
    ensureNoPortalNotification(win2);
    await BrowserTestUtils.closeWindow(win2);
    await BrowserTestUtils.closeWindow(win1);
    await waitForBrowserWindowActive(window);
  },
];

for (let testcase of testCasesForBothSuccessAndAbort) {
  add_task(testcase.bind(null, true));
  add_task(testcase.bind(null, false));
}