| 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 char kSuppressEventJS[] = | 38 const wchar_t kSuppressEventJS[] = |
| 39 "window.domAutomationController.send(setDefaultAction('%ls', %ls));"; | 39 L"window.domAutomationController.send(setDefaultAction('%ls', %ls));"; |
| 40 const char kGetResultJS[] = | 40 const wchar_t kGetResultJS[] = |
| 41 "window.domAutomationController.send(keyEventResult[%d]);"; | 41 L"window.domAutomationController.send(keyEventResult[%d]);"; |
| 42 const char kGetResultLengthJS[] = | 42 const wchar_t kGetResultLengthJS[] = |
| 43 "window.domAutomationController.send(keyEventResult.length);"; | 43 L"window.domAutomationController.send(keyEventResult.length);"; |
| 44 const char kGetFocusedElementJS[] = | 44 const wchar_t kGetFocusedElementJS[] = |
| 45 "window.domAutomationController.send(focusedElement);"; | 45 L"window.domAutomationController.send(focusedElement);"; |
| 46 const char kSetFocusedElementJS[] = | 46 const wchar_t kSetFocusedElementJS[] = |
| 47 "window.domAutomationController.send(setFocusedElement('%ls'));"; | 47 L"window.domAutomationController.send(setFocusedElement('%ls'));"; |
| 48 const char kGetTextBoxValueJS[] = | 48 const wchar_t kGetTextBoxValueJS[] = |
| 49 "window.domAutomationController.send(" | 49 L"window.domAutomationController.send(" |
| 50 " document.getElementById('%ls').value);"; | 50 L"document.getElementById('%ls').value);"; |
| 51 const char kSetTextBoxValueJS[] = | 51 const wchar_t kSetTextBoxValueJS[] = |
| 52 "window.domAutomationController.send(" | 52 L"window.domAutomationController.send(" |
| 53 " document.getElementById('%ls').value = '%ls');"; | 53 L"document.getElementById('%ls').value = '%ls');"; |
| 54 const char kStartTestJS[] = | 54 const wchar_t kStartTestJS[] = |
| 55 "window.domAutomationController.send(startTest(%d));"; | 55 L"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 "", | 151 L"", |
| 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 "", | 177 L"", kGetResultLengthJS, length)); |
| 178 kGetResultLengthJS, | |
| 179 length)); | |
| 180 } | 178 } |
| 181 | 179 |
| 182 void CheckResult(int tab_index, int length, const char* const result[]) { | 180 void CheckResult(int tab_index, int length, const char* const result[]) { |
| 183 ASSERT_LT(tab_index, browser()->tab_count()); | 181 ASSERT_LT(tab_index, browser()->tab_count()); |
| 184 int actual_length; | 182 int actual_length; |
| 185 ASSERT_NO_FATAL_FAILURE(GetResultLength(tab_index, &actual_length)); | 183 ASSERT_NO_FATAL_FAILURE(GetResultLength(tab_index, &actual_length)); |
| 186 ASSERT_GE(actual_length, length); | 184 ASSERT_GE(actual_length, length); |
| 187 for (int i = 0; i < actual_length; ++i) { | 185 for (int i = 0; i < actual_length; ++i) { |
| 188 std::string actual; | 186 std::string actual; |
| 189 ASSERT_TRUE(content::ExecuteJavaScriptAndExtractString( | 187 ASSERT_TRUE(content::ExecuteJavaScriptAndExtractString( |
| 190 chrome::GetWebContentsAt(browser(), tab_index)->GetRenderViewHost(), | 188 chrome::GetWebContentsAt(browser(), tab_index)->GetRenderViewHost(), |
| 191 "", | 189 L"", base::StringPrintf(kGetResultJS, i), &actual)); |
| 192 base::StringPrintf(kGetResultJS, i), | |
| 193 &actual)); | |
| 194 | 190 |
| 195 // If more events were received than expected, then the additional events | 191 // If more events were received than expected, then the additional events |
| 196 // must be keyup events. | 192 // must be keyup events. |
| 197 if (i < length) | 193 if (i < length) |
| 198 ASSERT_STREQ(result[i], actual.c_str()); | 194 ASSERT_STREQ(result[i], actual.c_str()); |
| 199 else | 195 else |
| 200 ASSERT_EQ('U', actual[0]); | 196 ASSERT_EQ('U', actual[0]); |
| 201 } | 197 } |
| 202 } | 198 } |
| 203 | 199 |
| 204 void CheckFocusedElement(int tab_index, const wchar_t* focused) { | 200 void CheckFocusedElement(int tab_index, const wchar_t* focused) { |
| 205 ASSERT_LT(tab_index, browser()->tab_count()); | 201 ASSERT_LT(tab_index, browser()->tab_count()); |
| 206 std::string actual; | 202 std::string actual; |
| 207 ASSERT_TRUE(content::ExecuteJavaScriptAndExtractString( | 203 ASSERT_TRUE(content::ExecuteJavaScriptAndExtractString( |
| 208 chrome::GetWebContentsAt(browser(), tab_index)->GetRenderViewHost(), | 204 chrome::GetWebContentsAt(browser(), tab_index)->GetRenderViewHost(), |
| 209 "", | 205 L"", kGetFocusedElementJS, &actual)); |
| 210 kGetFocusedElementJS, | |
| 211 &actual)); | |
| 212 ASSERT_EQ(WideToUTF8(focused), actual); | 206 ASSERT_EQ(WideToUTF8(focused), actual); |
| 213 } | 207 } |
| 214 | 208 |
| 215 void SetFocusedElement(int tab_index, const wchar_t* focused) { | 209 void SetFocusedElement(int tab_index, const wchar_t* focused) { |
| 216 ASSERT_LT(tab_index, browser()->tab_count()); | 210 ASSERT_LT(tab_index, browser()->tab_count()); |
| 217 bool actual; | 211 bool actual; |
| 218 ASSERT_TRUE(content::ExecuteJavaScriptAndExtractBool( | 212 ASSERT_TRUE(content::ExecuteJavaScriptAndExtractBool( |
| 219 chrome::GetWebContentsAt(browser(), tab_index)->GetRenderViewHost(), | 213 chrome::GetWebContentsAt(browser(), tab_index)->GetRenderViewHost(), |
| 220 "", | 214 L"", |
| 221 base::StringPrintf(kSetFocusedElementJS, focused), | 215 base::StringPrintf(kSetFocusedElementJS, focused), |
| 222 &actual)); | 216 &actual)); |
| 223 ASSERT_TRUE(actual); | 217 ASSERT_TRUE(actual); |
| 224 } | 218 } |
| 225 | 219 |
| 226 void CheckTextBoxValue(int tab_index, const wchar_t* id, | 220 void CheckTextBoxValue(int tab_index, const wchar_t* id, |
| 227 const wchar_t* value) { | 221 const wchar_t* value) { |
| 228 ASSERT_LT(tab_index, browser()->tab_count()); | 222 ASSERT_LT(tab_index, browser()->tab_count()); |
| 229 std::string actual; | 223 std::string actual; |
| 230 ASSERT_TRUE(content::ExecuteJavaScriptAndExtractString( | 224 ASSERT_TRUE(content::ExecuteJavaScriptAndExtractString( |
| 231 chrome::GetWebContentsAt(browser(), tab_index)->GetRenderViewHost(), | 225 chrome::GetWebContentsAt(browser(), tab_index)->GetRenderViewHost(), |
| 232 "", | 226 L"", |
| 233 base::StringPrintf(kGetTextBoxValueJS, id), | 227 base::StringPrintf(kGetTextBoxValueJS, id), |
| 234 &actual)); | 228 &actual)); |
| 235 ASSERT_EQ(WideToUTF8(value), actual); | 229 ASSERT_EQ(WideToUTF8(value), actual); |
| 236 } | 230 } |
| 237 | 231 |
| 238 void SetTextBoxValue(int tab_index, const wchar_t* id, | 232 void SetTextBoxValue(int tab_index, const wchar_t* id, |
| 239 const wchar_t* value) { | 233 const wchar_t* value) { |
| 240 ASSERT_LT(tab_index, browser()->tab_count()); | 234 ASSERT_LT(tab_index, browser()->tab_count()); |
| 241 std::string actual; | 235 std::string actual; |
| 242 ASSERT_TRUE(content::ExecuteJavaScriptAndExtractString( | 236 ASSERT_TRUE(content::ExecuteJavaScriptAndExtractString( |
| 243 chrome::GetWebContentsAt(browser(), tab_index)->GetRenderViewHost(), | 237 chrome::GetWebContentsAt(browser(), tab_index)->GetRenderViewHost(), |
| 244 "", | 238 L"", |
| 245 base::StringPrintf(kSetTextBoxValueJS, id, value), | 239 base::StringPrintf(kSetTextBoxValueJS, id, value), |
| 246 &actual)); | 240 &actual)); |
| 247 ASSERT_EQ(WideToUTF8(value), actual); | 241 ASSERT_EQ(WideToUTF8(value), actual); |
| 248 } | 242 } |
| 249 | 243 |
| 250 void StartTest(int tab_index, int result_length) { | 244 void StartTest(int tab_index, int result_length) { |
| 251 ASSERT_LT(tab_index, browser()->tab_count()); | 245 ASSERT_LT(tab_index, browser()->tab_count()); |
| 252 bool actual; | 246 bool actual; |
| 253 ASSERT_TRUE(content::ExecuteJavaScriptAndExtractBool( | 247 ASSERT_TRUE(content::ExecuteJavaScriptAndExtractBool( |
| 254 chrome::GetWebContentsAt(browser(), tab_index)->GetRenderViewHost(), | 248 chrome::GetWebContentsAt(browser(), tab_index)->GetRenderViewHost(), |
| 255 "", | 249 L"", base::StringPrintf(kStartTestJS, result_length), &actual)); |
| 256 base::StringPrintf(kStartTestJS, result_length), | |
| 257 &actual)); | |
| 258 ASSERT_TRUE(actual); | 250 ASSERT_TRUE(actual); |
| 259 } | 251 } |
| 260 | 252 |
| 261 void TestKeyEvent(int tab_index, const KeyEventTestData& test) { | 253 void TestKeyEvent(int tab_index, const KeyEventTestData& test) { |
| 262 ASSERT_LT(tab_index, browser()->tab_count()); | 254 ASSERT_LT(tab_index, browser()->tab_count()); |
| 263 ASSERT_EQ(tab_index, browser()->active_index()); | 255 ASSERT_EQ(tab_index, browser()->active_index()); |
| 264 | 256 |
| 265 // Inform our testing web page that we are about to start testing a key | 257 // Inform our testing web page that we are about to start testing a key |
| 266 // event. | 258 // event. |
| 267 ASSERT_NO_FATAL_FAILURE(StartTest(tab_index, test.result_length)); | 259 ASSERT_NO_FATAL_FAILURE(StartTest(tab_index, test.result_length)); |
| (...skipping 624 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 892 EXPECT_NO_FATAL_FAILURE(TestKeyEvent(tab_index, kTestAltKeySuppress)); | 884 EXPECT_NO_FATAL_FAILURE(TestKeyEvent(tab_index, kTestAltKeySuppress)); |
| 893 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER)); | 885 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER)); |
| 894 | 886 |
| 895 // Ctrl+Alt should have no effect. | 887 // Ctrl+Alt should have no effect. |
| 896 EXPECT_NO_FATAL_FAILURE(TestKeyEvent(tab_index, kTestCtrlAltKey)); | 888 EXPECT_NO_FATAL_FAILURE(TestKeyEvent(tab_index, kTestCtrlAltKey)); |
| 897 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER)); | 889 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER)); |
| 898 } | 890 } |
| 899 #endif | 891 #endif |
| 900 | 892 |
| 901 } // namespace | 893 } // namespace |
| OLD | NEW |