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/string_util.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_paths.h" | 15 #include "chrome/common/chrome_paths.h" |
16 #include "chrome/test/in_process_browser_test.h" | 16 #include "chrome/test/in_process_browser_test.h" |
17 #include "chrome/test/ui_test_utils.h" | 17 #include "chrome/test/ui_test_utils.h" |
18 #include "content/browser/renderer_host/render_view_host.h" | 18 #include "content/browser/renderer_host/render_view_host.h" |
19 #include "content/browser/renderer_host/render_widget_host_view.h" | 19 #include "content/browser/renderer_host/render_widget_host_view.h" |
20 #include "content/browser/tab_contents/tab_contents.h" | 20 #include "content/browser/tab_contents/tab_contents.h" |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
137 | 137 |
138 // Set the suppress flag of an event specified by |type|. If |suppress| is | 138 // Set the suppress flag of an event specified by |type|. If |suppress| is |
139 // true then the web page will suppress all events with |type|. Following | 139 // true then the web page will suppress all events with |type|. Following |
140 // event types are supported: keydown, keypress, keyup and textInput. | 140 // event types are supported: keydown, keypress, keyup and textInput. |
141 void SuppressEventByType(int tab_index, const wchar_t* type, bool suppress) { | 141 void SuppressEventByType(int tab_index, const wchar_t* type, bool suppress) { |
142 ASSERT_LT(tab_index, browser()->tab_count()); | 142 ASSERT_LT(tab_index, browser()->tab_count()); |
143 bool actual; | 143 bool actual; |
144 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool( | 144 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool( |
145 browser()->GetTabContentsAt(tab_index)->render_view_host(), | 145 browser()->GetTabContentsAt(tab_index)->render_view_host(), |
146 L"", | 146 L"", |
147 StringPrintf(kSuppressEventJS, type, GetBoolString(!suppress)), | 147 base::StringPrintf(kSuppressEventJS, type, GetBoolString(!suppress)), |
148 &actual)); | 148 &actual)); |
149 ASSERT_EQ(!suppress, actual); | 149 ASSERT_EQ(!suppress, actual); |
150 } | 150 } |
151 | 151 |
152 void SuppressEvents(int tab_index, bool keydown, bool keypress, | 152 void SuppressEvents(int tab_index, bool keydown, bool keypress, |
153 bool keyup, bool textinput) { | 153 bool keyup, bool textinput) { |
154 ASSERT_NO_FATAL_FAILURE( | 154 ASSERT_NO_FATAL_FAILURE( |
155 SuppressEventByType(tab_index, L"keydown", keydown)); | 155 SuppressEventByType(tab_index, L"keydown", keydown)); |
156 ASSERT_NO_FATAL_FAILURE( | 156 ASSERT_NO_FATAL_FAILURE( |
157 SuppressEventByType(tab_index, L"keypress", keypress)); | 157 SuppressEventByType(tab_index, L"keypress", keypress)); |
(...skipping 16 matching lines...) Expand all Loading... |
174 | 174 |
175 void CheckResult(int tab_index, int length, const char* const result[]) { | 175 void CheckResult(int tab_index, int length, const char* const result[]) { |
176 ASSERT_LT(tab_index, browser()->tab_count()); | 176 ASSERT_LT(tab_index, browser()->tab_count()); |
177 int actual_length; | 177 int actual_length; |
178 ASSERT_NO_FATAL_FAILURE(GetResultLength(tab_index, &actual_length)); | 178 ASSERT_NO_FATAL_FAILURE(GetResultLength(tab_index, &actual_length)); |
179 ASSERT_GE(actual_length, length); | 179 ASSERT_GE(actual_length, length); |
180 for (int i = 0; i < actual_length; ++i) { | 180 for (int i = 0; i < actual_length; ++i) { |
181 std::string actual; | 181 std::string actual; |
182 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString( | 182 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString( |
183 browser()->GetTabContentsAt(tab_index)->render_view_host(), | 183 browser()->GetTabContentsAt(tab_index)->render_view_host(), |
184 L"", StringPrintf(kGetResultJS, i), &actual)); | 184 L"", base::StringPrintf(kGetResultJS, i), &actual)); |
185 | 185 |
186 // If more events were received than expected, then the additional events | 186 // If more events were received than expected, then the additional events |
187 // must be keyup events. | 187 // must be keyup events. |
188 if (i < length) | 188 if (i < length) |
189 ASSERT_STREQ(result[i], actual.c_str()); | 189 ASSERT_STREQ(result[i], actual.c_str()); |
190 else | 190 else |
191 ASSERT_EQ('U', actual[0]); | 191 ASSERT_EQ('U', actual[0]); |
192 } | 192 } |
193 } | 193 } |
194 | 194 |
195 void CheckFocusedElement(int tab_index, const wchar_t* focused) { | 195 void CheckFocusedElement(int tab_index, const wchar_t* focused) { |
196 ASSERT_LT(tab_index, browser()->tab_count()); | 196 ASSERT_LT(tab_index, browser()->tab_count()); |
197 std::string actual; | 197 std::string actual; |
198 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString( | 198 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString( |
199 browser()->GetTabContentsAt(tab_index)->render_view_host(), | 199 browser()->GetTabContentsAt(tab_index)->render_view_host(), |
200 L"", kGetFocusedElementJS, &actual)); | 200 L"", kGetFocusedElementJS, &actual)); |
201 ASSERT_EQ(WideToUTF8(focused), actual); | 201 ASSERT_EQ(WideToUTF8(focused), actual); |
202 } | 202 } |
203 | 203 |
204 void SetFocusedElement(int tab_index, const wchar_t* focused) { | 204 void SetFocusedElement(int tab_index, const wchar_t* focused) { |
205 ASSERT_LT(tab_index, browser()->tab_count()); | 205 ASSERT_LT(tab_index, browser()->tab_count()); |
206 bool actual; | 206 bool actual; |
207 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool( | 207 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool( |
208 browser()->GetTabContentsAt(tab_index)->render_view_host(), | 208 browser()->GetTabContentsAt(tab_index)->render_view_host(), |
209 L"", | 209 L"", |
210 StringPrintf(kSetFocusedElementJS, focused), | 210 base::StringPrintf(kSetFocusedElementJS, focused), |
211 &actual)); | 211 &actual)); |
212 ASSERT_TRUE(actual); | 212 ASSERT_TRUE(actual); |
213 } | 213 } |
214 | 214 |
215 void CheckTextBoxValue(int tab_index, const wchar_t* id, | 215 void CheckTextBoxValue(int tab_index, const wchar_t* id, |
216 const wchar_t* value) { | 216 const wchar_t* value) { |
217 ASSERT_LT(tab_index, browser()->tab_count()); | 217 ASSERT_LT(tab_index, browser()->tab_count()); |
218 std::string actual; | 218 std::string actual; |
219 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString( | 219 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString( |
220 browser()->GetTabContentsAt(tab_index)->render_view_host(), | 220 browser()->GetTabContentsAt(tab_index)->render_view_host(), |
221 L"", | 221 L"", |
222 StringPrintf(kGetTextBoxValueJS, id), | 222 base::StringPrintf(kGetTextBoxValueJS, id), |
223 &actual)); | 223 &actual)); |
224 ASSERT_EQ(WideToUTF8(value), actual); | 224 ASSERT_EQ(WideToUTF8(value), actual); |
225 } | 225 } |
226 | 226 |
227 void SetTextBoxValue(int tab_index, const wchar_t* id, | 227 void SetTextBoxValue(int tab_index, const wchar_t* id, |
228 const wchar_t* value) { | 228 const wchar_t* value) { |
229 ASSERT_LT(tab_index, browser()->tab_count()); | 229 ASSERT_LT(tab_index, browser()->tab_count()); |
230 std::string actual; | 230 std::string actual; |
231 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString( | 231 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString( |
232 browser()->GetTabContentsAt(tab_index)->render_view_host(), | 232 browser()->GetTabContentsAt(tab_index)->render_view_host(), |
233 L"", | 233 L"", |
234 StringPrintf(kSetTextBoxValueJS, id, value), | 234 base::StringPrintf(kSetTextBoxValueJS, id, value), |
235 &actual)); | 235 &actual)); |
236 ASSERT_EQ(WideToUTF8(value), actual); | 236 ASSERT_EQ(WideToUTF8(value), actual); |
237 } | 237 } |
238 | 238 |
239 void StartTest(int tab_index, int result_length) { | 239 void StartTest(int tab_index, int result_length) { |
240 ASSERT_LT(tab_index, browser()->tab_count()); | 240 ASSERT_LT(tab_index, browser()->tab_count()); |
241 bool actual; | 241 bool actual; |
242 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool( | 242 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool( |
243 browser()->GetTabContentsAt(tab_index)->render_view_host(), | 243 browser()->GetTabContentsAt(tab_index)->render_view_host(), |
244 L"", StringPrintf(kStartTestJS, result_length), &actual)); | 244 L"", base::StringPrintf(kStartTestJS, result_length), &actual)); |
245 ASSERT_TRUE(actual); | 245 ASSERT_TRUE(actual); |
246 } | 246 } |
247 | 247 |
248 void TestKeyEvent(int tab_index, const KeyEventTestData& test) { | 248 void TestKeyEvent(int tab_index, const KeyEventTestData& test) { |
249 ASSERT_LT(tab_index, browser()->tab_count()); | 249 ASSERT_LT(tab_index, browser()->tab_count()); |
250 ASSERT_EQ(tab_index, browser()->active_index()); | 250 ASSERT_EQ(tab_index, browser()->active_index()); |
251 | 251 |
252 // Inform our testing web page that we are about to start testing a key | 252 // Inform our testing web page that we are about to start testing a key |
253 // event. | 253 // event. |
254 ASSERT_NO_FATAL_FAILURE(StartTest(tab_index, test.result_length)); | 254 ASSERT_NO_FATAL_FAILURE(StartTest(tab_index, test.result_length)); |
255 ASSERT_NO_FATAL_FAILURE(SuppressEvents( | 255 ASSERT_NO_FATAL_FAILURE(SuppressEvents( |
256 tab_index, test.suppress_keydown, test.suppress_keypress, | 256 tab_index, test.suppress_keydown, test.suppress_keypress, |
257 test.suppress_keyup, test.suppress_textinput)); | 257 test.suppress_keyup, test.suppress_textinput)); |
258 | 258 |
259 // We need to create a finish observer before sending the key event, | 259 // We need to create a finish observer before sending the key event, |
260 // because the test finished message might be arrived before returning | 260 // because the test finished message might be arrived before returning |
261 // from the SendKeyPressSync() method. | 261 // from the SendKeyPressSync() method. |
262 TestFinishObserver finish_observer( | 262 TestFinishObserver finish_observer( |
263 browser()->GetTabContentsAt(tab_index)->render_view_host()); | 263 browser()->GetTabContentsAt(tab_index)->render_view_host()); |
264 | 264 |
265 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( | 265 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( |
266 browser(), test.key, test.ctrl, test.shift, test.alt, test.command)); | 266 browser(), test.key, test.ctrl, test.shift, test.alt, test.command)); |
267 ASSERT_TRUE(finish_observer.WaitForFinish()); | 267 ASSERT_TRUE(finish_observer.WaitForFinish()); |
268 ASSERT_NO_FATAL_FAILURE(CheckResult( | 268 ASSERT_NO_FATAL_FAILURE(CheckResult( |
269 tab_index, test.result_length, test.result)); | 269 tab_index, test.result_length, test.result)); |
270 } | 270 } |
271 | 271 |
272 std::string GetTestDataDescription(const KeyEventTestData& data) { | 272 std::string GetTestDataDescription(const KeyEventTestData& data) { |
273 std::string desc = StringPrintf( | 273 std::string desc = base::StringPrintf( |
274 " VKEY:0x%02x, ctrl:%d, shift:%d, alt:%d, command:%d\n" | 274 " VKEY:0x%02x, ctrl:%d, shift:%d, alt:%d, command:%d\n" |
275 " Suppress: keydown:%d, keypress:%d, keyup:%d, textInput:%d\n" | 275 " Suppress: keydown:%d, keypress:%d, keyup:%d, textInput:%d\n" |
276 " Expected results(%d):\n", | 276 " Expected results(%d):\n", |
277 data.key, data.ctrl, data.shift, data.alt, data.command, | 277 data.key, data.ctrl, data.shift, data.alt, data.command, |
278 data.suppress_keydown, data.suppress_keypress, data.suppress_keyup, | 278 data.suppress_keydown, data.suppress_keypress, data.suppress_keyup, |
279 data.suppress_textinput, data.result_length); | 279 data.suppress_textinput, data.result_length); |
280 for (int i = 0; i < data.result_length; ++i) { | 280 for (int i = 0; i < data.result_length; ++i) { |
281 desc.append(" "); | 281 desc.append(" "); |
282 desc.append(data.result[i]); | 282 desc.append(data.result[i]); |
283 desc.append("\n"); | 283 desc.append("\n"); |
(...skipping 565 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
849 EXPECT_NO_FATAL_FAILURE(TestKeyEvent(tab_index, kTestAltKeySuppress)); | 849 EXPECT_NO_FATAL_FAILURE(TestKeyEvent(tab_index, kTestAltKeySuppress)); |
850 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); | 850 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); |
851 | 851 |
852 // Ctrl+Alt should have no effect. | 852 // Ctrl+Alt should have no effect. |
853 EXPECT_NO_FATAL_FAILURE(TestKeyEvent(tab_index, kTestCtrlAltKey)); | 853 EXPECT_NO_FATAL_FAILURE(TestKeyEvent(tab_index, kTestCtrlAltKey)); |
854 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); | 854 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); |
855 } | 855 } |
856 #endif | 856 #endif |
857 | 857 |
858 } // namespace | 858 } // namespace |
OLD | NEW |