Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / LayoutTests / http / tests / serviceworker / ServiceWorkerGlobalScope / postmessage.html
blob7a78f5360baf49430a0ee32091aa31e09be75938
1 <!DOCTYPE html>
2 <title>ServiceWorkerGlobalScope: postMessage</title>
3 <script src='../../resources/testharness.js'></script>
4 <script src='../../resources/testharnessreport.js'></script>
5 <script src='../resources/test-helpers.js'></script>
6 <script>
8 promise_test(function(t) {
9 var script = 'resources/postmessage-loopback-worker.js';
10 var scope = 'resources/scope/postmessage-loopback';
11 var registration;
13 return service_worker_unregister_and_register(t, script, scope)
14 .then(function(r) {
15 registration = r;
16 return wait_for_state(t, registration.installing, 'activated');
18 .then(function() {
19 var channel = new MessageChannel();
20 var saw_message = new Promise(function(resolve) {
21 channel.port1.onmessage = function(event) {
22 resolve(event.data);
24 });
25 registration.active.postMessage({port: channel.port2},
26 [channel.port2]);
27 return saw_message;
29 .then(function(result) {
30 assert_equals(result, 'OK');
31 return service_worker_unregister_and_done(t, scope);
32 });
33 }, 'Post loopback messages');
35 promise_test(function(t) {
36 var script1 = 'resources/postmessage-ping-worker.js';
37 var script2 = 'resources/postmessage-pong-worker.js';
38 var scope = 'resources/scope/postmessage-pingpong';
39 var registration;
40 var frame;
42 return service_worker_unregister_and_register(t, script1, scope)
43 .then(function(r) {
44 registration = r;
45 return wait_for_state(t, registration.installing, 'activated');
47 .then(function() {
48 // A controlled frame is necessary for keeping a waiting worker.
49 return with_iframe(scope);
51 .then(function(f) {
52 frame = f;
53 return navigator.serviceWorker.register(script2, {scope: scope});
55 .then(function(r) {
56 return wait_for_state(t, r.installing, 'installed');
58 .then(function() {
59 var channel = new MessageChannel();
60 var saw_message = new Promise(function(resolve) {
61 channel.port1.onmessage = function(event) {
62 resolve(event.data);
64 });
65 registration.active.postMessage({port: channel.port2},
66 [channel.port2]);
67 return saw_message;
69 .then(function(result) {
70 assert_equals(result, 'OK');
71 frame.remove();
72 return service_worker_unregister_and_done(t, scope);
73 });
74 }, 'Post messages among service workers');
76 </script>