OLD | NEW |
1 // Adapter for testharness.js-style tests with Service Workers | 1 // Adapter for testharness.js-style tests with Service Workers |
2 | 2 |
3 function service_worker_unregister_and_register(test, url, scope) { | 3 function service_worker_unregister_and_register(test, url, scope) { |
4 if (!scope || scope.length == 0) | 4 if (!scope || scope.length == 0) |
5 return Promise.reject(new Error('tests must define a scope')); | 5 return Promise.reject(new Error('tests must define a scope')); |
6 | 6 |
7 var options = { scope: scope }; | 7 var options = { scope: scope }; |
8 return service_worker_unregister(test, scope) | 8 return service_worker_unregister(test, scope) |
9 .then(function() { | 9 .then(function() { |
10 return navigator.serviceWorker.register(url, options); | 10 return navigator.serviceWorker.register(url, options); |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
126 } | 126 } |
127 | 127 |
128 return new Promise(test.step_func(function(resolve) { | 128 return new Promise(test.step_func(function(resolve) { |
129 worker.addEventListener('statechange', test.step_func(function() { | 129 worker.addEventListener('statechange', test.step_func(function() { |
130 if (worker.state === state) | 130 if (worker.state === state) |
131 resolve(state); | 131 resolve(state); |
132 })); | 132 })); |
133 })); | 133 })); |
134 } | 134 } |
135 | 135 |
136 function wait_for_activated(test, registration) { | |
137 var expected_state = 'activated'; | |
138 if (registration.active) | |
139 return wait_for_state(test, registration.active, expected_state); | |
140 if (registration.waiting) | |
141 return wait_for_state(test, registration.waiting, expected_state); | |
142 if (registration.installing) | |
143 return wait_for_state(test, registration.installing, expected_state); | |
144 return Promise.reject( | |
145 new Error('registration must have at least one version')); | |
146 } | |
147 | |
148 // Declare a test that runs entirely in the ServiceWorkerGlobalScope. The |url| | 136 // Declare a test that runs entirely in the ServiceWorkerGlobalScope. The |url| |
149 // is the service worker script URL. This function: | 137 // is the service worker script URL. This function: |
150 // - Instantiates a new test with the description specified in |description|. | 138 // - Instantiates a new test with the description specified in |description|. |
151 // The test will succeed if the specified service worker can be successfully | 139 // The test will succeed if the specified service worker can be successfully |
152 // registered and installed. | 140 // registered and installed. |
153 // - Creates a new ServiceWorker registration with a scope unique to the current | 141 // - Creates a new ServiceWorker registration with a scope unique to the current |
154 // document URL. Note that this doesn't allow more than one | 142 // document URL. Note that this doesn't allow more than one |
155 // service_worker_test() to be run from the same document. | 143 // service_worker_test() to be run from the same document. |
156 // - Waits for the new worker to begin installing. | 144 // - Waits for the new worker to begin installing. |
157 // - Imports tests results from tests running inside the ServiceWorker. | 145 // - Imports tests results from tests running inside the ServiceWorker. |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
214 channel.port1.onmessage = test.step_func(function() { | 202 channel.port1.onmessage = test.step_func(function() { |
215 unload_iframe(frame).catch(function() {}); | 203 unload_iframe(frame).catch(function() {}); |
216 resolve(); | 204 resolve(); |
217 }); | 205 }); |
218 frame.contentWindow.postMessage( | 206 frame.contentWindow.postMessage( |
219 {username: username, password: password, cookie: cookie}, | 207 {username: username, password: password, cookie: cookie}, |
220 [channel.port2], origin); | 208 [channel.port2], origin); |
221 })); | 209 })); |
222 }); | 210 }); |
223 } | 211 } |
OLD | NEW |