summaryrefslogtreecommitdiffstats
path: root/dom/serviceworkers/test/test_claim.html
blob: e72f1173e8445ff28f29469dad370ebdc291488b (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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<!--
  Any copyright is dedicated to the Public Domain.
  http://creativecommons.org/publicdomain/zero/1.0/
-->
<!DOCTYPE HTML>
<html>
<head>
  <title>Bug 1130684 - Test service worker clients claim onactivate </title>
  <script 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 registration_1;
  var registration_2;
  var client;

  function register_1() {
    return navigator.serviceWorker.register("claim_worker_1.js",
                                            { scope: "./" })
      .then((swr) => registration_1 = swr);
  }

  function register_2() {
    return navigator.serviceWorker.register("claim_worker_2.js",
                                            { scope: "./claim_clients/client.html" })
      .then((swr) => registration_2 = swr);
  }

  function unregister(reg) {
    return reg.unregister().then(function(result) {
      ok(result, "Unregister should return true.");
    });
  }

  function createClient() {
    var p = new Promise(function(res, rej) {
      window.onmessage = function(e) {
        if (e.data === "READY") {
          res();
        }
      }
    });

    content = document.getElementById("content");
    ok(content, "parent exists.");

    client = document.createElement("iframe");
    client.setAttribute('src', "claim_clients/client.html");
    content.appendChild(client);

    return p;
  }

  function testController() {
    ok(navigator.serviceWorker.controller.scriptURL.match("claim_worker_1"),
        "Controlling service worker has the correct url.");
  }

  function testClientWasClaimed(expected) {
    var resolveClientMessage, resolveClientControllerChange;
    var messageFromClient = new Promise(function(res, rej) {
      resolveClientMessage = res;
    });
    var controllerChangeFromClient = new Promise(function(res, rej) {
      resolveClientControllerChange = res;
    });
    window.onmessage = function(e) {
      if (!e.data.event) {
        ok(false, "Unknown message received: " + e.data);
      }

      if (e.data.event === "controllerchange") {
        ok(e.data.controller,
           "Client was claimed and received controllerchange event.");
        resolveClientControllerChange();
      }

      if (e.data.event === "message") {
        ok(e.data.data.resolve_value === undefined,
            "Claim should resolve with undefined.");
        ok(e.data.data.message === expected.message,
           "Client received message from claiming worker.");
        ok(e.data.data.match_count_before === expected.match_count_before,
            "MatchAll clients count before claim should be " + expected.match_count_before);
        ok(e.data.data.match_count_after === expected.match_count_after,
            "MatchAll clients count after claim should be " + expected.match_count_after);
        resolveClientMessage();
      }
    }

    return Promise.all([messageFromClient, controllerChangeFromClient])
      .then(() => window.onmessage = null);
  }

  function testClaimFirstWorker() {
    // wait for the worker to control us
    var controllerChange = new Promise(function(res, rej) {
      navigator.serviceWorker.oncontrollerchange = function(e) {
        ok(true, "controller changed event received.");
        res();
      };
    });

    var messageFromWorker = new Promise(function(res, rej) {
      navigator.serviceWorker.onmessage = function(e) {
        ok(e.data.resolve_value === undefined,
            "Claim should resolve with undefined.");
        ok(e.data.message === "claim_worker_1",
            "Received message from claiming worker.");
        ok(e.data.match_count_before === 0,
            "Worker doesn't control any client before claim.");
        ok(e.data.match_count_after === 2, "Worker should claim 2 clients.");
        res();
      }
    });

    var clientClaim = testClientWasClaimed({
      message: "claim_worker_1",
      match_count_before: 0,
      match_count_after: 2
    });

    return Promise.all([controllerChange, messageFromWorker, clientClaim])
      .then(testController);
  }

  function testClaimSecondWorker() {
    navigator.serviceWorker.oncontrollerchange = function(e) {
      ok(false, "Claim_worker_2 shouldn't claim this window.");
    }

    navigator.serviceWorker.onmessage = function(e) {
      ok(false, "Claim_worker_2 shouldn't claim this window.");
    }

    var clientClaim = testClientWasClaimed({
      message: "claim_worker_2",
      match_count_before: 0,
      match_count_after: 1
    });

    return clientClaim.then(testController);
  }

  function runTest() {
    createClient()
      .then(register_1)
      .then(testClaimFirstWorker)
      .then(register_2)
      .then(testClaimSecondWorker)
      .then(function() { return unregister(registration_1); })
      .then(function() { return unregister(registration_2); })
      .catch(function(e) {
        ok(false, "Some test failed with error " + e);
      }).then(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>