Skip to content

Commit

Permalink
DevTools: Add commands option to Input.dispatchKeyEvent
Browse files Browse the repository at this point in the history
Key events emulated with DevTools can now use the commands option
to send editing commands that will be executed if the event is not
canceled. This is important for shortcuts like Meta+A = SelectAll to
work on a Mac.

See puppeteer/puppeteer#1313
and microsoft/playwright#1067

Change-Id: Id258668bfc71ef9f7f47477ef9de3422ada1d4b5
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2211929
Commit-Queue: Joel Einbinder <einbinder@chromium.org>
Reviewed-by: Andrey Kosyakov <caseq@chromium.org>
Reviewed-by: Dmitry Gozman <dgozman@chromium.org>
Reviewed-by: Yang Guo <yangguo@chromium.org>
Cr-Original-Commit-Position: refs/heads/master@{#780848}
Cr-Mirrored-From: https://chromium.googlesource.com/chromium/src
Cr-Mirrored-Commit: 3d3ee67f6904c527aca96b54588cb1008a17b23e
  • Loading branch information
JoelEinbinder authored and Commit Bot committed Jun 22, 2020
1 parent 90aef15 commit a0df1bb
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 0 deletions.
4 changes: 4 additions & 0 deletions blink/public/devtools_protocol/browser_protocol.pdl
Original file line number Diff line number Diff line change
Expand Up @@ -3502,6 +3502,10 @@ domain Input
# Whether the event was from the left or right side of the keyboard. 1=Left, 2=Right (default:
# 0).
optional integer location
# Editing commands to send with the key event (e.g., 'selectAll') (default: []).
# These are related to but not equal the command names used in `document.execCommand` and NSStandardKeyBindingResponding.
# See https://source.chromium.org/chromium/chromium/src/+/master:third_party/blink/renderer/core/editing/commands/editor_command_names.h for valid command names.
experimental optional array of string commands

# This method emulates inserting text that doesn't come from a key press,
# for example an emoji keyboard or an IME.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
Tests Input.dispatchKeyEvent commands option.

Sending "b"
with commands ["selectAll"]
hello world
~~~~~~~~~~~^

Sending "Backspace"

^

Sending "c"
c
^

Canceling the next keydown

Sending "d"
with commands ["selectAll"]
c
^

Sending "e"
ce
^

Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
(async function(testRunner) {
const {session, dp} = await testRunner.startBlank(`Tests Input.dispatchKeyEvent commands option.`);

await session.evaluate(`
const textarea = document.createElement('textarea');
document.body.appendChild(textarea);
textarea.value = 'hello world';
textarea.focus();
function selectedText() {
const textarea = document.querySelector('textarea');
return textarea.value + '\\n' + ' '.repeat(textarea.selectionStart) + '~'.repeat(textarea.selectionEnd-textarea.selectionStart) + '^'
}
`);

async function dumpErrorAndLogs(options) {
testRunner.log('');
testRunner.log(`Sending "${options.key}"`);
if (options.commands && options.commands.length)
testRunner.log(`with commands ${JSON.stringify(options.commands)}`)
const message = await dp.Input.dispatchKeyEvent(options);
if (message.error)
testRunner.log('Error: ' + message.error.message);

testRunner.log(await session.evaluate(`selectedText()`));
}

await dumpErrorAndLogs({
type: 'keyDown',
key: 'b',
modifiers: 2,
commands: ['selectAll'],
windowsVirtualKeyCode: 66,
code: 'KeyB'
});
await dumpErrorAndLogs({
type: 'keyDown',
windowsVirtualKeyCode: 8,
key: 'Backspace',
code: 'Backspace'
});

await dumpErrorAndLogs({
type: 'keyDown',
key: 'c',
text: 'c',
unmodifiedText: 'c',
commands: [],
windowsVirtualKeyCode: 67,
code: 'KeyC'
});

testRunner.log('');
testRunner.log('Canceling the next keydown');

await session.evaluate(`
window.addEventListener('keydown', event => event.preventDefault(), {once: true});
`);
await dumpErrorAndLogs({
type: 'keyDown',
key: 'd',
modifiers: 2,
commands: ['selectAll'],
windowsVirtualKeyCode: 67,
code: 'KeyC'
});
await dumpErrorAndLogs({
type: 'keyDown',
key: 'e',
text: 'e',
unmodifiedText: 'e',
commands: [],
windowsVirtualKeyCode: 68,
code: 'KeyE'
});

testRunner.completeTest();
});

0 comments on commit a0df1bb

Please sign in to comment.