Commit 3603c1cd authored by Hikaru Nishida's avatar Hikaru Nishida Committed by Commit Bot

Rewrite query-command-state.html to utilize selection_test()

This patch changes "query-command-state.html" to utilize
|selection_test()| for ease of maintenance.
Before this patch, selectFirstTwoWords() is actually selects
latter two words of three words, so this patch tests actual
behavior cases.

Change-Id: I40c60de8ad510d025d6896e294eb6f71fb59acef
Reviewed-on: https://chromium-review.googlesource.com/945592Reviewed-by: default avatarXiaocheng Hu <xiaochengh@chromium.org>
Reviewed-by: default avatarYoichi Osato <yoichio@chromium.org>
Commit-Queue: Hikaru Nishida <hikarun@google.com>
Cr-Commit-Position: refs/heads/master@{#542005}
parent e6fcba83
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN"> <!doctype html>
<html> <script src="../../resources/testharness.js"></script>
<head> <script src="../../resources/testharnessreport.js"></script>
<script src="../../resources/js-test.js"></script> <script src="../assert_selection.js"></script>
</head> <script>
<body> const isMac = navigator.platform.indexOf('Mac') === 0;
<p id="description"></p> function query_command_test(command, contents, expectedStates) {
<div id="console"></div> const expectedState = expectedStates[isMac ? 'mac' : 'other'];
<script src="script-tests/query-command-state.js"></script> selection_test(
</body> `<div contenteditable>${contents}</div>`,
</html> selection => {
assert_equals(
selection.document.queryCommandState(command),
expectedState,
"State");
assert_equals(
selection.document.queryCommandValue(command),
expectedState.toString(),
"Value");
},
`<div contenteditable>${contents}</div>`,
`queryCommandState(${command}) returns ${expectedState} when selecting ${contents}`);
}
query_command_test(
'bold', '^hello|',
{mac: false, other: false});
query_command_test(
'bold', '^<i>hello</i>|',
{mac: false, other: false});
query_command_test(
'bold', '^<b>hello</b>|',
{mac: true, other: true});
query_command_test(
'bold', '^hello <b>world</b>|',
{mac: false, other: false});
query_command_test(
'bold', '^<b>hello</b> world|',
{mac: true, other: false});
query_command_test(
'bold', 'hello <b>^world</b> |Webkit',
{mac: true, other: false});
query_command_test(
'bold', '<b>hello</b> ^world| <b>WebKit</b>',
{mac: false, other: false});
query_command_test(
'bold', '<i>hello <b>^world</b> |WebKit</i>',
{mac: true, other: false});
query_command_test(
'bold', '<b>hello <i>^hello|</i> WebKit</b>',
{mac: true, other: true});
query_command_test(
'bold', '<b><div>hello <i>^hello|</i> WebKit</div></b>',
{mac: true, other: true});
query_command_test(
'bold', '^<b style="font-weight: normal;">hello</b>|',
{mac: false, other: false});
query_command_test(
'bold', '^<i style="font-weight: bold;">hello</i>|',
{mac: true, other: true});
query_command_test(
'bold', '^<b>hello</b> world <b>WebKit</b>|',
{mac: true, other: false});
query_command_test(
'bold', '^<b>hello</b><b> world</b>|',
{mac: true, other: true});
query_command_test(
'bold', '^<div><b>hello</b></div><p><b> WebKit</b></p>|',
{mac: true, other: true});
query_command_test(
'italic', '^hello|',
{mac: false, other: false});
query_command_test(
'italic', '^<b>hello</b>|',
{mac: false, other: false});
query_command_test(
'italic', '^<i>hello</i>|',
{mac: true, other: true});
query_command_test(
'italic', '^<i>hello</i> world|',
{mac: true, other: false});
query_command_test(
'italic', '^hello <i>world</i>|',
{mac: false, other: false});
query_command_test(
'italic', '^<i><div>hello world</div></i>|',
{mac: true, other: true});
query_command_test(
'italic',
'^<div style="font-style: italic">hello <span style="font-style: normal;">hello</span></div>|',
{mac: true, other: false});
query_command_test(
'subscript', '^hello|',
{mac: false, other: false});
query_command_test(
'subscript', '^<sup>hello</sup>|',
{mac: false, other: false});
query_command_test(
'subscript', '^<sub>hello</sub>|',
{mac: true, other: true});
query_command_test(
'subscript', '^<sub>hello</sub> world|',
{mac: true, other: false});
query_command_test(
'subscript', '^hello <sub>world</sub>|',
{mac: false, other: false});
query_command_test(
'subscript', '^<div style="vertical-align: sub;">hello world</div>|',
{mac: true, other: true});
query_command_test(
'subscript', 'hello <span style="vertical-align: sub;">^world</span> |WebKit',
{mac: true, other: false});
query_command_test(
'superscript', '^hello|',
{mac: false, other: false});
query_command_test(
'superscript', '^<sub>hello</sub>|',
{mac: false, other: false});
query_command_test(
'superscript', '^<sup>hello</sup>|',
{mac: true, other: true});
query_command_test(
'superscript', '^<sup>hello</sup> world|',
{mac: true, other: false});
query_command_test(
'superscript', '^hello <sup>world</sup>|',
{mac: false, other: false});
query_command_test(
'superscript',
'^<span style="vertical-align: super;">hello</span><span style="vertical-align: sub;">world</span>|',
{mac: true, other: false});
query_command_test(
'superscript', '^hello<span style="vertical-align: super;">world</span>|',
{mac: false, other: false});
query_command_test(
'underline', '^hello|',
{mac: false, other: false});
query_command_test(
'underline', '^<s>hello</s>|',
{mac: false, other: false});
query_command_test(
'underline', '^<u>hello</u>|',
{mac: true, other: true});
query_command_test(
'underline', '^<u>hello</u> world|',
{mac: true, other: false});
query_command_test(
'underline', '^hello <u>world</u>|',
{mac: false, other: false});
query_command_test(
'underline', '^<u><div>hello world</div></u>|',
{mac: true, other: true});
query_command_test(
'underline', '<u><s><div>hello ^world |WebKit</div></s></u>',
{mac: true, other: true});
query_command_test(
'underline', '<s><u>hello</u> ^world</s> |WebKit',
{mac: false, other: false});
query_command_test(
'underline', '<u><s>hello</s> ^world</u> |WebKit',
{mac: true, other: false});
query_command_test(
'underline', '<s>hello <u>^world|</u></s><u> WebKit</u>',
{mac: true, other: true});
query_command_test(
'strikeThrough', '^hello|',
{mac: false, other: false});
query_command_test(
'strikeThrough', '^<u>hello</u>|',
{mac: false, other: false});
query_command_test(
'strikeThrough', '^<s>hello</s>|',
{mac: true, other: true});
query_command_test(
'strikeThrough', '^<s>hello</s> world|',
{mac: true, other: false});
query_command_test(
'strikeThrough', '^hello <s>world</s>|',
{mac: false, other: false});
query_command_test(
'strikeThrough', '^<s><div>hello world</div></s>|',
{mac: true, other: true});
query_command_test(
'strikeThrough', '<s><u><div>hello ^world |WebKit</div></u></s>',
{mac: true, other: true});
query_command_test(
'strikeThrough', '<u><s>hello</s> ^world</u> |WebKit',
{mac: false, other: false});
query_command_test(
'strikeThrough', 'hello <s>^world |WebKit</s>',
{mac: true, other: true});
query_command_test(
'strikeThrough', 'hello^ <s>world |WebKit</s>',
{mac: false, other: false});
</script>
description("Test to make sure queryCommandState returns correct values.")
var testContainer = document.createElement("div");
testContainer.contentEditable = true;
document.body.appendChild(testContainer);
function testQueryCommandState(command, contents, selector, expectedState)
{
testContainer.innerHTML = contents;
var selected = selector(testContainer);
var actualState = document.queryCommandState(command);
var actualValue = document.queryCommandValue(command);
var action = 'queryCommandState("' + command + '") returns ' + actualState + ' when selecting ' + selected + ' of "' + contents + '"';
if (actualState != expectedState)
testFailed(action + ', expected ' + expectedState + '');
else if (actualValue != actualState.toString())
testFailed(action + ' but queryCommandValue returned ' + actualValue);
else
testPassed(action);
}
function selectAll(container) {
window.getSelection().selectAllChildren(container);
return 'all';
}
function selectSecondWord(container) {
window.getSelection().collapse(container, 0);
window.getSelection().modify('move', 'forward', 'word');
window.getSelection().modify('move', 'forward', 'word');
window.getSelection().modify('move', 'backward', 'word');
window.getSelection().modify('extend', 'forward', 'word');
return 'second word';
}
function selectFirstTwoWords(container) {
window.getSelection().collapse(container, 0);
window.getSelection().modify('move', 'forward', 'word');
window.getSelection().modify('extend', 'forward', 'word');
window.getSelection().modify('extend', 'forward', 'word');
return 'first two words';
}
function runTests(editingBehavior) {
if (window.internals)
internals.settings.setEditingBehavior(editingBehavior);
debug('Tests for ' + editingBehavior)
testQueryCommandState("bold", 'hello', selectAll, {'mac': false, 'win': false}[editingBehavior]);
testQueryCommandState("bold", '<i>hello</i>', selectAll, {'mac': false, 'win': false}[editingBehavior]);
testQueryCommandState("bold", '<b>hello</b>', selectAll, {'mac': true, 'win': true}[editingBehavior]);
testQueryCommandState("bold", 'hello <b>world</b>', selectAll, {'mac': false, 'win': false}[editingBehavior]);
testQueryCommandState("bold", '<b>hello</b> world', selectAll, {'mac': true, 'win': false}[editingBehavior]);
testQueryCommandState("bold", 'hello <b>world</b> WebKit', selectSecondWord, {'mac': true, 'win': false}[editingBehavior]);
testQueryCommandState("bold", '<b>hello</b> world <b>WebKit</b>', selectSecondWord, {'mac': false, 'win': false}[editingBehavior]);
testQueryCommandState("bold", '<i>hello <b>hello</b> WebKit</i>', selectSecondWord, {'mac': true, 'win': false}[editingBehavior]);
testQueryCommandState("bold", '<b>hello <i>hello</i> WebKit</b>', selectSecondWord, {'mac': true, 'win': true}[editingBehavior]);
testQueryCommandState("bold", '<b><div>hello <i>hello</i> WebKit</div></b>', selectSecondWord, {'mac': true, 'win': true}[editingBehavior]);
testQueryCommandState("bold", '<b style="font-weight: normal;">hello</b>', selectAll, {'mac': false, 'win': false}[editingBehavior]);
testQueryCommandState("bold", '<i style="font-weight: bold;">hello</i>', selectAll, {'mac': true, 'win': true}[editingBehavior]);
testQueryCommandState("bold", '<b>hello</b> world <b>WebKit</b>', selectAll, {'mac': true, 'win': false}[editingBehavior]);
testQueryCommandState("bold", '<b>hello</b><b> world</b>', selectAll, {'mac': true, 'win': true}[editingBehavior]);
testQueryCommandState("bold", '<div><b>hello</b></div><p><b> WebKit</b></p>', selectAll, {'mac': true, 'win': true}[editingBehavior]);
testQueryCommandState("italic", 'hello', selectAll, {'mac': false, 'win': false}[editingBehavior]);
testQueryCommandState("italic", '<b>hello</b>', selectAll, {'mac': false, 'win': false}[editingBehavior]);
testQueryCommandState("italic", '<i>hello</i>', selectAll, {'mac': true, 'win': true}[editingBehavior]);
testQueryCommandState("italic", '<i>hello</i> world', selectAll, {'mac': true, 'win': false}[editingBehavior]);
testQueryCommandState("italic", 'hello <i>world</i>', selectAll, {'mac': false, 'win': false}[editingBehavior]);
testQueryCommandState("italic", '<i><div>hello world</div></i>', selectAll, {'mac': true, 'win': true}[editingBehavior]);
testQueryCommandState("italic", '<div style="font-style: italic">hello <span style="font-style: normal;">hello</span></div>', selectAll, {'mac': true, 'win': false}[editingBehavior]);
testQueryCommandState("subscript", 'hello', selectAll, {'mac': false, 'win': false}[editingBehavior]);
testQueryCommandState("subscript", '<sup>hello</sup>', selectAll, {'mac': false, 'win': false}[editingBehavior]);
testQueryCommandState("subscript", '<sub>hello</sub>', selectAll, {'mac': true, 'win': true}[editingBehavior]);
testQueryCommandState("subscript", '<sub>hello</sub> world', selectAll, {'mac': true, 'win': false}[editingBehavior]);
testQueryCommandState("subscript", 'hello <sub>world</sub>', selectAll, {'mac': false, 'win': false}[editingBehavior]);
testQueryCommandState("subscript", '<div style="vertical-align: sub;">hello world</div>', selectAll, {'mac': true, 'win': true}[editingBehavior]);
testQueryCommandState("subscript", 'hello <span style="vertical-align: sub;">world</span> WebKit', selectSecondWord, {'mac': true, 'win': false}[editingBehavior]);
testQueryCommandState("superscript", 'hello', selectAll, {'mac': false, 'win': false}[editingBehavior]);
testQueryCommandState("superscript", '<sub>hello</sub>', selectAll, {'mac': false, 'win': false}[editingBehavior]);
testQueryCommandState("superscript", '<sup>hello</sup>', selectAll, {'mac': true, 'win': true}[editingBehavior]);
testQueryCommandState("superscript", '<sup>hello</sup> world', selectAll, {'mac': true, 'win': false}[editingBehavior]);
testQueryCommandState("superscript", 'hello <sup>world</sup>', selectAll, {'mac': false, 'win': false}[editingBehavior]);
testQueryCommandState("superscript", '<span style="vertical-align: super;">hello</span><span style="vertical-align: sub;">world</span>', selectAll, {'mac': true, 'win': false}[editingBehavior]);
testQueryCommandState("superscript", 'hello<span style="vertical-align: super;">world</span>', selectAll, {'mac': false, 'win': false}[editingBehavior]);
testQueryCommandState("underline", 'hello', selectAll, {'mac': false, 'win': false}[editingBehavior]);
testQueryCommandState("underline", '<s>hello</s>', selectAll, {'mac': false, 'win': false}[editingBehavior]);
testQueryCommandState("underline", '<u>hello</u>', selectAll, {'mac': true, 'win': true}[editingBehavior]);
testQueryCommandState("underline", '<u>hello</u> world', selectAll, {'mac': true, 'win': false}[editingBehavior]);
testQueryCommandState("underline", 'hello <u>world</u>', selectAll, {'mac': false, 'win': false}[editingBehavior]);
testQueryCommandState("underline", '<u><div>hello world</div></u>', selectAll, {'mac': true, 'win': true}[editingBehavior]);
testQueryCommandState("underline", '<u><s><div>hello world WebKit</div></s></u>', selectSecondWord, {'mac': true, 'win': true}[editingBehavior]);
testQueryCommandState("underline", '<s><u>hello</u> world</s> WebKit', selectSecondWord, {'mac': false, 'win': false}[editingBehavior]);
testQueryCommandState("underline", '<u><s>hello</s> world</u> WebKit', selectSecondWord, {'mac': true, 'win': false}[editingBehavior]);
testQueryCommandState("underline", '<s>hello <u>world</s> WebKit</u>', selectSecondWord, {'mac': true, 'win': true}[editingBehavior]);
testQueryCommandState("strikeThrough", 'hello', selectAll, {'mac': false, 'win': false}[editingBehavior]);
testQueryCommandState("strikeThrough", '<u>hello</u>', selectAll, {'mac': false, 'win': false}[editingBehavior]);
testQueryCommandState("strikeThrough", '<s>hello</s>', selectAll, {'mac': true, 'win': true}[editingBehavior]);
testQueryCommandState("strikeThrough", '<s>hello</s> world', selectAll, {'mac': true, 'win': false}[editingBehavior]);
testQueryCommandState("strikeThrough", 'hello <s>world</s>', selectAll, {'mac': false, 'win': false}[editingBehavior]);
testQueryCommandState("strikeThrough", '<s><div>hello world</div></s>', selectAll, {'mac': true, 'win': true}[editingBehavior]);
testQueryCommandState("strikeThrough", '<s><u><div>hello world WebKit</div></u></s>', selectSecondWord, {'mac': true, 'win': true}[editingBehavior]);
testQueryCommandState("strikeThrough", '<u><s>hello</s> world</u> WebKit', selectSecondWord, {'mac': false, 'win': false}[editingBehavior]);
testQueryCommandState("strikeThrough", 'hello <s>world WebKit</s>', selectSecondWord, {'mac': true, 'win': true}[editingBehavior]);
testQueryCommandState("strikeThrough", 'hello <s>world WebKit</s>', selectFirstTwoWords, {'mac': false, 'win': true}[editingBehavior]);
}
runTests('win');
debug('')
runTests('mac');
//document.body.removeChild(testContainer);
var successfullyParsed = true;
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