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/test-helpers.sub.js"></script> | 5 <script src="resources/test-helpers.sub.js"></script> |
6 <script src="/common/get-host-info.sub.js"></script> | 6 <script src="/common/get-host-info.sub.js"></script> |
7 <script src="../fetch/resources/fetch-test-helpers.sub.js"></script> | |
8 <script> | 7 <script> |
9 // Purge the existing registrations for the origin. | 8 // Purge the existing registrations for the origin. |
10 // getRegistrations() is used in order to avoid adding additional complexity | 9 // getRegistrations() is used in order to avoid adding additional complexity |
11 // e.g. adding an internal function. | 10 // e.g. adding an internal function. |
12 promise_test(function(t) { | 11 promise_test(function() { |
13 return navigator.serviceWorker.getRegistrations() | 12 return navigator.serviceWorker.getRegistrations() |
14 .then(function(registrations) { | 13 .then(function(registrations) { |
15 return registrations.reduce(function(sequence, registration) { | 14 return registrations.reduce(function(sequence, registration) { |
16 return sequence.then(function() { | 15 return sequence.then(function() { |
17 return registration.unregister(); | 16 return registration.unregister(); |
18 }); | 17 }); |
19 }, Promise.resolve()); | 18 }, Promise.resolve()); |
20 }); | 19 }) |
21 }, 'Purge the existing registrations.'); | 20 .then(function() { |
22 | 21 return navigator.serviceWorker.getRegistrations(); |
23 promise_test(function(t) { | 22 }) |
24 return navigator.serviceWorker.getRegistrations() | |
25 .then(function(value) { | 23 .then(function(value) { |
26 assert_array_equals( | 24 assert_array_equals( |
27 value, | 25 value, |
28 [], | 26 [], |
29 'getRegistrations should resolve with an empty array.'); | 27 'getRegistrations should resolve with an empty array.'); |
30 }); | 28 }); |
31 }, 'getRegistrations'); | 29 }, 'registrations are not returned following unregister'); |
32 | 30 |
33 promise_test(function(t) { | 31 promise_test(function(t) { |
34 var scope = 'resources/scope/getregistrations/normal'; | 32 var scope = 'resources/scope/getregistrations/normal'; |
35 var script = 'resources/empty-worker.js'; | 33 var script = 'resources/empty-worker.js'; |
36 var registrations = []; | 34 var registrations = []; |
37 return service_worker_unregister_and_register(t, script, scope) | 35 return service_worker_unregister_and_register(t, script, scope) |
38 .then(function(r) { | 36 .then(function(r) { |
39 registrations.push(r); | 37 registrations.push(r); |
40 return navigator.serviceWorker.getRegistrations(); | 38 return navigator.serviceWorker.getRegistrations(); |
41 }) | 39 }) |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
89 value, | 87 value, |
90 [], | 88 [], |
91 'getRegistrations should resolve with an empty array.'); | 89 'getRegistrations should resolve with an empty array.'); |
92 }); | 90 }); |
93 }, 'Register then Unregister then getRegistrations'); | 91 }, 'Register then Unregister then getRegistrations'); |
94 | 92 |
95 promise_test(function(t) { | 93 promise_test(function(t) { |
96 var scope = 'resources/scope/getregistrations/register-unregister-controlled
'; | 94 var scope = 'resources/scope/getregistrations/register-unregister-controlled
'; |
97 var script = 'resources/empty-worker.js'; | 95 var script = 'resources/empty-worker.js'; |
98 var registrations; | 96 var registrations; |
99 var frame; | |
100 return service_worker_unregister_and_register(t, script, scope) | 97 return service_worker_unregister_and_register(t, script, scope) |
101 .then(function(r) { | 98 .then(function(r) { |
102 registration = r; | 99 registration = r; |
103 return wait_for_state(t, registration.installing, 'activated'); | 100 return wait_for_state(t, registration.installing, 'activated'); |
104 }) | 101 }) |
105 .then(function() { | 102 .then(function() { |
106 return with_iframe(scope); | 103 return with_iframe(scope); |
107 }) | 104 }) |
108 .then(function(f) { | 105 .then(function(f) { |
109 frame = f; | 106 t.add_cleanup(function() { f.remove(); }); |
110 return registration.unregister(); | 107 return registration.unregister(); |
111 }) | 108 }) |
112 .then(function() { | 109 .then(function() { |
113 return navigator.serviceWorker.getRegistrations(); | 110 return navigator.serviceWorker.getRegistrations(); |
114 }) | 111 }) |
115 .then(function(value) { | 112 .then(function(value) { |
116 assert_array_equals( | 113 assert_array_equals( |
117 value, | 114 value, |
118 [], | 115 [], |
119 'getRegistrations should resolve with an empty array.'); | 116 'getRegistrations should resolve with an empty array.'); |
120 assert_equals(registration.installing, null); | 117 assert_equals(registration.installing, null); |
121 assert_equals(registration.waiting, null); | 118 assert_equals(registration.waiting, null); |
122 assert_equals(registration.active.state, 'activated'); | 119 assert_equals(registration.active.state, 'activated'); |
123 frame.remove(); | |
124 }); | 120 }); |
125 }, 'Register then Unregister with controlled frame then getRegistrations'); | 121 }, 'Register then Unregister with controlled frame then getRegistrations'); |
126 | 122 |
127 promise_test(function(t) { | 123 promise_test(function(t) { |
128 var host_info = get_host_info(); | 124 var host_info = get_host_info(); |
129 // Rewrite the url to point to remote origin. | 125 // Rewrite the url to point to remote origin. |
130 var frame_same_origin_url = new URL("resources/frame-for-getregistrations.ht
ml", window.location); | 126 var frame_same_origin_url = new URL("resources/frame-for-getregistrations.ht
ml", window.location); |
131 var frame_url = host_info['HTTPS_REMOTE_ORIGIN'] + frame_same_origin_url.pat
hname; | 127 var frame_url = host_info['HTTPS_REMOTE_ORIGIN'] + frame_same_origin_url.pat
hname; |
132 var scope = 'resources/scope-for-getregistrations'; | 128 var scope = 'resources/scope-for-getregistrations'; |
133 var script = 'resources/empty-worker.js'; | 129 var script = 'resources/empty-worker.js'; |
(...skipping 16 matching lines...) Expand all Loading... |
150 }); | 146 }); |
151 } | 147 } |
152 | 148 |
153 // We need this special frame loading function because the frame is going | 149 // We need this special frame loading function because the frame is going |
154 // to register it's own service worker and there is the possibility that tha
t | 150 // to register it's own service worker and there is the possibility that tha
t |
155 // register() finishes after the register() for the same domain later in the | 151 // register() finishes after the register() for the same domain later in the |
156 // test. So we have to wait until the cross origin register() is done, and n
ot | 152 // test. So we have to wait until the cross origin register() is done, and n
ot |
157 // just until the frame loads. | 153 // just until the frame loads. |
158 return with_iframe_ready(frame_url) | 154 return with_iframe_ready(frame_url) |
159 .then(function(f) { | 155 .then(function(f) { |
| 156 t.add_cleanup(function() { f.remove(); }); |
160 frame = f; | 157 frame = f; |
161 return service_worker_unregister_and_register(t, script, scope); | 158 return service_worker_unregister_and_register(t, script, scope); |
162 }) | 159 }) |
163 .then(function(r) { | 160 .then(function(r) { |
164 registrations.push(r); | 161 registrations.push(r); |
165 return navigator.serviceWorker.getRegistrations(); | 162 return navigator.serviceWorker.getRegistrations(); |
166 }) | 163 }) |
167 .then(function(value) { | 164 .then(function(value) { |
168 assert_array_equals( | 165 assert_array_equals( |
169 value, | 166 value, |
170 registrations, | 167 registrations, |
171 'getRegistrations should only return same origin registrations.'); | 168 'getRegistrations should only return same origin registrations.'); |
172 | 169 |
173 var channel = new MessageChannel(); | 170 var channel = new MessageChannel(); |
174 var resolve; | 171 var resolve; |
175 var p = new Promise(function(r) { resolve = r; }); | 172 var p = new Promise(function(r) { resolve = r; }); |
176 | 173 |
177 channel.port1.onmessage = function(e) { | 174 channel.port1.onmessage = function(e) { |
178 if (e.data == 'unregistered') | 175 if (e.data == 'unregistered') |
179 resolve(); | 176 resolve(); |
180 }; | 177 }; |
181 frame.contentWindow.postMessage('unregister', '*', [channel.port2]); | 178 frame.contentWindow.postMessage('unregister', '*', [channel.port2]); |
182 return p; | 179 return p; |
183 }) | 180 }) |
184 .then(function() { | 181 .then(function() { |
185 frame.remove(); | |
186 return service_worker_unregister(t, scope); | 182 return service_worker_unregister(t, scope); |
187 }); | 183 }); |
188 }, 'getRegistrations promise resolves only with same origin registrations.'); | 184 }, 'getRegistrations promise resolves only with same origin registrations.'); |
189 | |
190 done(); | |
191 </script> | 185 </script> |
OLD | NEW |