OLD | NEW |
1 // Adapter for testharness.js-style tests with Service Workers | 1 // Adapter for testharness.js-style tests with Service Workers |
2 | 2 |
3 function service_worker_test(url, description) { | 3 function service_worker_test(url, description) { |
4 var t = async_test(description); | 4 var t = async_test(description); |
5 t.step(function() { | 5 t.step(function() { |
6 var scope = 'nonexistent'; | 6 var scope = 'nonexistent'; |
7 service_worker_unregister_and_register(t, url, scope, onRegistered); | 7 service_worker_unregister_and_register(t, url, scope).then(t.step_func(o
nRegistered)); |
8 | 8 |
9 function onRegistered(worker) { | 9 function onRegistered(worker) { |
10 var messageChannel = new MessageChannel(); | 10 var messageChannel = new MessageChannel(); |
11 messageChannel.port1.onmessage = t.step_func(onMessage); | 11 messageChannel.port1.onmessage = t.step_func(onMessage); |
12 worker.postMessage({port:messageChannel.port2}, [messageChannel.port
2]); | 12 worker.postMessage({port:messageChannel.port2}, [messageChannel.port
2]); |
13 } | 13 } |
14 | 14 |
15 function onMessage(e) { | 15 function onMessage(e) { |
16 assert_equals(e.data, 'pass'); | 16 assert_equals(e.data, 'pass'); |
17 service_worker_unregister_and_done(t, scope); | 17 service_worker_unregister_and_done(t, scope); |
18 } | 18 } |
19 }); | 19 }); |
20 } | 20 } |
21 | 21 |
22 function service_worker_unregister_and_register(test, url, scope, onregister) { | 22 function service_worker_unregister_and_register(test, url, scope) { |
23 var options = scope ? { scope: scope } : {}; | 23 var options = scope ? { scope: scope } : {}; |
24 return navigator.serviceWorker.unregister(scope).then( | 24 return navigator.serviceWorker.unregister(scope).then( |
25 // FIXME: Wrap this with test.step_func once testharness.js is updated. | 25 test.step_func(function() { |
26 function() { | |
27 return navigator.serviceWorker.register(url, options); | 26 return navigator.serviceWorker.register(url, options); |
28 }, | 27 }), |
29 unreached_rejection(test, 'Unregister should not fail') | 28 unreached_rejection(test, 'Unregister should not fail') |
30 ).then( | 29 ).then(test.step_func(function(worker) { |
31 test.step_func(onregister), | 30 return Promise.resolve(worker); |
| 31 }), |
32 unreached_rejection(test, 'Registration should not fail') | 32 unreached_rejection(test, 'Registration should not fail') |
33 ); | 33 ); |
34 } | 34 } |
35 | 35 |
36 function service_worker_unregister_and_done(test, scope) { | 36 function service_worker_unregister_and_done(test, scope) { |
37 return navigator.serviceWorker.unregister(scope).then( | 37 return navigator.serviceWorker.unregister(scope).then( |
38 test.done.bind(test), | 38 test.done.bind(test), |
39 unreached_rejection(test, 'Unregister should not fail')); | 39 unreached_rejection(test, 'Unregister should not fail')); |
40 } | 40 } |
41 | 41 |
(...skipping 15 matching lines...) Expand all Loading... |
57 } | 57 } |
58 resolve(frame); | 58 resolve(frame); |
59 }; | 59 }; |
60 document.body.appendChild(frame); | 60 document.body.appendChild(frame); |
61 }); | 61 }); |
62 } | 62 } |
63 | 63 |
64 function normalizeURL(url) { | 64 function normalizeURL(url) { |
65 return new URL(url, document.location).toString().replace(/#.*$/, ''); | 65 return new URL(url, document.location).toString().replace(/#.*$/, ''); |
66 } | 66 } |
OLD | NEW |