| 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/bind.h" |
| 7 #include "base/file_util.h" | 8 #include "base/file_util.h" |
| 8 #include "base/format_macros.h" | 9 #include "base/format_macros.h" |
| 9 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
| 10 #include "base/path_service.h" | 11 #include "base/path_service.h" |
| 11 #include "base/string_number_conversions.h" | 12 #include "base/string_number_conversions.h" |
| 12 #include "base/stringprintf.h" | 13 #include "base/stringprintf.h" |
| 13 #include "base/utf_string_conversions.h" | 14 #include "base/utf_string_conversions.h" |
| 14 #include "chrome/browser/tabs/tab_strip_model.h" | 15 #include "chrome/browser/tabs/tab_strip_model.h" |
| 15 #include "chrome/browser/ui/browser.h" | 16 #include "chrome/browser/ui/browser.h" |
| 16 #include "chrome/browser/ui/browser_window.h" | 17 #include "chrome/browser/ui/browser_window.h" |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 70 // Flaky, http://crbug.com/62537. | 71 // Flaky, http://crbug.com/62537. |
| 71 #define MAYBE_TabsRememberFocusFindInPage FLAKY_TabsRememberFocusFindInPage | 72 #define MAYBE_TabsRememberFocusFindInPage FLAKY_TabsRememberFocusFindInPage |
| 72 #endif | 73 #endif |
| 73 | 74 |
| 74 namespace { | 75 namespace { |
| 75 | 76 |
| 76 // The delay waited in some cases where we don't have a notifications for an | 77 // The delay waited in some cases where we don't have a notifications for an |
| 77 // action we take. | 78 // action we take. |
| 78 const int kActionDelayMs = 500; | 79 const int kActionDelayMs = 500; |
| 79 | 80 |
| 81 // Maxiumum time to wait until the focus is moved to expected view. |
| 82 const int kFocusChangeTimeoutMs = 500; |
| 83 |
| 80 const char kSimplePage[] = "files/focus/page_with_focus.html"; | 84 const char kSimplePage[] = "files/focus/page_with_focus.html"; |
| 81 const char kStealFocusPage[] = "files/focus/page_steals_focus.html"; | 85 const char kStealFocusPage[] = "files/focus/page_steals_focus.html"; |
| 82 const char kTypicalPage[] = "files/focus/typical_page.html"; | 86 const char kTypicalPage[] = "files/focus/typical_page.html"; |
| 83 const char kTypicalPageName[] = "typical_page.html"; | 87 const char kTypicalPageName[] = "typical_page.html"; |
| 84 | 88 |
| 85 // Test to make sure Chrome is in the foreground as we start testing. This is | 89 // Test to make sure Chrome is in the foreground as we start testing. This is |
| 86 // required for tests that synthesize input to the Chrome window. | 90 // required for tests that synthesize input to the Chrome window. |
| 87 bool ChromeInForeground() { | 91 bool ChromeInForeground() { |
| 88 #if defined(OS_WIN) | 92 #if defined(OS_WIN) |
| 89 HWND window = ::GetForegroundWindow(); | 93 HWND window = ::GetForegroundWindow(); |
| (...skipping 29 matching lines...) Expand all Loading... |
| 119 << "Process in foreground: " << filename.c_str() << "\n" | 123 << "Process in foreground: " << filename.c_str() << "\n" |
| 120 << "Window: " << window << "\n" | 124 << "Window: " << window << "\n" |
| 121 << "Caption: " << caption.c_str(); | 125 << "Caption: " << caption.c_str(); |
| 122 return chrome_window_in_foreground; | 126 return chrome_window_in_foreground; |
| 123 #else | 127 #else |
| 124 // Windows only at the moment. | 128 // Windows only at the moment. |
| 125 return true; | 129 return true; |
| 126 #endif | 130 #endif |
| 127 } | 131 } |
| 128 | 132 |
| 133 // Wait the focus change in message loop. |
| 134 void CheckFocus(Browser* browser, ViewID id, const base::Time& timeout) { |
| 135 if (ui_test_utils::IsViewFocused(browser, id) || |
| 136 base::Time::Now() > timeout) { |
| 137 MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::QuitClosure()); |
| 138 } else { |
| 139 MessageLoop::current()->PostDelayedTask( |
| 140 FROM_HERE, |
| 141 base::Bind(&CheckFocus, browser, id, timeout), |
| 142 10); |
| 143 } |
| 144 }; |
| 145 |
| 129 class BrowserFocusTest : public InProcessBrowserTest { | 146 class BrowserFocusTest : public InProcessBrowserTest { |
| 130 public: | 147 public: |
| 131 BrowserFocusTest() : | 148 BrowserFocusTest() : |
| 132 #if defined(USE_AURA) | 149 #if defined(USE_AURA) |
| 133 location_bar_focus_view_id_(VIEW_ID_OMNIBOX) | 150 location_bar_focus_view_id_(VIEW_ID_OMNIBOX) |
| 134 #else | 151 #else |
| 135 location_bar_focus_view_id_(VIEW_ID_LOCATION_BAR) | 152 location_bar_focus_view_id_(VIEW_ID_LOCATION_BAR) |
| 136 #endif | 153 #endif |
| 137 { | 154 { |
| 138 set_show_window(true); | 155 set_show_window(true); |
| 139 EnableDOMAutomation(); | 156 EnableDOMAutomation(); |
| 140 } | 157 } |
| 141 | 158 |
| 142 bool IsViewFocused(ViewID vid) { | 159 bool IsViewFocused(ViewID vid) { |
| 143 return ui_test_utils::IsViewFocused(browser(), vid); | 160 return ui_test_utils::IsViewFocused(browser(), vid); |
| 144 } | 161 } |
| 145 | 162 |
| 146 void ClickOnView(ViewID vid) { | 163 void ClickOnView(ViewID vid) { |
| 147 ui_test_utils::ClickOnView(browser(), vid); | 164 ui_test_utils::ClickOnView(browser(), vid); |
| 148 } | 165 } |
| 149 | 166 |
| 167 bool WaitForFocusChange(ViewID vid) { |
| 168 const base::Time timeout = base::Time::Now() + |
| 169 base::TimeDelta::FromMilliseconds(kFocusChangeTimeoutMs); |
| 170 MessageLoop::current()->PostDelayedTask( |
| 171 FROM_HERE, |
| 172 base::Bind(&CheckFocus, browser(), vid, timeout), |
| 173 100); |
| 174 ui_test_utils::RunMessageLoop(); |
| 175 return IsViewFocused(vid); |
| 176 } |
| 177 |
| 150 ViewID location_bar_focus_view_id_; | 178 ViewID location_bar_focus_view_id_; |
| 151 }; | 179 }; |
| 152 | 180 |
| 153 class TestInterstitialPage : public InterstitialPage { | 181 class TestInterstitialPage : public InterstitialPage { |
| 154 public: | 182 public: |
| 155 TestInterstitialPage(WebContents* tab, bool new_navigation, const GURL& url) | 183 TestInterstitialPage(WebContents* tab, bool new_navigation, const GURL& url) |
| 156 : InterstitialPage(tab, new_navigation, url) { | 184 : InterstitialPage(tab, new_navigation, url) { |
| 157 FilePath file_path; | 185 FilePath file_path; |
| 158 bool r = PathService::Get(chrome::DIR_TEST_DATA, &file_path); | 186 bool r = PathService::Get(chrome::DIR_TEST_DATA, &file_path); |
| 159 EXPECT_TRUE(r); | 187 EXPECT_TRUE(r); |
| (...skipping 593 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 753 #if defined(OS_MACOSX) | 781 #if defined(OS_MACOSX) |
| 754 // Press Cmd+F, which will make the Find box open and request focus. | 782 // Press Cmd+F, which will make the Find box open and request focus. |
| 755 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( | 783 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( |
| 756 browser(), ui::VKEY_F, false, false, false, true)); | 784 browser(), ui::VKEY_F, false, false, false, true)); |
| 757 #else | 785 #else |
| 758 // Press Ctrl+F, which will make the Find box open and request focus. | 786 // Press Ctrl+F, which will make the Find box open and request focus. |
| 759 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( | 787 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( |
| 760 browser(), ui::VKEY_F, true, false, false, false)); | 788 browser(), ui::VKEY_F, true, false, false, false)); |
| 761 #endif | 789 #endif |
| 762 | 790 |
| 763 // Ideally, we wouldn't sleep here and instead would intercept the | 791 ASSERT_TRUE(WaitForFocusChange(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); |
| 764 // RenderViewHostDelegate::HandleKeyboardEvent() callback. To do that, we | |
| 765 // could create a RenderViewHostDelegate wrapper and hook-it up by either: | |
| 766 // - creating a factory used to create the delegate | |
| 767 // - making the test a private and overwriting the delegate member directly. | |
| 768 MessageLoop::current()->PostDelayedTask( | |
| 769 FROM_HERE, MessageLoop::QuitClosure(), kActionDelayMs); | |
| 770 ui_test_utils::RunMessageLoop(); | |
| 771 | |
| 772 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); | |
| 773 | 792 |
| 774 browser()->FocusLocationBar(); | 793 browser()->FocusLocationBar(); |
| 775 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_)); | 794 ASSERT_TRUE(IsViewFocused(location_bar_focus_view_id_)); |
| 776 | 795 |
| 777 // Now press Ctrl+F again and focus should move to the Find box. | 796 // Now press Ctrl+F again and focus should move to the Find box. |
| 778 #if defined(OS_MACOSX) | 797 #if defined(OS_MACOSX) |
| 779 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( | 798 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( |
| 780 browser(), ui::VKEY_F, false, false, false, true)); | 799 browser(), ui::VKEY_F, false, false, false, true)); |
| 781 #else | 800 #else |
| 782 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( | 801 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( |
| 783 browser(), ui::VKEY_F, true, false, false, false)); | 802 browser(), ui::VKEY_F, true, false, false, false)); |
| 784 #endif | 803 #endif |
| 785 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); | 804 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); |
| 786 | 805 |
| 787 // Set focus to the page. | 806 // Set focus to the page. |
| 788 ClickOnView(VIEW_ID_TAB_CONTAINER); | 807 ClickOnView(VIEW_ID_TAB_CONTAINER); |
| 789 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); | 808 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); |
| 790 | 809 |
| 791 // Now press Ctrl+F again and focus should move to the Find box. | 810 // Now press Ctrl+F again and focus should move to the Find box. |
| 792 #if defined(OS_MACOSX) | 811 #if defined(OS_MACOSX) |
| 793 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( | 812 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( |
| 794 browser(), ui::VKEY_F, false, false, false, true)); | 813 browser(), ui::VKEY_F, false, false, false, true)); |
| 795 #else | 814 #else |
| 796 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( | 815 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( |
| 797 browser(), ui::VKEY_F, true, false, false, false)); | 816 browser(), ui::VKEY_F, true, false, false, false)); |
| 798 #endif | 817 #endif |
| 799 | 818 |
| 800 // See remark above on why we wait. | 819 ASSERT_TRUE(WaitForFocusChange(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); |
| 801 MessageLoop::current()->PostDelayedTask( | |
| 802 FROM_HERE, MessageLoop::QuitClosure(), kActionDelayMs); | |
| 803 ui_test_utils::RunMessageLoop(); | |
| 804 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); | |
| 805 } | 820 } |
| 806 | 821 |
| 807 // Makes sure the focus is in the right location when opening the different | 822 // Makes sure the focus is in the right location when opening the different |
| 808 // types of tabs. | 823 // types of tabs. |
| 809 // Flaky, http://crbug.com/62539. | 824 // Flaky, http://crbug.com/62539. |
| 810 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FLAKY_TabInitialFocus) { | 825 IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FLAKY_TabInitialFocus) { |
| 811 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); | 826 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); |
| 812 | 827 |
| 813 // Open the history tab, focus should be on the tab contents. | 828 // Open the history tab, focus should be on the tab contents. |
| 814 browser()->ShowHistoryTab(); | 829 browser()->ShowHistoryTab(); |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 899 browser()->Reload(CURRENT_TAB); | 914 browser()->Reload(CURRENT_TAB); |
| 900 observer.Wait(); | 915 observer.Wait(); |
| 901 } | 916 } |
| 902 | 917 |
| 903 // Focus should now be on the tab contents. | 918 // Focus should now be on the tab contents. |
| 904 browser()->ShowDownloadsTab(); | 919 browser()->ShowDownloadsTab(); |
| 905 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); | 920 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); |
| 906 } | 921 } |
| 907 | 922 |
| 908 } // namespace | 923 } // namespace |
| OLD | NEW |