| 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" |
| 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/ui/browser.h" | 13 #include "chrome/browser/ui/browser.h" |
| 14 #include "chrome/browser/ui/browser_tabstrip.h" | 14 #include "chrome/browser/ui/browser_tabstrip.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/public/browser/dom_operation_notification_details.h" | 19 #include "content/public/browser/dom_operation_notification_details.h" |
| 20 #include "content/public/browser/notification_registrar.h" | 20 #include "content/public/browser/notification_registrar.h" |
| 21 #include "content/public/browser/notification_service.h" | 21 #include "content/public/browser/notification_service.h" |
| 22 #include "content/public/browser/render_view_host.h" | 22 #include "content/public/browser/render_view_host.h" |
| 23 #include "content/public/browser/render_widget_host_view.h" | 23 #include "content/public/browser/render_widget_host_view.h" |
| 24 #include "content/public/browser/web_contents.h" | 24 #include "content/public/browser/web_contents.h" |
| 25 #include "content/public/browser/web_contents_view.h" | 25 #include "content/public/browser/web_contents_view.h" |
| 26 #include "content/public/test/browser_test_utils.h" |
| 26 #include "net/test/test_server.h" | 27 #include "net/test/test_server.h" |
| 27 #include "ui/base/keycodes/keyboard_codes.h" | 28 #include "ui/base/keycodes/keyboard_codes.h" |
| 28 | 29 |
| 29 using content::DomOperationNotificationDetails; | 30 using content::DomOperationNotificationDetails; |
| 30 using content::NavigationController; | 31 using content::NavigationController; |
| 31 using content::RenderViewHost; | 32 using content::RenderViewHost; |
| 32 | 33 |
| 33 namespace { | 34 namespace { |
| 34 | 35 |
| 35 const char kTestingPage[] = "files/keyevents_test.html"; | 36 const char kTestingPage[] = "files/keyevents_test.html"; |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 137 void ClickOnView(ViewID vid) { | 138 void ClickOnView(ViewID vid) { |
| 138 ui_test_utils::ClickOnView(browser(), vid); | 139 ui_test_utils::ClickOnView(browser(), vid); |
| 139 } | 140 } |
| 140 | 141 |
| 141 // Set the suppress flag of an event specified by |type|. If |suppress| is | 142 // Set the suppress flag of an event specified by |type|. If |suppress| is |
| 142 // true then the web page will suppress all events with |type|. Following | 143 // true then the web page will suppress all events with |type|. Following |
| 143 // event types are supported: keydown, keypress, keyup and textInput. | 144 // event types are supported: keydown, keypress, keyup and textInput. |
| 144 void SuppressEventByType(int tab_index, const wchar_t* type, bool suppress) { | 145 void SuppressEventByType(int tab_index, const wchar_t* type, bool suppress) { |
| 145 ASSERT_LT(tab_index, browser()->tab_count()); | 146 ASSERT_LT(tab_index, browser()->tab_count()); |
| 146 bool actual; | 147 bool actual; |
| 147 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool( | 148 ASSERT_TRUE(content::ExecuteJavaScriptAndExtractBool( |
| 148 chrome::GetWebContentsAt(browser(), tab_index)->GetRenderViewHost(), | 149 chrome::GetWebContentsAt(browser(), tab_index)->GetRenderViewHost(), |
| 149 L"", | 150 L"", |
| 150 base::StringPrintf(kSuppressEventJS, type, GetBoolString(!suppress)), | 151 base::StringPrintf(kSuppressEventJS, type, GetBoolString(!suppress)), |
| 151 &actual)); | 152 &actual)); |
| 152 ASSERT_EQ(!suppress, actual); | 153 ASSERT_EQ(!suppress, actual); |
| 153 } | 154 } |
| 154 | 155 |
| 155 void SuppressEvents(int tab_index, bool keydown, bool keypress, | 156 void SuppressEvents(int tab_index, bool keydown, bool keypress, |
| 156 bool keyup, bool textinput) { | 157 bool keyup, bool textinput) { |
| 157 ASSERT_NO_FATAL_FAILURE( | 158 ASSERT_NO_FATAL_FAILURE( |
| 158 SuppressEventByType(tab_index, L"keydown", keydown)); | 159 SuppressEventByType(tab_index, L"keydown", keydown)); |
| 159 ASSERT_NO_FATAL_FAILURE( | 160 ASSERT_NO_FATAL_FAILURE( |
| 160 SuppressEventByType(tab_index, L"keypress", keypress)); | 161 SuppressEventByType(tab_index, L"keypress", keypress)); |
| 161 ASSERT_NO_FATAL_FAILURE( | 162 ASSERT_NO_FATAL_FAILURE( |
| 162 SuppressEventByType(tab_index, L"keyup", keyup)); | 163 SuppressEventByType(tab_index, L"keyup", keyup)); |
| 163 ASSERT_NO_FATAL_FAILURE( | 164 ASSERT_NO_FATAL_FAILURE( |
| 164 SuppressEventByType(tab_index, L"textInput", textinput)); | 165 SuppressEventByType(tab_index, L"textInput", textinput)); |
| 165 } | 166 } |
| 166 | 167 |
| 167 void SuppressAllEvents(int tab_index, bool suppress) { | 168 void SuppressAllEvents(int tab_index, bool suppress) { |
| 168 SuppressEvents(tab_index, suppress, suppress, suppress, suppress); | 169 SuppressEvents(tab_index, suppress, suppress, suppress, suppress); |
| 169 } | 170 } |
| 170 | 171 |
| 171 void GetResultLength(int tab_index, int* length) { | 172 void GetResultLength(int tab_index, int* length) { |
| 172 ASSERT_LT(tab_index, browser()->tab_count()); | 173 ASSERT_LT(tab_index, browser()->tab_count()); |
| 173 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractInt( | 174 ASSERT_TRUE(content::ExecuteJavaScriptAndExtractInt( |
| 174 chrome::GetWebContentsAt(browser(), tab_index)->GetRenderViewHost(), | 175 chrome::GetWebContentsAt(browser(), tab_index)->GetRenderViewHost(), |
| 175 L"", kGetResultLengthJS, length)); | 176 L"", kGetResultLengthJS, length)); |
| 176 } | 177 } |
| 177 | 178 |
| 178 void CheckResult(int tab_index, int length, const char* const result[]) { | 179 void CheckResult(int tab_index, int length, const char* const result[]) { |
| 179 ASSERT_LT(tab_index, browser()->tab_count()); | 180 ASSERT_LT(tab_index, browser()->tab_count()); |
| 180 int actual_length; | 181 int actual_length; |
| 181 ASSERT_NO_FATAL_FAILURE(GetResultLength(tab_index, &actual_length)); | 182 ASSERT_NO_FATAL_FAILURE(GetResultLength(tab_index, &actual_length)); |
| 182 ASSERT_GE(actual_length, length); | 183 ASSERT_GE(actual_length, length); |
| 183 for (int i = 0; i < actual_length; ++i) { | 184 for (int i = 0; i < actual_length; ++i) { |
| 184 std::string actual; | 185 std::string actual; |
| 185 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString( | 186 ASSERT_TRUE(content::ExecuteJavaScriptAndExtractString( |
| 186 chrome::GetWebContentsAt(browser(), tab_index)->GetRenderViewHost(), | 187 chrome::GetWebContentsAt(browser(), tab_index)->GetRenderViewHost(), |
| 187 L"", base::StringPrintf(kGetResultJS, i), &actual)); | 188 L"", base::StringPrintf(kGetResultJS, i), &actual)); |
| 188 | 189 |
| 189 // If more events were received than expected, then the additional events | 190 // If more events were received than expected, then the additional events |
| 190 // must be keyup events. | 191 // must be keyup events. |
| 191 if (i < length) | 192 if (i < length) |
| 192 ASSERT_STREQ(result[i], actual.c_str()); | 193 ASSERT_STREQ(result[i], actual.c_str()); |
| 193 else | 194 else |
| 194 ASSERT_EQ('U', actual[0]); | 195 ASSERT_EQ('U', actual[0]); |
| 195 } | 196 } |
| 196 } | 197 } |
| 197 | 198 |
| 198 void CheckFocusedElement(int tab_index, const wchar_t* focused) { | 199 void CheckFocusedElement(int tab_index, const wchar_t* focused) { |
| 199 ASSERT_LT(tab_index, browser()->tab_count()); | 200 ASSERT_LT(tab_index, browser()->tab_count()); |
| 200 std::string actual; | 201 std::string actual; |
| 201 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString( | 202 ASSERT_TRUE(content::ExecuteJavaScriptAndExtractString( |
| 202 chrome::GetWebContentsAt(browser(), tab_index)->GetRenderViewHost(), | 203 chrome::GetWebContentsAt(browser(), tab_index)->GetRenderViewHost(), |
| 203 L"", kGetFocusedElementJS, &actual)); | 204 L"", kGetFocusedElementJS, &actual)); |
| 204 ASSERT_EQ(WideToUTF8(focused), actual); | 205 ASSERT_EQ(WideToUTF8(focused), actual); |
| 205 } | 206 } |
| 206 | 207 |
| 207 void SetFocusedElement(int tab_index, const wchar_t* focused) { | 208 void SetFocusedElement(int tab_index, const wchar_t* focused) { |
| 208 ASSERT_LT(tab_index, browser()->tab_count()); | 209 ASSERT_LT(tab_index, browser()->tab_count()); |
| 209 bool actual; | 210 bool actual; |
| 210 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool( | 211 ASSERT_TRUE(content::ExecuteJavaScriptAndExtractBool( |
| 211 chrome::GetWebContentsAt(browser(), tab_index)->GetRenderViewHost(), | 212 chrome::GetWebContentsAt(browser(), tab_index)->GetRenderViewHost(), |
| 212 L"", | 213 L"", |
| 213 base::StringPrintf(kSetFocusedElementJS, focused), | 214 base::StringPrintf(kSetFocusedElementJS, focused), |
| 214 &actual)); | 215 &actual)); |
| 215 ASSERT_TRUE(actual); | 216 ASSERT_TRUE(actual); |
| 216 } | 217 } |
| 217 | 218 |
| 218 void CheckTextBoxValue(int tab_index, const wchar_t* id, | 219 void CheckTextBoxValue(int tab_index, const wchar_t* id, |
| 219 const wchar_t* value) { | 220 const wchar_t* value) { |
| 220 ASSERT_LT(tab_index, browser()->tab_count()); | 221 ASSERT_LT(tab_index, browser()->tab_count()); |
| 221 std::string actual; | 222 std::string actual; |
| 222 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString( | 223 ASSERT_TRUE(content::ExecuteJavaScriptAndExtractString( |
| 223 chrome::GetWebContentsAt(browser(), tab_index)->GetRenderViewHost(), | 224 chrome::GetWebContentsAt(browser(), tab_index)->GetRenderViewHost(), |
| 224 L"", | 225 L"", |
| 225 base::StringPrintf(kGetTextBoxValueJS, id), | 226 base::StringPrintf(kGetTextBoxValueJS, id), |
| 226 &actual)); | 227 &actual)); |
| 227 ASSERT_EQ(WideToUTF8(value), actual); | 228 ASSERT_EQ(WideToUTF8(value), actual); |
| 228 } | 229 } |
| 229 | 230 |
| 230 void SetTextBoxValue(int tab_index, const wchar_t* id, | 231 void SetTextBoxValue(int tab_index, const wchar_t* id, |
| 231 const wchar_t* value) { | 232 const wchar_t* value) { |
| 232 ASSERT_LT(tab_index, browser()->tab_count()); | 233 ASSERT_LT(tab_index, browser()->tab_count()); |
| 233 std::string actual; | 234 std::string actual; |
| 234 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString( | 235 ASSERT_TRUE(content::ExecuteJavaScriptAndExtractString( |
| 235 chrome::GetWebContentsAt(browser(), tab_index)->GetRenderViewHost(), | 236 chrome::GetWebContentsAt(browser(), tab_index)->GetRenderViewHost(), |
| 236 L"", | 237 L"", |
| 237 base::StringPrintf(kSetTextBoxValueJS, id, value), | 238 base::StringPrintf(kSetTextBoxValueJS, id, value), |
| 238 &actual)); | 239 &actual)); |
| 239 ASSERT_EQ(WideToUTF8(value), actual); | 240 ASSERT_EQ(WideToUTF8(value), actual); |
| 240 } | 241 } |
| 241 | 242 |
| 242 void StartTest(int tab_index, int result_length) { | 243 void StartTest(int tab_index, int result_length) { |
| 243 ASSERT_LT(tab_index, browser()->tab_count()); | 244 ASSERT_LT(tab_index, browser()->tab_count()); |
| 244 bool actual; | 245 bool actual; |
| 245 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool( | 246 ASSERT_TRUE(content::ExecuteJavaScriptAndExtractBool( |
| 246 chrome::GetWebContentsAt(browser(), tab_index)->GetRenderViewHost(), | 247 chrome::GetWebContentsAt(browser(), tab_index)->GetRenderViewHost(), |
| 247 L"", base::StringPrintf(kStartTestJS, result_length), &actual)); | 248 L"", base::StringPrintf(kStartTestJS, result_length), &actual)); |
| 248 ASSERT_TRUE(actual); | 249 ASSERT_TRUE(actual); |
| 249 } | 250 } |
| 250 | 251 |
| 251 void TestKeyEvent(int tab_index, const KeyEventTestData& test) { | 252 void TestKeyEvent(int tab_index, const KeyEventTestData& test) { |
| 252 ASSERT_LT(tab_index, browser()->tab_count()); | 253 ASSERT_LT(tab_index, browser()->tab_count()); |
| 253 ASSERT_EQ(tab_index, browser()->active_index()); | 254 ASSERT_EQ(tab_index, browser()->active_index()); |
| 254 | 255 |
| 255 // Inform our testing web page that we are about to start testing a key | 256 // Inform our testing web page that we are about to start testing a key |
| (...skipping 625 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 881 EXPECT_NO_FATAL_FAILURE(TestKeyEvent(tab_index, kTestAltKeySuppress)); | 882 EXPECT_NO_FATAL_FAILURE(TestKeyEvent(tab_index, kTestAltKeySuppress)); |
| 882 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER)); | 883 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER)); |
| 883 | 884 |
| 884 // Ctrl+Alt should have no effect. | 885 // Ctrl+Alt should have no effect. |
| 885 EXPECT_NO_FATAL_FAILURE(TestKeyEvent(tab_index, kTestCtrlAltKey)); | 886 EXPECT_NO_FATAL_FAILURE(TestKeyEvent(tab_index, kTestCtrlAltKey)); |
| 886 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER)); | 887 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER)); |
| 887 } | 888 } |
| 888 #endif | 889 #endif |
| 889 | 890 |
| 890 } // namespace | 891 } // namespace |
| OLD | NEW |