Commit fd364c78 authored by kochi's avatar kochi Committed by Commit bot

Convert remaining focus tests in shadow-dom to use testharness.js

The following tests are converted to use testharness.js
from js-test.js in LayoutTests/shadow-dom/.

* attach-shadow-with-parameter.html
* focus-slide-on-shadow-host.html
* focus-shadowhost-display-none.html

For attach-shadow-with-parameter.html, I added some return
value checks, but otherwise almost straightforward conversion
to testharness.js API.

Review-Url: https://codereview.chromium.org/2333623003
Cr-Commit-Position: refs/heads/master@{#418770}
parent 531b66b2
Attach open shadow root.
PASS host1.attachShadow({mode: 'open'}) is non-null.
Attach closed shadow root.
PASS host2.attachShadow({mode: 'closed'}) is non-null.
Attach shadow root whose mode is neither open nor closed.
PASS host4.attachShadow({mode: 'illegal'}) threw exception TypeError: Failed to execute 'attachShadow' on 'Element': The provided value 'illegal' is not a valid enum value of type ShadowRootMode..
Attach open shadow root with shadow-dom.js utility.
PASS [object ShadowRoot] is non-null.
Attach shadow root on already shadowed host will raise InvalidStateError exception.
PASS host1.attachShadow({mode: 'open'}) threw exception InvalidStateError: Failed to execute 'attachShadow' on 'Element': Shadow root cannot be created on a host which already hosts a shadow tree..
PASS successfullyParsed is true
TEST COMPLETE
<!doctype html> <!DOCTYPE html>
<script src="../resources/js-test.js"></script> <script src='../resources/testharness.js'></script>
<script src="../fast/dom/shadow/resources/shadow-dom.js"></script> <script src='../resources/testharnessreport.js'></script>
<body> <script src='resources/shadow-dom.js'></script>
<div id="host1"></div> <div id='host1'></div>
<div id="host2"></div> <div id='host2'></div>
<div id="host3"></div> <div id='host3'></div>
</body>
<script> <script>
debug('Attach open shadow root.'); 'use strict';
var host1 = document.querySelector('#host1');
shouldBeNonNull("host1.attachShadow({mode: 'open'})");
debug('Attach closed shadow root.'); test(() => {
var host2 = document.querySelector('#host2'); let root1 = host1.attachShadow({mode: 'open'});
shouldBeNonNull("host2.attachShadow({mode: 'closed'})"); assert_not_equals(root1, null, 'Attach open shadow root should succeed.');
assert_equals(Object.getPrototypeOf(root1), ShadowRoot.prototype,
'ShadowRoot object should be returned by attachShadow({mode:"open"}).');
debug('Attach shadow root whose mode is neither open nor closed.'); let root2 = host2.attachShadow({mode: 'closed'});
var host4 = document.querySelector('#host3'); assert_not_equals(root2, null, 'Attach closed shadow root should succeed.');
shouldThrow("host4.attachShadow({mode: 'illegal'})"); assert_equals(Object.getPrototypeOf(root2), ShadowRoot.prototype,
'ShadowRoot object should be returned by attachShadow({mode:"closed"}).');
debug('Attach open shadow root with shadow-dom.js utility.'); assert_throws({name: 'TypeError'}, () => { host3.attachShadow({mode: 'illegal'}); },
document.body.appendChild( 'Attach shadow root whose mode is neither open nor closed should throw TypeError.');
createDOM('div', {id: 'host5'},
attachShadow({mode: 'open'})));
var host5 = document.querySelector('#host5');
var root5 = host5.shadowRoot;
shouldBeNonNull(root5);
debug('Attach shadow root on already shadowed host will raise InvalidStateError exception.'); assert_throws({name: 'InvalidStateError'}, () => { host1.attachShadow({mode: 'open'}); },
shouldThrow("host1.attachShadow({mode: 'open'})"); 'Attach shadow on a host which has open shadow root will raise InvalidStateError exception.');
assert_throws({name: 'InvalidStateError'}, () => { host2.attachShadow({mode: 'open'}); },
'Attach shadow on a host wich has closed shadow root will raise InvalidStateError exception.');
}, 'Test for Element.attachShadow() with mode parameter.');
</script> </script>
Check if shadow host with display:none CSS rule for :focus works. crbug.com/482830
On success, you will see a series of "PASS" messages, followed by "TEST COMPLETE".
when shadow host itself is focused, it should match display:none, lose focus then becomes display:block again.
PASS window.getComputedStyle(host).display is "none"
PASS document.activeElement is host
PASS root.activeElement is null
PASS successfullyParsed is true
TEST COMPLETE
PASS window.getComputedStyle(host).display is "block"
PASS document.activeElement is document.body
PASS root.activeElement is null
when shadow host with delegatesFocus=true has focused element inside the shadow, it should also match display:none, then lose focus and become display:block again.
PASS window.getComputedStyle(host).display is "none"
PASS document.activeElement is host
PASS root.activeElement is input
PASS window.getComputedStyle(host).display is "block"
PASS document.activeElement is document.body
PASS root.activeElement is null
<!DOCTYPE html> <!DOCTYPE html>
<script src="../resources/js-test.js"></script> <script src='../resources/testharness.js'></script>
<script src='../resources/testharnessreport.js'></script>
<style> <style>
div#host:focus { display: none; } #host:focus { display: none; }
</style> </style>
<div id="sandbox"></div> <div id='sandbox'></div>
<div id="host"></div>
<script> <script>
description('Check if shadow host with display:none CSS rule for :focus works. crbug.com/482830'); 'use strict';
// Check if shadow host with display:none CSS rule for :focus works. crbug.com/482830
var host; var host;
var root; var root;
...@@ -17,57 +19,50 @@ function setupShadowDOM(delegatesFocus) { ...@@ -17,57 +19,50 @@ function setupShadowDOM(delegatesFocus) {
host = sandbox.appendChild(document.createElement('div')); host = sandbox.appendChild(document.createElement('div'));
host.id = 'host'; host.id = 'host';
root = host.attachShadow({ 'mode': 'open', 'delegatesFocus': delegatesFocus }); root = host.attachShadow({mode: 'open', delegatesFocus: delegatesFocus});
input = document.createElement('input'); input = document.createElement('input');
root.appendChild(input); root.appendChild(input);
host.tabIndex = 0; host.tabIndex = 0;
} }
function testFocusShadowHost() { promise_test(() => {
debug('when shadow host itself is focused, it should match display:none, lose focus then becomes display:block again.');
setupShadowDOM(false); setupShadowDOM(false);
return new Promise( return new Promise(
function(resolve) { function(resolve) {
host.focus(); host.focus();
shouldBeEqualToString('window.getComputedStyle(host).display', 'none'); assert_equals(window.getComputedStyle(host).display, 'none');
shouldBe('document.activeElement', 'host'); assert_equals(document.activeElement, host);
shouldBeNull('root.activeElement'); assert_equals(root.activeElement, null);
function onBlur() { function onBlur() {
shouldBeEqualToString('window.getComputedStyle(host).display', 'block'); assert_equals(window.getComputedStyle(host).display, 'block');
shouldBe('document.activeElement', 'document.body'); assert_equals(document.activeElement, document.body);
shouldBeNull('root.activeElement'); assert_equals(root.activeElement, null);
host.removeEventListener('blur', onBlur); host.removeEventListener('blur', onBlur);
resolve(); resolve();
} }
host.addEventListener('blur', onBlur); host.addEventListener('blur', onBlur);
}); });
} }, 'when shadow host itself is focused, it should match display:none, lose focus then becomes display:block again.');
function testFocusInsideShadowRoot() { promise_test(() => {
debug('when shadow host with delegatesFocus=true has focused element inside the shadow, it should also match display:none, then lose focus and become display:block again.');
setupShadowDOM(true); setupShadowDOM(true);
return new Promise( return new Promise(
function(resolve) { function(resolve) {
input.focus(); input.focus();
shouldBeEqualToString('window.getComputedStyle(host).display', 'none'); assert_equals(window.getComputedStyle(host).display, 'none');
shouldBe('document.activeElement', 'host'); assert_equals(document.activeElement, host);
shouldBe('root.activeElement', 'input'); assert_equals(root.activeElement, input);
function onBlur() { function onBlur() {
shouldBeEqualToString('window.getComputedStyle(host).display', 'block'); assert_equals(window.getComputedStyle(host).display, 'block');
shouldBe('document.activeElement', 'document.body'); assert_equals(document.activeElement, document.body);
shouldBeNull('root.activeElement'); assert_equals(root.activeElement, null);
input.removeEventListener('blur', onBlur); input.removeEventListener('blur', onBlur);
resolve(); resolve();
} }
input.addEventListener('blur', onBlur); input.addEventListener('blur', onBlur);
}); });
} }, 'when shadow host with delegatesFocus=true has focused element inside the shadow, it should also match display:none, then lose focus and become display:block again.');
if (window.testRunner) {
testFocusShadowHost().then(testFocusInsideShadowRoot).then(function(){ testRunner.notifyDone(); });
testRunner.waitUntilDone();
}
</script> </script>
Click inside focusable shadow host should focus the host.
On success, you will see a series of "PASS" messages, followed by "TEST COMPLETE".
click on inner div should focus shadow host
PASS innermost active element is shadowHost
PASS innermost active element is shadowHost/inputA
PASS innermost active element is shadowHost
PASS innermost active element is shadowHost
click on inner div should focus inner focusable (with delegatesFocus = true)
PASS innermost active element is shadowHost/inputA
PASS inputA.selectionStart is 0
PASS inputA.selectionEnd is 9
PASS innermost active element is shadowHost/inputA
PASS inputA.selectionStart is 0
PASS inputA.selectionEnd is 9
PASS innermost active element is shadowHost/inputA
PASS innermost active element is shadowHost/inputA
PASS inputA.selectionStart is 1
PASS inputA.selectionEnd is 1
PASS innermost active element is shadowHost/inputB
PASS innermost active element is shadowHost/inputB
PASS inputB.selectionStart is 1
PASS inputB.selectionEnd is 1
PASS successfullyParsed is true
TEST COMPLETE
<!DOCTYPE html> <!DOCTYPE html>
<script src="../resources/js-test.js"></script> <script src='../resources/testharness.js'></script>
<script src="../fast/dom/shadow/resources/shadow-dom.js"></script> <script src='../resources/testharnessreport.js'></script>
<body> <script src='resources/shadow-dom.js'></script>
<pre id="console"></pre> <script src='resources/focus-utils.js'></script>
<input id="defaultFocus"> <input id='defaultFocus'>
<div id="sandbox"></div> <div id='sandbox'></div>
</body>
<script> <script>
description('Click inside focusable shadow host should focus the host.'); 'use strict';
function prepareDOMTree(parent, delegatesFocus) function prepareDOMTree(parent, delegatesFocus)
{ {
parent.innerHTML = ''; parent.innerHTML = '';
let host = document.createElement('div');
parent.appendChild( host.id = 'shadowHost';
createDOM('div', {'id': 'shadowHost', 'tabindex': '0'}, host.setAttribute('tabindex', '0');
attachShadow({'mode': 'open', 'delegatesFocus': delegatesFocus}, var root = host.attachShadow({mode: 'open', delegatesFocus: delegatesFocus});
createDOM('div', {'id': 'innerDiv'}, root.innerHTML = '<div id="innerDiv">Blink</div><input id="inputA"><input id="inputB">';
document.createTextNode('Blink')), parent.appendChild(host);
createDOM('input', {'id': 'inputA'}),
createDOM('input', {'id': 'inputB'}))));
} }
var host; var host;
...@@ -38,74 +34,71 @@ function resetFocus() { ...@@ -38,74 +34,71 @@ function resetFocus() {
document.querySelector('#defaultFocus').focus(); document.querySelector('#defaultFocus').focus();
} }
function checkInnermostActiveElement(id) { function assert_innermost_active_element(path) {
if (isInnermostActiveElement(id)) assert_true(isInnermostActiveElement(path), 'innermost active element should be ' + path);
debug('PASS innermost active element is ' + id);
} }
function runTest() { test(() => {
var sandbox = document.querySelector('#sandbox');
prepareDOMTree(sandbox, false); prepareDOMTree(sandbox, false);
resetFocus(); resetFocus();
host = getNodeInComposedTree('shadowHost'); let host = getNodeInComposedTree('shadowHost');
innerDiv = getNodeInComposedTree('shadowHost/innerDiv'); let innerDiv = getNodeInComposedTree('shadowHost/innerDiv');
inputA = getNodeInComposedTree('shadowHost/inputA'); let inputA = getNodeInComposedTree('shadowHost/inputA');
inputB = getNodeInComposedTree('shadowHost/inputB'); let inputB = getNodeInComposedTree('shadowHost/inputB');
debug('click on inner div should focus shadow host');
clickOn(innerDiv); clickOn(innerDiv);
checkInnermostActiveElement('shadowHost'); assert_innermost_active_element('shadowHost');
inputA.focus(); inputA.focus();
checkInnermostActiveElement('shadowHost/inputA'); assert_innermost_active_element('shadowHost/inputA');
clickOn(innerDiv); clickOn(innerDiv);
checkInnermostActiveElement('shadowHost'); assert_innermost_active_element('shadowHost');
inputB.focus(); inputB.focus();
assert_innermost_active_element('shadowHost/inputB');
clickOn(innerDiv); clickOn(innerDiv);
checkInnermostActiveElement('shadowHost'); assert_innermost_active_element('shadowHost');
}, 'click on inner div should focus shadow host (with delegatesFocus = false).');
debug('click on inner div should focus inner focusable (with delegatesFocus = true)'); test(() => {
prepareDOMTree(sandbox, true); prepareDOMTree(sandbox, true);
resetFocus(); resetFocus();
host = getNodeInComposedTree('shadowHost'); let host = getNodeInComposedTree('shadowHost');
innerDiv = getNodeInComposedTree('shadowHost/innerDiv'); let innerDiv = getNodeInComposedTree('shadowHost/innerDiv');
inputA = getNodeInComposedTree('shadowHost/inputA'); let inputA = getNodeInComposedTree('shadowHost/inputA');
inputB = getNodeInComposedTree('shadowHost/inputB'); let inputB = getNodeInComposedTree('shadowHost/inputB');
inputA.value = 'wonderful'; // len = 9 inputA.value = 'wonderful'; // len = 9
inputB.value = 'beautiful'; inputB.value = 'beautiful';
clickOn(innerDiv); clickOn(innerDiv);
checkInnermostActiveElement('shadowHost/inputA'); assert_innermost_active_element('shadowHost/inputA');
// If focus slides from shadow host, all the content will be selected. // If focus slides from shadow host, all the content will be selected.
shouldBe('inputA.selectionStart', '0'); assert_equals(inputA.selectionStart, 0);
shouldBe('inputA.selectionEnd', '9'); assert_equals(inputA.selectionEnd, 9);
// Clicking on non-focusable area inside shadow should not change the focus state. // Clicking on non-focusable area inside shadow should not change the focus state.
clickOn(innerDiv); clickOn(innerDiv);
checkInnermostActiveElement('shadowHost/inputA'); assert_innermost_active_element('shadowHost/inputA');
shouldBe('inputA.selectionStart', '0'); assert_equals(inputA.selectionStart, 0);
shouldBe('inputA.selectionEnd', '9'); assert_equals(inputA.selectionEnd, 9);
// Clicking on focusable directly will cause the element to be focused. // Clicking on focusable directly will cause the element to be focused.
clickOn(inputA); clickOn(inputA);
checkInnermostActiveElement('shadowHost/inputA'); assert_innermost_active_element('shadowHost/inputA');
clickOn(innerDiv); clickOn(innerDiv);
checkInnermostActiveElement('shadowHost/inputA'); assert_innermost_active_element('shadowHost/inputA');
shouldBe('inputA.selectionStart', '1'); assert_equals(inputA.selectionStart, 1);
shouldBe('inputA.selectionEnd', '1'); assert_equals(inputA.selectionEnd, 1);
clickOn(inputB); clickOn(inputB);
checkInnermostActiveElement('shadowHost/inputB'); assert_innermost_active_element('shadowHost/inputB');
clickOn(innerDiv); clickOn(innerDiv);
checkInnermostActiveElement('shadowHost/inputB'); assert_innermost_active_element('shadowHost/inputB');
shouldBe('inputB.selectionStart', '1'); assert_equals(inputB.selectionStart, 1);
shouldBe('inputB.selectionEnd', '1'); assert_equals(inputB.selectionEnd, 1);
} }, 'click on inner div should focus inner focusable (with delegatesFocus = true).');
runTest();
</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