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(() => {
'<div id="second">four |<span id="end"></span>five six</div>',
].join(''));
}, '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>
......@@ -993,28 +993,13 @@ function commonPrefixOf(str1, str2) {
* @param {function(!Selection)|string} tester
* @param {string} expectedText
* @param {Object=} opt_options
* @return {!Sample}
* @return {!Sample|!Promise}
*/
function assertSelectionAndReturnSample(
passedInputText, tester, passedExpectedText, opt_options = {}) {
const kDescription = 'description';
const kDumpAs = 'dumpAs';
const kRemoveSampleIfSucceeded = 'removeSampleIfSucceeded';
const kDumpFromRoot = 'dumpFromRoot';
/** @type {!Object} */
const options = typeof(opt_options) === 'string'
? {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 = (() => {
if (typeof(passedInputText) === 'string')
return passedInputText;
......@@ -1033,14 +1018,42 @@ function assertSelectionAndReturnSample(
checkExpectedText(expectedText);
const sample = new Sample(inputText);
if (typeof(tester) === 'function') {
tester.call(window, sample.selection, sample.window.testRunner);
} else if (typeof(tester) === 'string') {
const strings = tester.split(/ (.+)/);
sample.document.execCommand(strings[0], false, strings[1]);
} else {
const result = (() => {
if (typeof(tester) === 'function')
return tester.call(window, sample.selection, sample.window.testRunner);
if (typeof(tester) === 'string') {
const strings = tester.split(/ (.+)/);
return sample.document.execCommand(strings[0], false, strings[1]);
}
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} */
const traversal = (() => {
......@@ -1085,7 +1098,8 @@ function assertSelectionAndReturnSample(
*/
function assertSelection(
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,
const description = typeof(opt_options) === 'string' ? opt_options
: opt_description;
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),
description);
}
......
<!DOCTYPE html>
<script src="../../../resources/testharness.js"></script>
<script src="../../../resources/testharnessreport.js"></script>
<style>
#target { text-transform: uppercase; }
</style>
<p id="target">&#xDF;abc</p>
<script src="../../assert_selection.js"></script>
<script>
// Drag through text with 'text-transform'. Shouldn't crash.
function dragThrough(x1, x2, y) {
return new Promise((resolve, reject) => {
assert_own_property(window, 'chrome');
assert_own_property(window.chrome, 'gpuBenchmarking');
chrome.gpuBenchmarking.pointerActionSequence([{
source: 'mouse',
actions: [
{name: 'pointerDown', x: x1, y: y},
{name: 'pointerMove', x: x2, y:y},
{name: 'pointerUp'}
]}], resolve);
});
}
promise_test(async () => {
const target = document.getElementById('target');
const x1 = target.offsetLeft;
const x2 = x1 + target.offsetWidth;
const y = target.offsetTop + target.offsetHeight;
await dragThrough(x1, x2, y);
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);
});
const kStyle = '#target { text-transform: uppercase; }';
selection_test(
'<p id="target">&#xDF;xyz</p>', // "SSxyz"
async function(selection) {
const target = selection.document.getElementById('target');
const x1 = selection.computeLeft(target);
const x2 = x1 + target.offsetWidth;
const y = selection.computeLeft(target) + target.offsetHeight / 2;
if (!window.chrome || !chrome.gpuBenchmarking)
throw 'This test requires chrome.gpuBenchmarking';
return new Promise(resolve =>
chrome.gpuBenchmarking.pointerActionSequence([{
source: 'mouse',
actions: [
{name: 'pointerDown', x: x1, y: y},
{name: 'pointerMove', x: x2, y: y},
{name: 'pointerUp'}
]}], resolve));
},
'<p id="target">^\u00DFxyz|</p>');
</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