Skip to content

Commit

Permalink
[headless] Add clipboard operations test using simulated keystrokes
Browse files Browse the repository at this point in the history
This ensures that copy paste operations work as expected in new headless after appropriate permissions are granted.

Change-Id: Iee256961286ef33085b2e1e1ab4e212007639ef2
Cq-Include-Trybots: luci.chromium.try:mac12-tests
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4228279
Reviewed-by: Andrey Kosyakov <caseq@chromium.org>
Commit-Queue: Peter Kvitek <kvitekp@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1104152}
  • Loading branch information
Peter Kvitek authored and Chromium LUCI CQ committed Feb 11, 2023
1 parent 85d5ba6 commit c3e8e3c
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ void HeadlessModeProtocolBrowserTest::OnConsoleAPICalled(
HEADLESS_MODE_PROTOCOL_TEST(DomFocus, "input/dom-focus.js")
HEADLESS_MODE_PROTOCOL_TEST(FocusBlurNotifications,
"input/focus-blur-notifications.js")
HEADLESS_MODE_PROTOCOL_TEST(InputClipboardOps, "input/input-clipboard-ops.js")

// https://crbug.com/1411976
#if BUILDFLAG(IS_WIN)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Tests input field clipboard operations.
input: input_value
input: 123
input: 123input_value
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
(async function(testRunner) {
const html = `<!doctype html>
<html><body>
<input type="text" id="input" value="input_value" autofocus>
</body></html>
`;

const {page, session, dp} = await testRunner.startHTML(
html, `Tests input field clipboard operations.`);

async function logElementValue(id) {
const value = await session.evaluate(`
document.getElementById("${id}").value;
`);
testRunner.log(`${id}: ${value}`);
}

async function sendKey(
text, modifiers = 0, commands = []) {
const keyCode = text.charCodeAt(0);
await dp.Input.dispatchKeyEvent({
type: 'keyDown',
modifiers: modifiers,
text: text,
nativeVirtualKeyCode: keyCode,
commands: commands
});
await dp.Input.dispatchKeyEvent({
type: 'keyUp',
modifiers: modifiers,
nativeVirtualKeyCode: keyCode
});
}

await dp.Browser.grantPermissions({permissions: ['clipboardReadWrite']});

const kControl = 2;
const kCommand = 4;
const mod = navigator.platform.includes('Mac') ? kCommand : kControl;

await logElementValue('input');
await sendKey('a', mod, ['selectAll']);
await sendKey('c', mod, ['copy']);

await sendKey('1');
await sendKey('2');
await sendKey('3');
await logElementValue('input');

// Don't send Ctrl+A here because this would cause clipboard copy on
// systems that support selection clipboard, e.g. Linux.
await sendKey('v', mod, ['paste']);
await logElementValue('input');

testRunner.completeTest();
})

0 comments on commit c3e8e3c

Please sign in to comment.