OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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" |
11 #include "base/utf_string_conversions.h" | 11 #include "base/utf_string_conversions.h" |
12 #include "base/values.h" | 12 #include "base/values.h" |
13 #include "chrome/browser/dom_operation_notification_details.h" | 13 #include "chrome/browser/dom_operation_notification_details.h" |
14 #include "chrome/browser/ui/browser.h" | 14 #include "chrome/browser/ui/browser.h" |
15 #include "chrome/common/chrome_notification_types.h" | 15 #include "chrome/common/chrome_notification_types.h" |
16 #include "chrome/common/chrome_paths.h" | 16 #include "chrome/common/chrome_paths.h" |
17 #include "chrome/test/base/in_process_browser_test.h" | 17 #include "chrome/test/base/in_process_browser_test.h" |
18 #include "chrome/test/base/ui_test_utils.h" | 18 #include "chrome/test/base/ui_test_utils.h" |
19 #include "content/browser/renderer_host/render_view_host.h" | 19 #include "content/browser/renderer_host/render_view_host.h" |
20 #include "content/browser/renderer_host/render_widget_host_view.h" | 20 #include "content/browser/renderer_host/render_widget_host_view.h" |
21 #include "content/browser/tab_contents/tab_contents.h" | |
22 #include "content/browser/tab_contents/tab_contents_view.h" | 21 #include "content/browser/tab_contents/tab_contents_view.h" |
23 #include "content/public/browser/notification_registrar.h" | 22 #include "content/public/browser/notification_registrar.h" |
24 #include "content/public/browser/notification_service.h" | 23 #include "content/public/browser/notification_service.h" |
| 24 #include "content/public/browser/web_contents.h" |
25 #include "net/test/test_server.h" | 25 #include "net/test/test_server.h" |
26 #include "ui/base/keycodes/keyboard_codes.h" | 26 #include "ui/base/keycodes/keyboard_codes.h" |
27 | 27 |
28 namespace { | 28 namespace { |
29 | 29 |
30 const char kTestingPage[] = "files/keyevents_test.html"; | 30 const char kTestingPage[] = "files/keyevents_test.html"; |
31 const wchar_t kSuppressEventJS[] = | 31 const wchar_t kSuppressEventJS[] = |
32 L"window.domAutomationController.send(setDefaultAction('%ls', %ls));"; | 32 L"window.domAutomationController.send(setDefaultAction('%ls', %ls));"; |
33 const wchar_t kGetResultJS[] = | 33 const wchar_t kGetResultJS[] = |
34 L"window.domAutomationController.send(keyEventResult[%d]);"; | 34 L"window.domAutomationController.send(keyEventResult[%d]);"; |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
136 ui_test_utils::ClickOnView(browser(), vid); | 136 ui_test_utils::ClickOnView(browser(), vid); |
137 } | 137 } |
138 | 138 |
139 // Set the suppress flag of an event specified by |type|. If |suppress| is | 139 // Set the suppress flag of an event specified by |type|. If |suppress| is |
140 // true then the web page will suppress all events with |type|. Following | 140 // true then the web page will suppress all events with |type|. Following |
141 // event types are supported: keydown, keypress, keyup and textInput. | 141 // event types are supported: keydown, keypress, keyup and textInput. |
142 void SuppressEventByType(int tab_index, const wchar_t* type, bool suppress) { | 142 void SuppressEventByType(int tab_index, const wchar_t* type, bool suppress) { |
143 ASSERT_LT(tab_index, browser()->tab_count()); | 143 ASSERT_LT(tab_index, browser()->tab_count()); |
144 bool actual; | 144 bool actual; |
145 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool( | 145 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool( |
146 browser()->GetTabContentsAt(tab_index)->GetRenderViewHost(), | 146 browser()->GetWebContentsAt(tab_index)->GetRenderViewHost(), |
147 L"", | 147 L"", |
148 base::StringPrintf(kSuppressEventJS, type, GetBoolString(!suppress)), | 148 base::StringPrintf(kSuppressEventJS, type, GetBoolString(!suppress)), |
149 &actual)); | 149 &actual)); |
150 ASSERT_EQ(!suppress, actual); | 150 ASSERT_EQ(!suppress, actual); |
151 } | 151 } |
152 | 152 |
153 void SuppressEvents(int tab_index, bool keydown, bool keypress, | 153 void SuppressEvents(int tab_index, bool keydown, bool keypress, |
154 bool keyup, bool textinput) { | 154 bool keyup, bool textinput) { |
155 ASSERT_NO_FATAL_FAILURE( | 155 ASSERT_NO_FATAL_FAILURE( |
156 SuppressEventByType(tab_index, L"keydown", keydown)); | 156 SuppressEventByType(tab_index, L"keydown", keydown)); |
157 ASSERT_NO_FATAL_FAILURE( | 157 ASSERT_NO_FATAL_FAILURE( |
158 SuppressEventByType(tab_index, L"keypress", keypress)); | 158 SuppressEventByType(tab_index, L"keypress", keypress)); |
159 ASSERT_NO_FATAL_FAILURE( | 159 ASSERT_NO_FATAL_FAILURE( |
160 SuppressEventByType(tab_index, L"keyup", keyup)); | 160 SuppressEventByType(tab_index, L"keyup", keyup)); |
161 ASSERT_NO_FATAL_FAILURE( | 161 ASSERT_NO_FATAL_FAILURE( |
162 SuppressEventByType(tab_index, L"textInput", textinput)); | 162 SuppressEventByType(tab_index, L"textInput", textinput)); |
163 } | 163 } |
164 | 164 |
165 void SuppressAllEvents(int tab_index, bool suppress) { | 165 void SuppressAllEvents(int tab_index, bool suppress) { |
166 SuppressEvents(tab_index, suppress, suppress, suppress, suppress); | 166 SuppressEvents(tab_index, suppress, suppress, suppress, suppress); |
167 } | 167 } |
168 | 168 |
169 void GetResultLength(int tab_index, int* length) { | 169 void GetResultLength(int tab_index, int* length) { |
170 ASSERT_LT(tab_index, browser()->tab_count()); | 170 ASSERT_LT(tab_index, browser()->tab_count()); |
171 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractInt( | 171 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractInt( |
172 browser()->GetTabContentsAt(tab_index)->GetRenderViewHost(), | 172 browser()->GetWebContentsAt(tab_index)->GetRenderViewHost(), |
173 L"", kGetResultLengthJS, length)); | 173 L"", kGetResultLengthJS, length)); |
174 } | 174 } |
175 | 175 |
176 void CheckResult(int tab_index, int length, const char* const result[]) { | 176 void CheckResult(int tab_index, int length, const char* const result[]) { |
177 ASSERT_LT(tab_index, browser()->tab_count()); | 177 ASSERT_LT(tab_index, browser()->tab_count()); |
178 int actual_length; | 178 int actual_length; |
179 ASSERT_NO_FATAL_FAILURE(GetResultLength(tab_index, &actual_length)); | 179 ASSERT_NO_FATAL_FAILURE(GetResultLength(tab_index, &actual_length)); |
180 ASSERT_GE(actual_length, length); | 180 ASSERT_GE(actual_length, length); |
181 for (int i = 0; i < actual_length; ++i) { | 181 for (int i = 0; i < actual_length; ++i) { |
182 std::string actual; | 182 std::string actual; |
183 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString( | 183 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString( |
184 browser()->GetTabContentsAt(tab_index)->GetRenderViewHost(), | 184 browser()->GetWebContentsAt(tab_index)->GetRenderViewHost(), |
185 L"", base::StringPrintf(kGetResultJS, i), &actual)); | 185 L"", base::StringPrintf(kGetResultJS, i), &actual)); |
186 | 186 |
187 // If more events were received than expected, then the additional events | 187 // If more events were received than expected, then the additional events |
188 // must be keyup events. | 188 // must be keyup events. |
189 if (i < length) | 189 if (i < length) |
190 ASSERT_STREQ(result[i], actual.c_str()); | 190 ASSERT_STREQ(result[i], actual.c_str()); |
191 else | 191 else |
192 ASSERT_EQ('U', actual[0]); | 192 ASSERT_EQ('U', actual[0]); |
193 } | 193 } |
194 } | 194 } |
195 | 195 |
196 void CheckFocusedElement(int tab_index, const wchar_t* focused) { | 196 void CheckFocusedElement(int tab_index, const wchar_t* focused) { |
197 ASSERT_LT(tab_index, browser()->tab_count()); | 197 ASSERT_LT(tab_index, browser()->tab_count()); |
198 std::string actual; | 198 std::string actual; |
199 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString( | 199 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString( |
200 browser()->GetTabContentsAt(tab_index)->GetRenderViewHost(), | 200 browser()->GetWebContentsAt(tab_index)->GetRenderViewHost(), |
201 L"", kGetFocusedElementJS, &actual)); | 201 L"", kGetFocusedElementJS, &actual)); |
202 ASSERT_EQ(WideToUTF8(focused), actual); | 202 ASSERT_EQ(WideToUTF8(focused), actual); |
203 } | 203 } |
204 | 204 |
205 void SetFocusedElement(int tab_index, const wchar_t* focused) { | 205 void SetFocusedElement(int tab_index, const wchar_t* focused) { |
206 ASSERT_LT(tab_index, browser()->tab_count()); | 206 ASSERT_LT(tab_index, browser()->tab_count()); |
207 bool actual; | 207 bool actual; |
208 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool( | 208 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool( |
209 browser()->GetTabContentsAt(tab_index)->GetRenderViewHost(), | 209 browser()->GetWebContentsAt(tab_index)->GetRenderViewHost(), |
210 L"", | 210 L"", |
211 base::StringPrintf(kSetFocusedElementJS, focused), | 211 base::StringPrintf(kSetFocusedElementJS, focused), |
212 &actual)); | 212 &actual)); |
213 ASSERT_TRUE(actual); | 213 ASSERT_TRUE(actual); |
214 } | 214 } |
215 | 215 |
216 void CheckTextBoxValue(int tab_index, const wchar_t* id, | 216 void CheckTextBoxValue(int tab_index, const wchar_t* id, |
217 const wchar_t* value) { | 217 const wchar_t* value) { |
218 ASSERT_LT(tab_index, browser()->tab_count()); | 218 ASSERT_LT(tab_index, browser()->tab_count()); |
219 std::string actual; | 219 std::string actual; |
220 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString( | 220 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString( |
221 browser()->GetTabContentsAt(tab_index)->GetRenderViewHost(), | 221 browser()->GetWebContentsAt(tab_index)->GetRenderViewHost(), |
222 L"", | 222 L"", |
223 base::StringPrintf(kGetTextBoxValueJS, id), | 223 base::StringPrintf(kGetTextBoxValueJS, id), |
224 &actual)); | 224 &actual)); |
225 ASSERT_EQ(WideToUTF8(value), actual); | 225 ASSERT_EQ(WideToUTF8(value), actual); |
226 } | 226 } |
227 | 227 |
228 void SetTextBoxValue(int tab_index, const wchar_t* id, | 228 void SetTextBoxValue(int tab_index, const wchar_t* id, |
229 const wchar_t* value) { | 229 const wchar_t* value) { |
230 ASSERT_LT(tab_index, browser()->tab_count()); | 230 ASSERT_LT(tab_index, browser()->tab_count()); |
231 std::string actual; | 231 std::string actual; |
232 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString( | 232 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString( |
233 browser()->GetTabContentsAt(tab_index)->GetRenderViewHost(), | 233 browser()->GetWebContentsAt(tab_index)->GetRenderViewHost(), |
234 L"", | 234 L"", |
235 base::StringPrintf(kSetTextBoxValueJS, id, value), | 235 base::StringPrintf(kSetTextBoxValueJS, id, value), |
236 &actual)); | 236 &actual)); |
237 ASSERT_EQ(WideToUTF8(value), actual); | 237 ASSERT_EQ(WideToUTF8(value), actual); |
238 } | 238 } |
239 | 239 |
240 void StartTest(int tab_index, int result_length) { | 240 void StartTest(int tab_index, int result_length) { |
241 ASSERT_LT(tab_index, browser()->tab_count()); | 241 ASSERT_LT(tab_index, browser()->tab_count()); |
242 bool actual; | 242 bool actual; |
243 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool( | 243 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool( |
244 browser()->GetTabContentsAt(tab_index)->GetRenderViewHost(), | 244 browser()->GetWebContentsAt(tab_index)->GetRenderViewHost(), |
245 L"", base::StringPrintf(kStartTestJS, result_length), &actual)); | 245 L"", base::StringPrintf(kStartTestJS, result_length), &actual)); |
246 ASSERT_TRUE(actual); | 246 ASSERT_TRUE(actual); |
247 } | 247 } |
248 | 248 |
249 void TestKeyEvent(int tab_index, const KeyEventTestData& test) { | 249 void TestKeyEvent(int tab_index, const KeyEventTestData& test) { |
250 ASSERT_LT(tab_index, browser()->tab_count()); | 250 ASSERT_LT(tab_index, browser()->tab_count()); |
251 ASSERT_EQ(tab_index, browser()->active_index()); | 251 ASSERT_EQ(tab_index, browser()->active_index()); |
252 | 252 |
253 // Inform our testing web page that we are about to start testing a key | 253 // Inform our testing web page that we are about to start testing a key |
254 // event. | 254 // event. |
255 ASSERT_NO_FATAL_FAILURE(StartTest(tab_index, test.result_length)); | 255 ASSERT_NO_FATAL_FAILURE(StartTest(tab_index, test.result_length)); |
256 ASSERT_NO_FATAL_FAILURE(SuppressEvents( | 256 ASSERT_NO_FATAL_FAILURE(SuppressEvents( |
257 tab_index, test.suppress_keydown, test.suppress_keypress, | 257 tab_index, test.suppress_keydown, test.suppress_keypress, |
258 test.suppress_keyup, test.suppress_textinput)); | 258 test.suppress_keyup, test.suppress_textinput)); |
259 | 259 |
260 // We need to create a finish observer before sending the key event, | 260 // We need to create a finish observer before sending the key event, |
261 // because the test finished message might be arrived before returning | 261 // because the test finished message might be arrived before returning |
262 // from the SendKeyPressSync() method. | 262 // from the SendKeyPressSync() method. |
263 TestFinishObserver finish_observer( | 263 TestFinishObserver finish_observer( |
264 browser()->GetTabContentsAt(tab_index)->GetRenderViewHost()); | 264 browser()->GetWebContentsAt(tab_index)->GetRenderViewHost()); |
265 | 265 |
266 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( | 266 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( |
267 browser(), test.key, test.ctrl, test.shift, test.alt, test.command)); | 267 browser(), test.key, test.ctrl, test.shift, test.alt, test.command)); |
268 ASSERT_TRUE(finish_observer.WaitForFinish()); | 268 ASSERT_TRUE(finish_observer.WaitForFinish()); |
269 ASSERT_NO_FATAL_FAILURE(CheckResult( | 269 ASSERT_NO_FATAL_FAILURE(CheckResult( |
270 tab_index, test.result_length, test.result)); | 270 tab_index, test.result_length, test.result)); |
271 } | 271 } |
272 | 272 |
273 std::string GetTestDataDescription(const KeyEventTestData& data) { | 273 std::string GetTestDataDescription(const KeyEventTestData& data) { |
274 std::string desc = base::StringPrintf( | 274 std::string desc = base::StringPrintf( |
(...skipping 431 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
706 // Make sure the focus is in the testing page. | 706 // Make sure the focus is in the testing page. |
707 ASSERT_NO_FATAL_FAILURE(ClickOnView(VIEW_ID_TAB_CONTAINER)); | 707 ASSERT_NO_FATAL_FAILURE(ClickOnView(VIEW_ID_TAB_CONTAINER)); |
708 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); | 708 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); |
709 | 709 |
710 // Reserved accelerators can't be suppressed. | 710 // Reserved accelerators can't be suppressed. |
711 ASSERT_NO_FATAL_FAILURE(SuppressAllEvents(1, true)); | 711 ASSERT_NO_FATAL_FAILURE(SuppressAllEvents(1, true)); |
712 | 712 |
713 ui_test_utils::WindowedNotificationObserver wait_for_tab_closed( | 713 ui_test_utils::WindowedNotificationObserver wait_for_tab_closed( |
714 content::NOTIFICATION_TAB_CLOSED, | 714 content::NOTIFICATION_TAB_CLOSED, |
715 content::Source<content::NavigationController>( | 715 content::Source<content::NavigationController>( |
716 &browser()->GetTabContentsAt(1)->GetController())); | 716 &browser()->GetWebContentsAt(1)->GetController())); |
717 | 717 |
718 // Press Ctrl/Cmd+W, which will close the tab. | 718 // Press Ctrl/Cmd+W, which will close the tab. |
719 #if defined(OS_MACOSX) | 719 #if defined(OS_MACOSX) |
720 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( | 720 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( |
721 browser(), ui::VKEY_W, false, false, false, true)); | 721 browser(), ui::VKEY_W, false, false, false, true)); |
722 #else | 722 #else |
723 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( | 723 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( |
724 browser(), ui::VKEY_W, true, false, false, false)); | 724 browser(), ui::VKEY_W, true, false, false, false)); |
725 #endif | 725 #endif |
726 | 726 |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
857 EXPECT_NO_FATAL_FAILURE(TestKeyEvent(tab_index, kTestAltKeySuppress)); | 857 EXPECT_NO_FATAL_FAILURE(TestKeyEvent(tab_index, kTestAltKeySuppress)); |
858 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); | 858 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); |
859 | 859 |
860 // Ctrl+Alt should have no effect. | 860 // Ctrl+Alt should have no effect. |
861 EXPECT_NO_FATAL_FAILURE(TestKeyEvent(tab_index, kTestCtrlAltKey)); | 861 EXPECT_NO_FATAL_FAILURE(TestKeyEvent(tab_index, kTestCtrlAltKey)); |
862 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); | 862 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); |
863 } | 863 } |
864 #endif | 864 #endif |
865 | 865 |
866 } // namespace | 866 } // namespace |
OLD | NEW |