OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "build/build_config.h" | 5 #include "build/build_config.h" |
6 | 6 |
7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
10 #include "base/stringprintf.h" | 10 #include "base/stringprintf.h" |
(...skipping 17 matching lines...) Expand all Loading... |
28 #include "net/test/test_server.h" | 28 #include "net/test/test_server.h" |
29 #include "ui/base/keycodes/keyboard_codes.h" | 29 #include "ui/base/keycodes/keyboard_codes.h" |
30 | 30 |
31 using content::DomOperationNotificationDetails; | 31 using content::DomOperationNotificationDetails; |
32 using content::NavigationController; | 32 using content::NavigationController; |
33 using content::RenderViewHost; | 33 using content::RenderViewHost; |
34 | 34 |
35 namespace { | 35 namespace { |
36 | 36 |
37 const char kTestingPage[] = "files/keyevents_test.html"; | 37 const char kTestingPage[] = "files/keyevents_test.html"; |
38 const wchar_t kSuppressEventJS[] = | 38 const char kSuppressEventJS[] = |
39 L"window.domAutomationController.send(setDefaultAction('%ls', %ls));"; | 39 "window.domAutomationController.send(setDefaultAction('%ls', %ls));"; |
40 const wchar_t kGetResultJS[] = | 40 const char kGetResultJS[] = |
41 L"window.domAutomationController.send(keyEventResult[%d]);"; | 41 "window.domAutomationController.send(keyEventResult[%d]);"; |
42 const wchar_t kGetResultLengthJS[] = | 42 const char kGetResultLengthJS[] = |
43 L"window.domAutomationController.send(keyEventResult.length);"; | 43 "window.domAutomationController.send(keyEventResult.length);"; |
44 const wchar_t kGetFocusedElementJS[] = | 44 const char kGetFocusedElementJS[] = |
45 L"window.domAutomationController.send(focusedElement);"; | 45 "window.domAutomationController.send(focusedElement);"; |
46 const wchar_t kSetFocusedElementJS[] = | 46 const char kSetFocusedElementJS[] = |
47 L"window.domAutomationController.send(setFocusedElement('%ls'));"; | 47 "window.domAutomationController.send(setFocusedElement('%ls'));"; |
48 const wchar_t kGetTextBoxValueJS[] = | 48 const char kGetTextBoxValueJS[] = |
49 L"window.domAutomationController.send(" | 49 "window.domAutomationController.send(" |
50 L"document.getElementById('%ls').value);"; | 50 " document.getElementById('%ls').value);"; |
51 const wchar_t kSetTextBoxValueJS[] = | 51 const char kSetTextBoxValueJS[] = |
52 L"window.domAutomationController.send(" | 52 "window.domAutomationController.send(" |
53 L"document.getElementById('%ls').value = '%ls');"; | 53 " document.getElementById('%ls').value = '%ls');"; |
54 const wchar_t kStartTestJS[] = | 54 const char kStartTestJS[] = |
55 L"window.domAutomationController.send(startTest(%d));"; | 55 "window.domAutomationController.send(startTest(%d));"; |
56 | 56 |
57 // Maximum lenght of the result array in KeyEventTestData structure. | 57 // Maximum lenght of the result array in KeyEventTestData structure. |
58 const size_t kMaxResultLength = 10; | 58 const size_t kMaxResultLength = 10; |
59 | 59 |
60 // A structure holding test data of a keyboard event. | 60 // A structure holding test data of a keyboard event. |
61 // Each keyboard event may generate multiple result strings representing | 61 // Each keyboard event may generate multiple result strings representing |
62 // the result of keydown, keypress, keyup and textInput events. | 62 // the result of keydown, keypress, keyup and textInput events. |
63 // For keydown, keypress and keyup events, the format of the result string is: | 63 // For keydown, keypress and keyup events, the format of the result string is: |
64 // <type> <keyCode> <charCode> <ctrlKey> <shiftKey> <altKey> <commandKey> | 64 // <type> <keyCode> <charCode> <ctrlKey> <shiftKey> <altKey> <commandKey> |
65 // where <type> may be 'D' (keydown), 'P' (keypress) or 'U' (keyup). | 65 // where <type> may be 'D' (keydown), 'P' (keypress) or 'U' (keyup). |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
141 } | 141 } |
142 | 142 |
143 // Set the suppress flag of an event specified by |type|. If |suppress| is | 143 // Set the suppress flag of an event specified by |type|. If |suppress| is |
144 // true then the web page will suppress all events with |type|. Following | 144 // true then the web page will suppress all events with |type|. Following |
145 // event types are supported: keydown, keypress, keyup and textInput. | 145 // event types are supported: keydown, keypress, keyup and textInput. |
146 void SuppressEventByType(int tab_index, const wchar_t* type, bool suppress) { | 146 void SuppressEventByType(int tab_index, const wchar_t* type, bool suppress) { |
147 ASSERT_LT(tab_index, browser()->tab_count()); | 147 ASSERT_LT(tab_index, browser()->tab_count()); |
148 bool actual; | 148 bool actual; |
149 ASSERT_TRUE(content::ExecuteJavaScriptAndExtractBool( | 149 ASSERT_TRUE(content::ExecuteJavaScriptAndExtractBool( |
150 chrome::GetWebContentsAt(browser(), tab_index)->GetRenderViewHost(), | 150 chrome::GetWebContentsAt(browser(), tab_index)->GetRenderViewHost(), |
151 L"", | 151 "", |
152 base::StringPrintf(kSuppressEventJS, type, GetBoolString(!suppress)), | 152 base::StringPrintf(kSuppressEventJS, type, GetBoolString(!suppress)), |
153 &actual)); | 153 &actual)); |
154 ASSERT_EQ(!suppress, actual); | 154 ASSERT_EQ(!suppress, actual); |
155 } | 155 } |
156 | 156 |
157 void SuppressEvents(int tab_index, bool keydown, bool keypress, | 157 void SuppressEvents(int tab_index, bool keydown, bool keypress, |
158 bool keyup, bool textinput) { | 158 bool keyup, bool textinput) { |
159 ASSERT_NO_FATAL_FAILURE( | 159 ASSERT_NO_FATAL_FAILURE( |
160 SuppressEventByType(tab_index, L"keydown", keydown)); | 160 SuppressEventByType(tab_index, L"keydown", keydown)); |
161 ASSERT_NO_FATAL_FAILURE( | 161 ASSERT_NO_FATAL_FAILURE( |
162 SuppressEventByType(tab_index, L"keypress", keypress)); | 162 SuppressEventByType(tab_index, L"keypress", keypress)); |
163 ASSERT_NO_FATAL_FAILURE( | 163 ASSERT_NO_FATAL_FAILURE( |
164 SuppressEventByType(tab_index, L"keyup", keyup)); | 164 SuppressEventByType(tab_index, L"keyup", keyup)); |
165 ASSERT_NO_FATAL_FAILURE( | 165 ASSERT_NO_FATAL_FAILURE( |
166 SuppressEventByType(tab_index, L"textInput", textinput)); | 166 SuppressEventByType(tab_index, L"textInput", textinput)); |
167 } | 167 } |
168 | 168 |
169 void SuppressAllEvents(int tab_index, bool suppress) { | 169 void SuppressAllEvents(int tab_index, bool suppress) { |
170 SuppressEvents(tab_index, suppress, suppress, suppress, suppress); | 170 SuppressEvents(tab_index, suppress, suppress, suppress, suppress); |
171 } | 171 } |
172 | 172 |
173 void GetResultLength(int tab_index, int* length) { | 173 void GetResultLength(int tab_index, int* length) { |
174 ASSERT_LT(tab_index, browser()->tab_count()); | 174 ASSERT_LT(tab_index, browser()->tab_count()); |
175 ASSERT_TRUE(content::ExecuteJavaScriptAndExtractInt( | 175 ASSERT_TRUE(content::ExecuteJavaScriptAndExtractInt( |
176 chrome::GetWebContentsAt(browser(), tab_index)->GetRenderViewHost(), | 176 chrome::GetWebContentsAt(browser(), tab_index)->GetRenderViewHost(), |
177 L"", kGetResultLengthJS, length)); | 177 "", |
| 178 kGetResultLengthJS, |
| 179 length)); |
178 } | 180 } |
179 | 181 |
180 void CheckResult(int tab_index, int length, const char* const result[]) { | 182 void CheckResult(int tab_index, int length, const char* const result[]) { |
181 ASSERT_LT(tab_index, browser()->tab_count()); | 183 ASSERT_LT(tab_index, browser()->tab_count()); |
182 int actual_length; | 184 int actual_length; |
183 ASSERT_NO_FATAL_FAILURE(GetResultLength(tab_index, &actual_length)); | 185 ASSERT_NO_FATAL_FAILURE(GetResultLength(tab_index, &actual_length)); |
184 ASSERT_GE(actual_length, length); | 186 ASSERT_GE(actual_length, length); |
185 for (int i = 0; i < actual_length; ++i) { | 187 for (int i = 0; i < actual_length; ++i) { |
186 std::string actual; | 188 std::string actual; |
187 ASSERT_TRUE(content::ExecuteJavaScriptAndExtractString( | 189 ASSERT_TRUE(content::ExecuteJavaScriptAndExtractString( |
188 chrome::GetWebContentsAt(browser(), tab_index)->GetRenderViewHost(), | 190 chrome::GetWebContentsAt(browser(), tab_index)->GetRenderViewHost(), |
189 L"", base::StringPrintf(kGetResultJS, i), &actual)); | 191 "", |
| 192 base::StringPrintf(kGetResultJS, i), |
| 193 &actual)); |
190 | 194 |
191 // If more events were received than expected, then the additional events | 195 // If more events were received than expected, then the additional events |
192 // must be keyup events. | 196 // must be keyup events. |
193 if (i < length) | 197 if (i < length) |
194 ASSERT_STREQ(result[i], actual.c_str()); | 198 ASSERT_STREQ(result[i], actual.c_str()); |
195 else | 199 else |
196 ASSERT_EQ('U', actual[0]); | 200 ASSERT_EQ('U', actual[0]); |
197 } | 201 } |
198 } | 202 } |
199 | 203 |
200 void CheckFocusedElement(int tab_index, const wchar_t* focused) { | 204 void CheckFocusedElement(int tab_index, const wchar_t* focused) { |
201 ASSERT_LT(tab_index, browser()->tab_count()); | 205 ASSERT_LT(tab_index, browser()->tab_count()); |
202 std::string actual; | 206 std::string actual; |
203 ASSERT_TRUE(content::ExecuteJavaScriptAndExtractString( | 207 ASSERT_TRUE(content::ExecuteJavaScriptAndExtractString( |
204 chrome::GetWebContentsAt(browser(), tab_index)->GetRenderViewHost(), | 208 chrome::GetWebContentsAt(browser(), tab_index)->GetRenderViewHost(), |
205 L"", kGetFocusedElementJS, &actual)); | 209 "", |
| 210 kGetFocusedElementJS, |
| 211 &actual)); |
206 ASSERT_EQ(WideToUTF8(focused), actual); | 212 ASSERT_EQ(WideToUTF8(focused), actual); |
207 } | 213 } |
208 | 214 |
209 void SetFocusedElement(int tab_index, const wchar_t* focused) { | 215 void SetFocusedElement(int tab_index, const wchar_t* focused) { |
210 ASSERT_LT(tab_index, browser()->tab_count()); | 216 ASSERT_LT(tab_index, browser()->tab_count()); |
211 bool actual; | 217 bool actual; |
212 ASSERT_TRUE(content::ExecuteJavaScriptAndExtractBool( | 218 ASSERT_TRUE(content::ExecuteJavaScriptAndExtractBool( |
213 chrome::GetWebContentsAt(browser(), tab_index)->GetRenderViewHost(), | 219 chrome::GetWebContentsAt(browser(), tab_index)->GetRenderViewHost(), |
214 L"", | 220 "", |
215 base::StringPrintf(kSetFocusedElementJS, focused), | 221 base::StringPrintf(kSetFocusedElementJS, focused), |
216 &actual)); | 222 &actual)); |
217 ASSERT_TRUE(actual); | 223 ASSERT_TRUE(actual); |
218 } | 224 } |
219 | 225 |
220 void CheckTextBoxValue(int tab_index, const wchar_t* id, | 226 void CheckTextBoxValue(int tab_index, const wchar_t* id, |
221 const wchar_t* value) { | 227 const wchar_t* value) { |
222 ASSERT_LT(tab_index, browser()->tab_count()); | 228 ASSERT_LT(tab_index, browser()->tab_count()); |
223 std::string actual; | 229 std::string actual; |
224 ASSERT_TRUE(content::ExecuteJavaScriptAndExtractString( | 230 ASSERT_TRUE(content::ExecuteJavaScriptAndExtractString( |
225 chrome::GetWebContentsAt(browser(), tab_index)->GetRenderViewHost(), | 231 chrome::GetWebContentsAt(browser(), tab_index)->GetRenderViewHost(), |
226 L"", | 232 "", |
227 base::StringPrintf(kGetTextBoxValueJS, id), | 233 base::StringPrintf(kGetTextBoxValueJS, id), |
228 &actual)); | 234 &actual)); |
229 ASSERT_EQ(WideToUTF8(value), actual); | 235 ASSERT_EQ(WideToUTF8(value), actual); |
230 } | 236 } |
231 | 237 |
232 void SetTextBoxValue(int tab_index, const wchar_t* id, | 238 void SetTextBoxValue(int tab_index, const wchar_t* id, |
233 const wchar_t* value) { | 239 const wchar_t* value) { |
234 ASSERT_LT(tab_index, browser()->tab_count()); | 240 ASSERT_LT(tab_index, browser()->tab_count()); |
235 std::string actual; | 241 std::string actual; |
236 ASSERT_TRUE(content::ExecuteJavaScriptAndExtractString( | 242 ASSERT_TRUE(content::ExecuteJavaScriptAndExtractString( |
237 chrome::GetWebContentsAt(browser(), tab_index)->GetRenderViewHost(), | 243 chrome::GetWebContentsAt(browser(), tab_index)->GetRenderViewHost(), |
238 L"", | 244 "", |
239 base::StringPrintf(kSetTextBoxValueJS, id, value), | 245 base::StringPrintf(kSetTextBoxValueJS, id, value), |
240 &actual)); | 246 &actual)); |
241 ASSERT_EQ(WideToUTF8(value), actual); | 247 ASSERT_EQ(WideToUTF8(value), actual); |
242 } | 248 } |
243 | 249 |
244 void StartTest(int tab_index, int result_length) { | 250 void StartTest(int tab_index, int result_length) { |
245 ASSERT_LT(tab_index, browser()->tab_count()); | 251 ASSERT_LT(tab_index, browser()->tab_count()); |
246 bool actual; | 252 bool actual; |
247 ASSERT_TRUE(content::ExecuteJavaScriptAndExtractBool( | 253 ASSERT_TRUE(content::ExecuteJavaScriptAndExtractBool( |
248 chrome::GetWebContentsAt(browser(), tab_index)->GetRenderViewHost(), | 254 chrome::GetWebContentsAt(browser(), tab_index)->GetRenderViewHost(), |
249 L"", base::StringPrintf(kStartTestJS, result_length), &actual)); | 255 "", |
| 256 base::StringPrintf(kStartTestJS, result_length), |
| 257 &actual)); |
250 ASSERT_TRUE(actual); | 258 ASSERT_TRUE(actual); |
251 } | 259 } |
252 | 260 |
253 void TestKeyEvent(int tab_index, const KeyEventTestData& test) { | 261 void TestKeyEvent(int tab_index, const KeyEventTestData& test) { |
254 ASSERT_LT(tab_index, browser()->tab_count()); | 262 ASSERT_LT(tab_index, browser()->tab_count()); |
255 ASSERT_EQ(tab_index, browser()->active_index()); | 263 ASSERT_EQ(tab_index, browser()->active_index()); |
256 | 264 |
257 // Inform our testing web page that we are about to start testing a key | 265 // Inform our testing web page that we are about to start testing a key |
258 // event. | 266 // event. |
259 ASSERT_NO_FATAL_FAILURE(StartTest(tab_index, test.result_length)); | 267 ASSERT_NO_FATAL_FAILURE(StartTest(tab_index, test.result_length)); |
(...skipping 624 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
884 EXPECT_NO_FATAL_FAILURE(TestKeyEvent(tab_index, kTestAltKeySuppress)); | 892 EXPECT_NO_FATAL_FAILURE(TestKeyEvent(tab_index, kTestAltKeySuppress)); |
885 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER)); | 893 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER)); |
886 | 894 |
887 // Ctrl+Alt should have no effect. | 895 // Ctrl+Alt should have no effect. |
888 EXPECT_NO_FATAL_FAILURE(TestKeyEvent(tab_index, kTestCtrlAltKey)); | 896 EXPECT_NO_FATAL_FAILURE(TestKeyEvent(tab_index, kTestCtrlAltKey)); |
889 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER)); | 897 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER)); |
890 } | 898 } |
891 #endif | 899 #endif |
892 | 900 |
893 } // namespace | 901 } // namespace |
OLD | NEW |