feat: Add support for dispatching device orientation events (#27960)

Fixes #27887
This commit is contained in:
Mattias Wallander 2023-11-08 18:50:25 +01:00 committed by GitHub
parent c759e6a6f6
commit 5a9fa69c6d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 66 additions and 1 deletions

View File

@ -67,6 +67,10 @@ export type HitTargetInterceptionResult = {
stop: () => 'done' | { hitTargetDescription: string };
};
interface WebKitLegacyDeviceOrientationEvent extends DeviceOrientationEvent {
readonly initDeviceOrientationEvent: (type: string, bubbles: boolean, cancelable: boolean, alpha: number, beta: number, gamma: number, absolute: boolean) => void;
}
export class InjectedScript {
private _engines: Map<string, SelectorEngine>;
_evaluator: SelectorEvaluatorImpl;
@ -1036,6 +1040,15 @@ export class InjectedScript {
case 'focus': event = new FocusEvent(type, eventInit); break;
case 'drag': event = new DragEvent(type, eventInit); break;
case 'wheel': event = new WheelEvent(type, eventInit); break;
case 'deviceorientation':
try {
event = new DeviceOrientationEvent(type, eventInit);
} catch {
const { bubbles, cancelable, alpha, beta, gamma, absolute } = eventInit as {bubbles: boolean, cancelable: boolean, alpha: number, beta: number, gamma: number, absolute: boolean};
event = this.document.createEvent('DeviceOrientationEvent') as WebKitLegacyDeviceOrientationEvent;
event.initDeviceOrientationEvent(type, bubbles, cancelable, alpha, beta, gamma, absolute);
}
break;
default: event = new Event(type, eventInit); break;
}
node.dispatchEvent(event);
@ -1371,7 +1384,7 @@ function oneLine(s: string): string {
return s.replace(/\n/g, '↵').replace(/\t/g, '⇆');
}
const eventType = new Map<string, 'mouse' | 'keyboard' | 'touch' | 'pointer' | 'focus' | 'drag' | 'wheel'>([
const eventType = new Map<string, 'mouse' | 'keyboard' | 'touch' | 'pointer' | 'focus' | 'drag' | 'wheel' | 'deviceorientation'>([
['auxclick', 'mouse'],
['click', 'mouse'],
['dblclick', 'mouse'],
@ -1419,6 +1432,9 @@ const eventType = new Map<string, 'mouse' | 'keyboard' | 'touch' | 'pointer' | '
['drop', 'drag'],
['wheel', 'wheel'],
['deviceorientation', 'deviceorientation'],
['deviceorientationabsolute', 'deviceorientation'],
]);
const kHoverHitTargetInterceptorEvents = new Set(['mousemove']);

View File

@ -0,0 +1,29 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>Device orientation test</title>
</head>
<body>
<script>
window.result = 'Was not oriented';
window.alpha = undefined;
window.beta = undefined;
window.gamma = undefined;
window.absolute = undefined;
document.addEventListener('deviceorientation', onOrientation, false);
document.addEventListener('deviceorientationabsolute', onOrientation, false);
function onOrientation(event) {
window.result = 'Oriented';
window.alpha = event.alpha;
window.beta = event.beta;
window.gamma = event.gamma;
window.absolute = event.absolute;
}
</script>
</body>
</html>

View File

@ -171,3 +171,23 @@ it('should dispatch wheel event', async ({ page, server }) => {
expect(await eventsHandle.evaluate(e => e[0] instanceof WheelEvent)).toBeTruthy();
expect(await eventsHandle.evaluate(e => ({ deltaX: e[0].deltaX, deltaY: e[0].deltaY }))).toEqual({ deltaX: 100, deltaY: 200 });
});
it('should dispatch device orientation event', async ({ page, server }) => {
await page.goto(server.PREFIX + '/device-orientation.html');
await page.locator('html').dispatchEvent('deviceorientation', { alpha: 10, beta: 20, gamma: 30 });
expect(await page.evaluate('result')).toBe('Oriented');
expect(await page.evaluate('alpha')).toBe(10);
expect(await page.evaluate('beta')).toBe(20);
expect(await page.evaluate('gamma')).toBe(30);
expect(await page.evaluate('absolute')).toBeFalsy();
});
it('should dispatch absolute device orientation event', async ({ page, server }) => {
await page.goto(server.PREFIX + '/device-orientation.html');
await page.locator('html').dispatchEvent('deviceorientationabsolute', { alpha: 10, beta: 20, gamma: 30, absolute: true });
expect(await page.evaluate('result')).toBe('Oriented');
expect(await page.evaluate('alpha')).toBe(10);
expect(await page.evaluate('beta')).toBe(20);
expect(await page.evaluate('gamma')).toBe(30);
expect(await page.evaluate('absolute')).toBeTruthy();
});