Commit cece50c6 authored by Yoshifumi Inoue's avatar Yoshifumi Inoue Committed by Commit Bot

Make selection_test() and assert_selection() faster

This patch changes |Sample| class used in |assert_selection()| by reusing IFRAME
for making |selection_test()| and |assert_selection()| faster.

# Changes of tests
## assert_selection.html
Because of |assert_selection| doesn't remove IFRAME used by |Sample|, this
patch changes assertion to check IFRAME, which "id" is "sample", available.

## overtype.html
This patch changes each test starts with overwrite mode is off, because of
the overwrite mode state is state of |LocalFrame|, each test should reset it.

## spellcheck_test.js
This patch changes |spellcheck_test()| not to reuse IFRAME used by |Sample|.

Change-Id: I6513a2ffa01caeb0ad18360b543d0ebb4bad253a
Reviewed-on: https://chromium-review.googlesource.com/987632
Commit-Queue: Yoshifumi Inoue <yosin@chromium.org>
Reviewed-by: default avatarYoichi Osato <yoichio@chromium.org>
Cr-Commit-Position: refs/heads/master@{#547423}
parent e0a4f7c2
......@@ -366,14 +366,22 @@ test(() => {
const sample2 = assert_selection('abc', '', 'abc',
{removeSampleIfSucceeded: true});
assert_equals(sample2.iframe_.parentNode, null,
'removeSampleIfSucceeded: true');
assert_equals(sample2.iframe_.id, Sample.playgroundId,
'removeSampleIfSucceeded: true: id');
assert_equals(sample2.iframe_.style.display, 'none',
'removeSampleIfSucceeded: true: style.display');
const sample3 = assert_selection('abc', '', 'abc');
assert_equals(sample3.iframe_.parentNode, null, 'with default options');
assert_equals(sample3.iframe_.id, Sample.playgroundId,
'with default options: id');
assert_equals(sample3.iframe_.style.display, 'none',
'with default options: id');
const sample4 = assert_selection('abc', '', 'abc', 'description');
assert_equals(sample4.iframe_.parentNode, null, 'with description');
assert_equals(sample4.iframe_.id, Sample.playgroundId,
'with description: id');
assert_equals(sample4.iframe_.style.display, 'none',
'with description: id');
}, 'removeSampleIfSucceeded');
test(() => {
......
......@@ -721,10 +721,7 @@ class Sample {
*/
constructor(sampleText) {
/** @const @type {!HTMLIFrameElement} */
this.iframe_ = document.createElement('iframe');
if (!document.body)
document.body = document.createElement("body");
document.body.appendChild(this.iframe_);
this.iframe_ = Sample.getOrCreatePlayground();
/** @const @type {!HTMLDocument} */
this.document_ = this.iframe_.contentDocument;
......@@ -748,6 +745,17 @@ class Sample {
/** @return {!Selection} */
get selection() { return this.selection_; }
/** @return {string} */
static get playgroundId() { return 'playground'; }
/**
* @public
* Marks this sample not to be reused.
*/
keep() {
this.iframe_.removeAttribute('id');
}
/**
* @private
* @param {string} sampleText
......@@ -814,6 +822,32 @@ class Sample {
*/
remove() { this.iframe_.remove(); }
/**
* @public
*/
reset() {
if (window.internals && internals.isOverwriteModeEnabled(this.document_))
internals.toggleOverwriteModeEnabled(this.document_);
this.document_.documentElement.innerHTML = '<head></head><body></body>';
this.selection.removeAllRanges();
this.iframe_.style.display = 'none';
}
/** @return {HTMLIFrameElement} */
static getOrCreatePlayground() {
const present = document.getElementById(Sample.playgroundId);
if (present) {
present.style.display = 'block';
return present;
}
const iframe = document.createElement('iframe');
iframe.setAttribute('id', Sample.playgroundId);
if (!document.body)
document.body = document.createElement("body");
document.body.appendChild(iframe);
return iframe;
}
/**
* @public
* @param {!Traversal} traversal
......@@ -967,9 +1001,12 @@ function assertSelection(
// case.
if (actualText === expectedText) {
if (removeSampleIfSucceeded)
sample.remove();
sample.reset();
else
sample.keep();
return sample;
}
sample.keep();
throw new Error(`${description}\n` +
`\t expected ${expectedText},\n` +
`\t but got ${actualText},\n` +
......@@ -996,4 +1033,5 @@ function selectionTest(inputText, tester, expectedText, opt_options,
window.Sample = Sample;
window.assert_selection = assertSelection;
window.selection_test = selectionTest;
window.DOMTreeTraversal = DOMTreeTraversal;
})();
......@@ -386,6 +386,8 @@ add_result_callback(testObj => {
if (shouldRemoveSample)
testObj.sample.remove();
else
testObj.sample.keep();
// This is the earliest timing when a new spellcheck_test can be started.
spellcheckTestRunning = false;
......
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