[email protected] | 265ccd9 | 2010-04-29 17:57:17 | [diff] [blame] | 1 | // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
[email protected] | d9fde8d | 2009-10-08 19:59:30 | [diff] [blame] | 5 | #include "build/build_config.h" |
| 6 | |
[email protected] | 3985ba8 | 2010-07-29 21:44:12 | [diff] [blame] | 7 | #include "base/file_util.h" |
[email protected] | f07d7bf | 2010-04-06 08:02:42 | [diff] [blame] | 8 | #include "base/format_macros.h" |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 9 | #include "base/message_loop.h" |
[email protected] | 3985ba8 | 2010-07-29 21:44:12 | [diff] [blame] | 10 | #include "base/path_service.h" |
[email protected] | 34877b3 | 2010-07-31 17:47:05 | [diff] [blame] | 11 | #include "base/string_util.h" |
[email protected] | be1ce6a7 | 2010-08-03 14:35:22 | [diff] [blame] | 12 | #include "base/utf_string_conversions.h" |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 13 | #include "chrome/browser/browser.h" |
[email protected] | 134c47b9 | 2009-08-19 03:33:44 | [diff] [blame] | 14 | #include "chrome/browser/browser_window.h" |
| 15 | #include "chrome/browser/renderer_host/render_view_host.h" |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 16 | #include "chrome/browser/renderer_host/render_widget_host_view.h" |
| 17 | #include "chrome/browser/tab_contents/interstitial_page.h" |
[email protected] | 186f13f | 2009-08-19 20:34:00 | [diff] [blame] | 18 | #include "chrome/browser/tab_contents/tab_contents.h" |
| 19 | #include "chrome/browser/tab_contents/tab_contents_view.h" |
[email protected] | c848d3d9 | 2010-09-16 21:57:45 | [diff] [blame] | 20 | #include "chrome/browser/tabs/tab_strip_model.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 21 | #include "chrome/browser/view_ids.h" |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 22 | #include "chrome/common/chrome_paths.h" |
[email protected] | cecc93a | 2010-10-05 15:58:55 | [diff] [blame] | 23 | #include "chrome/common/url_constants.h" |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 24 | #include "chrome/test/in_process_browser_test.h" |
| 25 | #include "chrome/test/ui_test_utils.h" |
[email protected] | 3985ba8 | 2010-07-29 21:44:12 | [diff] [blame] | 26 | #include "net/test/test_server.h" |
[email protected] | 853300a8 | 2010-07-27 21:17:57 | [diff] [blame] | 27 | |
| 28 | #if defined(TOOLKIT_VIEWS) || defined(OS_WIN) |
[email protected] | 2362e4f | 2009-05-08 00:34:05 | [diff] [blame] | 29 | #include "views/focus/focus_manager.h" |
| 30 | #include "views/view.h" |
| 31 | #include "views/window/window.h" |
[email protected] | 853300a8 | 2010-07-27 21:17:57 | [diff] [blame] | 32 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 33 | |
[email protected] | 134c47b9 | 2009-08-19 03:33:44 | [diff] [blame] | 34 | #if defined(TOOLKIT_VIEWS) |
| 35 | #include "chrome/browser/views/frame/browser_view.h" |
[email protected] | 265ccd9 | 2010-04-29 17:57:17 | [diff] [blame] | 36 | #include "chrome/browser/views/location_bar/location_bar_view.h" |
[email protected] | 134c47b9 | 2009-08-19 03:33:44 | [diff] [blame] | 37 | #include "chrome/browser/views/tab_contents/tab_contents_container.h" |
| 38 | #endif |
| 39 | |
[email protected] | 753efc4 | 2010-03-09 19:52:16 | [diff] [blame] | 40 | #if defined(TOOLKIT_USES_GTK) |
[email protected] | b982188 | 2009-08-17 22:25:17 | [diff] [blame] | 41 | #include "chrome/browser/gtk/view_id_util.h" |
| 42 | #endif |
| 43 | |
[email protected] | fc2e087 | 2009-08-21 22:14:41 | [diff] [blame] | 44 | #if defined(OS_LINUX) |
[email protected] | 853300a8 | 2010-07-27 21:17:57 | [diff] [blame] | 45 | #define MAYBE_FocusTraversal FocusTraversal |
[email protected] | 252250d | 2010-09-27 18:36:18 | [diff] [blame] | 46 | #define MAYBE_FocusTraversalOnInterstitial FocusTraversalOnInterstitial |
[email protected] | cb7e254 | 2009-12-14 22:02:35 | [diff] [blame] | 47 | // TODO(jcampan): http://crbug.com/23683 |
[email protected] | bbe383d | 2010-07-13 21:49:59 | [diff] [blame] | 48 | #define MAYBE_TabsRememberFocusFindInPage FAILS_TabsRememberFocusFindInPage |
[email protected] | 853300a8 | 2010-07-27 21:17:57 | [diff] [blame] | 49 | #elif defined(OS_MACOSX) |
| 50 | // TODO(suzhe): http://crbug.com/49738 (following two tests) |
[email protected] | a5eb2d67 | 2010-10-22 07:24:48 | [diff] [blame^] | 51 | #define MAYBE_FocusTraversal DISABLED_FocusTraversal |
| 52 | #define MAYBE_FocusTraversalOnInterstitial DISABLED_FocusTraversalOnInterstitial |
[email protected] | 853300a8 | 2010-07-27 21:17:57 | [diff] [blame] | 53 | // TODO(suzhe): http://crbug.com/49737 |
| 54 | #define MAYBE_TabsRememberFocusFindInPage FAILS_TabsRememberFocusFindInPage |
| 55 | #elif defined(OS_WIN) |
| 56 | #define MAYBE_FocusTraversal FocusTraversal |
[email protected] | e4f4e0b | 2009-10-13 19:58:21 | [diff] [blame] | 57 | #define MAYBE_FocusTraversalOnInterstitial FocusTraversalOnInterstitial |
[email protected] | cb7e254 | 2009-12-14 22:02:35 | [diff] [blame] | 58 | #define MAYBE_TabsRememberFocusFindInPage TabsRememberFocusFindInPage |
[email protected] | fc2e087 | 2009-08-21 22:14:41 | [diff] [blame] | 59 | #endif |
| 60 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 61 | namespace { |
| 62 | |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 63 | // The delay waited in some cases where we don't have a notifications for an |
| 64 | // action we take. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 65 | const int kActionDelayMs = 500; |
| 66 | |
[email protected] | f72a1cc | 2010-04-30 07:17:30 | [diff] [blame] | 67 | const char kSimplePage[] = "files/focus/page_with_focus.html"; |
| 68 | const char kStealFocusPage[] = "files/focus/page_steals_focus.html"; |
| 69 | const char kTypicalPage[] = "files/focus/typical_page.html"; |
[email protected] | b65de8b9 | 2009-09-14 19:36:31 | [diff] [blame] | 70 | const char kTypicalPageName[] = "typical_page.html"; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 71 | |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 72 | class BrowserFocusTest : public InProcessBrowserTest { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 73 | public: |
| 74 | BrowserFocusTest() { |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 75 | set_show_window(true); |
| 76 | EnableDOMAutomation(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 77 | } |
[email protected] | b982188 | 2009-08-17 22:25:17 | [diff] [blame] | 78 | |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 79 | bool IsViewFocused(ViewID vid) { |
| 80 | return ui_test_utils::IsViewFocused(browser(), vid); |
[email protected] | b982188 | 2009-08-17 22:25:17 | [diff] [blame] | 81 | } |
| 82 | |
[email protected] | fc2e087 | 2009-08-21 22:14:41 | [diff] [blame] | 83 | void ClickOnView(ViewID vid) { |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 84 | ui_test_utils::ClickOnView(browser(), vid); |
[email protected] | fc2e087 | 2009-08-21 22:14:41 | [diff] [blame] | 85 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 86 | }; |
| 87 | |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 88 | class TestInterstitialPage : public InterstitialPage { |
| 89 | public: |
| 90 | TestInterstitialPage(TabContents* tab, bool new_navigation, const GURL& url) |
[email protected] | a6e602f | 2010-09-28 22:28:30 | [diff] [blame] | 91 | : InterstitialPage(tab, new_navigation, url) { |
[email protected] | b65de8b9 | 2009-09-14 19:36:31 | [diff] [blame] | 92 | FilePath file_path; |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 93 | bool r = PathService::Get(chrome::DIR_TEST_DATA, &file_path); |
| 94 | EXPECT_TRUE(r); |
[email protected] | b65de8b9 | 2009-09-14 19:36:31 | [diff] [blame] | 95 | file_path = file_path.AppendASCII("focus"); |
| 96 | file_path = file_path.AppendASCII(kTypicalPageName); |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 97 | r = file_util::ReadFileToString(file_path, &html_contents_); |
| 98 | EXPECT_TRUE(r); |
| 99 | } |
| 100 | |
| 101 | virtual std::string GetHTMLContents() { |
| 102 | return html_contents_; |
| 103 | } |
| 104 | |
[email protected] | a6e602f | 2010-09-28 22:28:30 | [diff] [blame] | 105 | // Exposing render_view_host() to be public; it is declared as protected in |
| 106 | // the superclass. |
| 107 | virtual RenderViewHost* render_view_host() { |
| 108 | return InterstitialPage::render_view_host(); |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | bool HasFocus() { |
| 112 | return render_view_host()->view()->HasFocus(); |
| 113 | } |
| 114 | |
[email protected] | 130efb0 | 2009-09-18 18:54:35 | [diff] [blame] | 115 | protected: |
| 116 | virtual void FocusedNodeChanged() { |
[email protected] | a6e602f | 2010-09-28 22:28:30 | [diff] [blame] | 117 | NotificationService::current()->Notify( |
| 118 | NotificationType::FOCUS_CHANGED_IN_PAGE, |
| 119 | Source<RenderViewHost>(render_view_host()), |
| 120 | NotificationService::NoDetails()); |
[email protected] | 130efb0 | 2009-09-18 18:54:35 | [diff] [blame] | 121 | } |
| 122 | |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 123 | private: |
| 124 | std::string html_contents_; |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 125 | }; |
[email protected] | b982188 | 2009-08-17 22:25:17 | [diff] [blame] | 126 | |
[email protected] | e4f4e0b | 2009-10-13 19:58:21 | [diff] [blame] | 127 | IN_PROC_BROWSER_TEST_F(BrowserFocusTest, ClickingMovesFocus) { |
[email protected] | a6e602f | 2010-09-28 22:28:30 | [diff] [blame] | 128 | ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); |
[email protected] | 853300a8 | 2010-07-27 21:17:57 | [diff] [blame] | 129 | #if defined(USE_X11) || defined(OS_MACOSX) |
[email protected] | fc2e087 | 2009-08-21 22:14:41 | [diff] [blame] | 130 | // It seems we have to wait a little bit for the widgets to spin up before |
| 131 | // we can start clicking on them. |
| 132 | MessageLoop::current()->PostDelayedTask(FROM_HERE, |
| 133 | new MessageLoop::QuitTask(), |
| 134 | kActionDelayMs); |
| 135 | ui_test_utils::RunMessageLoop(); |
| 136 | #endif |
| 137 | |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 138 | ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR)); |
[email protected] | 186f13f | 2009-08-19 20:34:00 | [diff] [blame] | 139 | |
[email protected] | fc2e087 | 2009-08-21 22:14:41 | [diff] [blame] | 140 | ClickOnView(VIEW_ID_TAB_CONTAINER); |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 141 | ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); |
[email protected] | 186f13f | 2009-08-19 20:34:00 | [diff] [blame] | 142 | |
[email protected] | fc2e087 | 2009-08-21 22:14:41 | [diff] [blame] | 143 | ClickOnView(VIEW_ID_LOCATION_BAR); |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 144 | ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR)); |
[email protected] | 186f13f | 2009-08-19 20:34:00 | [diff] [blame] | 145 | } |
[email protected] | 186f13f | 2009-08-19 20:34:00 | [diff] [blame] | 146 | |
[email protected] | e4f4e0b | 2009-10-13 19:58:21 | [diff] [blame] | 147 | IN_PROC_BROWSER_TEST_F(BrowserFocusTest, BrowsersRememberFocus) { |
[email protected] | a6e602f | 2010-09-28 22:28:30 | [diff] [blame] | 148 | ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 149 | ASSERT_TRUE(test_server()->Start()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 150 | |
| 151 | // First we navigate to our test page. |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 152 | GURL url = test_server()->GetURL(kSimplePage); |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 153 | ui_test_utils::NavigateToURL(browser(), url); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 154 | |
[email protected] | 186f13f | 2009-08-19 20:34:00 | [diff] [blame] | 155 | gfx::NativeWindow window = browser()->window()->GetNativeHandle(); |
| 156 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 157 | // The focus should be on the Tab contents. |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 158 | ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 159 | // Now hide the window, show it again, the focus should not have changed. |
[email protected] | 853300a8 | 2010-07-27 21:17:57 | [diff] [blame] | 160 | ui_test_utils::HideNativeWindow(window); |
| 161 | ui_test_utils::ShowAndFocusNativeWindow(window); |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 162 | ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 163 | |
[email protected] | 186f13f | 2009-08-19 20:34:00 | [diff] [blame] | 164 | browser()->FocusLocationBar(); |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 165 | ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 166 | // Hide the window, show it again, the focus should not have changed. |
[email protected] | 853300a8 | 2010-07-27 21:17:57 | [diff] [blame] | 167 | ui_test_utils::HideNativeWindow(window); |
| 168 | ui_test_utils::ShowAndFocusNativeWindow(window); |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 169 | ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 170 | |
[email protected] | 186f13f | 2009-08-19 20:34:00 | [diff] [blame] | 171 | // The rest of this test does not make sense on Linux because the behavior |
| 172 | // of Activate() is not well defined and can vary by window manager. |
| 173 | #if defined(OS_WIN) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 174 | // Open a new browser window. |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 175 | Browser* browser2 = Browser::Create(browser()->profile()); |
| 176 | ASSERT_TRUE(browser2); |
[email protected] | e0c7c26 | 2009-04-23 23:09:43 | [diff] [blame] | 177 | browser2->tabstrip_model()->delegate()->AddBlankTab(true); |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 178 | browser2->window()->Show(); |
| 179 | ui_test_utils::NavigateToURL(browser2, url); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 180 | |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 181 | HWND hwnd2 = reinterpret_cast<HWND>(browser2->window()->GetNativeHandle()); |
[email protected] | 4a507a6 | 2009-05-28 00:10:00 | [diff] [blame] | 182 | BrowserView* browser_view2 = |
| 183 | BrowserView::GetBrowserViewForNativeWindow(hwnd2); |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 184 | ASSERT_TRUE(browser_view2); |
| 185 | views::FocusManager* focus_manager2 = |
[email protected] | 82166b6 | 2009-06-30 18:48:00 | [diff] [blame] | 186 | views::FocusManager::GetFocusManagerForNativeView(hwnd2); |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 187 | ASSERT_TRUE(focus_manager2); |
[email protected] | 7e38369 | 2009-06-12 19:14:54 | [diff] [blame] | 188 | EXPECT_EQ(browser_view2->GetTabContentsContainerView(), |
[email protected] | 610d36a | 2009-05-22 23:00:38 | [diff] [blame] | 189 | focus_manager2->GetFocusedView()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 190 | |
| 191 | // Switch to the 1st browser window, focus should still be on the location |
| 192 | // bar and the second browser should have nothing focused. |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 193 | browser()->window()->Activate(); |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 194 | ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR)); |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 195 | EXPECT_EQ(NULL, focus_manager2->GetFocusedView()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 196 | |
| 197 | // Switch back to the second browser, focus should still be on the page. |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 198 | browser2->window()->Activate(); |
[email protected] | 186f13f | 2009-08-19 20:34:00 | [diff] [blame] | 199 | EXPECT_EQ(NULL, |
| 200 | views::FocusManager::GetFocusManagerForNativeView( |
| 201 | browser()->window()->GetNativeHandle())->GetFocusedView()); |
[email protected] | 7e38369 | 2009-06-12 19:14:54 | [diff] [blame] | 202 | EXPECT_EQ(browser_view2->GetTabContentsContainerView(), |
[email protected] | 610d36a | 2009-05-22 23:00:38 | [diff] [blame] | 203 | focus_manager2->GetFocusedView()); |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 204 | |
| 205 | // Close the 2nd browser to avoid a DCHECK(). |
| 206 | browser_view2->Close(); |
[email protected] | 186f13f | 2009-08-19 20:34:00 | [diff] [blame] | 207 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | // Tabs remember focus. |
[email protected] | e4f4e0b | 2009-10-13 19:58:21 | [diff] [blame] | 211 | IN_PROC_BROWSER_TEST_F(BrowserFocusTest, TabsRememberFocus) { |
[email protected] | a6e602f | 2010-09-28 22:28:30 | [diff] [blame] | 212 | ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 213 | ASSERT_TRUE(test_server()->Start()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 214 | |
| 215 | // First we navigate to our test page. |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 216 | GURL url = test_server()->GetURL(kSimplePage); |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 217 | ui_test_utils::NavigateToURL(browser(), url); |
| 218 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 219 | // Create several tabs. |
[email protected] | 22735af6 | 2009-04-07 21:09:58 | [diff] [blame] | 220 | for (int i = 0; i < 4; ++i) { |
[email protected] | cecc93a | 2010-10-05 15:58:55 | [diff] [blame] | 221 | Browser::AddTabWithURLParams params(url, PageTransition::TYPED); |
| 222 | browser()->AddTabWithURL(¶ms); |
| 223 | EXPECT_EQ(browser(), params.target); |
[email protected] | 22735af6 | 2009-04-07 21:09:58 | [diff] [blame] | 224 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 225 | |
| 226 | // Alternate focus for the tab. |
| 227 | const bool kFocusPage[3][5] = { |
| 228 | { true, true, true, true, false }, |
| 229 | { false, false, false, false, false }, |
| 230 | { false, true, false, true, false } |
| 231 | }; |
| 232 | |
| 233 | for (int i = 1; i < 3; i++) { |
| 234 | for (int j = 0; j < 5; j++) { |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 235 | // Activate the tab. |
| 236 | browser()->SelectTabContentsAt(j, true); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 237 | |
| 238 | // Activate the location bar or the page. |
[email protected] | 7e38369 | 2009-06-12 19:14:54 | [diff] [blame] | 239 | if (kFocusPage[i][j]) { |
[email protected] | 186f13f | 2009-08-19 20:34:00 | [diff] [blame] | 240 | browser()->GetTabContentsAt(j)->view()->Focus(); |
[email protected] | 7e38369 | 2009-06-12 19:14:54 | [diff] [blame] | 241 | } else { |
[email protected] | 186f13f | 2009-08-19 20:34:00 | [diff] [blame] | 242 | browser()->FocusLocationBar(); |
[email protected] | 7e38369 | 2009-06-12 19:14:54 | [diff] [blame] | 243 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | // Now come back to the tab and check the right view is focused. |
| 247 | for (int j = 0; j < 5; j++) { |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 248 | // Activate the tab. |
| 249 | browser()->SelectTabContentsAt(j, true); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 250 | |
[email protected] | 186f13f | 2009-08-19 20:34:00 | [diff] [blame] | 251 | ViewID vid = kFocusPage[i][j] ? VIEW_ID_TAB_CONTAINER_FOCUS_VIEW : |
| 252 | VIEW_ID_LOCATION_BAR; |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 253 | ASSERT_TRUE(IsViewFocused(vid)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 254 | } |
[email protected] | cb7e254 | 2009-12-14 22:02:35 | [diff] [blame] | 255 | |
[email protected] | cb7e254 | 2009-12-14 22:02:35 | [diff] [blame] | 256 | browser()->SelectTabContentsAt(0, true); |
| 257 | // Try the above, but with ctrl+tab. Since tab normally changes focus, |
| 258 | // this has regressed in the past. Loop through several times to be sure. |
| 259 | for (int j = 0; j < 15; j++) { |
| 260 | ViewID vid = kFocusPage[i][j % 5] ? VIEW_ID_TAB_CONTAINER_FOCUS_VIEW : |
| 261 | VIEW_ID_LOCATION_BAR; |
| 262 | ASSERT_TRUE(IsViewFocused(vid)); |
| 263 | |
[email protected] | 1d00068 | 2010-08-23 16:21:28 | [diff] [blame] | 264 | ASSERT_TRUE(ui_test_utils::SendKeyPressSync( |
[email protected] | a6e602f | 2010-09-28 22:28:30 | [diff] [blame] | 265 | browser(), app::VKEY_TAB, true, false, false, false)); |
[email protected] | cb7e254 | 2009-12-14 22:02:35 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | // As above, but with ctrl+shift+tab. |
| 269 | browser()->SelectTabContentsAt(4, true); |
| 270 | for (int j = 14; j >= 0; --j) { |
| 271 | ViewID vid = kFocusPage[i][j % 5] ? VIEW_ID_TAB_CONTAINER_FOCUS_VIEW : |
| 272 | VIEW_ID_LOCATION_BAR; |
| 273 | ASSERT_TRUE(IsViewFocused(vid)); |
| 274 | |
[email protected] | 1d00068 | 2010-08-23 16:21:28 | [diff] [blame] | 275 | ASSERT_TRUE(ui_test_utils::SendKeyPressSync( |
[email protected] | a6e602f | 2010-09-28 22:28:30 | [diff] [blame] | 276 | browser(), app::VKEY_TAB, true, true, false, false)); |
[email protected] | cb7e254 | 2009-12-14 22:02:35 | [diff] [blame] | 277 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 278 | } |
| 279 | } |
| 280 | |
[email protected] | ae40b57 | 2009-10-02 21:17:45 | [diff] [blame] | 281 | // Tabs remember focus with find-in-page box. |
[email protected] | cb7e254 | 2009-12-14 22:02:35 | [diff] [blame] | 282 | IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_TabsRememberFocusFindInPage) { |
[email protected] | a6e602f | 2010-09-28 22:28:30 | [diff] [blame] | 283 | ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 284 | ASSERT_TRUE(test_server()->Start()); |
[email protected] | ae40b57 | 2009-10-02 21:17:45 | [diff] [blame] | 285 | |
| 286 | // First we navigate to our test page. |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 287 | GURL url = test_server()->GetURL(kSimplePage); |
[email protected] | ae40b57 | 2009-10-02 21:17:45 | [diff] [blame] | 288 | ui_test_utils::NavigateToURL(browser(), url); |
| 289 | |
| 290 | browser()->Find(); |
| 291 | ui_test_utils::FindInPage(browser()->GetSelectedTabContents(), |
| 292 | ASCIIToUTF16("a"), true, false, NULL); |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 293 | ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); |
[email protected] | ae40b57 | 2009-10-02 21:17:45 | [diff] [blame] | 294 | |
| 295 | // Focus the location bar. |
| 296 | browser()->FocusLocationBar(); |
| 297 | |
| 298 | // Create a 2nd tab. |
[email protected] | cecc93a | 2010-10-05 15:58:55 | [diff] [blame] | 299 | Browser::AddTabWithURLParams params(url, PageTransition::TYPED); |
| 300 | browser()->AddTabWithURL(¶ms); |
| 301 | EXPECT_EQ(browser(), params.target); |
[email protected] | ae40b57 | 2009-10-02 21:17:45 | [diff] [blame] | 302 | |
| 303 | // Focus should be on the recently opened tab page. |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 304 | ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); |
[email protected] | ae40b57 | 2009-10-02 21:17:45 | [diff] [blame] | 305 | |
| 306 | // Select 1st tab, focus should still be on the location-bar. |
| 307 | // (bug http://crbug.com/23296) |
| 308 | browser()->SelectTabContentsAt(0, true); |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 309 | ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR)); |
[email protected] | ae40b57 | 2009-10-02 21:17:45 | [diff] [blame] | 310 | |
| 311 | // Now open the find box again, switch to another tab and come back, the focus |
| 312 | // should return to the find box. |
| 313 | browser()->Find(); |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 314 | ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); |
[email protected] | ae40b57 | 2009-10-02 21:17:45 | [diff] [blame] | 315 | browser()->SelectTabContentsAt(1, true); |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 316 | ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); |
[email protected] | ae40b57 | 2009-10-02 21:17:45 | [diff] [blame] | 317 | browser()->SelectTabContentsAt(0, true); |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 318 | ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); |
[email protected] | ae40b57 | 2009-10-02 21:17:45 | [diff] [blame] | 319 | } |
| 320 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 321 | // Background window does not steal focus. |
[email protected] | e4f4e0b | 2009-10-13 19:58:21 | [diff] [blame] | 322 | IN_PROC_BROWSER_TEST_F(BrowserFocusTest, BackgroundBrowserDontStealFocus) { |
[email protected] | a6e602f | 2010-09-28 22:28:30 | [diff] [blame] | 323 | ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 324 | ASSERT_TRUE(test_server()->Start()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 325 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 326 | // Open a new browser window. |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 327 | Browser* browser2 = Browser::Create(browser()->profile()); |
| 328 | ASSERT_TRUE(browser2); |
[email protected] | e0c7c26 | 2009-04-23 23:09:43 | [diff] [blame] | 329 | browser2->tabstrip_model()->delegate()->AddBlankTab(true); |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 330 | browser2->window()->Show(); |
[email protected] | 186f13f | 2009-08-19 20:34:00 | [diff] [blame] | 331 | |
[email protected] | ed179ee | 2009-10-03 21:02:51 | [diff] [blame] | 332 | Browser* focused_browser = NULL; |
| 333 | Browser* unfocused_browser = NULL; |
[email protected] | 753efc4 | 2010-03-09 19:52:16 | [diff] [blame] | 334 | #if defined(USE_X11) |
| 335 | // On X11, calling Activate() is not guaranteed to move focus, so we have |
[email protected] | 186f13f | 2009-08-19 20:34:00 | [diff] [blame] | 336 | // to figure out which browser does have focus. |
| 337 | if (browser2->window()->IsActive()) { |
| 338 | focused_browser = browser2; |
| 339 | unfocused_browser = browser(); |
| 340 | } else if (browser()->window()->IsActive()) { |
| 341 | focused_browser = browser(); |
| 342 | unfocused_browser = browser2; |
| 343 | } else { |
[email protected] | 579c3d8 | 2010-10-06 03:53:51 | [diff] [blame] | 344 | FAIL() << "Could not determine which browser has focus"; |
[email protected] | 186f13f | 2009-08-19 20:34:00 | [diff] [blame] | 345 | } |
| 346 | #elif defined(OS_WIN) |
| 347 | focused_browser = browser(); |
| 348 | unfocused_browser = browser2; |
[email protected] | 853300a8 | 2010-07-27 21:17:57 | [diff] [blame] | 349 | #elif defined(OS_MACOSX) |
| 350 | // On Mac, the newly created window always gets the focus. |
| 351 | focused_browser = browser2; |
| 352 | unfocused_browser = browser(); |
[email protected] | 186f13f | 2009-08-19 20:34:00 | [diff] [blame] | 353 | #endif |
| 354 | |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 355 | GURL steal_focus_url = test_server()->GetURL(kStealFocusPage); |
[email protected] | 186f13f | 2009-08-19 20:34:00 | [diff] [blame] | 356 | ui_test_utils::NavigateToURL(unfocused_browser, steal_focus_url); |
[email protected] | 1e187af | 2009-02-25 02:02:46 | [diff] [blame] | 357 | |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 358 | // Activate the first browser. |
[email protected] | 186f13f | 2009-08-19 20:34:00 | [diff] [blame] | 359 | focused_browser->window()->Activate(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 360 | |
[email protected] | 579c3d8 | 2010-10-06 03:53:51 | [diff] [blame] | 361 | ASSERT_TRUE(ui_test_utils::ExecuteJavaScript( |
| 362 | unfocused_browser->GetSelectedTabContents()->render_view_host(), L"", |
| 363 | L"stealFocus();")); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 364 | |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 365 | // Make sure the first browser is still active. |
[email protected] | 186f13f | 2009-08-19 20:34:00 | [diff] [blame] | 366 | EXPECT_TRUE(focused_browser->window()->IsActive()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | // Page cannot steal focus when focus is on location bar. |
[email protected] | e4f4e0b | 2009-10-13 19:58:21 | [diff] [blame] | 370 | IN_PROC_BROWSER_TEST_F(BrowserFocusTest, LocationBarLockFocus) { |
[email protected] | a6e602f | 2010-09-28 22:28:30 | [diff] [blame] | 371 | ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 372 | ASSERT_TRUE(test_server()->Start()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 373 | |
| 374 | // Open the page that steals focus. |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 375 | GURL url = test_server()->GetURL(kStealFocusPage); |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 376 | ui_test_utils::NavigateToURL(browser(), url); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 377 | |
[email protected] | 186f13f | 2009-08-19 20:34:00 | [diff] [blame] | 378 | browser()->FocusLocationBar(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 379 | |
[email protected] | 579c3d8 | 2010-10-06 03:53:51 | [diff] [blame] | 380 | ASSERT_TRUE(ui_test_utils::ExecuteJavaScript( |
| 381 | browser()->GetSelectedTabContents()->render_view_host(), L"", |
| 382 | L"stealFocus();")); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 383 | |
| 384 | // Make sure the location bar is still focused. |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 385 | ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 386 | } |
| 387 | |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 388 | // Focus traversal on a regular page. |
[email protected] | 130efb0 | 2009-09-18 18:54:35 | [diff] [blame] | 389 | // Note that this test relies on a notification from the renderer that the |
| 390 | // focus has changed in the page. The notification in the renderer may change |
| 391 | // at which point this test would fail (see comment in |
| 392 | // RenderWidget::didFocus()). |
[email protected] | 853300a8 | 2010-07-27 21:17:57 | [diff] [blame] | 393 | IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusTraversal) { |
[email protected] | a6e602f | 2010-09-28 22:28:30 | [diff] [blame] | 394 | ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 395 | ASSERT_TRUE(test_server()->Start()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 396 | |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 397 | // First we navigate to our test page. |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 398 | GURL url = test_server()->GetURL(kTypicalPage); |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 399 | ui_test_utils::NavigateToURL(browser(), url); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 400 | |
[email protected] | 186f13f | 2009-08-19 20:34:00 | [diff] [blame] | 401 | browser()->FocusLocationBar(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 402 | |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 403 | const char* kExpElementIDs[] = { |
| 404 | "", // Initially no element in the page should be focused |
| 405 | // (the location bar is focused). |
| 406 | "textEdit", "searchButton", "luckyButton", "googleLink", "gmailLink", |
| 407 | "gmapLink" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 408 | }; |
| 409 | |
| 410 | // Test forward focus traversal. |
| 411 | for (int i = 0; i < 3; ++i) { |
[email protected] | f07d7bf | 2010-04-06 08:02:42 | [diff] [blame] | 412 | SCOPED_TRACE(StringPrintf("outer loop: %d", i)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 413 | // Location bar should be focused. |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 414 | ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 415 | |
| 416 | // Now let's press tab to move the focus. |
[email protected] | 130efb0 | 2009-09-18 18:54:35 | [diff] [blame] | 417 | for (size_t j = 0; j < arraysize(kExpElementIDs); ++j) { |
[email protected] | f07d7bf | 2010-04-06 08:02:42 | [diff] [blame] | 418 | SCOPED_TRACE(StringPrintf("inner loop %" PRIuS, j)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 419 | // Let's make sure the focus is on the expected element in the page. |
[email protected] | 4567161 | 2009-04-29 22:24:01 | [diff] [blame] | 420 | std::string actual; |
| 421 | ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString( |
[email protected] | 17c4f3c | 2009-07-04 16:36:25 | [diff] [blame] | 422 | browser()->GetSelectedTabContents()->render_view_host(), |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 423 | L"", |
[email protected] | 4567161 | 2009-04-29 22:24:01 | [diff] [blame] | 424 | L"window.domAutomationController.send(getFocusedElement());", |
| 425 | &actual)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 426 | ASSERT_STREQ(kExpElementIDs[j], actual.c_str()); |
| 427 | |
[email protected] | a6e602f | 2010-09-28 22:28:30 | [diff] [blame] | 428 | NotificationType::Type notification_type; |
| 429 | NotificationSource notification_source = |
| 430 | NotificationService::AllSources(); |
[email protected] | 130efb0 | 2009-09-18 18:54:35 | [diff] [blame] | 431 | if (j < arraysize(kExpElementIDs) - 1) { |
[email protected] | a6e602f | 2010-09-28 22:28:30 | [diff] [blame] | 432 | notification_type = NotificationType::FOCUS_CHANGED_IN_PAGE; |
| 433 | notification_source = Source<RenderViewHost>( |
| 434 | browser()->GetSelectedTabContents()->render_view_host()); |
[email protected] | 130efb0 | 2009-09-18 18:54:35 | [diff] [blame] | 435 | } else { |
| 436 | // On the last tab key press, the focus returns to the browser. |
[email protected] | a6e602f | 2010-09-28 22:28:30 | [diff] [blame] | 437 | notification_type = NotificationType::FOCUS_RETURNED_TO_BROWSER; |
| 438 | notification_source = Source<Browser>(browser()); |
[email protected] | 130efb0 | 2009-09-18 18:54:35 | [diff] [blame] | 439 | } |
[email protected] | a6e602f | 2010-09-28 22:28:30 | [diff] [blame] | 440 | |
| 441 | ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait( |
| 442 | browser(), app::VKEY_TAB, false, false, false, false, |
| 443 | notification_type, notification_source)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 444 | } |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 445 | |
| 446 | // At this point the renderer has sent us a message asking to advance the |
| 447 | // focus (as the end of the focus loop was reached in the renderer). |
| 448 | // We need to run the message loop to process it. |
[email protected] | f07d7bf | 2010-04-06 08:02:42 | [diff] [blame] | 449 | ui_test_utils::RunAllPendingInMessageLoop(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | // Now let's try reverse focus traversal. |
| 453 | for (int i = 0; i < 3; ++i) { |
[email protected] | f07d7bf | 2010-04-06 08:02:42 | [diff] [blame] | 454 | SCOPED_TRACE(StringPrintf("outer loop: %d", i)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 455 | // Location bar should be focused. |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 456 | ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 457 | |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 458 | // Now let's press shift-tab to move the focus in reverse. |
[email protected] | a6e602f | 2010-09-28 22:28:30 | [diff] [blame] | 459 | for (size_t j = 0; j < arraysize(kExpElementIDs); ++j) { |
[email protected] | f07d7bf | 2010-04-06 08:02:42 | [diff] [blame] | 460 | SCOPED_TRACE(StringPrintf("inner loop: %" PRIuS, j)); |
[email protected] | 130efb0 | 2009-09-18 18:54:35 | [diff] [blame] | 461 | |
[email protected] | a6e602f | 2010-09-28 22:28:30 | [diff] [blame] | 462 | NotificationType::Type notification_type; |
| 463 | NotificationSource notification_source = |
| 464 | NotificationService::AllSources(); |
[email protected] | 130efb0 | 2009-09-18 18:54:35 | [diff] [blame] | 465 | if (j < arraysize(kExpElementIDs) - 1) { |
[email protected] | a6e602f | 2010-09-28 22:28:30 | [diff] [blame] | 466 | notification_type = NotificationType::FOCUS_CHANGED_IN_PAGE; |
| 467 | notification_source = Source<RenderViewHost>( |
| 468 | browser()->GetSelectedTabContents()->render_view_host()); |
[email protected] | 130efb0 | 2009-09-18 18:54:35 | [diff] [blame] | 469 | } else { |
| 470 | // On the last tab key press, the focus returns to the browser. |
[email protected] | a6e602f | 2010-09-28 22:28:30 | [diff] [blame] | 471 | notification_type = NotificationType::FOCUS_RETURNED_TO_BROWSER; |
| 472 | notification_source = Source<Browser>(browser()); |
[email protected] | 130efb0 | 2009-09-18 18:54:35 | [diff] [blame] | 473 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 474 | |
[email protected] | a6e602f | 2010-09-28 22:28:30 | [diff] [blame] | 475 | ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait( |
| 476 | browser(), app::VKEY_TAB, false, true, false, false, |
| 477 | notification_type, notification_source)); |
| 478 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 479 | // Let's make sure the focus is on the expected element in the page. |
[email protected] | 4567161 | 2009-04-29 22:24:01 | [diff] [blame] | 480 | std::string actual; |
| 481 | ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString( |
[email protected] | 17c4f3c | 2009-07-04 16:36:25 | [diff] [blame] | 482 | browser()->GetSelectedTabContents()->render_view_host(), |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 483 | L"", |
[email protected] | 4567161 | 2009-04-29 22:24:01 | [diff] [blame] | 484 | L"window.domAutomationController.send(getFocusedElement());", |
| 485 | &actual)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 486 | ASSERT_STREQ(kExpElementIDs[6 - j], actual.c_str()); |
| 487 | } |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 488 | |
| 489 | // At this point the renderer has sent us a message asking to advance the |
| 490 | // focus (as the end of the focus loop was reached in the renderer). |
| 491 | // We need to run the message loop to process it. |
[email protected] | f07d7bf | 2010-04-06 08:02:42 | [diff] [blame] | 492 | ui_test_utils::RunAllPendingInMessageLoop(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 493 | } |
| 494 | } |
| 495 | |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 496 | // Focus traversal while an interstitial is showing. |
[email protected] | fc2e087 | 2009-08-21 22:14:41 | [diff] [blame] | 497 | IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusTraversalOnInterstitial) { |
[email protected] | a6e602f | 2010-09-28 22:28:30 | [diff] [blame] | 498 | ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 499 | ASSERT_TRUE(test_server()->Start()); |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 500 | |
| 501 | // First we navigate to our test page. |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 502 | GURL url = test_server()->GetURL(kSimplePage); |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 503 | ui_test_utils::NavigateToURL(browser(), url); |
| 504 | |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 505 | // Focus should be on the page. |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 506 | ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 507 | |
| 508 | // Let's show an interstitial. |
| 509 | TestInterstitialPage* interstitial_page = |
| 510 | new TestInterstitialPage(browser()->GetSelectedTabContents(), |
| 511 | true, GURL("http://interstitial.com")); |
| 512 | interstitial_page->Show(); |
| 513 | // Give some time for the interstitial to show. |
| 514 | MessageLoop::current()->PostDelayedTask(FROM_HERE, |
| 515 | new MessageLoop::QuitTask(), |
| 516 | 1000); |
| 517 | ui_test_utils::RunMessageLoop(); |
| 518 | |
[email protected] | fc2e087 | 2009-08-21 22:14:41 | [diff] [blame] | 519 | browser()->FocusLocationBar(); |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 520 | |
| 521 | const char* kExpElementIDs[] = { |
| 522 | "", // Initially no element in the page should be focused |
| 523 | // (the location bar is focused). |
| 524 | "textEdit", "searchButton", "luckyButton", "googleLink", "gmailLink", |
| 525 | "gmapLink" |
| 526 | }; |
| 527 | |
| 528 | // Test forward focus traversal. |
| 529 | for (int i = 0; i < 2; ++i) { |
| 530 | // Location bar should be focused. |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 531 | ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR)); |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 532 | |
| 533 | // Now let's press tab to move the focus. |
[email protected] | 130efb0 | 2009-09-18 18:54:35 | [diff] [blame] | 534 | for (size_t j = 0; j < 7; ++j) { |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 535 | // Let's make sure the focus is on the expected element in the page. |
[email protected] | a6e602f | 2010-09-28 22:28:30 | [diff] [blame] | 536 | std::string actual; |
| 537 | ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString( |
| 538 | interstitial_page->render_view_host(), L"", |
| 539 | L"window.domAutomationController.send(getFocusedElement());", |
| 540 | &actual)); |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 541 | ASSERT_STREQ(kExpElementIDs[j], actual.c_str()); |
| 542 | |
[email protected] | a6e602f | 2010-09-28 22:28:30 | [diff] [blame] | 543 | NotificationType::Type notification_type; |
| 544 | NotificationSource notification_source = |
| 545 | NotificationService::AllSources(); |
[email protected] | 130efb0 | 2009-09-18 18:54:35 | [diff] [blame] | 546 | if (j < arraysize(kExpElementIDs) - 1) { |
[email protected] | a6e602f | 2010-09-28 22:28:30 | [diff] [blame] | 547 | notification_type = NotificationType::FOCUS_CHANGED_IN_PAGE; |
| 548 | notification_source = Source<RenderViewHost>( |
| 549 | interstitial_page->render_view_host()); |
[email protected] | 130efb0 | 2009-09-18 18:54:35 | [diff] [blame] | 550 | } else { |
| 551 | // On the last tab key press, the focus returns to the browser. |
[email protected] | a6e602f | 2010-09-28 22:28:30 | [diff] [blame] | 552 | notification_type = NotificationType::FOCUS_RETURNED_TO_BROWSER; |
| 553 | notification_source = Source<Browser>(browser()); |
[email protected] | 130efb0 | 2009-09-18 18:54:35 | [diff] [blame] | 554 | } |
[email protected] | a6e602f | 2010-09-28 22:28:30 | [diff] [blame] | 555 | |
| 556 | ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait( |
| 557 | browser(), app::VKEY_TAB, false, false, false, false, |
| 558 | notification_type, notification_source)); |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 559 | } |
| 560 | |
| 561 | // At this point the renderer has sent us a message asking to advance the |
| 562 | // focus (as the end of the focus loop was reached in the renderer). |
| 563 | // We need to run the message loop to process it. |
[email protected] | f07d7bf | 2010-04-06 08:02:42 | [diff] [blame] | 564 | ui_test_utils::RunAllPendingInMessageLoop(); |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 565 | } |
| 566 | |
| 567 | // Now let's try reverse focus traversal. |
| 568 | for (int i = 0; i < 2; ++i) { |
| 569 | // Location bar should be focused. |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 570 | ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR)); |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 571 | |
| 572 | // Now let's press shift-tab to move the focus in reverse. |
[email protected] | 130efb0 | 2009-09-18 18:54:35 | [diff] [blame] | 573 | for (size_t j = 0; j < 7; ++j) { |
[email protected] | a6e602f | 2010-09-28 22:28:30 | [diff] [blame] | 574 | NotificationType::Type notification_type; |
| 575 | NotificationSource notification_source = |
| 576 | NotificationService::AllSources(); |
[email protected] | 130efb0 | 2009-09-18 18:54:35 | [diff] [blame] | 577 | if (j < arraysize(kExpElementIDs) - 1) { |
[email protected] | a6e602f | 2010-09-28 22:28:30 | [diff] [blame] | 578 | notification_type = NotificationType::FOCUS_CHANGED_IN_PAGE; |
| 579 | notification_source = Source<RenderViewHost>( |
| 580 | interstitial_page->render_view_host()); |
[email protected] | 130efb0 | 2009-09-18 18:54:35 | [diff] [blame] | 581 | } else { |
| 582 | // On the last tab key press, the focus returns to the browser. |
[email protected] | a6e602f | 2010-09-28 22:28:30 | [diff] [blame] | 583 | notification_type = NotificationType::FOCUS_RETURNED_TO_BROWSER; |
| 584 | notification_source = Source<Browser>(browser()); |
[email protected] | 130efb0 | 2009-09-18 18:54:35 | [diff] [blame] | 585 | } |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 586 | |
[email protected] | a6e602f | 2010-09-28 22:28:30 | [diff] [blame] | 587 | ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait( |
| 588 | browser(), app::VKEY_TAB, false, true, false, false, |
| 589 | notification_type, notification_source)); |
| 590 | |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 591 | // Let's make sure the focus is on the expected element in the page. |
[email protected] | a6e602f | 2010-09-28 22:28:30 | [diff] [blame] | 592 | std::string actual; |
| 593 | ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString( |
| 594 | interstitial_page->render_view_host(), L"", |
| 595 | L"window.domAutomationController.send(getFocusedElement());", |
| 596 | &actual)); |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 597 | ASSERT_STREQ(kExpElementIDs[6 - j], actual.c_str()); |
| 598 | } |
| 599 | |
| 600 | // At this point the renderer has sent us a message asking to advance the |
| 601 | // focus (as the end of the focus loop was reached in the renderer). |
| 602 | // We need to run the message loop to process it. |
[email protected] | f07d7bf | 2010-04-06 08:02:42 | [diff] [blame] | 603 | ui_test_utils::RunAllPendingInMessageLoop(); |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 604 | } |
| 605 | } |
| 606 | |
| 607 | // Focus stays on page with interstitials. |
[email protected] | e4f4e0b | 2009-10-13 19:58:21 | [diff] [blame] | 608 | IN_PROC_BROWSER_TEST_F(BrowserFocusTest, InterstitialFocus) { |
[email protected] | a6e602f | 2010-09-28 22:28:30 | [diff] [blame] | 609 | ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 610 | ASSERT_TRUE(test_server()->Start()); |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 611 | |
| 612 | // First we navigate to our test page. |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 613 | GURL url = test_server()->GetURL(kSimplePage); |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 614 | ui_test_utils::NavigateToURL(browser(), url); |
| 615 | |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 616 | // Page should have focus. |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 617 | ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 618 | EXPECT_TRUE(browser()->GetSelectedTabContents()->render_view_host()->view()-> |
| 619 | HasFocus()); |
| 620 | |
[email protected] | 9d8a464 | 2009-07-29 17:25:30 | [diff] [blame] | 621 | // Let's show an interstitial. |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 622 | TestInterstitialPage* interstitial_page = |
| 623 | new TestInterstitialPage(browser()->GetSelectedTabContents(), |
| 624 | true, GURL("http://interstitial.com")); |
| 625 | interstitial_page->Show(); |
| 626 | // Give some time for the interstitial to show. |
| 627 | MessageLoop::current()->PostDelayedTask(FROM_HERE, |
| 628 | new MessageLoop::QuitTask(), |
| 629 | 1000); |
| 630 | ui_test_utils::RunMessageLoop(); |
| 631 | |
| 632 | // The interstitial should have focus now. |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 633 | ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 634 | EXPECT_TRUE(interstitial_page->HasFocus()); |
| 635 | |
| 636 | // Hide the interstitial. |
| 637 | interstitial_page->DontProceed(); |
| 638 | |
| 639 | // Focus should be back on the original page. |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 640 | ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 641 | } |
| 642 | |
[email protected] | 9bd491ee | 2008-12-10 22:31:07 | [diff] [blame] | 643 | // Make sure Find box can request focus, even when it is already open. |
[email protected] | e4f4e0b | 2009-10-13 19:58:21 | [diff] [blame] | 644 | IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FindFocusTest) { |
[email protected] | a6e602f | 2010-09-28 22:28:30 | [diff] [blame] | 645 | ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 646 | ASSERT_TRUE(test_server()->Start()); |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 647 | |
[email protected] | 9bd491ee | 2008-12-10 22:31:07 | [diff] [blame] | 648 | // Open some page (any page that doesn't steal focus). |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 649 | GURL url = test_server()->GetURL(kTypicalPage); |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 650 | ui_test_utils::NavigateToURL(browser(), url); |
[email protected] | 9bd491ee | 2008-12-10 22:31:07 | [diff] [blame] | 651 | |
[email protected] | 853300a8 | 2010-07-27 21:17:57 | [diff] [blame] | 652 | #if defined(OS_MACOSX) |
| 653 | // Press Cmd+F, which will make the Find box open and request focus. |
[email protected] | 1d00068 | 2010-08-23 16:21:28 | [diff] [blame] | 654 | ASSERT_TRUE(ui_test_utils::SendKeyPressSync( |
[email protected] | a6e602f | 2010-09-28 22:28:30 | [diff] [blame] | 655 | browser(), app::VKEY_F, false, false, false, true)); |
[email protected] | 853300a8 | 2010-07-27 21:17:57 | [diff] [blame] | 656 | #else |
[email protected] | 9bd491ee | 2008-12-10 22:31:07 | [diff] [blame] | 657 | // Press Ctrl+F, which will make the Find box open and request focus. |
[email protected] | 1d00068 | 2010-08-23 16:21:28 | [diff] [blame] | 658 | ASSERT_TRUE(ui_test_utils::SendKeyPressSync( |
[email protected] | a6e602f | 2010-09-28 22:28:30 | [diff] [blame] | 659 | browser(), app::VKEY_F, true, false, false, false)); |
[email protected] | 853300a8 | 2010-07-27 21:17:57 | [diff] [blame] | 660 | #endif |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 661 | |
| 662 | // Ideally, we wouldn't sleep here and instead would intercept the |
| 663 | // RenderViewHostDelegate::HandleKeyboardEvent() callback. To do that, we |
| 664 | // could create a RenderViewHostDelegate wrapper and hook-it up by either: |
| 665 | // - creating a factory used to create the delegate |
| 666 | // - making the test a private and overwriting the delegate member directly. |
[email protected] | fc2e087 | 2009-08-21 22:14:41 | [diff] [blame] | 667 | MessageLoop::current()->PostDelayedTask( |
| 668 | FROM_HERE, new MessageLoop::QuitTask(), kActionDelayMs); |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 669 | ui_test_utils::RunMessageLoop(); |
| 670 | |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 671 | ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); |
[email protected] | 9bd491ee | 2008-12-10 22:31:07 | [diff] [blame] | 672 | |
[email protected] | fc2e087 | 2009-08-21 22:14:41 | [diff] [blame] | 673 | browser()->FocusLocationBar(); |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 674 | ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR)); |
[email protected] | 9bd491ee | 2008-12-10 22:31:07 | [diff] [blame] | 675 | |
| 676 | // Now press Ctrl+F again and focus should move to the Find box. |
[email protected] | 853300a8 | 2010-07-27 21:17:57 | [diff] [blame] | 677 | #if defined(OS_MACOSX) |
[email protected] | 1d00068 | 2010-08-23 16:21:28 | [diff] [blame] | 678 | ASSERT_TRUE(ui_test_utils::SendKeyPressSync( |
[email protected] | a6e602f | 2010-09-28 22:28:30 | [diff] [blame] | 679 | browser(), app::VKEY_F, false, false, false, true)); |
[email protected] | 853300a8 | 2010-07-27 21:17:57 | [diff] [blame] | 680 | #else |
[email protected] | 1d00068 | 2010-08-23 16:21:28 | [diff] [blame] | 681 | ASSERT_TRUE(ui_test_utils::SendKeyPressSync( |
[email protected] | a6e602f | 2010-09-28 22:28:30 | [diff] [blame] | 682 | browser(), app::VKEY_F, true, false, false, false)); |
[email protected] | 853300a8 | 2010-07-27 21:17:57 | [diff] [blame] | 683 | #endif |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 684 | ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); |
[email protected] | 9bd491ee | 2008-12-10 22:31:07 | [diff] [blame] | 685 | |
| 686 | // Set focus to the page. |
[email protected] | fc2e087 | 2009-08-21 22:14:41 | [diff] [blame] | 687 | ClickOnView(VIEW_ID_TAB_CONTAINER); |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 688 | ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); |
[email protected] | 9bd491ee | 2008-12-10 22:31:07 | [diff] [blame] | 689 | |
| 690 | // Now press Ctrl+F again and focus should move to the Find box. |
[email protected] | 853300a8 | 2010-07-27 21:17:57 | [diff] [blame] | 691 | #if defined(OS_MACOSX) |
[email protected] | 1d00068 | 2010-08-23 16:21:28 | [diff] [blame] | 692 | ASSERT_TRUE(ui_test_utils::SendKeyPressSync( |
[email protected] | a6e602f | 2010-09-28 22:28:30 | [diff] [blame] | 693 | browser(), app::VKEY_F, false, false, false, true)); |
[email protected] | 853300a8 | 2010-07-27 21:17:57 | [diff] [blame] | 694 | #else |
[email protected] | 1d00068 | 2010-08-23 16:21:28 | [diff] [blame] | 695 | ASSERT_TRUE(ui_test_utils::SendKeyPressSync( |
[email protected] | a6e602f | 2010-09-28 22:28:30 | [diff] [blame] | 696 | browser(), app::VKEY_F, true, false, false, false)); |
[email protected] | 853300a8 | 2010-07-27 21:17:57 | [diff] [blame] | 697 | #endif |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 698 | |
| 699 | // See remark above on why we wait. |
[email protected] | fc2e087 | 2009-08-21 22:14:41 | [diff] [blame] | 700 | MessageLoop::current()->PostDelayedTask( |
| 701 | FROM_HERE, new MessageLoop::QuitTask(), kActionDelayMs); |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 702 | ui_test_utils::RunMessageLoop(); |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 703 | ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); |
[email protected] | 9bd491ee | 2008-12-10 22:31:07 | [diff] [blame] | 704 | } |
[email protected] | 401513c | 2009-03-12 00:21:28 | [diff] [blame] | 705 | |
| 706 | // Makes sure the focus is in the right location when opening the different |
| 707 | // types of tabs. |
[email protected] | a3f343f | 2010-10-06 23:39:42 | [diff] [blame] | 708 | IN_PROC_BROWSER_TEST_F(BrowserFocusTest, TabInitialFocus) { |
[email protected] | a6e602f | 2010-09-28 22:28:30 | [diff] [blame] | 709 | ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); |
[email protected] | 9ba21ede | 2010-07-30 01:11:07 | [diff] [blame] | 710 | |
[email protected] | 401513c | 2009-03-12 00:21:28 | [diff] [blame] | 711 | // Open the history tab, focus should be on the tab contents. |
| 712 | browser()->ShowHistoryTab(); |
[email protected] | a3f343f | 2010-10-06 23:39:42 | [diff] [blame] | 713 | ASSERT_NO_FATAL_FAILURE(ui_test_utils::WaitForLoadStop( |
| 714 | &browser()->GetSelectedTabContents()->controller())); |
| 715 | EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); |
[email protected] | 401513c | 2009-03-12 00:21:28 | [diff] [blame] | 716 | |
| 717 | // Open the new tab, focus should be on the location bar. |
| 718 | browser()->NewTab(); |
[email protected] | a3f343f | 2010-10-06 23:39:42 | [diff] [blame] | 719 | ASSERT_NO_FATAL_FAILURE(ui_test_utils::WaitForLoadStop( |
| 720 | &browser()->GetSelectedTabContents()->controller())); |
| 721 | EXPECT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR)); |
[email protected] | 401513c | 2009-03-12 00:21:28 | [diff] [blame] | 722 | |
| 723 | // Open the download tab, focus should be on the tab contents. |
| 724 | browser()->ShowDownloadsTab(); |
[email protected] | a3f343f | 2010-10-06 23:39:42 | [diff] [blame] | 725 | ASSERT_NO_FATAL_FAILURE(ui_test_utils::WaitForLoadStop( |
| 726 | &browser()->GetSelectedTabContents()->controller())); |
| 727 | EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); |
[email protected] | 3e3f0eb | 2009-06-22 18:33:43 | [diff] [blame] | 728 | |
| 729 | // Open about:blank, focus should be on the location bar. |
[email protected] | cecc93a | 2010-10-05 15:58:55 | [diff] [blame] | 730 | browser()->AddSelectedTabWithURL(GURL(chrome::kAboutBlankURL), |
| 731 | PageTransition::LINK); |
[email protected] | a3f343f | 2010-10-06 23:39:42 | [diff] [blame] | 732 | ASSERT_NO_FATAL_FAILURE(ui_test_utils::WaitForLoadStop( |
| 733 | &browser()->GetSelectedTabContents()->controller())); |
| 734 | EXPECT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR)); |
[email protected] | 401513c | 2009-03-12 00:21:28 | [diff] [blame] | 735 | } |
[email protected] | 9d8a464 | 2009-07-29 17:25:30 | [diff] [blame] | 736 | |
| 737 | // Tests that focus goes where expected when using reload. |
[email protected] | e4f4e0b | 2009-10-13 19:58:21 | [diff] [blame] | 738 | IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FocusOnReload) { |
[email protected] | a6e602f | 2010-09-28 22:28:30 | [diff] [blame] | 739 | ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 740 | ASSERT_TRUE(test_server()->Start()); |
[email protected] | 9d8a464 | 2009-07-29 17:25:30 | [diff] [blame] | 741 | |
[email protected] | 9d8a464 | 2009-07-29 17:25:30 | [diff] [blame] | 742 | // Open the new tab, reload. |
| 743 | browser()->NewTab(); |
[email protected] | f07d7bf | 2010-04-06 08:02:42 | [diff] [blame] | 744 | ui_test_utils::RunAllPendingInMessageLoop(); |
| 745 | |
[email protected] | cb84d64 | 2010-06-10 00:56:28 | [diff] [blame] | 746 | browser()->Reload(CURRENT_TAB); |
[email protected] | b7a20d3 | 2009-08-15 00:02:40 | [diff] [blame] | 747 | ASSERT_TRUE(ui_test_utils::WaitForNavigationInCurrentTab(browser())); |
[email protected] | 9d8a464 | 2009-07-29 17:25:30 | [diff] [blame] | 748 | // Focus should stay on the location bar. |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 749 | ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR)); |
[email protected] | 9d8a464 | 2009-07-29 17:25:30 | [diff] [blame] | 750 | |
| 751 | // Open a regular page, focus the location bar, reload. |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 752 | ui_test_utils::NavigateToURL(browser(), test_server()->GetURL(kSimplePage)); |
[email protected] | fc2e087 | 2009-08-21 22:14:41 | [diff] [blame] | 753 | browser()->FocusLocationBar(); |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 754 | ASSERT_TRUE(IsViewFocused(VIEW_ID_LOCATION_BAR)); |
[email protected] | cb84d64 | 2010-06-10 00:56:28 | [diff] [blame] | 755 | browser()->Reload(CURRENT_TAB); |
[email protected] | b7a20d3 | 2009-08-15 00:02:40 | [diff] [blame] | 756 | ASSERT_TRUE(ui_test_utils::WaitForNavigationInCurrentTab(browser())); |
[email protected] | 9ba21ede | 2010-07-30 01:11:07 | [diff] [blame] | 757 | |
[email protected] | 9d8a464 | 2009-07-29 17:25:30 | [diff] [blame] | 758 | // Focus should now be on the tab contents. |
[email protected] | fc2e087 | 2009-08-21 22:14:41 | [diff] [blame] | 759 | browser()->ShowDownloadsTab(); |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 760 | ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); |
[email protected] | 9d8a464 | 2009-07-29 17:25:30 | [diff] [blame] | 761 | } |
| 762 | |
[email protected] | 0e7c82ed | 2010-08-31 20:39:19 | [diff] [blame] | 763 | #if (defined(OS_CHROMEOS) || defined(OS_LINUX)) && !defined(NDEBUG) |
| 764 | // Hangy, http://crbug.com/50025. |
[email protected] | c38a190 | 2010-07-23 20:55:15 | [diff] [blame] | 765 | #define MAYBE_FocusOnReloadCrashedTab DISABLED_FocusOnReloadCrashedTab |
[email protected] | 517d2fe7 | 2010-07-23 15:40:41 | [diff] [blame] | 766 | #else |
| 767 | #define MAYBE_FocusOnReloadCrashedTab FocusOnReloadCrashedTab |
| 768 | #endif |
| 769 | |
[email protected] | 9d8a464 | 2009-07-29 17:25:30 | [diff] [blame] | 770 | // Tests that focus goes where expected when using reload on a crashed tab. |
[email protected] | 517d2fe7 | 2010-07-23 15:40:41 | [diff] [blame] | 771 | IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusOnReloadCrashedTab) { |
[email protected] | a6e602f | 2010-09-28 22:28:30 | [diff] [blame] | 772 | ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 773 | ASSERT_TRUE(test_server()->Start()); |
[email protected] | 9d8a464 | 2009-07-29 17:25:30 | [diff] [blame] | 774 | |
[email protected] | 9d8a464 | 2009-07-29 17:25:30 | [diff] [blame] | 775 | // Open a regular page, crash, reload. |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 776 | ui_test_utils::NavigateToURL(browser(), test_server()->GetURL(kSimplePage)); |
[email protected] | 9d8a464 | 2009-07-29 17:25:30 | [diff] [blame] | 777 | ui_test_utils::CrashTab(browser()->GetSelectedTabContents()); |
[email protected] | cb84d64 | 2010-06-10 00:56:28 | [diff] [blame] | 778 | browser()->Reload(CURRENT_TAB); |
[email protected] | b7a20d3 | 2009-08-15 00:02:40 | [diff] [blame] | 779 | ASSERT_TRUE(ui_test_utils::WaitForNavigationInCurrentTab(browser())); |
[email protected] | 9ba21ede | 2010-07-30 01:11:07 | [diff] [blame] | 780 | |
[email protected] | 9d8a464 | 2009-07-29 17:25:30 | [diff] [blame] | 781 | // Focus should now be on the tab contents. |
[email protected] | fc2e087 | 2009-08-21 22:14:41 | [diff] [blame] | 782 | browser()->ShowDownloadsTab(); |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 783 | ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); |
[email protected] | 9d8a464 | 2009-07-29 17:25:30 | [diff] [blame] | 784 | } |
[email protected] | 629e034 | 2010-07-27 23:30:13 | [diff] [blame] | 785 | |
| 786 | } // namespace |