|
| 1 | + |
| 2 | + |
| 3 | + |
| 4 | + |
| 5 | + |
| 6 | + |
| 7 | + |
| 8 | + |
| 9 | + |
| 10 | + |
| 11 | + |
| 12 | +function service_worker_unregister_and_register(test, url, scope, options) { |
| 13 | + if (!scope || scope.length == 0) |
| 14 | + return Promise.reject(new Error('tests must define a scope')); |
| 15 | + |
| 16 | + if (options && options.scope) |
| 17 | + return Promise.reject(new Error('scope must not be passed in options')); |
| 18 | + |
| 19 | + options = Object.assign({ scope: scope }, options); |
| 20 | + return service_worker_unregister(test, scope) |
| 21 | + .then(function() { |
| 22 | + return navigator.serviceWorker.register(url, options); |
| 23 | + }) |
| 24 | + .catch(unreached_rejection(test, |
| 25 | + 'unregister and register should not fail')); |
| 26 | +} |
| 27 | + |
| 28 | + |
| 29 | + |
| 30 | +function service_worker_unregister(test, scope) { |
| 31 | + var absoluteScope = (new URL(scope, window.location).href); |
| 32 | + return navigator.serviceWorker.getRegistration(scope) |
| 33 | + .then(function(registration) { |
| 34 | + if (registration && registration.scope === absoluteScope) |
| 35 | + return registration.unregister(); |
| 36 | + }) |
| 37 | + .catch(unreached_rejection(test, 'unregister should not fail')); |
| 38 | +} |
| 39 | + |
| 40 | +function service_worker_unregister_and_done(test, scope) { |
| 41 | + return service_worker_unregister(test, scope) |
| 42 | + .then(test.done.bind(test)); |
| 43 | +} |
| 44 | + |
| 45 | +function unreached_fulfillment(test, prefix) { |
| 46 | + return test.step_func(function(result) { |
| 47 | + var error_prefix = prefix || 'unexpected fulfillment'; |
| 48 | + assert_unreached(error_prefix + ': ' + result); |
| 49 | + }); |
| 50 | +} |
| 51 | + |
| 52 | + |
| 53 | +function unreached_rejection(test, prefix) { |
| 54 | + return test.step_func(function(error) { |
| 55 | + var reason = error.message || error.name || error; |
| 56 | + var error_prefix = prefix || 'unexpected rejection'; |
| 57 | + assert_unreached(error_prefix + ': ' + reason); |
| 58 | + }); |
| 59 | +} |
| 60 | + |
| 61 | + |
| 62 | + |
| 63 | + |
| 64 | + |
| 65 | + |
| 66 | + |
| 67 | + |
| 68 | + |
| 69 | +function with_iframe(url) { |
| 70 | + return new Promise(function(resolve) { |
| 71 | + var frame = document.createElement('iframe'); |
| 72 | + frame.className = 'test-iframe'; |
| 73 | + frame.src = url; |
| 74 | + frame.onload = function() { resolve(frame); }; |
| 75 | + document.body.appendChild(frame); |
| 76 | + }); |
| 77 | +} |
| 78 | + |
| 79 | +function normalizeURL(url) { |
| 80 | + return new URL(url, self.location).toString().replace(/#.*$/, ''); |
| 81 | +} |
| 82 | + |
| 83 | +function wait_for_update(test, registration) { |
| 84 | + if (!registration || registration.unregister == undefined) { |
| 85 | + return Promise.reject(new Error( |
| 86 | + 'wait_for_update must be passed a ServiceWorkerRegistration')); |
| 87 | + } |
| 88 | + |
| 89 | + return new Promise(test.step_func(function(resolve) { |
| 90 | + var handler = test.step_func(function() { |
| 91 | + registration.removeEventListener('updatefound', handler); |
| 92 | + resolve(registration.installing); |
| 93 | + }); |
| 94 | + registration.addEventListener('updatefound', handler); |
| 95 | + })); |
| 96 | +} |
| 97 | + |
| 98 | + |
| 99 | +function is_state_advanced(state_a, state_b) { |
| 100 | + if (state_b === 'installing') { |
| 101 | + switch (state_a) { |
| 102 | + case 'installed': |
| 103 | + case 'activating': |
| 104 | + case 'activated': |
| 105 | + case 'redundant': |
| 106 | + return true; |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + if (state_b === 'installed') { |
| 111 | + switch (state_a) { |
| 112 | + case 'activating': |
| 113 | + case 'activated': |
| 114 | + case 'redundant': |
| 115 | + return true; |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + if (state_b === 'activating') { |
| 120 | + switch (state_a) { |
| 121 | + case 'activated': |
| 122 | + case 'redundant': |
| 123 | + return true; |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + if (state_b === 'activated') { |
| 128 | + switch (state_a) { |
| 129 | + case 'redundant': |
| 130 | + return true; |
| 131 | + } |
| 132 | + } |
| 133 | + return false; |
| 134 | +} |
| 135 | + |
| 136 | +function wait_for_state(test, worker, state) { |
| 137 | + if (!worker || worker.state == undefined) { |
| 138 | + return Promise.reject(new Error( |
| 139 | + 'wait_for_state needs a ServiceWorker object to be passed.')); |
| 140 | + } |
| 141 | + if (worker.state === state) |
| 142 | + return Promise.resolve(state); |
| 143 | + |
| 144 | + if (is_state_advanced(worker.state, state)) { |
| 145 | + return Promise.reject(new Error( |
| 146 | + `Waiting for ${state} but the worker is already ${worker.state}.`)); |
| 147 | + } |
| 148 | + return new Promise(test.step_func(function(resolve, reject) { |
| 149 | + worker.addEventListener('statechange', test.step_func(function() { |
| 150 | + if (worker.state === state) |
| 151 | + resolve(state); |
| 152 | + |
| 153 | + if (is_state_advanced(worker.state, state)) { |
| 154 | + reject(new Error( |
| 155 | + `The state of the worker becomes ${worker.state} while waiting` + |
| 156 | + `for ${state}.`)); |
| 157 | + } |
| 158 | + })); |
| 159 | + })); |
| 160 | +} |
| 161 | + |
| 162 | + |
| 163 | + |
| 164 | + |
| 165 | + |
| 166 | + |
| 167 | + |
| 168 | + |
| 169 | + |
| 170 | + |
| 171 | + |
| 172 | +function service_worker_test(url, description) { |
| 173 | + |
| 174 | + |
| 175 | + |
| 176 | + var scope = new URL('scope' + window.location.pathname, |
| 177 | + new URL(url, window.location)).toString(); |
| 178 | + promise_test(function(test) { |
| 179 | + return service_worker_unregister_and_register(test, url, scope) |
| 180 | + .then(function(registration) { |
| 181 | + add_completion_callback(function() { |
| 182 | + registration.unregister(); |
| 183 | + }); |
| 184 | + return wait_for_update(test, registration) |
| 185 | + .then(function(worker) { |
| 186 | + return fetch_tests_from_worker(worker); |
| 187 | + }); |
| 188 | + }); |
| 189 | + }, description); |
| 190 | +} |
| 191 | + |
| 192 | +function base_path() { |
| 193 | + return location.pathname.replace(/\/[^\/]*$/, '/'); |
| 194 | +} |
| 195 | + |
| 196 | +function test_login(test, origin, username, password, cookie) { |
| 197 | + return new Promise(function(resolve, reject) { |
| 198 | + with_iframe( |
| 199 | + origin + base_path() + |
| 200 | + 'resources/fetch-access-control-login.html') |
| 201 | + .then(test.step_func(function(frame) { |
| 202 | + var channel = new MessageChannel(); |
| 203 | + channel.port1.onmessage = test.step_func(function() { |
| 204 | + frame.remove(); |
| 205 | + resolve(); |
| 206 | + }); |
| 207 | + frame.contentWindow.postMessage( |
| 208 | + {username: username, password: password, cookie: cookie}, |
| 209 | + origin, [channel.port2]); |
| 210 | + })); |
| 211 | + }); |
| 212 | +} |
| 213 | + |
| 214 | +function test_websocket(test, frame, url) { |
| 215 | + return new Promise(function(resolve, reject) { |
| 216 | + var ws = new frame.contentWindow.WebSocket(url, ['echo', 'chat']); |
| 217 | + var openCalled = false; |
| 218 | + ws.addEventListener('open', test.step_func(function(e) { |
| 219 | + assert_equals(ws.readyState, 1, "The WebSocket should be open"); |
| 220 | + openCalled = true; |
| 221 | + ws.close(); |
| 222 | + }), true); |
| 223 | + |
| 224 | + ws.addEventListener('close', test.step_func(function(e) { |
| 225 | + assert_true(openCalled, "The WebSocket should be closed after being opened"); |
| 226 | + resolve(); |
| 227 | + }), true); |
| 228 | + |
| 229 | + ws.addEventListener('error', reject); |
| 230 | + }); |
| 231 | +} |
| 232 | + |
| 233 | +function login_https(test) { |
| 234 | + var host_info = get_host_info(); |
| 235 | + return test_login(test, host_info.HTTPS_REMOTE_ORIGIN, |
| 236 | + 'username1s', 'password1s', 'cookie1') |
| 237 | + .then(function() { |
| 238 | + return test_login(test, host_info.HTTPS_ORIGIN, |
| 239 | + 'username2s', 'password2s', 'cookie2'); |
| 240 | + }); |
| 241 | +} |
| 242 | + |
| 243 | +function websocket(test, frame) { |
| 244 | + return test_websocket(test, frame, get_websocket_url()); |
| 245 | +} |
| 246 | + |
| 247 | +function get_websocket_url() { |
| 248 | + return 'wss://{{host}}:{{ports[wss][0]}}/echo'; |
| 249 | +} |
| 250 | + |
| 251 | + |
| 252 | + |
| 253 | + |
| 254 | + |
| 255 | + |
| 256 | + |
| 257 | +function get_newest_worker(registration) { |
| 258 | + if (registration.installing) |
| 259 | + return registration.installing; |
| 260 | + if (registration.waiting) |
| 261 | + return registration.waiting; |
| 262 | + if (registration.active) |
| 263 | + return registration.active; |
| 264 | +} |
| 265 | + |
| 266 | +function register_using_link(script, options) { |
| 267 | + var scope = options.scope; |
| 268 | + var link = document.createElement('link'); |
| 269 | + link.setAttribute('rel', 'serviceworker'); |
| 270 | + link.setAttribute('href', script); |
| 271 | + link.setAttribute('scope', scope); |
| 272 | + document.getElementsByTagName('head')[0].appendChild(link); |
| 273 | + return new Promise(function(resolve, reject) { |
| 274 | + link.onload = resolve; |
| 275 | + link.onerror = reject; |
| 276 | + }) |
| 277 | + .then(() => navigator.serviceWorker.getRegistration(scope)); |
| 278 | +} |
| 279 | + |
| 280 | +function with_sandboxed_iframe(url, sandbox) { |
| 281 | + return new Promise(function(resolve) { |
| 282 | + var frame = document.createElement('iframe'); |
| 283 | + frame.sandbox = sandbox; |
| 284 | + frame.src = url; |
| 285 | + frame.onload = function() { resolve(frame); }; |
| 286 | + document.body.appendChild(frame); |
| 287 | + }); |
| 288 | +} |
| 289 | + |
| 290 | + |
| 291 | + |
| 292 | + |
| 293 | + |
| 294 | + |
| 295 | + |
| 296 | +async function wait_for_activation_on_sample_scope(t, window_or_workerglobalscope) { |
| 297 | + const script = '/service-workers/service-worker/resources/empty-worker.js'; |
| 298 | + const scope = 'resources/there/is/no/there/there?' + Date.now(); |
| 299 | + let registration = await window_or_workerglobalscope.navigator.serviceWorker.register(script, { scope }); |
| 300 | + await wait_for_state(t, registration.installing, 'activated'); |
| 301 | + await registration.unregister(); |
| 302 | +} |
| 303 | + |
0 commit comments