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