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