summaryrefslogtreecommitdiffstats
path: root/browser/base/content/test/captivePortal/browser_CaptivePortalWatcher.js
blob: e9c0fad6dad010a763ef36372d714adf67e47c79 (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
"use strict";

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.
   */
  function* test_detectedWithNoBrowserWindow_Open(aSuccess) {
    yield portalDetected();
    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 focusWindowAndWaitForPortalUI(true);
    yield freePortal(aSuccess);
    ensureNoPortalTab(win);
    ensureNoPortalNotification(win);
    yield closeWindowAndWaitForXulWindowVisible(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.
   */
  function* test_detectedWithNoBrowserWindow_GoneBeforeOpen(aSuccess) {
    yield portalDetected();
    yield freePortal(aSuccess);
    let win = yield openWindowAndWaitForFocus();
    // Wait for a while to make sure no UI is shown.
    yield new Promise(resolve => {
      setTimeout(resolve, 1000);
    });
    ensureNoPortalTab(win);
    ensureNoPortalNotification(win);
    yield closeWindowAndWaitForXulWindowVisible(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.
   */
  function* test_detectedWithFocus(aSuccess) {
    let win1 = yield openWindowAndWaitForFocus();
    let win2 = yield openWindowAndWaitForFocus();
    yield portalDetected();
    ensureNoPortalTab(win1);
    ensureNoPortalTab(win2);
    ensurePortalNotification(win1);
    ensurePortalNotification(win2);
    yield freePortal(aSuccess);
    ensureNoPortalNotification(win1);
    ensureNoPortalNotification(win2);
    yield closeWindowAndWaitForXulWindowVisible(win2);
    yield closeWindowAndWaitForXulWindowVisible(win1);
  },
];

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