| 1 |
/** |
| 2 |
* AUTO-GENERATED - DO NOT EDIT. Source: https://github.com/gpuweb/cts |
| 3 |
**/import { registerShutdownTask, runShutdownTasks } from '../../framework/on_shutdown.js';import { LogMessageWithStack } from '../../internal/logging/log_message.js'; |
| 4 |
|
| 5 |
|
| 6 |
import { timeout } from '../../util/timeout.js'; |
| 7 |
import { assert } from '../../util/util.js'; |
| 8 |
|
| 9 |
import { kDefaultCTSOptions } from './options.js'; |
| 10 |
|
| 11 |
|
| 12 |
/** Query all currently-registered service workers, and unregister them. */ |
| 13 |
function unregisterAllServiceWorkers() { |
| 14 |
void navigator.serviceWorker.getRegistrations().then((registrations) => { |
| 15 |
for (const registration of registrations) { |
| 16 |
void registration.unregister(); |
| 17 |
} |
| 18 |
}); |
| 19 |
} |
| 20 |
|
| 21 |
// In practice all Web-based runtimes should be including this file, so take the opportunity to set |
| 22 |
// up runShutdownTasks. None of these events are guaranteed to happen, but cleaning up is very |
| 23 |
// important, so we try our best (and don't worry about shutdown performance or disabling bfcache). |
| 24 |
// (We could try 'visibilitychange', but since it can happen in the middle of the page lifetime, |
| 25 |
// it is more likely to have unintended consequences and would need to do different stuff.) |
| 26 |
// - 'unload' supposedly always disables the bfcache, but is deprecated in Chrome. |
| 27 |
// - 'beforeunload' may disable the bfcache but may be called more reliably than 'unload'. |
| 28 |
window.addEventListener('beforeunload', runShutdownTasks); |
| 29 |
// - 'pagehide' won't disable the bfcache but may be called more reliably than the others. |
| 30 |
window.addEventListener('pagehide', runShutdownTasks); |
| 31 |
|
| 32 |
class TestBaseWorker { |
| 33 |
|
| 34 |
resolvers = new Map(); |
| 35 |
|
| 36 |
constructor(worker, ctsOptions) { |
| 37 |
this.ctsOptions = { ...(ctsOptions || kDefaultCTSOptions), ...{ worker } }; |
| 38 |
} |
| 39 |
|
| 40 |
onmessage(ev) { |
| 41 |
const query = ev.data.query; |
| 42 |
const transferredResult = ev.data.result; |
| 43 |
|
| 44 |
const result = { |
| 45 |
status: transferredResult.status, |
| 46 |
timems: transferredResult.timems, |
| 47 |
logs: transferredResult.logs?.map((l) => new LogMessageWithStack(l)) |
| 48 |
}; |
| 49 |
|
| 50 |
this.resolvers.get(query)(result); |
| 51 |
this.resolvers.delete(query); |
| 52 |
|
| 53 |
// MAINTENANCE_TODO(kainino0x): update the Logger with this result (or don't have a logger and |
| 54 |
// update the entire results JSON somehow at some point). |
| 55 |
} |
| 56 |
|
| 57 |
makeRequestAndRecordResult( |
| 58 |
target, |
| 59 |
query, |
| 60 |
expectations) |
| 61 |
{ |
| 62 |
const request = { |
| 63 |
query, |
| 64 |
expectations, |
| 65 |
ctsOptions: this.ctsOptions |
| 66 |
}; |
| 67 |
target.postMessage(request); |
| 68 |
|
| 69 |
return new Promise((resolve) => { |
| 70 |
assert(!this.resolvers.has(query), "can't request same query twice simultaneously"); |
| 71 |
this.resolvers.set(query, resolve); |
| 72 |
}); |
| 73 |
} |
| 74 |
|
| 75 |
async run( |
| 76 |
rec, |
| 77 |
query, |
| 78 |
expectations = []) |
| 79 |
{ |
| 80 |
try { |
| 81 |
rec.injectResult(await this.runImpl(query, expectations)); |
| 82 |
} catch (ex) { |
| 83 |
rec.start(); |
| 84 |
rec.threw(ex); |
| 85 |
rec.finish(); |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
} |
| 94 |
|
| 95 |
export class TestDedicatedWorker extends TestBaseWorker { |
| 96 |
|
| 97 |
|
| 98 |
constructor(ctsOptions) { |
| 99 |
super('dedicated', ctsOptions); |
| 100 |
try { |
| 101 |
if (typeof Worker === 'undefined') { |
| 102 |
throw new Error('Dedicated Workers not available'); |
| 103 |
} |
| 104 |
|
| 105 |
const selfPath = import.meta.url; |
| 106 |
const selfPathDir = selfPath.substring(0, selfPath.lastIndexOf('/')); |
| 107 |
const workerPath = selfPathDir + '/test_worker-worker.js'; |
| 108 |
const worker = new Worker(workerPath, { type: 'module' }); |
| 109 |
this.worker = worker; |
| 110 |
this.worker.onmessage = (ev) => this.onmessage(ev); |
| 111 |
|
| 112 |
// Try to send a shutdown signal to the worker on shutdown. |
| 113 |
registerShutdownTask(() => worker.postMessage('shutdown')); |
| 114 |
} catch (ex) { |
| 115 |
assert(ex instanceof Error); |
| 116 |
// Save the exception to re-throw in runImpl(). |
| 117 |
this.worker = ex; |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
runImpl(query, expectations = []) { |
| 122 |
if (this.worker instanceof Worker) { |
| 123 |
return this.makeRequestAndRecordResult(this.worker, query, expectations); |
| 124 |
} else { |
| 125 |
throw this.worker; |
| 126 |
} |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
/** @deprecated Use TestDedicatedWorker instead. */ |
| 131 |
export class TestWorker extends TestDedicatedWorker {} |
| 132 |
|
| 133 |
export class TestSharedWorker extends TestBaseWorker { |
| 134 |
/** MessagePort to the SharedWorker, or an Error if it couldn't be initialized. */ |
| 135 |
|
| 136 |
|
| 137 |
constructor(ctsOptions) { |
| 138 |
super('shared', ctsOptions); |
| 139 |
try { |
| 140 |
if (typeof SharedWorker === 'undefined') { |
| 141 |
throw new Error('Shared Workers not available'); |
| 142 |
} |
| 143 |
|
| 144 |
const selfPath = import.meta.url; |
| 145 |
const selfPathDir = selfPath.substring(0, selfPath.lastIndexOf('/')); |
| 146 |
const workerPath = selfPathDir + '/test_worker-worker.js'; |
| 147 |
const worker = new SharedWorker(workerPath, { type: 'module' }); |
| 148 |
this.port = worker.port; |
| 149 |
this.port.start(); |
| 150 |
this.port.onmessage = (ev) => this.onmessage(ev); |
| 151 |
|
| 152 |
// Try to send a shutdown signal to the worker on shutdown. |
| 153 |
registerShutdownTask(() => worker.port.postMessage('shutdown')); |
| 154 |
} catch (ex) { |
| 155 |
assert(ex instanceof Error); |
| 156 |
// Save the exception to re-throw in runImpl(). |
| 157 |
this.port = ex; |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
runImpl(query, expectations = []) { |
| 162 |
if (this.port instanceof MessagePort) { |
| 163 |
return this.makeRequestAndRecordResult(this.port, query, expectations); |
| 164 |
} else { |
| 165 |
throw this.port; |
| 166 |
} |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
export class TestServiceWorker extends TestBaseWorker { |
| 171 |
lastServiceWorker = null; |
| 172 |
|
| 173 |
constructor(ctsOptions) { |
| 174 |
super('service', ctsOptions); |
| 175 |
|
| 176 |
// If the runtime is trying to use service workers, first clean up any service workers that may |
| 177 |
// have leaked from previous incarnations of the page. Service workers *shouldn't* affect |
| 178 |
// worker=none/dedicated/shared, so it should be OK if this constructor isn't called. |
| 179 |
unregisterAllServiceWorkers(); |
| 180 |
|
| 181 |
// Try to send a shutdown signal to the worker on shutdown. |
| 182 |
registerShutdownTask(() => { |
| 183 |
this.runShutdownTasksOnLastServiceWorker(); |
| 184 |
unregisterAllServiceWorkers(); |
| 185 |
}); |
| 186 |
} |
| 187 |
|
| 188 |
runShutdownTasksOnLastServiceWorker() { |
| 189 |
this.lastServiceWorker?.postMessage('shutdown'); |
| 190 |
} |
| 191 |
|
| 192 |
async runImpl(query, expectations = []) { |
| 193 |
if (!('serviceWorker' in navigator)) { |
| 194 |
throw new Error('Service Workers not available'); |
| 195 |
} |
| 196 |
const [suite, name] = query.split(':', 2); |
| 197 |
const fileName = name.split(',').join('/'); |
| 198 |
|
| 199 |
const selfPath = import.meta.url; |
| 200 |
const selfPathDir = selfPath.substring(0, selfPath.lastIndexOf('/')); |
| 201 |
// Construct the path to the worker file, then use URL to resolve the `../` components. |
| 202 |
const serviceWorkerURL = new URL( |
| 203 |
`${selfPathDir}/../../../${suite}/webworker/${fileName}.as_worker.js` |
| 204 |
).toString(); |
| 205 |
|
| 206 |
// Ensure the correct service worker is registered. |
| 207 |
const registration = await navigator.serviceWorker.register(serviceWorkerURL, { |
| 208 |
type: 'module' |
| 209 |
}); |
| 210 |
const registrationPending = () => |
| 211 |
!registration.active || registration.active.scriptURL !== serviceWorkerURL; |
| 212 |
|
| 213 |
// Ensure the registration we just requested is active. (We don't worry about it being |
| 214 |
// outdated from a previous page load, because we wipe all service workers on shutdown/startup.) |
| 215 |
const isNewInstance = registrationPending(); |
| 216 |
if (isNewInstance) { |
| 217 |
this.runShutdownTasksOnLastServiceWorker(); |
| 218 |
do { |
| 219 |
await new Promise((resolve) => timeout(resolve, 0)); |
| 220 |
} while (registrationPending()); |
| 221 |
} |
| 222 |
const serviceWorker = registration.active; |
| 223 |
this.lastServiceWorker = serviceWorker; |
| 224 |
|
| 225 |
navigator.serviceWorker.onmessage = (ev) => this.onmessage(ev); |
| 226 |
return this.makeRequestAndRecordResult(serviceWorker, query, expectations); |
| 227 |
} |
| 228 |
} |