summaryrefslogtreecommitdiffstats
path: root/dom/workers/test/serviceworkers/test_scopes.html
blob: 2d8116f8373b360117a0dd3260753f9cde4c9725 (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
117
118
119
120
121
<!--
  Any copyright is dedicated to the Public Domain.
  http://creativecommons.org/publicdomain/zero/1.0/
-->
<!DOCTYPE HTML>
<html>
<head>
  <title>Bug 984048 - Test scope glob matching.</title>
  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<p id="display"></p>
<div id="content" style="display: none"></div>
<pre id="test"></pre>
<script class="testbody" type="text/javascript">

  var scriptsAndScopes = [
    [ "worker.js", "./sub/dir/"],
    [ "worker.js", "./sub/dir" ],
    [ "worker.js", "./sub/dir.html" ],
    [ "worker.js", "./sub/dir/a" ],
    [ "worker.js", "./sub" ],
    [ "worker.js", "./star*" ], // '*' has no special meaning
  ];

  function registerWorkers() {
    var registerArray = [];
    scriptsAndScopes.forEach(function(item) {
      registerArray.push(navigator.serviceWorker.register(item[0], { scope: item[1] }));
    });

    // Check register()'s step 4 which uses script's url with "./" as the scope if no scope is passed.
    // The other tests already check step 5.
    registerArray.push(navigator.serviceWorker.register("scope/scope_worker.js"));

    // Check that SW cannot be registered for a scope "above" the script's location.
    registerArray.push(new Promise(function(resolve, reject) {
      navigator.serviceWorker.register("scope/scope_worker.js", { scope: "./" })
        .then(function() {
          ok(false, "registration scope has to be inside service worker script scope.");
          reject();
        }, function() {
          ok(true, "registration scope has to be inside service worker script scope.");
          resolve();
        });
    }));
    return Promise.all(registerArray);
  }

  function unregisterWorkers() {
    var unregisterArray = [];
    scriptsAndScopes.forEach(function(item) {
      var p = navigator.serviceWorker.getRegistration(item[1]);
      unregisterArray.push(p.then(function(reg) {
        return reg.unregister();
      }));
    });

    unregisterArray.push(navigator.serviceWorker.getRegistration("scope/").then(function (reg) {
      return reg.unregister();
    }));

    return Promise.all(unregisterArray);
  }

  function testScopes() {
    return new Promise(function(resolve, reject) {
      var getScope = navigator.serviceWorker.getScopeForUrl.bind(navigator.serviceWorker);
      var base = new URL(".", document.baseURI);

      function p(s) {
        return base + s;
      }

      function fail(fn) {
        try {
          getScope(p("index.html"));
          ok(false, "No registration");
        } catch(e) {
          ok(true, "No registration");
        }
      }

      ok(getScope(p("sub.html")) === p("sub"), "Scope should match");
      ok(getScope(p("sub/dir.html")) === p("sub/dir.html"), "Scope should match");
      ok(getScope(p("sub/dir")) === p("sub/dir"), "Scope should match");
      ok(getScope(p("sub/dir/foo")) === p("sub/dir/"), "Scope should match");
      ok(getScope(p("sub/dir/afoo")) === p("sub/dir/a"), "Scope should match");
      ok(getScope(p("star*wars")) === p("star*"), "Scope should match");
      ok(getScope(p("scope/some_file.html")) === p("scope/"), "Scope should match");
      fail("index.html");
      fail("sua.html");
      fail("star/a.html");
      resolve();
    });
  }

  function runTest() {
    registerWorkers()
      .then(testScopes)
      .then(unregisterWorkers)
      .then(function() {
        SimpleTest.finish();
      }).catch(function(e) {
        ok(false, "Some test failed with error " + e);
        SimpleTest.finish();
      });
  }

  SimpleTest.waitForExplicitFinish();
  SpecialPowers.pushPrefEnv({"set": [
    ["dom.serviceWorkers.exemptFromPerDomainMax", true],
    ["dom.serviceWorkers.enabled", true],
    ["dom.serviceWorkers.testing.enabled", true]
  ]}, runTest);
</script>
</pre>
</body>
</html>