summaryrefslogtreecommitdiffstats
path: root/dom/serviceworkers/test/test_serviceworker_interfaces.html
blob: 3b4ec19134e284e3df003090f7a6db8c3d55a566 (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
<!-- 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 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 iframe;
    var worker = registration.installing ||
                 registration.waiting ||
                 registration.active;
    window.onmessage = function(event) {
      if (event.data.type == 'finish') {
        iframe.remove();
        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') {
        let result = {};
        event.data.prefs.forEach(function(pref) {
          result[pref] = SpecialPowers.Services.prefs.getBoolPref(pref);
        });
        worker.postMessage({
          type: 'returnPrefs',
          prefs: event.data.prefs,
          result
        });

      } else if (event.data.type == 'getHelperData') {
        const { AppConstants } = SpecialPowers.ChromeUtils.import(
          "resource://gre/modules/AppConstants.jsm"
        );
        const isNightly = AppConstants.NIGHTLY_BUILD;
        const isEarlyBetaOrEarlier = AppConstants.EARLY_BETA_OR_EARLIER;
        const isRelease = AppConstants.RELEASE_OR_BETA;
        const isDesktop = !/Mobile|Tablet/.test(navigator.userAgent);
        const isMac = AppConstants.platform == "macosx";
        const isWindows = AppConstants.platform == "win";
        const isAndroid = AppConstants.platform == "android";
        const isLinux = AppConstants.platform == "linux";
        const isInsecureContext = !window.isSecureContext;
        // Currently, MOZ_APP_NAME is always "fennec" for all mobile builds, so we can't use AppConstants for this
        const isFennec = isAndroid && SpecialPowers.Cc["@mozilla.org/android/bridge;1"].getService(SpecialPowers.Ci.nsIAndroidBridge).isFennec;

        const result = {
          isNightly, isEarlyBetaOrEarlier, isRelease, isDesktop, isMac,
          isWindows, isAndroid, isLinux, isInsecureContext, isFennec
        };

        worker.postMessage({
          type: 'returnHelperData', result
        });
      }
    }

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

    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.register("serviceworker_wrapper.js", {scope: "."})
      .then(setupSW);
  }

  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>