summaryrefslogtreecommitdiffstats
path: root/dom/workers/test/serviceworkers/test_serviceworker_interfaces.html
blob: 0130ca2d997fb34d03b7e7e173ab2a227cccbef5 (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
<!-- Any copyright is dedicated to the Public Domain.
   - http://creativecommons.org/publicdomain/zero/1.0/ -->
<!DOCTYPE HTML>
<html>
<head>
  <title>Validate Interfaces Exposed to Service Workers</title>
  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
  <script type="text/javascript" src="../worker_driver.js"></script>
</head>
<body>
<script class="testbody" type="text/javascript">

  function setupSW(registration) {
    var worker = registration.waiting ||
                 registration.active;
    window.onmessage = function(event) {
      if (event.data.type == 'finish') {
        registration.unregister().then(function(success) {
          ok(success, "The service worker should be unregistered successfully");

          SimpleTest.finish();
        }, function(e) {
          dump("Unregistering the SW failed with " + e + "\n");
          SimpleTest.finish();
        });
      } else if (event.data.type == 'status') {
        ok(event.data.status, event.data.msg);

      } else if (event.data.type == 'getPrefs') {
        var result = {};
        event.data.prefs.forEach(function(pref) {
          result[pref] = SpecialPowers.Services.prefs.getBoolPref(pref);
        });
        worker.postMessage({
          type: 'returnPrefs',
          prefs: event.data.prefs,
          result: result
        });

      } else if (event.data.type == 'getVersion') {
        var result = SpecialPowers.Cc['@mozilla.org/xre/app-info;1'].getService(SpecialPowers.Ci.nsIXULAppInfo).version;
        worker.postMessage({
          type: 'returnVersion',
          result: result
        });

      } else if (event.data.type == 'getUserAgent') {
        worker.postMessage({
          type: 'returnUserAgent',
          result: navigator.userAgent
        });
      } else if (event.data.type == 'getOSCPU') {
        worker.postMessage({
          type: 'returnOSCPU',
          result: navigator.oscpu
        });
      }
    }

    worker.onerror = function(event) {
      ok(false, 'Worker had an error: ' + event.data);
      SimpleTest.finish();
    };

    var iframe = document.createElement("iframe");
    iframe.src = "message_receiver.html";
    iframe.onload = function() {
      worker.postMessage({ script: "test_serviceworker_interfaces.js" });
    };
    document.body.appendChild(iframe);
  }

  function runTest() {
    navigator.serviceWorker.ready.then(setupSW);
    navigator.serviceWorker.register("serviceworker_wrapper.js", {scope: "."});
  }

  SimpleTest.waitForExplicitFinish();
  onload = function() {
    // The handling of "dom.caches.enabled" here is a bit complicated.  What we
    // want to happen is that Cache is always enabled in service workers.  So
    // if service workers are disabled by default we want to force on both
    // service workers and "dom.caches.enabled".  But if service workers are
    // enabled by default, we do not want to mess with the "dom.caches.enabled"
    // value, since that would defeat the purpose of the test.  Use a subframe
    // to decide whether service workers are enabled by default, so we don't
    // force creation of our own Navigator object before our prefs are set.
    var prefs = [
      ["dom.serviceWorkers.exemptFromPerDomainMax", true],
      ["dom.serviceWorkers.enabled", true],
      ["dom.serviceWorkers.testing.enabled", true],
    ];

    var subframe = document.createElement("iframe");
    document.body.appendChild(subframe);
    if (!("serviceWorker" in subframe.contentWindow.navigator)) {
	prefs.push(["dom.caches.enabled", true]);
    }
    subframe.remove();

    SpecialPowers.pushPrefEnv({"set": prefs}, runTest);
  };
</script>
</body>
</html>