| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 "base/message_loop.h" | 5 #include "base/message_loop.h" |
| 6 #include "base/ref_counted.h" | 6 #include "base/ref_counted.h" |
| 7 #include "chrome/browser/automation/ui_controls.h" | 7 #include "chrome/browser/automation/ui_controls.h" |
| 8 #include "chrome/browser/browser.h" | 8 #include "chrome/browser/browser.h" |
| 9 #include "chrome/browser/dom_operation_notification_details.h" | |
| 10 #include "chrome/browser/tab_contents/web_contents.h" | 9 #include "chrome/browser/tab_contents/web_contents.h" |
| 11 #include "chrome/browser/view_ids.h" | 10 #include "chrome/browser/view_ids.h" |
| 12 #include "chrome/browser/views/frame/browser_view.h" | 11 #include "chrome/browser/views/frame/browser_view.h" |
| 13 #include "chrome/browser/views/location_bar_view.h" | 12 #include "chrome/browser/views/location_bar_view.h" |
| 14 #include "chrome/common/notification_details.h" | |
| 15 #include "chrome/common/notification_observer.h" | |
| 16 #include "chrome/common/notification_service.h" | |
| 17 #include "chrome/common/notification_source.h" | |
| 18 #include "chrome/common/notification_type.h" | |
| 19 #include "chrome/views/focus/focus_manager.h" | 13 #include "chrome/views/focus/focus_manager.h" |
| 20 #include "chrome/views/view.h" | 14 #include "chrome/views/view.h" |
| 21 #include "chrome/views/window/window.h" | 15 #include "chrome/views/window/window.h" |
| 22 #include "chrome/test/in_process_browser_test.h" | 16 #include "chrome/test/in_process_browser_test.h" |
| 23 #include "chrome/test/ui_test_utils.h" | 17 #include "chrome/test/ui_test_utils.h" |
| 24 | 18 |
| 25 namespace { | 19 namespace { |
| 26 | 20 |
| 27 // The delay waited in some cases where we don't have a notifications for an | 21 // The delay waited in some cases where we don't have a notifications for an |
| 28 // action we take. | 22 // action we take. |
| 29 const int kActionDelayMs = 500; | 23 const int kActionDelayMs = 500; |
| 30 | 24 |
| 31 const wchar_t kSimplePage[] = L"files/focus/page_with_focus.html"; | 25 const wchar_t kSimplePage[] = L"files/focus/page_with_focus.html"; |
| 32 const wchar_t kStealFocusPage[] = L"files/focus/page_steals_focus.html"; | 26 const wchar_t kStealFocusPage[] = L"files/focus/page_steals_focus.html"; |
| 33 const wchar_t kTypicalPage[] = L"files/focus/typical_page.html"; | 27 const wchar_t kTypicalPage[] = L"files/focus/typical_page.html"; |
| 34 | 28 |
| 35 class BrowserFocusTest : public InProcessBrowserTest { | 29 class BrowserFocusTest : public InProcessBrowserTest { |
| 36 public: | 30 public: |
| 37 BrowserFocusTest() { | 31 BrowserFocusTest() { |
| 38 set_show_window(true); | 32 set_show_window(true); |
| 39 EnableDOMAutomation(); | 33 EnableDOMAutomation(); |
| 40 } | 34 } |
| 41 }; | 35 }; |
| 42 | 36 |
| 43 class JavaScriptRunner : public NotificationObserver { | |
| 44 public: | |
| 45 JavaScriptRunner(WebContents* web_contents, | |
| 46 const std::wstring& frame_xpath, | |
| 47 const std::wstring& jscript) | |
| 48 : web_contents_(web_contents), | |
| 49 frame_xpath_(frame_xpath), | |
| 50 jscript_(jscript) { | |
| 51 NotificationService::current()-> | |
| 52 AddObserver(this, NotificationType::DOM_OPERATION_RESPONSE, | |
| 53 Source<WebContents>(web_contents)); | |
| 54 } | |
| 55 | |
| 56 virtual void Observe(NotificationType type, | |
| 57 const NotificationSource& source, | |
| 58 const NotificationDetails& details) { | |
| 59 Details<DomOperationNotificationDetails> dom_op_details(details); | |
| 60 result_ = dom_op_details->json(); | |
| 61 // The Jasonified response has quotes, remove them. | |
| 62 if (result_.length() > 1 && result_[0] == '"') | |
| 63 result_ = result_.substr(1, result_.length() - 2); | |
| 64 | |
| 65 NotificationService::current()-> | |
| 66 RemoveObserver(this, NotificationType::DOM_OPERATION_RESPONSE, | |
| 67 Source<WebContents>(web_contents_)); | |
| 68 MessageLoop::current()->PostTask(FROM_HERE, new MessageLoop::QuitTask()); | |
| 69 } | |
| 70 | |
| 71 std::string Run() { | |
| 72 // The DOMAutomationController requires an automation ID, eventhough we are | |
| 73 // not using it in this case. | |
| 74 web_contents_->render_view_host()->ExecuteJavascriptInWebFrame( | |
| 75 frame_xpath_, L"window.domAutomationController.setAutomationId(0);"); | |
| 76 | |
| 77 web_contents_->render_view_host()->ExecuteJavascriptInWebFrame(frame_xpath_, | |
| 78 jscript_); | |
| 79 ui_test_utils::RunMessageLoop(); | |
| 80 return result_; | |
| 81 } | |
| 82 | |
| 83 private: | |
| 84 WebContents* web_contents_; | |
| 85 std::wstring frame_xpath_; | |
| 86 std::wstring jscript_; | |
| 87 std::string result_; | |
| 88 | |
| 89 DISALLOW_COPY_AND_ASSIGN(JavaScriptRunner); | |
| 90 }; | |
| 91 | |
| 92 } // namespace | 37 } // namespace |
| 93 | 38 |
| 94 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_BrowsersRememberFocus) { | 39 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_BrowsersRememberFocus) { |
| 95 HTTPTestServer* server = StartHTTPServer(); | 40 HTTPTestServer* server = StartHTTPServer(); |
| 96 | 41 |
| 97 // First we navigate to our test page. | 42 // First we navigate to our test page. |
| 98 GURL url = server->TestServerPageW(kSimplePage); | 43 GURL url = server->TestServerPageW(kSimplePage); |
| 99 ui_test_utils::NavigateToURL(browser(), url); | 44 ui_test_utils::NavigateToURL(browser(), url); |
| 100 | 45 |
| 101 // The focus should be on the Tab contents. | 46 // The focus should be on the Tab contents. |
| (...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 312 }; | 257 }; |
| 313 | 258 |
| 314 // Test forward focus traversal. | 259 // Test forward focus traversal. |
| 315 for (int i = 0; i < 3; ++i) { | 260 for (int i = 0; i < 3; ++i) { |
| 316 // Location bar should be focused. | 261 // Location bar should be focused. |
| 317 EXPECT_EQ(location_bar, focus_manager->GetFocusedView()); | 262 EXPECT_EQ(location_bar, focus_manager->GetFocusedView()); |
| 318 | 263 |
| 319 // Now let's press tab to move the focus. | 264 // Now let's press tab to move the focus. |
| 320 for (int j = 0; j < 7; ++j) { | 265 for (int j = 0; j < 7; ++j) { |
| 321 // Let's make sure the focus is on the expected element in the page. | 266 // Let's make sure the focus is on the expected element in the page. |
| 322 JavaScriptRunner js_runner( | 267 ui_test_utils::JavaScriptRunner js_runner( |
| 323 browser()->GetSelectedTabContents()->AsWebContents(), | 268 browser()->GetSelectedTabContents()->AsWebContents(), |
| 324 L"", | 269 L"", |
| 325 L"window.domAutomationController.send(getFocusedElement());"); | 270 L"window.domAutomationController.send(getFocusedElement());"); |
| 326 std::string actual = js_runner.Run(); | 271 std::string actual = js_runner.Run(); |
| 327 ASSERT_STREQ(kExpElementIDs[j], actual.c_str()); | 272 ASSERT_STREQ(kExpElementIDs[j], actual.c_str()); |
| 328 | 273 |
| 329 ui_controls::SendKeyPressNotifyWhenDone(L'\t', false, false, false, | 274 ui_controls::SendKeyPressNotifyWhenDone(L'\t', false, false, false, |
| 330 new MessageLoop::QuitTask()); | 275 new MessageLoop::QuitTask()); |
| 331 ui_test_utils::RunMessageLoop(); | 276 ui_test_utils::RunMessageLoop(); |
| 332 // Ideally, we wouldn't sleep here and instead would use the event | 277 // Ideally, we wouldn't sleep here and instead would use the event |
| 333 // processed ack nofification from the renderer. I am reluctant to create | 278 // processed ack notification from the renderer. I am reluctant to create |
| 334 // a new notification/callback for that purpose just for this test. | 279 // a new notification/callback for that purpose just for this test. |
| 335 ::Sleep(kActionDelayMs); | 280 ::Sleep(kActionDelayMs); |
| 336 } | 281 } |
| 337 | 282 |
| 338 // At this point the renderer has sent us a message asking to advance the | 283 // At this point the renderer has sent us a message asking to advance the |
| 339 // focus (as the end of the focus loop was reached in the renderer). | 284 // focus (as the end of the focus loop was reached in the renderer). |
| 340 // We need to run the message loop to process it. | 285 // We need to run the message loop to process it. |
| 341 MessageLoop::current()->PostTask(FROM_HERE, new MessageLoop::QuitTask()); | 286 MessageLoop::current()->PostTask(FROM_HERE, new MessageLoop::QuitTask()); |
| 342 ui_test_utils::RunMessageLoop(); | 287 ui_test_utils::RunMessageLoop(); |
| 343 } | 288 } |
| 344 | 289 |
| 345 // Now let's try reverse focus traversal. | 290 // Now let's try reverse focus traversal. |
| 346 for (int i = 0; i < 3; ++i) { | 291 for (int i = 0; i < 3; ++i) { |
| 347 // Location bar should be focused. | 292 // Location bar should be focused. |
| 348 EXPECT_EQ(location_bar, focus_manager->GetFocusedView()); | 293 EXPECT_EQ(location_bar, focus_manager->GetFocusedView()); |
| 349 | 294 |
| 350 // Now let's press shift-tab to move the focus in reverse. | 295 // Now let's press shift-tab to move the focus in reverse. |
| 351 for (int j = 0; j < 7; ++j) { | 296 for (int j = 0; j < 7; ++j) { |
| 352 ui_controls::SendKeyPressNotifyWhenDone(L'\t', false, true, false, | 297 ui_controls::SendKeyPressNotifyWhenDone(L'\t', false, true, false, |
| 353 new MessageLoop::QuitTask()); | 298 new MessageLoop::QuitTask()); |
| 354 ui_test_utils::RunMessageLoop(); | 299 ui_test_utils::RunMessageLoop(); |
| 355 ::Sleep(kActionDelayMs); | 300 ::Sleep(kActionDelayMs); |
| 356 | 301 |
| 357 // Let's make sure the focus is on the expected element in the page. | 302 // Let's make sure the focus is on the expected element in the page. |
| 358 JavaScriptRunner js_runner( | 303 ui_test_utils::JavaScriptRunner js_runner( |
| 359 browser()->GetSelectedTabContents()->AsWebContents(), | 304 browser()->GetSelectedTabContents()->AsWebContents(), |
| 360 L"", | 305 L"", |
| 361 L"window.domAutomationController.send(getFocusedElement());"); | 306 L"window.domAutomationController.send(getFocusedElement());"); |
| 362 std::string actual = js_runner.Run(); | 307 std::string actual = js_runner.Run(); |
| 363 ASSERT_STREQ(kExpElementIDs[6 - j], actual.c_str()); | 308 ASSERT_STREQ(kExpElementIDs[6 - j], actual.c_str()); |
| 364 } | 309 } |
| 365 | 310 |
| 366 // At this point the renderer has sent us a message asking to advance the | 311 // At this point the renderer has sent us a message asking to advance the |
| 367 // focus (as the end of the focus loop was reached in the renderer). | 312 // focus (as the end of the focus loop was reached in the renderer). |
| 368 // We need to run the message loop to process it. | 313 // We need to run the message loop to process it. |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 461 | 406 |
| 462 // Open the new tab, focus should be on the location bar. | 407 // Open the new tab, focus should be on the location bar. |
| 463 browser()->NewTab(); | 408 browser()->NewTab(); |
| 464 EXPECT_EQ(browser_view->GetLocationBarView(), | 409 EXPECT_EQ(browser_view->GetLocationBarView(), |
| 465 focus_manager->GetFocusedView()); | 410 focus_manager->GetFocusedView()); |
| 466 | 411 |
| 467 // Open the download tab, focus should be on the tab contents. | 412 // Open the download tab, focus should be on the tab contents. |
| 468 browser()->ShowDownloadsTab(); | 413 browser()->ShowDownloadsTab(); |
| 469 EXPECT_EQ(browser_view->GetContentsView(), focus_manager->GetFocusedView()); | 414 EXPECT_EQ(browser_view->GetContentsView(), focus_manager->GetFocusedView()); |
| 470 } | 415 } |
| OLD | NEW |