summaryrefslogtreecommitdiffstats
path: root/browser/base/content/test/captivePortal/browser_captivePortalTabReference.js
blob: fe463bbaf5a91ac4e993d5c7f50fb359356b4506 (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

const BAD_CERT_PAGE = "https://expired.example.com/";
const CPS = Cc["@mozilla.org/network/captive-portal-service;1"].getService(
  Ci.nsICaptivePortalService
);

async function setCaptivePortalLoginState() {
  let captivePortalStatePropagated = TestUtils.topicObserved(
    "ipc:network:captive-portal-set-state"
  );
  Services.obs.notifyObservers(null, "captive-portal-login");
  info(
    "Waiting for captive portal login state to propagate to the content process."
  );
  await captivePortalStatePropagated;
  info("Captive Portal login state has been set");
}

async function openCaptivePortalErrorTab() {
  // Open a page with a cert error.
  let certErrorLoaded;
  let errorTab = await BrowserTestUtils.openNewForegroundTab(
    gBrowser,
    () => {
      let tab = BrowserTestUtils.addTab(gBrowser, BAD_CERT_PAGE);
      gBrowser.selectedTab = tab;
      let browser = gBrowser.selectedBrowser;
      certErrorLoaded = BrowserTestUtils.waitForErrorPage(browser);
      return tab;
    },
    false
  );

  await certErrorLoaded;
  info("An error page was opened");
  let browser = errorTab.linkedBrowser;
  await SpecialPowers.spawn(browser, [], 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"
    );
  });

  return errorTab;
}

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

  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;
}

async function checkCaptivePortalTabReference(evt, currState) {
  await setCaptivePortalLoginState();
  let errorTab = await openCaptivePortalErrorTab();
  let portalTab = await openCaptivePortalLoginTab(errorTab);

  // Release the reference held to the portal tab by sending success/abort events.
  Services.obs.notifyObservers(null, evt);
  await TestUtils.waitForCondition(
    () => CPS.state == currState,
    "Captive portal has been released"
  );
  gBrowser.removeTab(errorTab);

  await setCaptivePortalLoginState();
  ok(CPS.state == CPS.LOCKED_PORTAL, "Captive portal is locked again");
  errorTab = await openCaptivePortalErrorTab();
  let portalTab2 = await openCaptivePortalLoginTab(errorTab);
  ok(
    portalTab != portalTab2,
    "waitForNewTab in openCaptivePortalLoginTab should not have completed at this point if references were held to the old captive portal tab after login/abort."
  );

  gBrowser.removeTab(errorTab);
  gBrowser.removeTab(portalTab);
  gBrowser.removeTab(portalTab2);
}

add_task(async function setup() {
  await SpecialPowers.pushPrefEnv({
    set: [
      ["captivedetect.canonicalURL", CANONICAL_URL],
      ["captivedetect.canonicalContent", CANONICAL_CONTENT],
    ],
  });
});

let capPortalStates = [
  {
    evt: "captive-portal-login-success",
    state: CPS.UNLOCKED_PORTAL,
  },
  {
    evt: "captive-portal-login-abort",
    state: CPS.UNKNOWN,
  },
];

for (let elem of capPortalStates) {
  add_task(checkCaptivePortalTabReference.bind(null, elem.evt, elem.state));
}