Commit 34b30fe7 authored by Yoshifumi Inoue's avatar Yoshifumi Inoue Committed by Commit Bot

Make selection_test() to accept async function.

This patch changes |selection_test()| to be able to accept |AsyncFunction| as
tester to use |chrome.gpuBenchmarking.pointerActionSequence()| for using mouse
event in tester.

Note: This patch is a preparation of http://crrev.com/c/2224485.

Bug: 707656, 679977
Change-Id: I8e32f043ec64d5f0b32655018fbc171e69831581
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2224483
Commit-Queue: Kent Tamura <tkent@chromium.org>
Reviewed-by: default avatarKent Tamura <tkent@chromium.org>
Auto-Submit: Yoshifumi Inoue <yosin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#773894}
parent b0f36a32
...@@ -427,4 +427,8 @@ test(() => { ...@@ -427,4 +427,8 @@ test(() => {
'<div id="second">four |<span id="end"></span>five six</div>', '<div id="second">four |<span id="end"></span>five six</div>',
].join('')); ].join(''));
}, 'computeLeft() and computeTop()'); }, 'computeLeft() and computeTop()');
// When |selection_test()| revives AsyncFunction as tester, |selection_test()|
// uses |promise_test()| API instead of |test()| API.
selection_test('abc', async () => 1, 'abc', 'use promise_test()');
</script> </script>
...@@ -993,28 +993,13 @@ function commonPrefixOf(str1, str2) { ...@@ -993,28 +993,13 @@ function commonPrefixOf(str1, str2) {
* @param {function(!Selection)|string} tester * @param {function(!Selection)|string} tester
* @param {string} expectedText * @param {string} expectedText
* @param {Object=} opt_options * @param {Object=} opt_options
* @return {!Sample} * @return {!Sample|!Promise}
*/ */
function assertSelectionAndReturnSample( function assertSelectionAndReturnSample(
passedInputText, tester, passedExpectedText, opt_options = {}) { passedInputText, tester, passedExpectedText, opt_options = {}) {
const kDescription = 'description';
const kDumpAs = 'dumpAs';
const kRemoveSampleIfSucceeded = 'removeSampleIfSucceeded';
const kDumpFromRoot = 'dumpFromRoot';
/** @type {!Object} */ /** @type {!Object} */
const options = typeof(opt_options) === 'string' const options = typeof(opt_options) === 'string'
? {description: opt_options} : opt_options; ? {description: opt_options} : opt_options;
/** @type {string} */
const description = kDescription in options
? options[kDescription] : assembleDescription();
/** @type {boolean} */
const removeSampleIfSucceeded = kRemoveSampleIfSucceeded in options
? !!options[kRemoveSampleIfSucceeded] : true;
/** @type {DumpAs} */
const dumpAs = options[kDumpAs] || DumpAs.DOM_TREE;
/** @type {boolean} */
const dumpFromRoot = options[kDumpFromRoot] || false;
const inputText = (() => { const inputText = (() => {
if (typeof(passedInputText) === 'string') if (typeof(passedInputText) === 'string')
return passedInputText; return passedInputText;
...@@ -1033,14 +1018,42 @@ function assertSelectionAndReturnSample( ...@@ -1033,14 +1018,42 @@ function assertSelectionAndReturnSample(
checkExpectedText(expectedText); checkExpectedText(expectedText);
const sample = new Sample(inputText); const sample = new Sample(inputText);
if (typeof(tester) === 'function') { const result = (() => {
tester.call(window, sample.selection, sample.window.testRunner); if (typeof(tester) === 'function')
} else if (typeof(tester) === 'string') { return tester.call(window, sample.selection, sample.window.testRunner);
const strings = tester.split(/ (.+)/); if (typeof(tester) === 'string') {
sample.document.execCommand(strings[0], false, strings[1]); const strings = tester.split(/ (.+)/);
} else { return sample.document.execCommand(strings[0], false, strings[1]);
}
throw new Error(`Invalid tester: ${tester}`); throw new Error(`Invalid tester: ${tester}`);
} })();
if (result instanceof Promise)
return result.then(() => getResult(sample, expectedText, options));
return getResult(sample, expectedText, options);
}
/**
* @param {!Sample} Sample
* @param {string} expectedText
* @param {Object} options
* @return {!Sample}
*/
function getResult(sample, expectedText, options) {
const kDescription = 'description';
const kDumpAs = 'dumpAs';
const kRemoveSampleIfSucceeded = 'removeSampleIfSucceeded';
const kDumpFromRoot = 'dumpFromRoot';
/** @type {string} */
const description = kDescription in options
? options[kDescription] : assembleDescription();
/** @type {boolean} */
const removeSampleIfSucceeded = kRemoveSampleIfSucceeded in options
? !!options[kRemoveSampleIfSucceeded] : true;
/** @type {DumpAs} */
const dumpAs = options[kDumpAs] || DumpAs.DOM_TREE;
/** @type {boolean} */
const dumpFromRoot = options[kDumpFromRoot] || false;
/** @type {!Traversal} */ /** @type {!Traversal} */
const traversal = (() => { const traversal = (() => {
...@@ -1085,7 +1098,8 @@ function assertSelectionAndReturnSample( ...@@ -1085,7 +1098,8 @@ function assertSelectionAndReturnSample(
*/ */
function assertSelection( function assertSelection(
passedInputText, tester, passedExpectedText, opt_options) { passedInputText, tester, passedExpectedText, opt_options) {
assertSelectionAndReturnSample(passedInputText, tester, passedExpectedText, opt_options); assertSelectionAndReturnSample(
passedInputText, tester, passedExpectedText, opt_options);
} }
/** /**
...@@ -1100,6 +1114,15 @@ function selectionTest(inputText, tester, expectedText, opt_options, ...@@ -1100,6 +1114,15 @@ function selectionTest(inputText, tester, expectedText, opt_options,
const description = typeof(opt_options) === 'string' ? opt_options const description = typeof(opt_options) === 'string' ? opt_options
: opt_description; : opt_description;
const options = typeof(opt_options) === 'string' ? undefined : opt_options; const options = typeof(opt_options) === 'string' ? undefined : opt_options;
if (tester.constructor.name === 'AsyncFunction') {
promise_test(
() => {
return assertSelectionAndReturnSample(
inputText, tester, expectedText, options);
}, description);
return;
}
test(() => assertSelection(inputText, tester, expectedText, options), test(() => assertSelection(inputText, tester, expectedText, options),
description); description);
} }
......
<!DOCTYPE html> <!DOCTYPE html>
<script src="../../../resources/testharness.js"></script> <script src="../../../resources/testharness.js"></script>
<script src="../../../resources/testharnessreport.js"></script> <script src="../../../resources/testharnessreport.js"></script>
<script src="../../assert_selection.js"></script>
<style>
#target { text-transform: uppercase; }
</style>
<p id="target">&#xDF;abc</p>
<script> <script>
// Drag through text with 'text-transform'. Shouldn't crash. // Drag through text with 'text-transform'. Shouldn't crash.
const kStyle = '#target { text-transform: uppercase; }';
function dragThrough(x1, x2, y) {
return new Promise((resolve, reject) => { selection_test(
assert_own_property(window, 'chrome'); '<p id="target">&#xDF;xyz</p>', // "SSxyz"
assert_own_property(window.chrome, 'gpuBenchmarking'); async function(selection) {
const target = selection.document.getElementById('target');
chrome.gpuBenchmarking.pointerActionSequence([{ const x1 = selection.computeLeft(target);
source: 'mouse', const x2 = x1 + target.offsetWidth;
actions: [ const y = selection.computeLeft(target) + target.offsetHeight / 2;
{name: 'pointerDown', x: x1, y: y},
{name: 'pointerMove', x: x2, y:y}, if (!window.chrome || !chrome.gpuBenchmarking)
{name: 'pointerUp'} throw 'This test requires chrome.gpuBenchmarking';
]}], resolve);
}); return new Promise(resolve =>
} chrome.gpuBenchmarking.pointerActionSequence([{
source: 'mouse',
promise_test(async () => { actions: [
const target = document.getElementById('target'); {name: 'pointerDown', x: x1, y: y},
const x1 = target.offsetLeft; {name: 'pointerMove', x: x2, y: y},
const x2 = x1 + target.offsetWidth; {name: 'pointerUp'}
const y = target.offsetTop + target.offsetHeight; ]}], resolve));
await dragThrough(x1, x2, y); },
'<p id="target">^\u00DFxyz|</p>');
const selection = getSelection();
const text = target.firstChild;
assert_false(selection.isCollapsed);
assert_equals(selection.anchorNode, text);
assert_equals(selection.anchorOffset, 0);
assert_equals(selection.focusNode, text);
assert_equals(selection.focusOffset, 4);
});
</script> </script>
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment