OLD | NEW |
1 <!DOCTYPE html> | 1 <!DOCTYPE html> |
2 <title>Service Worker: Current on load</title> | 2 <title>Service Worker: postMessage to Client</title> |
3 <script src="../resources/testharness.js"></script> | 3 <script src="../resources/testharness.js"></script> |
4 <script src="../resources/testharnessreport.js"></script> | 4 <script src="../resources/testharnessreport.js"></script> |
5 <script src="resources/test-helpers.js"></script> | 5 <script src="resources/test-helpers.js"></script> |
6 <body> | |
7 <script> | 6 <script> |
8 var t = async_test('current is set for a controlled document'); | 7 var t = async_test('postMessage from ServiceWorker to Client'); |
9 t.step(function() { | 8 t.step(function() { |
10 var scope = 'resources/blank.html' | 9 var scope = 'resources/blank.html' |
11 navigator.serviceWorker.unregister(scope).then( | 10 navigator.serviceWorker.unregister(scope).then( |
12 doTest, | 11 doRegister, |
13 unreached_rejection(t, 'Unregister should not fail') | 12 unreached_rejection(t, 'Unregister should not fail') |
14 ); | 13 ); |
15 | 14 |
16 function doTest() { | 15 function doRegister() { |
17 navigator.serviceWorker.register( | 16 navigator.serviceWorker.register( |
18 'resources/worker-no-op.js', {scope: scope} | 17 'resources/postmessage-to-client-worker.js', {scope: scope} |
19 ).then( | 18 ).then( |
20 onRegister, | 19 onRegister, |
21 unreached_rejection(t, 'Registration should succeed, but failed') | 20 unreached_rejection(t, 'Registration should succeed, but failed') |
22 ); | 21 ); |
23 } | 22 } |
24 | 23 |
25 function onRegister(worker) { | 24 function onRegister(worker) { |
26 worker.addEventListener('statechange', t.step_func(function(event) { | 25 worker.addEventListener('statechange', t.step_func(function(event) { |
27 if (event.target.state == 'active') | 26 if (event.target.state == 'active') |
28 onActive(); | 27 onActive(); |
29 })); | 28 })); |
30 } | 29 } |
31 | 30 |
32 function onActive() { | 31 function onActive() { |
33 with_iframe(scope, t.step_func(function(frame) { | 32 with_iframe(scope, t.step_func(function(frame) { |
34 var w = frame.contentWindow; | 33 var w = frame.contentWindow; |
35 assert_true(w.navigator.serviceWorker.current instanceof w.ServiceWo
rker, | 34 w.onmessage = t.step_func(onMessage); |
36 'current should be a ServiceWorker object'); | 35 w.navigator.serviceWorker.current.postMessage('ping'); |
| 36 })); |
| 37 } |
| 38 |
| 39 var result = []; |
| 40 var expected = ['Sending message via clients']; |
| 41 |
| 42 function onMessage(e) { |
| 43 var message = e.data; |
| 44 if (message === 'quit') { |
| 45 assert_array_equals(result, expected, 'Worker should post back expec
ted messages.'); |
37 t.done(); | 46 t.done(); |
38 })); | 47 } else { |
| 48 result.push(message); |
| 49 } |
39 } | 50 } |
40 }); | 51 }); |
41 </script> | 52 </script> |
42 </body> | |
OLD | NEW |