summaryrefslogtreecommitdiffstats
path: root/dom/workers/test/test_sharedWorker_privateBrowsing.html
blob: e93d65b3b814e229308371e53ead966e6213b0d4 (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
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title>Test for SharedWorker - Private Browsing</title>
  <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"?>
</head>
<body>

<script type="application/javascript">
const {BrowserTestUtils} = ChromeUtils.importESModule(
  "resource://testing-common/BrowserTestUtils.sys.mjs"
);
var mainWindow;

var contentPage = "http://mochi.test:8888/chrome/dom/workers/test/empty.html";

function testOnWindow(aIsPrivate, aCallback) {
  var win = mainWindow.OpenBrowserWindow({private: aIsPrivate});
  win.addEventListener("load", function() {
    win.addEventListener("DOMContentLoaded", function onInnerLoad() {
      if (win.content.location.href != contentPage) {
        BrowserTestUtils.startLoadingURIString(win.gBrowser, contentPage);
        return;
      }

      win.removeEventListener("DOMContentLoaded", onInnerLoad, true);
      SimpleTest.executeSoon(function() { aCallback(win); });
    }, true);
  }, {capture: true, once: true});
}

function setupWindow() {
  mainWindow = window.browsingContext.topChromeWindow;
  runTest();
}

var wN;
var wP;

function doTests() {
  testOnWindow(false, function(aWin) {
    wN = aWin;

    testOnWindow(true, function(win) {
      wP = win;

      var sharedWorker1 = new wP.content.SharedWorker('sharedWorker_privateBrowsing.js');
      sharedWorker1.port.onmessage = function(event) {
        is(event.data, 1, "Only 1 sharedworker expected in the private window");

        var sharedWorker2 = new wN.content.SharedWorker('sharedWorker_privateBrowsing.js');
        sharedWorker2.port.onmessage = function(event1) {
          is(event1.data, 1, "Only 1 sharedworker expected in the normal window");

          var sharedWorker3 = new wP.content.SharedWorker('sharedWorker_privateBrowsing.js');
          sharedWorker3.port.onmessage = function(event2) {
            is(event2.data, 2, "Only 2 sharedworker expected in the private window");
            runTest();
          }
        }
      }
    });
  });
}

function doSystemSharedWorkerTest() {
  try {
    let chromeShared =
      new wP.SharedWorker("chrome://mochitests/content/dom/workers/test/sharedWorker_privateBrowsing.js");
    ok(true, "system SharedWorker created without throwing or crashing!");
  } catch (_ex) {
    ok(false, "system SharedWorker should not throw or crash");
  }
  runTest();
}

var steps = [
  setupWindow,
  doTests,
  doSystemSharedWorkerTest,
];

function runTest() {
  if (!steps.length) {
    wN.close();
    wP.close();

    SimpleTest.finish();
    return;
  }

  var step = steps.shift();
  step();
}

SimpleTest.waitForExplicitFinish();
SpecialPowers.pushPrefEnv({"set": [
  ["browser.startup.page", 0],
  ["browser.startup.homepage_override.mstone", "ignore"],
]}, runTest);

</script>
</body>
</html>