postmessage.https.html (2797B)
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.sub.js'></script> 6 <script> 7 8 promise_test(function(t) { 9 var script = 'resources/postmessage-loopback-worker.js'; 10 var scope = 'resources/scope/postmessage-loopback'; 11 var registration; 12 13 return service_worker_unregister_and_register(t, script, scope) 14 .then(function(r) { 15 t.add_cleanup(function() { 16 return service_worker_unregister(t, scope); 17 }); 18 19 registration = r; 20 21 return wait_for_state(t, registration.installing, 'activated'); 22 }) 23 .then(function() { 24 var channel = new MessageChannel(); 25 var saw_message = new Promise(function(resolve) { 26 channel.port1.onmessage = function(event) { 27 resolve(event.data); 28 }; 29 }); 30 registration.active.postMessage({port: channel.port2}, 31 [channel.port2]); 32 return saw_message; 33 }) 34 .then(function(result) { 35 assert_equals(result, 'OK'); 36 }); 37 }, 'Post loopback messages'); 38 39 promise_test(function(t) { 40 var script1 = 'resources/postmessage-ping-worker.js'; 41 var script2 = 'resources/postmessage-pong-worker.js'; 42 var scope = 'resources/scope/postmessage-pingpong'; 43 var registration; 44 var frame; 45 46 return service_worker_unregister_and_register(t, script1, scope) 47 .then(function(r) { 48 t.add_cleanup(function() { 49 return service_worker_unregister(t, scope); 50 }); 51 52 registration = r; 53 return wait_for_state(t, registration.installing, 'activated'); 54 }) 55 .then(function() { 56 // A controlled frame is necessary for keeping a waiting worker. 57 return with_iframe(scope); 58 }) 59 .then(function(f) { 60 frame = f; 61 return navigator.serviceWorker.register(script2, {scope: scope}); 62 }) 63 .then(function(r) { 64 return wait_for_state(t, r.installing, 'installed'); 65 }) 66 .then(function() { 67 var channel = new MessageChannel(); 68 var saw_message = new Promise(function(resolve) { 69 channel.port1.onmessage = function(event) { 70 resolve(event.data); 71 }; 72 }); 73 registration.active.postMessage({port: channel.port2}, 74 [channel.port2]); 75 return saw_message; 76 }) 77 .then(function(result) { 78 assert_equals(result, 'OK'); 79 frame.remove(); 80 }); 81 }, 'Post messages among service workers'); 82 83 </script>