Bug 1562740 - Input:dispatchKeyEvent should wait for events in content process r=remote-protocol-reviewers,ochameau

Depends on D37165

Differential Revision: https://phabricator.services.mozilla.com/D37166

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Julian Descottes 2019-07-12 08:30:30 +00:00
parent 981f531b1f
commit 3958f91262
2 changed files with 47 additions and 8 deletions

View File

@ -10,4 +10,47 @@ const { ContentProcessDomain } = ChromeUtils.import(
"chrome://remote/content/domains/ContentProcessDomain.jsm"
);
class Input extends ContentProcessDomain {}
class Input extends ContentProcessDomain {
constructor(session) {
super(session);
// Map of event name -> event handler promise.
this._eventPromises = new Map();
}
/**
* Not a CDP method.
*
* Add an event listener in the content page for the provided eventName.
* To wait for the event, you should use `waitForContentEvent` with the same event name.
*
* Example usage from a parent process domain:
*
* await this.executeInChild("addContentEventListener", "click");
* // do something that triggers a click in content
* await this.executeInChild("waitForContentEvent", "click");
*/
addContentEventListener(eventName) {
const eventPromise = new Promise(r => {
this.chromeEventHandler.addEventListener(eventName, r, {
mozSystemGroup: true,
once: true,
});
});
this._eventPromises.set(eventName, eventPromise);
}
/**
* Not a CDP method.
*
* Wait for an event listener added via `addContentEventListener` to be fired.
*/
async waitForContentEvent(eventName) {
const eventPromise = this._eventPromises.get(eventName);
if (!eventPromise) {
throw new Error("No event promise available for " + eventName);
}
await eventPromise;
this._eventPromises.delete(eventName);
}
}

View File

@ -50,12 +50,7 @@ class Input extends Domain {
const browserWindow = browser.ownerGlobal;
const EventUtils = this._getEventUtils(browserWindow);
const onEvent = new Promise(r => {
browserWindow.addEventListener(domType, r, {
mozSystemGroup: true,
once: true,
});
});
await this.executeInChild("addContentEventListener", domType);
if (type == "char") {
// type == "char" is used when doing `await page.keyboard.type( 'Im a list' );`
@ -77,7 +72,8 @@ class Input extends Domain {
browserWindow
);
}
await onEvent;
await this.executeInChild("waitForContentEvent", domType);
}
async dispatchMouseEvent({ type, button, x, y, modifiers, clickCount }) {