summaryrefslogtreecommitdiffstats
path: root/browser/base/content/test/captivePortal/head.js
blob: 77105456a2beece1747be92250a4f6919439b0e5 (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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
XPCOMUtils.defineLazyServiceGetter(
  this,
  "cps",
  "@mozilla.org/network/captive-portal-service;1",
  "nsICaptivePortalService"
);

const CANONICAL_CONTENT = "success";
const CANONICAL_URL = "data:text/plain;charset=utf-8," + CANONICAL_CONTENT;
const CANONICAL_URL_REDIRECTED = "data:text/plain;charset=utf-8,redirected";
const PORTAL_NOTIFICATION_VALUE = "captive-portal-detected";
const BAD_CERT_PAGE = "https://expired.example.com/";

async function setupPrefsAndRecentWindowBehavior() {
  await SpecialPowers.pushPrefEnv({
    set: [
      ["captivedetect.canonicalURL", CANONICAL_URL],
      ["captivedetect.canonicalContent", CANONICAL_CONTENT],
    ],
  });
  // We need to test behavior when a portal is detected when there is no browser
  // window, but we can't close the default window opened by the test harness.
  // Instead, we deactivate CaptivePortalWatcher in the default window and
  // exclude it using an attribute to mask its presence.
  window.CaptivePortalWatcher.uninit();
  window.document.documentElement.setAttribute("ignorecaptiveportal", "true");

  registerCleanupFunction(function cleanUp() {
    window.CaptivePortalWatcher.init();
    window.document.documentElement.removeAttribute("ignorecaptiveportal");
  });
}

async function portalDetected() {
  Services.obs.notifyObservers(null, "captive-portal-login");
  await TestUtils.waitForCondition(() => {
    return cps.state == cps.LOCKED_PORTAL;
  }, "Waiting for Captive Portal Service to update state after portal detected.");
}

async function freePortal(aSuccess) {
  Services.obs.notifyObservers(
    null,
    "captive-portal-login-" + (aSuccess ? "success" : "abort")
  );
  await TestUtils.waitForCondition(() => {
    return cps.state != cps.LOCKED_PORTAL;
  }, "Waiting for Captive Portal Service to update state after portal freed.");
}

// If a window is provided, it will be focused. Otherwise, a new window
// will be opened and focused.
async 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.
  Services.prefs.setIntPref(
    "captivedetect.portalRecheckDelayMS",
    aLongRecheck ? -1 : 1000000
  );

  if (!win) {
    win = await BrowserTestUtils.openNewBrowserWindow();
  }
  let windowActivePromise = waitForBrowserWindowActive(win);
  win.focus();
  await windowActivePromise;

  // 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.
  await TestUtils.waitForCondition(() => {
    return win.CaptivePortalWatcher._waitingForRecheck;
  }, "Waiting for CaptivePortalWatcher to trigger a recheck.");
  Services.obs.notifyObservers(null, "captive-portal-check-complete");

  let notification = await ensurePortalNotification(win);

  if (aLongRecheck) {
    ensureNoPortalTab(win);
    await testShowLoginPageButtonVisibility(notification, "visible");
    return win;
  }

  let tab = win.gBrowser.tabs[1];
  if (tab.linkedBrowser.currentURI.spec != CANONICAL_URL) {
    // The tab should load the canonical URL, wait for it.
    await BrowserTestUtils.waitForLocationChange(win.gBrowser, CANONICAL_URL);
  }
  is(
    win.gBrowser.selectedTab,
    tab,
    "The captive portal tab should be open and selected in the new window."
  );
  await testShowLoginPageButtonVisibility(notification, "hidden");
  return win;
}

function ensurePortalTab(win) {
  // For the tests that call this function, it's enough to ensure there
  // are two tabs in the window - the default tab and the portal tab.
  is(
    win.gBrowser.tabs.length,
    2,
    "There should be a captive portal tab in the window."
  );
}

async function ensurePortalNotification(win) {
  await BrowserTestUtils.waitForMutationCondition(
    win.gNavToolbox,
    { childList: true },
    () =>
      win.gNavToolbox
        .querySelector("notification-message")
        ?.getAttribute("value") == PORTAL_NOTIFICATION_VALUE
  );

  let notification = win.gNotificationBox.getNotificationWithValue(
    PORTAL_NOTIFICATION_VALUE
  );
  isnot(
    notification,
    null,
    "There should be a captive portal notification in the window."
  );
  return notification;
}

