Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / chrome / test / data / keyevents_test.html
blob88ba7a87de10a8717d2ad6eb460c3235173bf36a
1 <html><head>
2 <meta http-equiv="content-type" content="text/html; charset=utf-8">
3 <script type="text/javascript">
4 var defaultActions = {
5 'keydown': true,
6 'keypress': true,
7 'keyup': true,
8 'textInput': true,
9 };
10 var keyEventResult = [];
11 var focusedElement = "";
12 var lastFocusedElement = "";
13 var testStarted = false;
14 var expectedEventCount = 0;
15 var eventCount = 0;
16 var keyDownCount = 0;
17 var keyUpCount = 0;
19 function init() {
20 document.addEventListener("keydown", handleEvent, false);
21 document.addEventListener("keypress", handleEvent, false);
22 document.addEventListener("keyup", handleEvent, false);
23 document.addEventListener("textInput", handleEvent, false);
24 window.addEventListener("blur", handleWindowBlur, false);
27 function log(text) {
28 document.getElementById('log').innerHTML += text + '<br/>';
31 function setDefaultAction(type, value) {
32 defaultActions[type] = value;
33 document.getElementById(type).checked = !value;
34 return defaultActions[type];
37 function startTest(count) {
38 if (!testStarted) {
39 clearResult();
40 testStarted = true;
41 expectedEventCount = count;
42 log("Start test.");
43 return true;
45 return false;
48 function finishTest() {
49 testStarted = false;
50 window.domAutomationController.setAutomationId(0);
51 window.domAutomationController.send("FINISHED");
52 log("Finish test.");
55 function handleEvent(e) {
56 var prefixes = {
57 'keydown': 'D',
58 'keypress': 'P',
59 'keyup': 'U',
60 'textInput': 'T',
63 var evt = e || window.event;
64 var result = prefixes[evt.type] + ' ';
65 if (evt.type == 'textInput') {
66 result += evt.data;
67 } else {
68 // On Linux, the keydown event of a modifier key doesn't have the
69 // corresponding modifier attribute set, while the keyup event does have,
70 // eg. pressing and releasing ctrl may generate a keydown event with
71 // ctrlKey=false and a keyup event with ctrlKey=true.
72 // But Windows and Mac have opposite behavior than Linux.
73 // To make the C++ testing code simpler, if it's a modifier key event,
74 // then ignores the corresponding modifier attribute by setting it to true.
75 var keyId = evt.keyIdentifier;
76 result += (evt.keyCode + ' ' + evt.charCode + ' ' +
77 (keyId == 'Control' ? true : evt.ctrlKey) + ' ' +
78 (keyId == 'Shift' ? true : evt.shiftKey) + ' ' +
79 (keyId == 'Alt' ? true : evt.altKey) + ' ' +
80 (keyId == 'Meta' ? true : evt.metaKey));
82 keyEventResult.push(result);
83 log(result);
85 if (testStarted) {
86 ++eventCount;
87 if (evt.type == "keydown") {
88 ++keyDownCount;
89 } else if (evt.type == "keyup") {
90 ++keyUpCount;
91 if (keyDownCount == keyUpCount || (eventCount >= expectedEventCount))
92 finishTest();
96 if (!defaultActions[evt.type]) {
97 if (evt.preventDefault) evt.preventDefault();
98 if (evt.stopPropagation) evt.stopPropagation();
100 return defaultActions[evt.type];
103 function handleWindowBlur() {
104 if (testStarted)
105 finishTest();
108 function clearResult() {
109 keyEventResult = [];
110 testStarted = false;
111 expectedEventCount = 0;
112 eventCount = 0;
113 keyDownCount = 0;
114 keyUpCount = 0;
115 document.getElementById('log').innerHTML = "";
116 return true;
119 function setFocusedElement(id) {
120 if (id == "" && focusedElement != "") {
121 var elem = document.getElementById(focusedElement);
122 if (elem) {
123 elem.blur();
124 return true;
126 } else {
127 var elem = document.getElementById(id);
128 if (elem) {
129 elem.focus();
130 return true;
133 return false;
136 function onFocus(element) {
137 focusedElement = element.id;
138 log("Focus: " + focusedElement);
141 function onBlur(element) {
142 focusedElement = "";
143 lastFocusedElement = element.id;
144 log("Blur: " + element.id);
147 function onClick(element) {
148 if (defaultActions[element.id] != undefined)
149 defaultActions[element.id] = !element.checked;
151 </script>
152 </head>
153 <body onload="init()">
154 <input type="checkbox" id="keydown" onclick="onClick(this)">keydown</input>
155 <input type="checkbox" id="keypress" onclick="onClick(this)">keypress</input>
156 <input type="checkbox" id="keyup" onclick="onClick(this)">keyup</input>
157 <input type="checkbox" id="textInput" onclick="onClick(this)">textInput</input>
158 <br/>
159 <input type="checkbox" id="1" accesskey='1'
160 onfocus="onFocus(this)" onblur="onBlur(this)"/>
161 <input type="checkbox" id="2" accesskey='2'
162 onfocus="onFocus(this)" onblur="onBlur(this)"/>
163 <input type="checkbox" id="3" accesskey='3'
164 onfocus="onFocus(this)" onblur="onBlur(this)"/>
165 <input type="checkbox" id="D" accesskey='D'
166 onfocus="onFocus(this)" onblur="onBlur(this)"/>
167 <input type="text" id="A" accesskey="A"
168 onfocus="onFocus(this)" onblur="onBlur(this)"/>
169 <input type="password" id="B" accesskey="B"
170 onfocus="onFocus(this)" onblur="onBlur(this)"/>
171 <button id="clear" accesskey='C' onclick="clearResult()">Clear</button>
172 <p id="log"></p>
173 </body>
174 </html>