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