gecko-dev/remote/test/browser/input/browser_dispatchMouseEvent.js
Etienne Bruines da5b77f74b Bug 1635496 - [remote] Fixed dispatchMouseEvent to not await undefined promises. r=remote-protocol-reviewers,whimboo
The "this" state is not persistent across spawn-calls, using "this" in
"function" also does not make a whole lot of sense.

In order to persist the Promise across spawn-calls, the value is stored
in "content" instead.

Since one cannot reliably return an Event from SpecialPowers.spawn (due
to serialization issues), we are awaiting them instead of returning.

Differential Revision: https://phabricator.services.mozilla.com/D73913
2020-05-05 20:04:16 +00:00

60 lines
1.6 KiB
JavaScript

/* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/publicdomain/zero/1.0/ */
"use strict";
add_task(async function testDispatchMouseEvent({ client }) {
await loadURL(toDataURL("<div>foo</div>"));
const { Input } = client;
await SpecialPowers.spawn(gBrowser.selectedBrowser, [], function() {
const div = content.document.querySelector("div");
content.mouseDownPromise = new Promise(resolve => {
div.addEventListener("mousedown", resolve, { once: true });
});
content.mouseUpPromise = new Promise(resolve => {
div.addEventListener("mouseup", resolve, { once: true });
});
content.clickPromise = new Promise(resolve => {
div.addEventListener("click", resolve, { once: true });
});
});
const { x, y } = await SpecialPowers.spawn(
gBrowser.selectedBrowser,
[],
() => {
return content.document.querySelector("div").getBoundingClientRect();
}
);
await Input.dispatchMouseEvent({
type: "mousePressed",
x,
y,
});
info("Waiting for DOM mousedown event on the div");
await SpecialPowers.spawn(gBrowser.selectedBrowser, [], async function() {
await content.mouseDownPromise;
});
await Input.dispatchMouseEvent({
type: "mouseReleased",
x,
y,
});
info("Waiting for DOM mouseup event on the div");
await SpecialPowers.spawn(gBrowser.selectedBrowser, [], async function() {
await content.mouseUpPromise;
});
info("Waiting for DOM click event on the div");
await SpecialPowers.spawn(gBrowser.selectedBrowser, [], async function() {
await content.clickPromise;
});
ok(true, "All events detected");
});