- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / test / data / keyevents_test.html
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;
18
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);
25 }
26
27 function log(text) {
28   document.getElementById('log').innerHTML += text + '<br/>';
29 }
30
31 function setDefaultAction(type, value) {
32   defaultActions[type] = value;
33   document.getElementById(type).checked = !value;
34   return defaultActions[type];
35 }
36
37 function startTest(count) {
38   if (!testStarted) {
39     clearResult();
40     testStarted = true;
41     expectedEventCount = count;
42     log("Start test.");
43     return true;
44   }
45   return false;
46 }
47
48 function finishTest() {
49   testStarted = false;
50   window.domAutomationController.setAutomationId(0);
51   window.domAutomationController.send("FINISHED");
52   log("Finish test.");
53 }
54
55 function handleEvent(e) {
56   var prefixes = {
57     'keydown': 'D',
58     'keypress': 'P',
59     'keyup': 'U',
60     'textInput': 'T',
61   };
62
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));
81   }
82   keyEventResult.push(result);
83   log(result);
84
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();
93     }
94   }
95
96   if (!defaultActions[evt.type]) {
97     if (evt.preventDefault) evt.preventDefault();
98     if (evt.stopPropagation) evt.stopPropagation();
99   }
100   return defaultActions[evt.type];
101 }
102
103 function handleWindowBlur() {
104   if (testStarted)
105     finishTest();
106 }
107
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;
117 }
118
119 function setFocusedElement(id) {
120   if (id == "" && focusedElement != "") {
121     var elem = document.getElementById(focusedElement);
122     if (elem) {
123       elem.blur();
124       return true;
125     }
126   } else {
127     var elem = document.getElementById(id);
128     if (elem) {
129       elem.focus();
130       return true;
131     }
132   }
133   return false;
134 }
135
136 function onFocus(element) {
137   focusedElement = element.id;
138   log("Focus: " + focusedElement);
139 }
140
141 function onBlur(element) {
142   focusedElement = "";
143   lastFocusedElement = element.id;
144   log("Blur: " + element.id);
145 }
146
147 function onClick(element) {
148   if (defaultActions[element.id] != undefined)
149     defaultActions[element.id] = !element.checked;
150 }
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>