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

"use strict";

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

async function checkCaptivePortalTabReference(evt, currState) {
  await portalDetected();
  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 portalDetected();
  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(portalTab);
  gBrowser.removeTab(portalTab2);

  let errorTabReloaded = BrowserTestUtils.waitForErrorPage(
    errorTab.linkedBrowser
  );
  Services.obs.notifyObservers(null, "captive-portal-login-success");
  await errorTabReloaded;

  gBrowser.removeTab(errorTab);
}

add_setup(async function () {
  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));
}