Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / LayoutTests / http / tests / serviceworker / chromium / postmessage-after-terminate.html
blob60b65c52bacd3640a096c04379497ba42e4aa231
1 <!DOCTYPE html>
2 <title>Tests that postMessage works during and after terminating a service worker</title>
3 <script src="../../resources/testharness.js"></script>
4 <script src="../../resources/testharnessreport.js"></script>
5 <script src="../../resources/testharness-helpers.js"></script>
6 <script src="../resources/test-helpers.js"></script>
7 <script>
8 function send_ping(worker) {
9 return new Promise(function(resolve) {
10 var channel = new MessageChannel();
11 channel.port1.onmessage = function(message) {
12 resolve(message.data);
14 worker.postMessage({port: channel.port2}, [channel.port2]);
15 });
18 function delay(ms) {
19 return new Promise(function(resolve) {
20 window.setTimeout(resolve, ms);
21 });
24 promise_test(function(test) {
25 var worker = 'resources/ping-worker.js';
26 var scope = 'resources/blank.html';
27 var sw;
28 return service_worker_unregister_and_register(test, worker, scope)
29 .then(function(registration) {
30 sw = registration.installing;
31 return send_ping(sw);
33 .then(function(reply) {
34 assert_equals(reply, 0);
35 return internals.terminateServiceWorker(sw);
37 .then(function() {
38 return send_ping(sw);
40 .then(function(reply) {
41 assert_equals(reply, 0);
42 return send_ping(sw);
44 .then(function(reply) {
45 assert_equals(reply, 1);
46 return service_worker_unregister_and_done(test, scope);
47 });
48 }, 'postMessage to a terminated service worker.');
50 async_test(function(t) {
51 var worker_script = 'resources/postmessage-worker.js';
52 var scope = 'resources/postmessage-worker-scope';
53 var worker;
54 var registration;
55 var port;
57 service_worker_unregister_and_register(
58 t, worker_script, scope)
59 .then(function(r) {
60 registration = r;
61 return wait_for_state(t, r.installing, 'activated');
63 .then(function() {
64 worker = registration.active;
65 return internals.terminateServiceWorker(worker);
67 .then(function() {
68 var messageChannel = new MessageChannel();
69 port = messageChannel.port1;
70 port.onmessage = t.step_func(on_message);
71 port.postMessage({value: 0});
72 worker.postMessage({port: messageChannel.port2},
73 [messageChannel.port2]);
74 port.postMessage({value: 1});
75 // Asynchronously sending more messages gives chrome a chance to be in
76 // a slightly different state, so wait a minimal amount of time.
77 return delay(1);
79 .then(function() {
80 port.postMessage({value: 2});
81 port.postMessage({done: true});
83 .catch(unreached_rejection(t));
85 var result = [];
86 var expected = [
87 'Acking value: 0',
88 'Acking value: 1',
89 'Acking value: 2',
92 function on_message(e) {
93 var message = e.data;
94 if (message === 'quit') {
95 assert_array_equals(result, expected,
96 'Worker should post back expected values.');
97 service_worker_unregister_and_done(t, scope);
98 } else {
99 result.push(message);
102 }, 'postMessage on a port that was sent to a terminated service worker');
104 async_test(function(t) {
105 var worker_script = 'resources/postmessage-port-worker.js';
106 var scope = 'resources/postmessage-port-worker-scope';
107 var worker;
108 var registration;
109 var port;
111 service_worker_unregister_and_register(
112 t, worker_script, scope)
113 .then(function(r) {
114 registration = r;
115 return wait_for_state(t, r.installing, 'activated');
117 .then(function() {
118 worker = registration.active;
119 return internals.terminateServiceWorker(worker);
121 .then(function() {
122 var innerChannel = new MessageChannel();
123 var outerChannel = new MessageChannel();
124 port = innerChannel.port1;
125 port.onmessage = t.step_func(on_message);
126 port.postMessage({value: 0});
127 outerChannel.port1.postMessage({port: innerChannel.port2},
128 [innerChannel.port2]);
129 worker.postMessage({port: outerChannel.port2}, [outerChannel.port2]);
130 port.postMessage({value: 1});
131 // Asynchronously sending more messages gives chrome a chance to be in
132 // a slightly different state, so wait a minimal amount of time.
133 return delay(1);
135 .then(function() {
136 port.postMessage({value: 2});
137 port.postMessage({done: true});
139 .catch(unreached_rejection(t));
141 var result = [];
142 var expected = [
143 'Acking value: 0',
144 'Acking value: 1',
145 'Acking value: 2',
148 function on_message(e) {
149 var message = e.data;
150 if (message === 'quit') {
151 assert_array_equals(result, expected,
152 'Worker should post back expected values.');
153 service_worker_unregister_and_done(t, scope);
154 } else {
155 result.push(message);
158 }, 'postMessage on a port that was sent on a port that was sent to a terminated service worker');
159 </script>