chore: fix BiDi mouse move implementation (#10956)

This commit is contained in:
jrandolf 2023-09-20 17:58:02 +02:00 committed by GitHub
parent 7b5eabcc37
commit c505c85b31
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 88 additions and 139 deletions

View File

@ -458,7 +458,7 @@ const getBidiButton = (button: MouseButton) => {
*/
export class BidiMouse extends Mouse {
#context: BrowsingContext;
#lastMovePoint?: Point;
#lastMovePoint: Point = {x: 0, y: 0};
constructor(context: BrowsingContext) {
super();
@ -466,7 +466,7 @@ export class BidiMouse extends Mouse {
}
override async reset(): Promise<void> {
this.#lastMovePoint = undefined;
this.#lastMovePoint = {x: 0, y: 0};
await this.#context.connection.send('input.releaseActions', {
context: this.#context.id,
});
@ -477,25 +477,35 @@ export class BidiMouse extends Mouse {
y: number,
options: Readonly<BidiMouseMoveOptions> = {}
): Promise<void> {
// https://w3c.github.io/webdriver-bidi/#command-input-performActions:~:text=input.PointerMoveAction%20%3D%20%7B%0A%20%20type%3A%20%22pointerMove%22%2C%0A%20%20x%3A%20js%2Dint%2C
this.#lastMovePoint = {
const from = this.#lastMovePoint;
const to = {
x: Math.round(x),
y: Math.round(y),
};
const actions: Bidi.Input.PointerSourceAction[] = [];
const steps = options.steps ?? 0;
for (let i = 0; i < steps; ++i) {
actions.push({
type: ActionType.PointerMove,
x: from.x + (to.x - from.x) * (i / steps),
y: from.y + (to.y - from.y) * (i / steps),
origin: options.origin,
});
}
actions.push({
type: ActionType.PointerMove,
...to,
origin: options.origin,
});
// https://w3c.github.io/webdriver-bidi/#command-input-performActions:~:text=input.PointerMoveAction%20%3D%20%7B%0A%20%20type%3A%20%22pointerMove%22%2C%0A%20%20x%3A%20js%2Dint%2C
this.#lastMovePoint = to;
await this.#context.connection.send('input.performActions', {
context: this.#context.id,
actions: [
{
type: SourceActionsType.Pointer,
id: InputId.Mouse,
actions: [
{
type: ActionType.PointerMove,
...this.#lastMovePoint,
duration: (options.steps ?? 0) * 50,
origin: options.origin,
},
],
actions,
},
],
});

View File

@ -131,6 +131,12 @@
"parameters": ["webDriverBiDi"],
"expectations": ["PASS"]
},
{
"testIdPattern": "[mouse.spec] *",
"platforms": ["darwin", "linux", "win32"],
"parameters": ["webDriverBiDi"],
"expectations": ["PASS"]
},
{
"testIdPattern": "[navigation.spec] *",
"platforms": ["darwin", "linux", "win32"],
@ -816,58 +822,16 @@
"expectations": ["SKIP"]
},
{
"testIdPattern": "[mouse.spec] Mouse should click the document",
"testIdPattern": "[mouse.spec] Mouse should not throw if buttons are pressed twice",
"platforms": ["darwin", "linux", "win32"],
"parameters": ["webDriverBiDi"],
"expectations": ["PASS"]
"parameters": ["cdp"],
"expectations": ["FAIL"]
},
{
"testIdPattern": "[mouse.spec] Mouse should not throw if clicking in parallel",
"testIdPattern": "[mouse.spec] Mouse should reset properly",
"platforms": ["darwin", "linux", "win32"],
"parameters": ["webDriverBiDi"],
"expectations": ["PASS"]
},
{
"testIdPattern": "[mouse.spec] Mouse should resize the textarea",
"platforms": ["darwin", "linux", "win32"],
"parameters": ["webDriverBiDi"],
"expectations": ["PASS"]
},
{
"testIdPattern": "[mouse.spec] Mouse should select the text with mouse",
"platforms": ["darwin", "linux", "win32"],
"parameters": ["webDriverBiDi"],
"expectations": ["PASS"]
},
{
"testIdPattern": "[mouse.spec] Mouse should send mouse wheel events",
"platforms": ["darwin", "linux", "win32"],
"parameters": ["webDriverBiDi"],
"expectations": ["PASS"]
},
{
"testIdPattern": "[mouse.spec] Mouse should set modifier keys on click",
"platforms": ["darwin", "linux", "win32"],
"parameters": ["webDriverBiDi"],
"expectations": ["PASS"]
},
{
"testIdPattern": "[mouse.spec] Mouse should trigger hover state",
"platforms": ["darwin", "linux", "win32"],
"parameters": ["webDriverBiDi"],
"expectations": ["PASS"]
},
{
"testIdPattern": "[mouse.spec] Mouse should trigger hover state with removed window.Node",
"platforms": ["darwin", "linux", "win32"],
"parameters": ["webDriverBiDi"],
"expectations": ["PASS"]
},
{
"testIdPattern": "[mouse.spec] Mouse should work with mobile viewports and cross process navigations",
"platforms": ["darwin", "linux", "win32"],
"parameters": ["webDriverBiDi"],
"expectations": ["PASS"]
"parameters": ["cdp"],
"expectations": ["FAIL", "TIMEOUT"]
},
{
"testIdPattern": "[navigation.spec] navigation \"after each\" hook for \"should work with both domcontentloaded and load\"",
@ -2429,72 +2393,18 @@
"parameters": ["cdp", "firefox"],
"expectations": ["SKIP"]
},
{
"testIdPattern": "[mouse.spec] Mouse should reset properly",
"platforms": ["darwin", "linux", "win32"],
"parameters": ["cdp", "firefox"],
"expectations": ["SKIP"]
},
{
"testIdPattern": "[mouse.spec] Mouse should resize the textarea",
"platforms": ["darwin", "linux", "win32"],
"parameters": ["firefox", "webDriverBiDi"],
"expectations": ["FAIL", "PASS"]
},
{
"testIdPattern": "[mouse.spec] Mouse should select the text with mouse",
"platforms": ["win32"],
"parameters": ["cdp", "chrome"],
"expectations": ["FAIL", "PASS"]
},
{
"testIdPattern": "[mouse.spec] Mouse should select the text with mouse",
"platforms": ["darwin", "linux", "win32"],
"parameters": ["chrome", "webDriverBiDi"],
"expectations": ["FAIL", "PASS"]
},
{
"testIdPattern": "[mouse.spec] Mouse should send mouse wheel events",
"platforms": ["darwin", "linux", "win32"],
"parameters": ["cdp", "firefox"],
"expectations": ["FAIL"]
},
{
"testIdPattern": "[mouse.spec] Mouse should send mouse wheel events",
"platforms": ["darwin", "linux", "win32"],
"parameters": ["chrome", "webDriverBiDi"],
"expectations": ["PASS"]
},
{
"testIdPattern": "[mouse.spec] Mouse should set modifier keys on click",
"platforms": ["darwin", "linux", "win32"],
"parameters": ["chrome", "webDriverBiDi"],
"expectations": ["PASS"]
},
{
"testIdPattern": "[mouse.spec] Mouse should trigger hover state",
"platforms": ["darwin", "linux", "win32"],
"parameters": ["cdp", "firefox"],
"expectations": ["FAIL", "PASS"]
},
{
"testIdPattern": "[mouse.spec] Mouse should trigger hover state with removed window.Node",
"platforms": ["darwin", "linux", "win32"],
"parameters": ["cdp", "firefox"],
"expectations": ["FAIL"]
},
{
"testIdPattern": "[mouse.spec] Mouse should tween mouse movement",
"platforms": ["darwin", "linux", "win32"],
"parameters": ["cdp", "firefox"],
"expectations": ["FAIL", "PASS"]
},
{
"testIdPattern": "[mouse.spec] Mouse should work with mobile viewports and cross process navigations",
"platforms": ["darwin", "linux", "win32"],
"parameters": ["chrome", "webDriverBiDi"],
"expectations": ["PASS"]
},
{
"testIdPattern": "[navigation.spec] navigation Frame.goto should navigate subframes",
"platforms": ["darwin", "linux", "win32"],

View File

@ -267,13 +267,13 @@ describe('Mouse', function () {
expect(await page.evaluate('result')).toEqual({x: 30, y: 40});
});
it('should throw if buttons are pressed incorrectly', async () => {
it('should not throw if buttons are pressed twice', async () => {
const {page, server} = await getTestState();
await page.goto(server.EMPTY_PAGE);
await page.mouse.down();
await expect(page.mouse.down()).rejects.toBeInstanceOf(Error);
await page.mouse.down();
});
interface AddMouseDataListenersOptions {
@ -303,6 +303,7 @@ describe('Mouse', function () {
}
document.addEventListener('mouseup', mouseEventListener);
document.addEventListener('click', mouseEventListener);
document.addEventListener('auxclick', mouseEventListener);
(window as unknown as {clicks: ClickData[]}).clicks = clicks;
}, options);
};
@ -366,7 +367,7 @@ describe('Mouse', function () {
});
it('should reset properly', async () => {
const {page, server} = await getTestState();
const {page, server, isChrome} = await getTestState();
await page.goto(server.EMPTY_PAGE);
@ -388,43 +389,71 @@ describe('Mouse', function () {
clientY: 5,
clientX: 5,
};
expect(data).toMatchObject([
expect(data.slice(0, 2)).toMatchObject([
{
...commonAttrs,
button: 0,
buttons: 6,
button: 2,
buttons: 5,
detail: 1,
type: 'mouseup',
},
{
...commonAttrs,
button: 0,
buttons: 6,
detail: 1,
type: 'click',
},
{
...commonAttrs,
button: 1,
buttons: 2,
detail: 0,
type: 'mouseup',
},
{
...commonAttrs,
button: 2,
buttons: 5,
detail: 1,
type: 'auxclick',
},
]);
// TODO(crbug/1485040): This should align with the firefox implementation.
if (isChrome) {
expect(data.slice(2)).toMatchObject([
{
...commonAttrs,
button: 1,
buttons: 1,
detail: 0,
type: 'mouseup',
},
{
...commonAttrs,
button: 0,
buttons: 0,
detail: 0,
type: 'mouseup',
},
]);
return;
}
expect(data.slice(2)).toMatchObject([
{
...commonAttrs,
button: 1,
buttons: 1,
detail: 1,
type: 'mouseup',
},
{
...commonAttrs,
button: 1,
buttons: 1,
detail: 1,
type: 'auxclick',
},
{
...commonAttrs,
button: 0,
buttons: 0,
detail: 0,
detail: 1,
type: 'mouseup',
},
{
...commonAttrs,
button: 0,
buttons: 0,
clientX: 0,
clientY: 0,
detail: 0,
type: 'mousemove',
detail: 1,
type: 'click',
},
]);
});