summaryrefslogtreecommitdiffstats
path: root/dom/workers/test/serviceworkers/test_privateBrowsing.html
blob: 976337711845cbc44907d43092f3842f6bfb6a74 (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
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title>Test for ServiceWorker - Private Browsing</title>
  <script type="application/javascript" 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 Ci = Components.interfaces;
var mainWindow;

var contentPage = "http://mochi.test:8888/chrome/dom/workers/test/empty.html";
var workerScope = "http://mochi.test:8888/chrome/dom/workers/test/serviceworkers/";
var workerURL = workerScope + "worker.js";

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

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

    if (!aIsPrivate) {
      win.gBrowser.loadURI(contentPage);
    }
  }, true);
}

function setupWindow() {
  mainWindow = window.QueryInterface(Ci.nsIInterfaceRequestor)
                     .getInterface(Ci.nsIWebNavigation)
                     .QueryInterface(Ci.nsIDocShellTreeItem)
                     .rootTreeItem
                     .QueryInterface(Ci.nsIInterfaceRequestor)
                     .getInterface(Ci.nsIDOMWindow);
  runTest();
}

var wN;
var registration;
var wP;

function testPrivateWindow() {
  testOnWindow(true, function(aWin) {
    wP = aWin;
    ok(!("serviceWorker" in wP.content.navigator), "ServiceWorkers are not available for private windows");
    runTest();
  });
}

function doTests() {
  testOnWindow(false, function(aWin) {
    wN = aWin;
    ok("serviceWorker" in wN.content.navigator, "ServiceWorkers are available for normal windows");

    wN.content.navigator.serviceWorker.register(workerURL,
                                                { scope: workerScope })
      .then(function(aRegistration) {
        registration = aRegistration;
        ok(registration, "Registering a service worker in a normal window should succeed");

        // Bug 1255621: We should be able to load a controlled document in a private window.
        testPrivateWindow();
      }, function(aError) {
        ok(false, "Error registering worker in normal window: " + aError);
        testPrivateWindow();
      });
  });
}

var steps = [
  setupWindow,
  doTests
];

function cleanup() {
  wN.close();
  wP.close();

  SimpleTest.finish();
}

function runTest() {
  if (!steps.length) {
    registration.unregister().then(cleanup, cleanup);

    return;
  }

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

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

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