OLD | NEW |
---|---|
1 <!DOCTYPE html> | 1 <!DOCTYPE html> |
2 <title>Service Worker: getRegistrations()</title> | 2 <title>Service Worker: getRegistrations()</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/testharness-helpers.js"></script> | 5 <script src="../resources/testharness-helpers.js"></script> |
6 <script src="resources/test-helpers.js"></script> | 6 <script src="resources/test-helpers.js"></script> |
7 <script src="../resources/get-host-info.js"></script> | 7 <script src="../resources/get-host-info.js"></script> |
8 <script> | 8 <script> |
9 // Purge the existing registrations for the origin. | 9 // Purge the existing registrations for the origin. |
10 // getRegistrations() is used in order to avoid adding additional complexity | 10 // getRegistrations() is used in order to avoid adding additional complexity |
11 // e.g. adding an internal function. | 11 // e.g. adding an internal function. |
12 sequential_promise_test(function(t) { | 12 sequential_promise_test(function(t) { |
13 return navigator.serviceWorker.getRegistrations() | 13 var resolve; |
14 .then(function(registrations) { | 14 var timer; |
15 return registrations.reduce(function(sequence, registration) { | 15 var p = new Promise(function(r) { resolve = r; }); |
16 return sequence.then(function() { | 16 navigator.serviceWorker.getRegistrations() |
17 return registration.unregister(); | 17 .then(function(regs) { |
18 }); | 18 return Promise.all(regs.map(function(r) { r.unregister(); })); |
19 }, Promise.resolve()); | 19 }) |
20 .then(function() { | |
21 // As registration.unregister() promises resolve before the | |
22 // corresponding registrations are being delete from the storage, we | |
falken
2015/06/26 07:50:31
"are deleted from storage" reads nicer
| |
23 // are checking whether the registrations have been acutally removed | |
falken
2015/06/26 07:50:31
acutally -> actually
maybe say: "must wait until t
| |
24 // from the storage. | |
25 // Spec reference: https://slightlyoff.github.io/ServiceWorker/spec/se rvice_worker/#unregister-algorithm | |
26 timer = setInterval(function() { | |
27 navigator.serviceWorker.getRegistrations() | |
28 .then(function(regs) { | |
29 if (regs.length == 0) { | |
30 clearInterval(timer); | |
31 resolve(); | |
32 } | |
33 }); | |
34 }, 100); | |
20 }); | 35 }); |
36 return p; | |
21 }, 'Purge the existing registrations.'); | 37 }, 'Purge the existing registrations.'); |
22 | 38 |
23 sequential_promise_test(function(t) { | 39 sequential_promise_test(function(t) { |
24 return navigator.serviceWorker.getRegistrations() | |
25 .then(function(value) { | |
26 assert_array_equals( | |
27 value, | |
28 [], | |
29 'getRegistrations should resolve with an empty array.'); | |
30 }); | |
31 }, 'getRegistrations'); | |
32 | |
33 sequential_promise_test(function(t) { | |
34 var scope = 'resources/scope/getregistrations/normal'; | 40 var scope = 'resources/scope/getregistrations/normal'; |
35 var script = 'resources/empty-worker.js'; | 41 var script = 'resources/empty-worker.js'; |
36 var registrations = []; | 42 var registrations = []; |
37 return service_worker_unregister_and_register(t, script, scope) | 43 return service_worker_unregister_and_register(t, script, scope) |
38 .then(function(r) { | 44 .then(function(r) { |
39 registrations.push(r); | 45 registrations.push(r); |
40 return navigator.serviceWorker.getRegistrations(); | 46 return navigator.serviceWorker.getRegistrations(); |
41 }) | 47 }) |
42 .then(function(value) { | 48 .then(function(value) { |
43 assert_array_equals( | 49 assert_array_equals( |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
86 }) | 92 }) |
87 .then(function(value) { | 93 .then(function(value) { |
88 assert_array_equals( | 94 assert_array_equals( |
89 value, | 95 value, |
90 [], | 96 [], |
91 'getRegistrations should resolve with an empty array.'); | 97 'getRegistrations should resolve with an empty array.'); |
92 }); | 98 }); |
93 }, 'Register then Unregister then getRegistrations'); | 99 }, 'Register then Unregister then getRegistrations'); |
94 | 100 |
95 sequential_promise_test(function(t) { | 101 sequential_promise_test(function(t) { |
96 // Top-level window's origin is http://127.0.0.1:8000 | 102 // Top-level window's origin: http://127.0.0.1:8000. |
97 // Set frame's origin to http://localhost:8000 | 103 // Frame's origin: http://localhost:8000. |
98 var host_info = get_host_info(); | 104 var host_info = get_host_info(); |
99 var frame_url = host_info['HTTP_REMOTE_ORIGIN'] + | 105 var frame_url = host_info['HTTP_REMOTE_ORIGIN'] + |
100 '/serviceworker/resources/frame-for-getregistrations.html'; | 106 '/serviceworker/resources/frame-for-getregistrations.html'; |
101 var scope = 'resources/scope-for-getregistrations'; | 107 var scope = 'resources/scope-for-getregistrations'; |
102 var script = 'resources/empty-worker.js'; | 108 var script = 'resources/empty-worker.js'; |
103 var frame; | 109 var frame; |
104 var registrations = []; | 110 var registrations = []; |
105 | 111 |
106 return with_iframe(frame_url) | 112 return with_iframe(frame_url) |
107 .then(function(f) { | 113 .then(function(f) { |
108 // frame registered its registration scoped | |
109 // http://localhost:8000/serviceworker/resources/scope-for-getregistra tions | |
110 frame = f; | 114 frame = f; |
111 // Top-level window registers its registration scoped | |
112 // http://127.0.0.1:8000/serviceworker/resources/scope-for-getregistra tions | |
113 return service_worker_unregister_and_register(t, script, scope); | |
114 }) | |
115 .then(function(r) { | |
116 registrations.push(r); | |
117 return navigator.serviceWorker.getRegistrations(); | |
118 }) | |
119 .then(function(value) { | |
120 assert_array_equals( | |
121 value, | |
122 registrations, | |
123 'getRegistrations should only return same origin registrations.'); | |
124 | 115 |
125 var channel = new MessageChannel(); | |
126 var resolve; | 116 var resolve; |
127 var p = new Promise(function(r) { resolve = r; }); | 117 var p = new Promise(function(r) { resolve = r; }); |
128 | 118 |
119 var channel = new MessageChannel(); | |
120 | |
129 channel.port1.onmessage = function(e) { | 121 channel.port1.onmessage = function(e) { |
130 if (e.data == 'unregistered') | 122 // Frame's registration is registered. |
123 if (e.data == 'registered') { | |
124 // Top-level window registers a registration scoped | |
125 // http://127.0.0.1:8000/serviceworker/resources/scope-for-getregi strations. | |
126 service_worker_unregister_and_register(t, script, scope) | |
127 .then(function(r) { | |
128 registrations.push(r); | |
129 return navigator.serviceWorker.getRegistrations(); | |
130 }) | |
131 .then(function(value) { | |
132 assert_array_equals(value, registrations, | |
133 'getRegistrations should return only the same origin ' + | |
134 'registrations.'); | |
135 channel.port1.postMessage('unregister'); | |
136 }); | |
137 } else if (e.data == 'unregistered') { | |
131 resolve(); | 138 resolve(); |
139 } | |
132 }; | 140 }; |
133 frame.contentWindow.postMessage('unregister', '*', [channel.port2]); | 141 frame.contentWindow.postMessage('register', '*', [channel.port2]); |
134 return p; | 142 return p; |
135 }) | 143 }) |
136 .then(function() { | 144 .then(function() { |
137 frame.remove(); | 145 frame.remove(); |
138 return service_worker_unregister(t, scope); | 146 return service_worker_unregister(t, scope); |
139 }); | 147 }); |
140 }, 'getRegistrations promise resolves only with same origin registrations.'); | 148 }, 'getRegistrations promise resolves only with same origin registrations.'); |
141 | 149 |
142 done(); | 150 done(); |
143 </script> | 151 </script> |
OLD | NEW |