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-Commit-Position: refs/heads/master@{#780848}
  • Loading branch information
JoelEinbinder authored and Commit Bot committed Jun 22, 2020
1 parent 47de01c commit 3d3ee67
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 2 deletions.
15 changes: 13 additions & 2 deletions content/browser/devtools/protocol/input_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,7 @@ class InputHandler::InputInjector
}

void InjectKeyboardEvent(const NativeWebKeyboardEvent& keyboard_event,
Maybe<Array<std::string>> commands,
std::unique_ptr<DispatchKeyEventCallback> callback) {
if (!widget_host_) {
callback->sendFailure(Response::InternalError());
Expand All @@ -383,7 +384,15 @@ class InputHandler::InputInjector
widget_host_->Focus();
input_queued_ = false;
pending_key_callbacks_.push_back(std::move(callback));
widget_host_->ForwardKeyboardEvent(keyboard_event);
ui::LatencyInfo latency;
std::vector<blink::mojom::EditCommandPtr> edit_commands;
if (commands.isJust()) {
for (const std::string& command : *commands.fromJust())
edit_commands.push_back(blink::mojom::EditCommand::New(command, ""));
}

widget_host_->ForwardKeyboardEventWithCommands(keyboard_event, latency,
std::move(edit_commands));
if (!input_queued_) {
pending_key_callbacks_.back()->sendSuccess();
pending_key_callbacks_.pop_back();
Expand Down Expand Up @@ -537,6 +546,7 @@ void InputHandler::DispatchKeyEvent(
Maybe<bool> is_keypad,
Maybe<bool> is_system_key,
Maybe<int> location,
Maybe<Array<std::string>> commands,
std::unique_ptr<DispatchKeyEventCallback> callback) {
blink::WebInputEvent::Type web_event_type;

Expand Down Expand Up @@ -609,7 +619,8 @@ void InputHandler::DispatchKeyEvent(
else
event.skip_in_browser = true;

EnsureInjector(widget_host)->InjectKeyboardEvent(event, std::move(callback));
EnsureInjector(widget_host)
->InjectKeyboardEvent(event, std::move(commands), std::move(callback));
}

void InputHandler::InsertText(const std::string& text,
Expand Down
1 change: 1 addition & 0 deletions content/browser/devtools/protocol/input_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class InputHandler : public DevToolsDomainHandler, public Input::Backend {
Maybe<bool> is_keypad,
Maybe<bool> is_system_key,
Maybe<int> location,
Maybe<Array<std::string>> commands,
std::unique_ptr<DispatchKeyEventCallback> callback) override;

void InsertText(const std::string& text,
Expand Down
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 3d3ee67

Please sign in to comment.