1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
|
<!DOCTYPE html>
<meta charset="utf-8">
<title>Input Event typing tests</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/resources/testdriver.js"></script>
<script src="/resources/testdriver-vendor.js"></script>
<script src="/resources/testdriver-actions.js"></script>
<div id="rich" contenteditable></div>
<textarea id="plain"></textarea>
<script>
let inputEventsLog = [];
const rich = document.getElementById('rich');
const plain = document.getElementById('plain');
function log(event) {
const clone = new event.constructor(event.type, event);
clone.state = rich.innerHTML;
inputEventsLog.push(clone);
}
function resetRich() {
inputEventsLog = [];
rich.innerHTML = '';
}
rich.addEventListener('beforeinput', log);
rich.addEventListener('input', log);
promise_test(async function() {
this.add_cleanup(resetRich);
rich.focus();
const message = 'Hello';
await test_driver.send_keys(rich, message);
// 10 events (5 beforeinput + 5 input events)
assert_equals(inputEventsLog.length, 10);
for (let i = 0; i < inputEventsLog.length; i += 2) {
const beforeInputEvent = inputEventsLog[i];
const inputEvent = inputEventsLog[i + 1];
assert_equals(beforeInputEvent.type, 'beforeinput');
assert_equals(inputEvent.type, 'input');
assert_equals(beforeInputEvent.inputType, 'insertText');
assert_equals(inputEvent.inputType, 'insertText');
assert_equals(beforeInputEvent.data, inputEvent.data);
assert_equals(inputEvent.data, message[i / 2]);
assert_equals(beforeInputEvent.state + message[i / 2], inputEvent.state);
}
}, 'It triggers beforeinput and input events on text typing');
promise_test(async function() {
this.add_cleanup(resetRich);
rich.focus();
await test_driver.send_keys(rich, "\uE006"); // Return
assert_equals(inputEventsLog.length, 2);
const beforeInputEvent = inputEventsLog[0];
const inputEvent = inputEventsLog[1];
assert_equals(beforeInputEvent.type, 'beforeinput');
assert_equals(inputEvent.type, 'input');
assert_equals(beforeInputEvent.inputType, 'insertParagraph');
assert_equals(inputEvent.inputType, 'insertParagraph');
assert_equals(beforeInputEvent.data, inputEvent.data);
}, 'It triggers beforeinput and input events on typing RETURN');
promise_test(async function() {
this.add_cleanup(resetRich);
rich.focus();
await new test_driver.Actions()
.keyDown('\uE008') // Shift
.keyDown('\uE006') // Return
.keyUp('\uE006')
.keyUp('\uE008')
.send();
assert_equals(inputEventsLog.length, 2);
const [beforeInputEvent, inputEvent] = inputEventsLog;
assert_equals(beforeInputEvent.type, 'beforeinput');
assert_equals(inputEvent.type, 'input');
assert_equals(beforeInputEvent.inputType, 'insertLineBreak');
assert_equals(inputEvent.inputType, 'insertLineBreak');
assert_equals(beforeInputEvent.data, inputEvent.data);
}, 'It triggers beforeinput and input events on typing Shift+RETURN');
promise_test(async function() {
this.add_cleanup(resetRich);
rich.innerHTML = '<p>Preexisting <i id="caret">c</i>ontent</p>';
const caret = document.querySelector('#caret');
await test_driver.click(caret);
await test_driver.send_keys(caret, "\uE017"); // Delete
assert_equals(inputEventsLog.length, 2);
const [beforeInputEvent, inputEvent] = inputEventsLog;
assert_equals(beforeInputEvent.type, 'beforeinput');
assert_equals(inputEvent.type, 'input');
assert_equals(beforeInputEvent.inputType, 'deleteContentForward');
assert_equals(inputEvent.inputType, 'deleteContentForward');
assert_equals(beforeInputEvent.data, inputEvent.data);
}, 'It triggers beforeinput and input events on typing DELETE with pre-existing content');
promise_test(async function() {
this.add_cleanup(resetRich);
rich.focus();
await test_driver.send_keys(rich, "\uE017"); // Delete
assert_equals(inputEventsLog.length, 2);
const [beforeInputEvent, inputEvent] = inputEventsLog;
assert_equals(beforeInputEvent.type, 'beforeinput');
assert_equals(inputEvent.type, 'input');
assert_equals(beforeInputEvent.inputType, 'deleteContentForward');
assert_equals(inputEvent.inputType, 'deleteContentForward');
assert_equals(beforeInputEvent.data, inputEvent.data);
}, 'It triggers beforeinput and input events on typing DELETE with no pre-existing content');
promise_test(async function() {
this.add_cleanup(resetRich);
rich.innerHTML = '<p>Preexisting <i id="caret">c</i>ontent</p>';
await test_driver.click(document.querySelector('#caret'));
await test_driver.send_keys(rich, "\uE003"); // Back Space
assert_equals(inputEventsLog.length, 2);
const [beforeInputEvent, inputEvent] = inputEventsLog;
assert_equals(beforeInputEvent.type, 'beforeinput');
assert_equals(inputEvent.type, 'input');
assert_equals(beforeInputEvent.inputType, 'deleteContentBackward');
assert_equals(inputEvent.inputType, 'deleteContentBackward');
assert_equals(beforeInputEvent.data, inputEvent.data);
}, 'It triggers beforeinput and input events on typing BACK_SPACE with pre-existing content');
promise_test(async function() {
this.add_cleanup(resetRich);
rich.focus();
await test_driver.send_keys(rich, "\uE003"); // Back Space
assert_equals(inputEventsLog.length, 2);
const [beforeInputEvent, inputEvent] = inputEventsLog;
assert_equals(beforeInputEvent.type, 'beforeinput');
assert_equals(inputEvent.type, 'input');
assert_equals(beforeInputEvent.inputType, 'deleteContentBackward');
assert_equals(inputEvent.inputType, 'deleteContentBackward');
assert_equals(beforeInputEvent.data, inputEvent.data);
}, 'It triggers beforeinput and input events on typing BACK_SPACE with no pre-existing content');
promise_test(async function() {
this.add_cleanup(resetRich);
rich.focus();
await test_driver.send_keys(rich, "hello");
// Decide whether to use Key.COMMAND (mac) or Key.CONTROL (everything else)
const modifierKey = navigator.platform === "MacIntel" ? '\u2318' : '\uE009';
// Undo
await new test_driver.Actions()
.keyDown(modifierKey)
.keyDown('z')
.keyUp('z')
.keyUp(modifierKey)
.send();
// Redo
await new test_driver.Actions()
.keyDown(modifierKey)
.keyDown('\uE008') // Shift
.keyDown('z')
.keyUp('z')
.keyUp('\uE008')
.keyUp(modifierKey)
.send();
// Ignore the initial typing of 'hello'
const historyInputEventsLog = inputEventsLog.slice(10);
assert_equals(historyInputEventsLog.length, 4);
const inputTypes = ['historyUndo', 'historyRedo'];
for (let i = 0; i < historyInputEventsLog.length; i += 2) {
// We are increaisng i by 2 as there should always be matching beforeinput and input events.
const beforeInputEvent = historyInputEventsLog[i];
const inputEvent = historyInputEventsLog[i + 1];
assert_equals(beforeInputEvent.type, 'beforeinput');
assert_equals(inputEvent.type, 'input');
assert_equals(beforeInputEvent.inputType, inputTypes[i / 2]);
assert_equals(inputEvent.inputType, inputTypes[i / 2]);
assert_equals(beforeInputEvent.data, inputEvent.data);
}
}, 'It triggers beforeinput and input events on typing Undo and Redo key combinations with an existing history');
promise_test(async function() {
this.add_cleanup(resetRich);
rich.focus();
// Decide whether to use Key.COMMAND (mac) or Key.CONTROL (everything else)
const modifierKey = navigator.platform === "MacIntel" ? '\u2318' : '\uE009';
// Undo
await new test_driver.Actions()
.keyDown(modifierKey)
.keyDown('z')
.keyUp('z')
.keyUp(modifierKey)
.send();
// Redo
await new test_driver.Actions()
.keyDown(modifierKey)
.keyDown('\uE008') // Shift
.keyDown('z')
.keyUp('z')
.keyUp('\uE008')
.keyUp(modifierKey)
.send();
assert_equals(inputEventsLog.length, 4);
const inputTypes = ['historyUndo', 'historyRedo'];
for (let i = 0; i < inputEventsLog.length; i += 2) {
const beforeInputEvent = inputEventsLog[i];
const inputEvent = inputEventsLog[i + 1];
assert_equals(beforeInputEvent.type, 'beforeinput');
assert_equals(inputEvent.type, 'input');
assert_equals(beforeInputEvent.inputType, inputTypes[i / 2]);
assert_equals(inputEvent.inputType, inputTypes[i / 2]);
assert_equals(beforeInputEvent.data, inputEvent.data);
}
}, 'It triggers beforeinput and input events on typing Undo and Redo key combinations without an existing history');
promise_test(async function() {
this.add_cleanup(resetRich);
const expectedResult = [
// Pressing 'a'.
'plain-keydown-a',
'plain-keypress-a',
'plain-beforeinput-a-null',
'plain-input-a-null',
'plain-keyup-a',
// Pressing Shift-'b'.
'plain-keydown-B',
'plain-keypress-B',
'plain-beforeinput-B-null',
'plain-input-B-null',
'plain-keyup-B',
// Pressing 'c'.
'rich-keydown-c',
'rich-keypress-c',
'rich-beforeinput-c-null',
'rich-input-c-null',
'rich-keyup-c',
// Pressing Shift-'d'.
'rich-keydown-D',
'rich-keypress-D',
'rich-beforeinput-D-null',
'rich-input-D-null',
'rich-keyup-D',
];
const result = [];
for (const eventType of ['beforeinput', 'input', 'keydown', 'keypress', 'keyup']) {
const listener = event => {
if (event.key === 'Shift') return;
const eventInfo = [event.target.id, event.type, event.data || event.key];
if (event instanceof InputEvent) eventInfo.push(String(event.dataTransfer));
result.push(eventInfo.join('-'));
}
rich.addEventListener(eventType, listener);
plain.addEventListener(eventType, listener);
}
plain.focus();
await new test_driver.Actions()
.keyDown('a')
.keyUp('a')
.keyDown('\uE008') // Shift
.keyDown('b')
.keyUp('b')
.keyUp('\uE008')
.send();
rich.focus();
await new test_driver.Actions()
.keyDown('c')
.keyUp('c')
.keyDown('\uE008') // Shift
.keyDown('d')
.keyUp('d')
.keyUp('\uE008')
.send();
assert_equals(expectedResult.length, result.length);
expectedResult.forEach((er, index) => assert_equals(er, result[index]));
}, 'InputEvents have correct data/order when typing on textarea and contenteditable');
</script>
|