// Helper to test whether the "Show Login Page" is visible in the captive portal
// notification (it should be hidden when the portal tab is selected).
async function testShowLoginPageButtonVisibility(notification, visibility) {
  await notification.updateComplete;
  let showLoginPageButton = notification.buttonContainer.querySelector(
    "button.notification-button"
  );
  // If the visibility property was never changed from default, it will be
  // an empty string, so we pretend it's "visible" (effectively the same).
  is(
    showLoginPageButton.style.visibility || "visible",
    visibility,
    'The "Show Login Page" button should be ' + visibility + "."
  );
}

function ensureNoPortalTab(win) {
  is(
    win.gBrowser.tabs.length,
    1,
    "There should be no captive portal tab in the window."
  );
}

function ensureNoPortalNotification(win) {
  is(
    win.gNotificationBox.getNotificationWithValue(PORTAL_NOTIFICATION_VALUE),
    null,
    "There should be no captive portal notification in the window."
  );
}

/**
 * Some tests open a new window and close it later. When the window is closed,
 * the original window opened by mochitest gains focus, generating an
 * activate event. If the next test also opens a new window
 * before this event has a chance to fire, CaptivePortalWatcher picks
 * up the first one instead of the one from the new window. To avoid this
 * unfortunate intermittent timing issue, we wait for the event from
 * the original window every time we close a window that we opened.
 */
function waitForBrowserWindowActive(win) {
  return new Promise(resolve => {
    if (Services.focus.activeWindow == win) {
      resolve();
    } else {
      win.addEventListener(
        "activate",
        () => {
          resolve();
        },
        { once: true }
      );
    }
  });
}

async function closeWindowAndWaitForWindowActivate(win) {
  let activationPromises = [];
  for (let w of BrowserWindowTracker.orderedWindows) {
    if (
      w != win &&
      !win.document.documentElement.getAttribute("ignorecaptiveportal")
    ) {
      activationPromises.push(waitForBrowserWindowActive(win));
    }
  }
  await BrowserTestUtils.closeWindow(win);
  await Promise.race(activationPromises);
}

/**
 * BrowserTestUtils.openNewBrowserWindow() does not guarantee the newly
 * opened window has received focus when the promise resolves, so we
 * have to manually wait every time.
 */
async function openWindowAndWaitForFocus() {
  let win = await BrowserTestUtils.openNewBrowserWindow();
  await waitForBrowserWindowActive(win);
  return win;
}

async function openCaptivePortalErrorTab() {
  // Open a page with a cert error.
  let browser;
  let certErrorLoaded;
  let errorTab = await BrowserTestUtils.openNewForegroundTab(
    gBrowser,
    () => {
      let tab = BrowserTestUtils.addTab(gBrowser, BAD_CERT_PAGE);
      gBrowser.selectedTab = tab;
      browser = gBrowser.selectedBrowser;
      certErrorLoaded = BrowserTestUtils.waitForErrorPage(browser);
      return tab;
    },
    false
  );
  await certErrorLoaded;
  info("A cert error page was opened");
  await SpecialPowers.spawn(errorTab.linkedBrowser, [], async () => {
    let doc = content.document;
    let loginButton = doc.getElementById("openPortalLoginPageButton");
    await ContentTaskUtils.waitForCondition(
      () => loginButton && doc.body.className == "captiveportal",
      "Captive portal error page UI is visible"
    );
  });
  info("Captive portal error page UI is visible");

  return errorTab;
}

async function openCaptivePortalLoginTab(
  errorTab,
  LOGIN_PAGE_URL = CANONICAL_URL
) {
  let portalTabPromise = BrowserTestUtils.waitForNewTab(
    gBrowser,
    LOGIN_PAGE_URL,
    true
  );

  await SpecialPowers.spawn(errorTab.linkedBrowser, [], async () => {
    let doc = content.document;
    let loginButton = doc.getElementById("openPortalLoginPageButton");
    info("Click on the login button on the captive portal error page");
    await EventUtils.synthesizeMouseAtCenter(loginButton, {}, content);
  });

  let portalTab = await portalTabPromise;
  is(
    gBrowser.selectedTab,
    portalTab,
    "Captive Portal login page is now open in a new foreground tab."
  );

  return portalTab;
}