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.sub.js"></script> |
| 6 <script src="resources/get-host-info.sub.js"></script> |
| 7 <script src="../fetch/resources/fetch-test-helpers.sub.js"></script> |
| 8 <script> |
| 9 // Purge the existing registrations for the origin. |
| 10 // getRegistrations() is used in order to avoid adding additional complexity |
| 11 // e.g. adding an internal function. |
| 12 promise_test(function(t) { |
| 13 return navigator.serviceWorker.getRegistrations() |
| 14 .then(function(registrations) { |
| 15 return registrations.reduce(function(sequence, registration) { |
| 16 return sequence.then(function() { |
| 17 return registration.unregister(); |
| 18 }); |
| 19 }, Promise.resolve()); |
| 20 }); |
| 21 }, 'Purge the existing registrations.'); |
| 22 |
| 23 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 promise_test(function(t) { |
| 34 var scope = 'resources/scope/getregistrations/normal'; |
| 35 var script = 'resources/empty-worker.js'; |
| 36 var registrations = []; |
| 37 return service_worker_unregister_and_register(t, script, scope) |
| 38 .then(function(r) { |
| 39 registrations.push(r); |
| 40 return navigator.serviceWorker.getRegistrations(); |
| 41 }) |
| 42 .then(function(value) { |
| 43 assert_array_equals( |
| 44 value, |
| 45 registrations, |
| 46 'getRegistrations should resolve with array of registrations.'); |
| 47 return service_worker_unregister(t, scope); |
| 48 }); |
| 49 }, 'Register then getRegistrations'); |
| 50 |
| 51 promise_test(function(t) { |
| 52 var scope1 = 'resources/scope/getregistrations/scope1'; |
| 53 var scope2 = 'resources/scope/getregistrations/scope2'; |
| 54 var script = 'resources/empty-worker.js'; |
| 55 var registrations = []; |
| 56 return service_worker_unregister_and_register(t, script, scope1) |
| 57 .then(function(r) { |
| 58 registrations.push(r); |
| 59 return service_worker_unregister_and_register(t, script, scope2); |
| 60 }) |
| 61 .then(function(r) { |
| 62 registrations.push(r); |
| 63 return navigator.serviceWorker.getRegistrations(); |
| 64 }) |
| 65 .then(function(value) { |
| 66 assert_array_equals( |
| 67 value, |
| 68 registrations, |
| 69 'getRegistrations should resolve with array of registrations.'); |
| 70 return service_worker_unregister(t, scope1); |
| 71 }) |
| 72 .then(function() { |
| 73 return service_worker_unregister(t, scope2); |
| 74 }); |
| 75 }, 'Register multiple times then getRegistrations'); |
| 76 |
| 77 promise_test(function(t) { |
| 78 var scope = 'resources/scope/getregistrations/register-unregister'; |
| 79 var script = 'resources/empty-worker.js'; |
| 80 return service_worker_unregister_and_register(t, script, scope) |
| 81 .then(function(registration) { |
| 82 return registration.unregister(); |
| 83 }) |
| 84 .then(function() { |
| 85 return navigator.serviceWorker.getRegistrations(); |
| 86 }) |
| 87 .then(function(value) { |
| 88 assert_array_equals( |
| 89 value, |
| 90 [], |
| 91 'getRegistrations should resolve with an empty array.'); |
| 92 }); |
| 93 }, 'Register then Unregister then getRegistrations'); |
| 94 |
| 95 promise_test(function(t) { |
| 96 var scope = 'resources/scope/getregistrations/register-unregister-controlled
'; |
| 97 var script = 'resources/empty-worker.js'; |
| 98 var registrations; |
| 99 var frame; |
| 100 return service_worker_unregister_and_register(t, script, scope) |
| 101 .then(function(r) { |
| 102 registration = r; |
| 103 return wait_for_state(t, registration.installing, 'activated'); |
| 104 }) |
| 105 .then(function() { |
| 106 return with_iframe(scope); |
| 107 }) |
| 108 .then(function(f) { |
| 109 frame = f; |
| 110 return registration.unregister(); |
| 111 }) |
| 112 .then(function() { |
| 113 return navigator.serviceWorker.getRegistrations(); |
| 114 }) |
| 115 .then(function(value) { |
| 116 assert_array_equals( |
| 117 value, |
| 118 [], |
| 119 'getRegistrations should resolve with an empty array.'); |
| 120 assert_equals(registration.installing, null); |
| 121 assert_equals(registration.waiting, null); |
| 122 assert_equals(registration.active.state, 'activated'); |
| 123 frame.remove(); |
| 124 }); |
| 125 }, 'Register then Unregister with controlled frame then getRegistrations'); |
| 126 |
| 127 promise_test(function(t) { |
| 128 var host_info = get_host_info(); |
| 129 // Rewrite the url to point to remote origin. |
| 130 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; |
| 132 var scope = 'resources/scope-for-getregistrations'; |
| 133 var script = 'resources/empty-worker.js'; |
| 134 var frame; |
| 135 var registrations = []; |
| 136 |
| 137 // Loads an iframe and waits for 'ready' message from it to resolve promise. |
| 138 // Caller is responsible for removing frame. |
| 139 function with_iframe_ready(url) { |
| 140 return new Promise(function(resolve) { |
| 141 var frame = document.createElement('iframe'); |
| 142 frame.src = url; |
| 143 window.addEventListener('message', function onMessage(e) { |
| 144 window.removeEventListener('message', onMessage); |
| 145 if (e.data == 'ready') { |
| 146 resolve(frame); |
| 147 } |
| 148 }); |
| 149 document.body.appendChild(frame); |
| 150 }); |
| 151 } |
| 152 |
| 153 // 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 |
| 155 // 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 |
| 157 // just until the frame loads. |
| 158 return with_iframe_ready(frame_url) |
| 159 .then(function(f) { |
| 160 frame = f; |
| 161 return service_worker_unregister_and_register(t, script, scope); |
| 162 }) |
| 163 .then(function(r) { |
| 164 registrations.push(r); |
| 165 return navigator.serviceWorker.getRegistrations(); |
| 166 }) |
| 167 .then(function(value) { |
| 168 assert_array_equals( |
| 169 value, |
| 170 registrations, |
| 171 'getRegistrations should only return same origin registrations.'); |
| 172 |
| 173 var channel = new MessageChannel(); |
| 174 var resolve; |
| 175 var p = new Promise(function(r) { resolve = r; }); |
| 176 |
| 177 channel.port1.onmessage = function(e) { |
| 178 if (e.data == 'unregistered') |
| 179 resolve(); |
| 180 }; |
| 181 frame.contentWindow.postMessage('unregister', '*', [channel.port2]); |
| 182 return p; |
| 183 }) |
| 184 .then(function() { |
| 185 frame.remove(); |
| 186 return service_worker_unregister(t, scope); |
| 187 }); |
| 188 }, 'getRegistrations promise resolves only with same origin registrations.'); |
| 189 |
| 190 done(); |
| 191 </script> |
OLD | NEW |