Skip to content

Add some tests related to GPUDevice extending EventTarget #4389

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions src/common/framework/fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -346,8 +346,22 @@ export class Fixture<S extends SubcaseBatchState = SubcaseBatchState> {
}
}

/** Expect that a condition is true. */
expect(cond: boolean, msg?: string): boolean {
/**
* Expect that a condition is true.
*
* Note: You can pass a boolean condition, or a function that returns a boolean.
* The advantage to passing a function is that if it's short it is self documenting.
*
* t.expect(size >= maxSize); // prints Expect OK:
* t.expect(() => size >= maxSize) // prints Expect OK: () => size >= maxSize
*/
expect(cond: boolean | (() => boolean), msg?: string): boolean {
if (typeof cond === 'function') {
if (msg === undefined) {
msg = cond.toString();
}
cond = cond();
}
if (cond) {
const m = msg ? ': ' + msg : '';
this.rec.debug(new Error('expect OK' + m));
Expand Down
220 changes: 110 additions & 110 deletions src/resources/cache/hashes.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

116 changes: 115 additions & 1 deletion src/webgpu/idl/javascript.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ Examples:
import { makeTestGroup } from '../../common/framework/test_group.js';
import { keysOf } from '../../common/util/data_tables.js';
import { getGPU } from '../../common/util/navigator_gpu.js';
import { assert, objectEquals, unreachable } from '../../common/util/util.js';
import {
assert,
objectEquals,
raceWithRejectOnTimeout,
unreachable,
} from '../../common/util/util.js';
import { getDefaultLimitsForDevice, kLimits } from '../capability_info.js';
import { AllFeaturesMaxLimitsGPUTest, GPUTest } from '../gpu_test.js';

Expand Down Expand Up @@ -562,3 +567,112 @@ and expect instances of these classes to respond correctly.
const fn = kClassInheritanceTests[t.params.type];
t.expect(fn(), fn.toString());
});

const kDispatchTests = {
async canPassEventThroughDevice(t: GPUTest) {
const result = await raceWithRejectOnTimeout(
new Promise(resolve => {
t.device.addEventListener('foo', resolve, { once: true });
t.device.dispatchEvent(new Event('foo'));
}),
500,
'timeout'
);
const event = result as Event;
t.expect(() => event instanceof Event, 'event');
t.expect(() => event.type === 'foo');
},
async canPassCustomEventThroughDevice(t: GPUTest) {
const result = await raceWithRejectOnTimeout(
new Promise(resolve => {
t.device.addEventListener('bar', resolve, { once: true });
t.device.dispatchEvent(new CustomEvent('bar'));
}),
500,
'timeout'
);
const event = result as CustomEvent;
t.expect(() => event instanceof CustomEvent);
t.expect(() => event instanceof Event);
t.expect(() => event.type === 'bar');
},
async patchingEventTargetAffectsDevice(t: GPUTest) {
let addEventListenerWasCalled = false;
let dispatchEventWasCalled = false;
let removeEventListenerWasCalled = false;

// eslint-disable-next-line @typescript-eslint/unbound-method
const origFnAddEventListener = EventTarget.prototype.addEventListener;
EventTarget.prototype.addEventListener = function (
...args: Parameters<typeof EventTarget.prototype.addEventListener>
) {
addEventListenerWasCalled = true;
return origFnAddEventListener.call(this, ...args);
};

// eslint-disable-next-line @typescript-eslint/unbound-method
const origFnDispatchEvent = EventTarget.prototype.dispatchEvent;
EventTarget.prototype.dispatchEvent = function (event: Event) {
dispatchEventWasCalled = true;
return origFnDispatchEvent.call(this, event);
};

// eslint-disable-next-line @typescript-eslint/unbound-method
const origFnRemoveEventListener = EventTarget.prototype.removeEventListener;
EventTarget.prototype.removeEventListener = function (
...args: Parameters<typeof EventTarget.prototype.removeEventListener>
) {
removeEventListenerWasCalled = true;
return origFnRemoveEventListener.call(this, ...args);
};

try {
await raceWithRejectOnTimeout(
new Promise(resolve => {
t.device.addEventListener('foo', resolve);
t.device.dispatchEvent(new Event('foo'));
t.device.removeEventListener('foo', resolve);
}),
500,
'timeout'
);
} finally {
EventTarget.prototype.addEventListener = origFnAddEventListener;
EventTarget.prototype.dispatchEvent = origFnDispatchEvent;
EventTarget.prototype.removeEventListener = origFnRemoveEventListener;
}
t.expect(addEventListenerWasCalled, 'overriding EventTarget addEventListener worked');
t.expect(dispatchEventWasCalled, 'overriding EventTarget dispatchEvent worked');
t.expect(removeEventListenerWasCalled, 'overriding EventTarget removeEventListener worked');
},
async passingGPUUncapturedErrorEventWorksThoughEventTarget(t: GPUTest) {
const target = new EventTarget();
const result = await raceWithRejectOnTimeout(
new Promise(resolve => {
target.addEventListener('uncapturederror', resolve, { once: true });
target.dispatchEvent(
new GPUUncapturedErrorEvent('uncapturederror', {
error: new GPUValidationError('test error'),
})
);
}),
500,
'timeout'
);
const event = result as GPUUncapturedErrorEvent;
t.expect(() => event instanceof GPUUncapturedErrorEvent);
t.expect(() => event.error instanceof GPUValidationError);
t.expect(() => event.error.message === 'test error');
},
} as const;

g.test('device,EventTarget')
.desc(
`
Test some repercussions of the fact that GPUDevice extends EventTarget
`
)
.params(u => u.combine('test', keysOf(kDispatchTests)))
.fn(async t => {
await kDispatchTests[t.params.test](t);
});
1 change: 1 addition & 0 deletions src/webgpu/listing_meta.json
Original file line number Diff line number Diff line change
Expand Up @@ -916,6 +916,7 @@
"webgpu:idl,constructable:gpu_errors:*": { "subcaseMS": 0.101 },
"webgpu:idl,constructable:pipeline_errors:*": { "subcaseMS": 0.101 },
"webgpu:idl,constructable:uncaptured_error_event:*": { "subcaseMS": 0.101 },
"webgpu:idl,javascript:device,EventTarget:*": { "subcaseMS": 79.123 },
"webgpu:shader,execution,expression,access,array,index:abstract_scalar:*": { "subcaseMS": 235.962 },
"webgpu:shader,execution,expression,access,array,index:bool:*": { "subcaseMS": 663.038 },
"webgpu:shader,execution,expression,access,array,index:concrete_scalar:*": { "subcaseMS": 1439.796 },
Expand Down