OLD | NEW |
| (Empty) |
1 <!DOCTYPE html> | |
2 <title>Service Worker: getRegistrations()</title> | |
3 <script src="../resources/testharness.js"></script> | |
4 <script src="../resources/testharnessreport.js"></script> | |
5 <script src="resources/test-helpers.js"></script> | |
6 <script src="../resources/get-host-info.js"></script> | |
7 <script> | |
8 // Purge the existing registrations for the origin. | |
9 // getRegistrations() is used in order to avoid adding additional complexity | |
10 // e.g. adding an internal function. | |
11 promise_test(function(t) { | |
12 var resolve; | |
13 var timer; | |
14 var p = new Promise(function(r) { resolve = r; }); | |
15 navigator.serviceWorker.getRegistrations() | |
16 .then(function(regs) { | |
17 return Promise.all(regs.map(function(r) { r.unregister(); })); | |
18 }) | |
19 .then(function() { | |
20 // As registration.unregister() promises resolve before the | |
21 // corresponding registrations are deleted from the storage, we must | |
22 // wait until the registrations are actually removed from the storage. | |
23 // Spec reference: https://slightlyoff.github.io/ServiceWorker/spec/se
rvice_worker/#unregister-algorithm | |
24 timer = setInterval(function() { | |
25 navigator.serviceWorker.getRegistrations() | |
26 .then(function(regs) { | |
27 if (regs.length == 0) { | |
28 clearInterval(timer); | |
29 resolve(); | |
30 } | |
31 }); | |
32 }, 100); | |
33 }); | |
34 return p; | |
35 }, 'Purge the existing registrations.'); | |
36 | |
37 promise_test(function(t) { | |
38 var scope = 'resources/scope/getregistrations/normal'; | |
39 var script = 'resources/empty-worker.js'; | |
40 var registrations = []; | |
41 return service_worker_unregister_and_register(t, script, scope) | |
42 .then(function(r) { | |
43 registrations.push(r); | |
44 return navigator.serviceWorker.getRegistrations(); | |
45 }) | |
46 .then(function(value) { | |
47 assert_array_equals( | |
48 value, | |
49 registrations, | |
50 'getRegistrations should resolve with array of registrations.'); | |
51 return service_worker_unregister(t, scope); | |
52 }); | |
53 }, 'Register then getRegistrations'); | |
54 | |
55 promise_test(function(t) { | |
56 var scope1 = 'resources/scope/getregistrations/scope1'; | |
57 var scope2 = 'resources/scope/getregistrations/scope2'; | |
58 var script = 'resources/empty-worker.js'; | |
59 var registrations = []; | |
60 return service_worker_unregister_and_register(t, script, scope1) | |
61 .then(function(r) { | |
62 registrations.push(r); | |
63 return service_worker_unregister_and_register(t, script, scope2); | |
64 }) | |
65 .then(function(r) { | |
66 registrations.push(r); | |
67 return navigator.serviceWorker.getRegistrations(); | |
68 }) | |
69 .then(function(value) { | |
70 assert_array_equals( | |
71 value, | |
72 registrations, | |
73 'getRegistrations should resolve with array of registrations.'); | |
74 return service_worker_unregister(t, scope1); | |
75 }) | |
76 .then(function() { | |
77 return service_worker_unregister(t, scope2); | |
78 }); | |
79 }, 'Register multiple times then getRegistrations'); | |
80 | |
81 promise_test(function(t) { | |
82 var scope = 'resources/scope/getregistrations/register-unregister'; | |
83 var script = 'resources/empty-worker.js'; | |
84 return service_worker_unregister_and_register(t, script, scope) | |
85 .then(function(registration) { | |
86 return registration.unregister(); | |
87 }) | |
88 .then(function() { | |
89 return navigator.serviceWorker.getRegistrations(); | |
90 }) | |
91 .then(function(value) { | |
92 assert_array_equals( | |
93 value, | |
94 [], | |
95 'getRegistrations should resolve with an empty array.'); | |
96 }); | |
97 }, 'Register then Unregister then getRegistrations'); | |
98 | |
99 promise_test(function(t) { | |
100 // Top-level window's origin: http://127.0.0.1:8000. | |
101 // Frame's origin: http://localhost:8000. | |
102 var host_info = get_host_info(); | |
103 var frame_url = host_info['HTTP_REMOTE_ORIGIN'] + | |
104 '/serviceworker/resources/frame-for-getregistrations.html'; | |
105 var scope = 'resources/scope-for-getregistrations'; | |
106 var script = 'resources/empty-worker.js'; | |
107 var frame; | |
108 var registrations = []; | |
109 | |
110 return with_iframe(frame_url) | |
111 .then(function(f) { | |
112 frame = f; | |
113 | |
114 var resolve; | |
115 var p = new Promise(function(r) { resolve = r; }); | |
116 | |
117 var channel = new MessageChannel(); | |
118 | |
119 channel.port1.onmessage = function(e) { | |
120 // Frame's registration is registered. | |
121 if (e.data == 'registered') { | |
122 // Top-level window registers a registration scoped | |
123 // http://127.0.0.1:8000/serviceworker/resources/scope-for-getregi
strations. | |
124 service_worker_unregister_and_register(t, script, scope) | |
125 .then(function(r) { | |
126 registrations.push(r); | |
127 return navigator.serviceWorker.getRegistrations(); | |
128 }) | |
129 .then(function(value) { | |
130 assert_array_equals(value, registrations, | |
131 'getRegistrations should return only the same origin ' + | |
132 'registrations.'); | |
133 channel.port1.postMessage('unregister'); | |
134 }); | |
135 } else if (e.data == 'unregistered') { | |
136 resolve(); | |
137 } | |
138 }; | |
139 frame.contentWindow.postMessage('register', '*', [channel.port2]); | |
140 return p; | |
141 }) | |
142 .then(function() { | |
143 frame.remove(); | |
144 return service_worker_unregister(t, scope); | |
145 }); | |
146 }, 'getRegistrations promise resolves only with same origin registrations.'); | |
147 | |
148 done(); | |
149 </script> | |
OLD | NEW |