blob: a9f7a94d7402f5457474d56f45e5f35a9c45ca8d [file] [log] [blame]
[email protected]a9c060ca2012-01-05 20:43:411// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
[email protected]a9c060ca2012-01-05 20:43:415#include "base/bind.h"
thestig18dfb7a52014-08-26 10:44:046#include "base/files/file_util.h"
[email protected]f07d7bf2010-04-06 08:02:427#include "base/format_macros.h"
[email protected]3cc55ad2013-07-17 22:17:418#include "base/message_loop/message_loop.h"
[email protected]3985ba82010-07-29 21:44:129#include "base/path_service.h"
[email protected]11521182013-06-11 04:06:3610#include "base/strings/stringprintf.h"
[email protected]5846d582013-06-08 16:02:1211#include "base/strings/utf_string_conversions.h"
[email protected]dcc8fbc2013-07-12 00:54:0912#include "chrome/browser/chrome_notification_types.h"
[email protected]7b5dc002010-11-16 23:08:1013#include "chrome/browser/ui/browser.h"
[email protected]a37d4b02012-06-25 21:56:1014#include "chrome/browser/ui/browser_commands.h"
thestige80821242015-09-30 23:46:0815#include "chrome/browser/ui/browser_navigator_params.h"
[email protected]52877dbc62012-06-29 22:22:0316#include "chrome/browser/ui/browser_tabstrip.h"
[email protected]00070c732011-04-09 15:31:3317#include "chrome/browser/ui/browser_window.h"
[email protected]5d9cace72012-06-21 16:07:1218#include "chrome/browser/ui/chrome_pages.h"
[email protected]cfd33792014-05-23 20:39:3419#include "chrome/browser/ui/find_bar/find_bar_host_unittest_util.h"
[email protected]d57ce6a2014-07-03 15:39:2320#include "chrome/browser/ui/location_bar/location_bar.h"
[email protected]b56e2e32012-05-11 21:18:0421#include "chrome/browser/ui/tabs/tab_strip_model.h"
[email protected]6a3ec2312010-12-02 19:30:1922#include "chrome/browser/ui/view_ids.h"
[email protected]9e0c83a2009-05-06 19:44:3723#include "chrome/common/chrome_paths.h"
[email protected]cecc93a2010-10-05 15:58:5524#include "chrome/common/url_constants.h"
[email protected]af44e7fb2011-07-29 18:32:3225#include "chrome/test/base/in_process_browser_test.h"
[email protected]3ea1b182013-02-08 22:38:4126#include "chrome/test/base/interactive_test_utils.h"
[email protected]477ae052011-11-18 23:53:5727#include "chrome/test/base/ui_test_utils.h"
blundell3f0c8bb42015-08-03 18:55:0528#include "components/omnibox/browser/omnibox_edit_controller.h"
blundell7dbd3792015-08-05 15:14:1929#include "components/omnibox/browser/omnibox_edit_model.h"
30#include "components/omnibox/browser/omnibox_view.h"
[email protected]cadaec52012-02-08 21:53:1331#include "content/public/browser/interstitial_page.h"
[email protected]85f0a572012-02-07 22:20:1332#include "content/public/browser/interstitial_page_delegate.h"
[email protected]477ae052011-11-18 23:53:5733#include "content/public/browser/notification_service.h"
khannansf3b27192015-01-09 17:28:1734#include "content/public/browser/render_frame_host.h"
[email protected]9c1662b2012-03-06 15:44:3335#include "content/public/browser/render_view_host.h"
avif9ab5d942015-10-15 14:05:4436#include "content/public/browser/render_widget_host.h"
[email protected]5626b0892012-02-20 14:46:5837#include "content/public/browser/render_widget_host_view.h"
[email protected]6acde6352012-01-04 16:52:2038#include "content/public/browser/web_contents.h"
[email protected]7d478cb2012-07-24 17:19:4239#include "content/public/test/browser_test_utils.h"
[email protected]c1dffe82013-06-26 20:59:0540#include "net/test/embedded_test_server/embedded_test_server.h"
[email protected]853300a82010-07-27 21:17:5741
[email protected]e0ee4b42014-05-23 06:16:1342#if defined(OS_WIN)
[email protected]cfd33792014-05-23 20:39:3443#include "base/win/windows_version.h"
[email protected]e0ee4b42014-05-23 06:16:1344#endif
45
[email protected]eaabba22012-03-07 15:02:1146using content::RenderViewHost;
[email protected]4b19ea52012-01-02 20:15:2547using content::WebContents;
48
[email protected]3a3cf4d2011-12-09 00:39:3549#if defined(OS_LINUX) || defined(OS_MACOSX)
50// TODO(jcampan): http://crbug.com/23683 for linux.
51// TODO(suzhe): http://crbug.com/49737 for mac.
[email protected]e00ccc92012-11-01 17:32:3052#define MAYBE_TabsRememberFocusFindInPage DISABLED_TabsRememberFocusFindInPage
[email protected]853300a82010-07-27 21:17:5753#elif defined(OS_WIN)
[email protected]c92928672010-11-09 18:31:0754// Flaky, http://crbug.com/62537.
[email protected]4d2451652012-02-14 23:54:2655#define MAYBE_TabsRememberFocusFindInPage DISABLED_TabsRememberFocusFindInPage
[email protected]fc2e0872009-08-21 22:14:4156#endif
57
initial.commit09911bf2008-07-26 23:55:2958namespace {
59
mgiuca88867d32015-07-08 04:08:2460#if defined(OS_POSIX)
[email protected]8bcdec92009-02-25 16:15:1861// The delay waited in some cases where we don't have a notifications for an
62// action we take.
initial.commit09911bf2008-07-26 23:55:2963const int kActionDelayMs = 500;
mgiuca88867d32015-07-08 04:08:2464#endif
initial.commit09911bf2008-07-26 23:55:2965
[email protected]c1dffe82013-06-26 20:59:0566const char kSimplePage[] = "/focus/page_with_focus.html";
67const char kStealFocusPage[] = "/focus/page_steals_focus.html";
68const char kTypicalPage[] = "/focus/typical_page.html";
[email protected]a9c060ca2012-01-05 20:43:4169
[email protected]8bcdec92009-02-25 16:15:1870class BrowserFocusTest : public InProcessBrowserTest {
initial.commit09911bf2008-07-26 23:55:2971 public:
thestige80821242015-09-30 23:46:0872 // InProcessBrowserTest overrides:
dcheng5dd5ff62014-10-21 12:42:3873 void SetUpOnMainThread() override {
svaldeza01f7d92015-11-18 17:47:5674 ASSERT_TRUE(embedded_test_server()->Start());
thestige80821242015-09-30 23:46:0875 }
[email protected]cfd33792014-05-23 20:39:3476
[email protected]21abcc742009-10-23 02:52:0677 bool IsViewFocused(ViewID vid) {
78 return ui_test_utils::IsViewFocused(browser(), vid);
[email protected]b9821882009-08-17 22:25:1779 }
80
[email protected]fc2e0872009-08-21 22:14:4181 void ClickOnView(ViewID vid) {
[email protected]21abcc742009-10-23 02:52:0682 ui_test_utils::ClickOnView(browser(), vid);
[email protected]fc2e0872009-08-21 22:14:4183 }
[email protected]0b8fa8b2011-12-07 00:54:5284
[email protected]cfd33792014-05-23 20:39:3485 void TestFocusTraversal(RenderViewHost* render_view_host, bool reverse) {
86 const char kGetFocusedElementJS[] =
87 "window.domAutomationController.send(getFocusedElement());";
88 const char* kExpectedIDs[] = { "textEdit", "searchButton", "luckyButton",
89 "googleLink", "gmailLink", "gmapLink" };
90 SCOPED_TRACE(base::StringPrintf("TestFocusTraversal: reverse=%d", reverse));
91 ui::KeyboardCode key = ui::VKEY_TAB;
92#if defined(OS_MACOSX)
93 // TODO(msw): Mac requires ui::VKEY_BACKTAB for reverse cycling. Sigh...
94 key = reverse ? ui::VKEY_BACKTAB : ui::VKEY_TAB;
95#elif defined(OS_WIN)
96 // This loop times out on Windows XP with no output. http://crbug.com/376635
97 if (base::win::GetVersion() < base::win::VERSION_VISTA)
98 return;
99#endif
100
101 // Loop through the focus chain twice for good measure.
102 for (size_t i = 0; i < 2; ++i) {
103 SCOPED_TRACE(base::StringPrintf("focus outer loop: %" PRIuS, i));
104 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
miu5d0cfcac2014-10-20 20:46:42105
[email protected]cfd33792014-05-23 20:39:34106 // Mac requires an extra Tab key press to traverse the app menu button
miu5d0cfcac2014-10-20 20:46:42107 // iff "Full Keyboard Access" is enabled. In reverse, four Tab key presses
108 // are required to traverse the back/forward buttons and the tab strip.
109#if defined(OS_MACOSX)
110 if (ui_controls::IsFullKeyboardAccessEnabled()) {
111 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
112 browser(), key, false, reverse, false, false));
113 if (reverse) {
114 for (int j = 0; j < 3; ++j) {
115 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
116 browser(), key, false, reverse, false, false));
117 }
118 }
119 }
120#endif
[email protected]cfd33792014-05-23 20:39:34121
palmere67e1742015-12-11 18:54:55122 if (reverse) {
123 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait(
124 browser(), key, false, reverse, false, false,
125 content::NOTIFICATION_ALL,
126 content::NotificationService::AllSources()));
127 }
128
[email protected]cfd33792014-05-23 20:39:34129 for (size_t j = 0; j < arraysize(kExpectedIDs); ++j) {
130 SCOPED_TRACE(base::StringPrintf("focus inner loop %" PRIuS, j));
131 const size_t index = reverse ? arraysize(kExpectedIDs) - 1 - j : j;
132 // The details are the node's editable state, i.e. true for "textEdit".
133 bool is_editable_node = index == 0;
134
135 // Press Tab (or Shift+Tab) and check the focused element id.
136 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWaitWithDetails(
137 browser(), key, false, reverse, false, false,
138 content::NOTIFICATION_FOCUS_CHANGED_IN_PAGE,
139 content::Source<RenderViewHost>(render_view_host),
140 content::Details<bool>(&is_editable_node)));
141 std::string focused_id;
142 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
143 render_view_host, kGetFocusedElementJS, &focused_id));
144 EXPECT_STREQ(kExpectedIDs[index], focused_id.c_str());
145 }
146
147#if defined(OS_MACOSX)
148 // TODO(msw): Mac doesn't post NOTIFICATION_FOCUS_RETURNED_TO_BROWSER and
149 // would also apparently require extra Tab key presses here. Sigh...
150 chrome::FocusLocationBar(browser());
151#else
152 // On the last Tab key press, focus returns to the browser.
153 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait(
154 browser(), key, false, reverse, false, false,
155 chrome::NOTIFICATION_FOCUS_RETURNED_TO_BROWSER,
156 content::Source<Browser>(browser())));
palmere67e1742015-12-11 18:54:55157 EXPECT_TRUE(
158 IsViewFocused(reverse ? VIEW_ID_OMNIBOX : VIEW_ID_LOCATION_ICON));
159
160 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait(
161 browser(), key, false, reverse, false, false,
162 content::NOTIFICATION_ALL,
163 content::NotificationService::AllSources()));
[email protected]cfd33792014-05-23 20:39:34164#endif
165 content::RunAllPendingInMessageLoop();
palmere67e1742015-12-11 18:54:55166 EXPECT_TRUE(
167 IsViewFocused(reverse ? VIEW_ID_LOCATION_ICON : VIEW_ID_OMNIBOX));
168 if (reverse) {
169 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait(
170 browser(), key, false, false, false, false,
171 content::NOTIFICATION_ALL,
172 content::NotificationService::AllSources()));
173 }
[email protected]cfd33792014-05-23 20:39:34174 }
[email protected]a9c060ca2012-01-05 20:43:41175 }
initial.commit09911bf2008-07-26 23:55:29176};
177
[email protected]cfd33792014-05-23 20:39:34178// A test interstitial page with typical HTML contents.
179class TestInterstitialPage : public content::InterstitialPageDelegate {
180 public:
181 explicit TestInterstitialPage(WebContents* tab) {
182 base::FilePath file_path;
183 bool success = PathService::Get(chrome::DIR_TEST_DATA, &file_path);
184 EXPECT_TRUE(success);
185 file_path = file_path.AppendASCII("focus/typical_page.html");
186 success = base::ReadFileToString(file_path, &html_contents_);
187 EXPECT_TRUE(success);
188 interstitial_page_ = content::InterstitialPage::Create(
189 tab, true, GURL("http://interstitial.com"), this);
190
191 // Show the interstitial and delay return until it has attached.
192 interstitial_page_->Show();
meacerb47bc8782014-10-07 03:40:01193 content::WaitForInterstitialAttach(tab);
194
[email protected]cfd33792014-05-23 20:39:34195 EXPECT_TRUE(tab->ShowingInterstitialPage());
[email protected]e0ee4b42014-05-23 06:16:13196 }
[email protected]ca9077f2014-05-22 19:00:21197
dcheng5dd5ff62014-10-21 12:42:38198 std::string GetHTMLContents() override { return html_contents_; }
[email protected]cfd33792014-05-23 20:39:34199
[email protected]85f0a572012-02-07 22:20:13200 RenderViewHost* render_view_host() {
khannansf3b27192015-01-09 17:28:17201 return interstitial_page_->GetMainFrame()->GetRenderViewHost();
[email protected]9e0c83a2009-05-06 19:44:37202 }
203
[email protected]cfd33792014-05-23 20:39:34204 void DontProceed() { interstitial_page_->DontProceed(); }
[email protected]4e85c112011-01-26 22:27:21205
avif9ab5d942015-10-15 14:05:44206 bool HasFocus() {
207 return render_view_host()->GetWidget()->GetView()->HasFocus();
208 }
[email protected]9e0c83a2009-05-06 19:44:37209
210 private:
211 std::string html_contents_;
[email protected]cfd33792014-05-23 20:39:34212 content::InterstitialPage* interstitial_page_; // Owns this.
213 DISALLOW_COPY_AND_ASSIGN(TestInterstitialPage);
[email protected]9e0c83a2009-05-06 19:44:37214};
[email protected]b9821882009-08-17 22:25:17215
sky4df637772015-10-22 17:20:18216// Flaky on Mac (http://crbug.com/67301).
217#if defined(OS_MACOSX)
[email protected]839e0802012-05-18 14:45:23218#define MAYBE_ClickingMovesFocus DISABLED_ClickingMovesFocus
[email protected]c39e233d2012-05-18 13:24:12219#else
sky4df637772015-10-22 17:20:18220// If this flakes, disable and log details in http://crbug.com/523255.
[email protected]839e0802012-05-18 14:45:23221#define MAYBE_ClickingMovesFocus ClickingMovesFocus
[email protected]c39e233d2012-05-18 13:24:12222#endif
223IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_ClickingMovesFocus) {
[email protected]a6e602f2010-09-28 22:28:30224 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]0ff0ff32010-12-21 19:34:42225#if defined(OS_POSIX)
[email protected]fc2e0872009-08-21 22:14:41226 // It seems we have to wait a little bit for the widgets to spin up before
227 // we can start clicking on them.
skyostil380bb2222015-06-12 12:07:05228 base::MessageLoop::current()->task_runner()->PostDelayedTask(
ki.stfuc4f8e242015-10-09 20:40:20229 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure(),
[email protected]7e560102012-03-08 20:58:42230 base::TimeDelta::FromMilliseconds(kActionDelayMs));
[email protected]729eb632012-07-26 04:45:26231 content::RunMessageLoop();
[email protected]0ff0ff32010-12-21 19:34:42232#endif // defined(OS_POSIX)
[email protected]fc2e0872009-08-21 22:14:41233
[email protected]5c4e4482013-03-03 00:09:18234 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
[email protected]186f13f2009-08-19 20:34:00235
[email protected]fc2e0872009-08-21 22:14:41236 ClickOnView(VIEW_ID_TAB_CONTAINER);
[email protected]f2159ba2012-04-17 19:13:21237 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]186f13f2009-08-19 20:34:00238
[email protected]5c4e4482013-03-03 00:09:18239 ClickOnView(VIEW_ID_OMNIBOX);
240 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
[email protected]186f13f2009-08-19 20:34:00241}
[email protected]186f13f2009-08-19 20:34:00242
[email protected]320948e02011-01-10 08:21:12243// Flaky, http://crbug.com/69034.
[email protected]4d2451652012-02-14 23:54:26244IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_BrowsersRememberFocus) {
[email protected]a6e602f2010-09-28 22:28:30245 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]cfd33792014-05-23 20:39:34246 const GURL url = embedded_test_server()->GetURL(kSimplePage);
[email protected]8bcdec92009-02-25 16:15:18247 ui_test_utils::NavigateToURL(browser(), url);
initial.commit09911bf2008-07-26 23:55:29248
[email protected]90556dd2012-06-07 20:26:18249 gfx::NativeWindow window = browser()->window()->GetNativeWindow();
[email protected]186f13f2009-08-19 20:34:00250
initial.commit09911bf2008-07-26 23:55:29251 // The focus should be on the Tab contents.
[email protected]f2159ba2012-04-17 19:13:21252 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
initial.commit09911bf2008-07-26 23:55:29253 // Now hide the window, show it again, the focus should not have changed.
[email protected]853300a82010-07-27 21:17:57254 ui_test_utils::HideNativeWindow(window);
[email protected]7807088c2011-11-09 15:16:29255 ASSERT_TRUE(ui_test_utils::ShowAndFocusNativeWindow(window));
[email protected]f2159ba2012-04-17 19:13:21256 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
initial.commit09911bf2008-07-26 23:55:29257
[email protected]a37d4b02012-06-25 21:56:10258 chrome::FocusLocationBar(browser());
[email protected]5c4e4482013-03-03 00:09:18259 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
initial.commit09911bf2008-07-26 23:55:29260 // Hide the window, show it again, the focus should not have changed.
[email protected]853300a82010-07-27 21:17:57261 ui_test_utils::HideNativeWindow(window);
[email protected]7807088c2011-11-09 15:16:29262 ASSERT_TRUE(ui_test_utils::ShowAndFocusNativeWindow(window));
[email protected]5c4e4482013-03-03 00:09:18263 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
initial.commit09911bf2008-07-26 23:55:29264}
265
266// Tabs remember focus.
[email protected]26153732010-11-09 18:47:39267// Disabled, http://crbug.com/62542.
268IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_TabsRememberFocus) {
[email protected]a6e602f2010-09-28 22:28:30269 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]cfd33792014-05-23 20:39:34270 const GURL url = embedded_test_server()->GetURL(kSimplePage);
[email protected]8bcdec92009-02-25 16:15:18271 ui_test_utils::NavigateToURL(browser(), url);
272
initial.commit09911bf2008-07-26 23:55:29273 // Create several tabs.
[email protected]52877dbc62012-06-29 22:22:03274 for (int i = 0; i < 4; ++i) {
275 chrome::AddSelectedTabWithURL(browser(), url,
Sylvain Defresnec6ccc77d2014-09-19 10:19:35276 ui::PAGE_TRANSITION_TYPED);
[email protected]52877dbc62012-06-29 22:22:03277 }
initial.commit09911bf2008-07-26 23:55:29278
279 // Alternate focus for the tab.
280 const bool kFocusPage[3][5] = {
281 { true, true, true, true, false },
282 { false, false, false, false, false },
283 { false, true, false, true, false }
284 };
285
286 for (int i = 1; i < 3; i++) {
287 for (int j = 0; j < 5; j++) {
[email protected]8bcdec92009-02-25 16:15:18288 // Activate the tab.
[email protected]57892182012-12-03 19:15:39289 browser()->tab_strip_model()->ActivateTabAt(j, true);
initial.commit09911bf2008-07-26 23:55:29290
291 // Activate the location bar or the page.
[email protected]7e383692009-06-12 19:14:54292 if (kFocusPage[i][j]) {
[email protected]fc2b46b2014-05-03 16:33:45293 browser()->tab_strip_model()->GetWebContentsAt(j)->Focus();
[email protected]7e383692009-06-12 19:14:54294 } else {
[email protected]a37d4b02012-06-25 21:56:10295 chrome::FocusLocationBar(browser());
[email protected]7e383692009-06-12 19:14:54296 }
initial.commit09911bf2008-07-26 23:55:29297 }
298
299 // Now come back to the tab and check the right view is focused.
300 for (int j = 0; j < 5; j++) {
[email protected]8bcdec92009-02-25 16:15:18301 // Activate the tab.
[email protected]57892182012-12-03 19:15:39302 browser()->tab_strip_model()->ActivateTabAt(j, true);
initial.commit09911bf2008-07-26 23:55:29303
[email protected]5c4e4482013-03-03 00:09:18304 ViewID vid = kFocusPage[i][j] ? VIEW_ID_TAB_CONTAINER : VIEW_ID_OMNIBOX;
[email protected]21abcc742009-10-23 02:52:06305 ASSERT_TRUE(IsViewFocused(vid));
initial.commit09911bf2008-07-26 23:55:29306 }
[email protected]cb7e2542009-12-14 22:02:35307
[email protected]57892182012-12-03 19:15:39308 browser()->tab_strip_model()->ActivateTabAt(0, true);
[email protected]cb7e2542009-12-14 22:02:35309 // Try the above, but with ctrl+tab. Since tab normally changes focus,
310 // this has regressed in the past. Loop through several times to be sure.
311 for (int j = 0; j < 15; j++) {
[email protected]f2159ba2012-04-17 19:13:21312 ViewID vid = kFocusPage[i][j % 5] ? VIEW_ID_TAB_CONTAINER :
[email protected]5c4e4482013-03-03 00:09:18313 VIEW_ID_OMNIBOX;
[email protected]cb7e2542009-12-14 22:02:35314 ASSERT_TRUE(IsViewFocused(vid));
315
[email protected]1d000682010-08-23 16:21:28316 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09317 browser(), ui::VKEY_TAB, true, false, false, false));
[email protected]cb7e2542009-12-14 22:02:35318 }
319
320 // As above, but with ctrl+shift+tab.
[email protected]57892182012-12-03 19:15:39321 browser()->tab_strip_model()->ActivateTabAt(4, true);
[email protected]cb7e2542009-12-14 22:02:35322 for (int j = 14; j >= 0; --j) {
[email protected]f2159ba2012-04-17 19:13:21323 ViewID vid = kFocusPage[i][j % 5] ? VIEW_ID_TAB_CONTAINER :
[email protected]5c4e4482013-03-03 00:09:18324 VIEW_ID_OMNIBOX;
[email protected]cb7e2542009-12-14 22:02:35325 ASSERT_TRUE(IsViewFocused(vid));
326
[email protected]1d000682010-08-23 16:21:28327 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09328 browser(), ui::VKEY_TAB, true, true, false, false));
[email protected]cb7e2542009-12-14 22:02:35329 }
initial.commit09911bf2008-07-26 23:55:29330 }
331}
332
[email protected]ae40b572009-10-02 21:17:45333// Tabs remember focus with find-in-page box.
[email protected]cb7e2542009-12-14 22:02:35334IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_TabsRememberFocusFindInPage) {
[email protected]a6e602f2010-09-28 22:28:30335 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]cfd33792014-05-23 20:39:34336 const GURL url = embedded_test_server()->GetURL(kSimplePage);
[email protected]ae40b572009-10-02 21:17:45337 ui_test_utils::NavigateToURL(browser(), url);
338
[email protected]a37d4b02012-06-25 21:56:10339 chrome::Find(browser());
[email protected]cc872372013-01-28 21:57:07340 ui_test_utils::FindInPage(
341 browser()->tab_strip_model()->GetActiveWebContents(),
[email protected]1f136902013-12-24 23:26:45342 base::ASCIIToUTF16("a"), true, false, NULL, NULL);
[email protected]21abcc742009-10-23 02:52:06343 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]ae40b572009-10-02 21:17:45344
345 // Focus the location bar.
[email protected]a37d4b02012-06-25 21:56:10346 chrome::FocusLocationBar(browser());
[email protected]ae40b572009-10-02 21:17:45347
348 // Create a 2nd tab.
Sylvain Defresnec6ccc77d2014-09-19 10:19:35349 chrome::AddSelectedTabWithURL(browser(), url, ui::PAGE_TRANSITION_TYPED);
[email protected]ae40b572009-10-02 21:17:45350
351 // Focus should be on the recently opened tab page.
[email protected]f2159ba2012-04-17 19:13:21352 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]ae40b572009-10-02 21:17:45353
354 // Select 1st tab, focus should still be on the location-bar.
355 // (bug http://crbug.com/23296)
[email protected]57892182012-12-03 19:15:39356 browser()->tab_strip_model()->ActivateTabAt(0, true);
[email protected]5c4e4482013-03-03 00:09:18357 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
[email protected]ae40b572009-10-02 21:17:45358
359 // Now open the find box again, switch to another tab and come back, the focus
360 // should return to the find box.
[email protected]a37d4b02012-06-25 21:56:10361 chrome::Find(browser());
[email protected]21abcc742009-10-23 02:52:06362 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]57892182012-12-03 19:15:39363 browser()->tab_strip_model()->ActivateTabAt(1, true);
[email protected]f2159ba2012-04-17 19:13:21364 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]57892182012-12-03 19:15:39365 browser()->tab_strip_model()->ActivateTabAt(0, true);
[email protected]21abcc742009-10-23 02:52:06366 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]ae40b572009-10-02 21:17:45367}
368
initial.commit09911bf2008-07-26 23:55:29369// Background window does not steal focus.
[email protected]c92928672010-11-09 18:31:07370// Flaky, http://crbug.com/62538.
371IN_PROC_BROWSER_TEST_F(BrowserFocusTest,
[email protected]4d2451652012-02-14 23:54:26372 DISABLED_BackgroundBrowserDontStealFocus) {
[email protected]a6e602f2010-09-28 22:28:30373 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
initial.commit09911bf2008-07-26 23:55:29374
initial.commit09911bf2008-07-26 23:55:29375 // Open a new browser window.
[email protected]c125cdc2013-02-25 19:10:21376 Browser* browser2 =
377 new Browser(Browser::CreateParams(browser()->profile(),
378 browser()->host_desktop_type()));
[email protected]8bcdec92009-02-25 16:15:18379 ASSERT_TRUE(browser2);
[email protected]00509a32013-11-17 17:45:37380 chrome::AddTabAt(browser2, GURL(), -1, true);
[email protected]8bcdec92009-02-25 16:15:18381 browser2->window()->Show();
[email protected]186f13f2009-08-19 20:34:00382
[email protected]ed179ee2009-10-03 21:02:51383 Browser* focused_browser = NULL;
384 Browser* unfocused_browser = NULL;
[email protected]753efc42010-03-09 19:52:16385#if defined(USE_X11)
386 // On X11, calling Activate() is not guaranteed to move focus, so we have
[email protected]186f13f2009-08-19 20:34:00387 // to figure out which browser does have focus.
388 if (browser2->window()->IsActive()) {
389 focused_browser = browser2;
390 unfocused_browser = browser();
391 } else if (browser()->window()->IsActive()) {
392 focused_browser = browser();
393 unfocused_browser = browser2;
394 } else {
[email protected]579c3d82010-10-06 03:53:51395 FAIL() << "Could not determine which browser has focus";
[email protected]186f13f2009-08-19 20:34:00396 }
397#elif defined(OS_WIN)
398 focused_browser = browser();
399 unfocused_browser = browser2;
[email protected]853300a82010-07-27 21:17:57400#elif defined(OS_MACOSX)
401 // On Mac, the newly created window always gets the focus.
402 focused_browser = browser2;
403 unfocused_browser = browser();
[email protected]186f13f2009-08-19 20:34:00404#endif
405
[email protected]cfd33792014-05-23 20:39:34406 const GURL steal_focus_url = embedded_test_server()->GetURL(kStealFocusPage);
[email protected]186f13f2009-08-19 20:34:00407 ui_test_utils::NavigateToURL(unfocused_browser, steal_focus_url);
[email protected]1e187af2009-02-25 02:02:46408
[email protected]8bcdec92009-02-25 16:15:18409 // Activate the first browser.
[email protected]186f13f2009-08-19 20:34:00410 focused_browser->window()->Activate();
initial.commit09911bf2008-07-26 23:55:29411
[email protected]b6987e02013-01-04 18:30:43412 ASSERT_TRUE(content::ExecuteScript(
[email protected]da2bfa42013-01-29 23:47:05413 unfocused_browser->tab_strip_model()->GetActiveWebContents(),
[email protected]06bc5d92013-01-02 22:44:13414 "stealFocus();"));
initial.commit09911bf2008-07-26 23:55:29415
[email protected]8bcdec92009-02-25 16:15:18416 // Make sure the first browser is still active.
[email protected]186f13f2009-08-19 20:34:00417 EXPECT_TRUE(focused_browser->window()->IsActive());
initial.commit09911bf2008-07-26 23:55:29418}
419
[email protected]ddabf402013-10-22 22:43:08420#if defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(USE_AURA)
421// TODO(erg): http://crbug.com/163931
422#define MAYBE_LocationBarLockFocus DISABLED_LocationBarLockFocus
423#else
424#define MAYBE_LocationBarLockFocus LocationBarLockFocus
425#endif
426
initial.commit09911bf2008-07-26 23:55:29427// Page cannot steal focus when focus is on location bar.
[email protected]ddabf402013-10-22 22:43:08428IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_LocationBarLockFocus) {
[email protected]a6e602f2010-09-28 22:28:30429 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
initial.commit09911bf2008-07-26 23:55:29430
431 // Open the page that steals focus.
[email protected]cfd33792014-05-23 20:39:34432 const GURL url = embedded_test_server()->GetURL(kStealFocusPage);
[email protected]8bcdec92009-02-25 16:15:18433 ui_test_utils::NavigateToURL(browser(), url);
initial.commit09911bf2008-07-26 23:55:29434
[email protected]a37d4b02012-06-25 21:56:10435 chrome::FocusLocationBar(browser());
initial.commit09911bf2008-07-26 23:55:29436
[email protected]b6987e02013-01-04 18:30:43437 ASSERT_TRUE(content::ExecuteScript(
[email protected]cc872372013-01-28 21:57:07438 browser()->tab_strip_model()->GetActiveWebContents(),
[email protected]06bc5d92013-01-02 22:44:13439 "stealFocus();"));
initial.commit09911bf2008-07-26 23:55:29440
441 // Make sure the location bar is still focused.
[email protected]5c4e4482013-03-03 00:09:18442 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
initial.commit09911bf2008-07-26 23:55:29443}
444
[email protected]cfd33792014-05-23 20:39:34445// Test forward and reverse focus traversal on a typical page.
bnc0f8fe372014-11-24 19:42:51446// Disabled for Mac because it is flaky on "Mac10.9 Tests (dbg)",
447// see https://crbug.com/60973.
448#if defined(OS_MACOSX)
449#define MAYBE_FocusTraversal DISABLED_FocusTraversal
450#else
451#define MAYBE_FocusTraversal FocusTraversal
452#endif
453IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusTraversal) {
[email protected]a6e602f2010-09-28 22:28:30454 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]cfd33792014-05-23 20:39:34455 const GURL url = embedded_test_server()->GetURL(kTypicalPage);
[email protected]8bcdec92009-02-25 16:15:18456 ui_test_utils::NavigateToURL(browser(), url);
[email protected]cfd33792014-05-23 20:39:34457 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]a37d4b02012-06-25 21:56:10458 chrome::FocusLocationBar(browser());
initial.commit09911bf2008-07-26 23:55:29459
[email protected]cfd33792014-05-23 20:39:34460 WebContents* tab = browser()->tab_strip_model()->GetActiveWebContents();
461 EXPECT_NO_FATAL_FAILURE(TestFocusTraversal(tab->GetRenderViewHost(), false));
462 EXPECT_NO_FATAL_FAILURE(TestFocusTraversal(tab->GetRenderViewHost(), true));
initial.commit09911bf2008-07-26 23:55:29463}
464
[email protected]cfd33792014-05-23 20:39:34465// Test forward and reverse focus traversal while an interstitial is showing.
[email protected]6d731ed2014-06-02 14:53:29466// Disabled, see http://crbug.com/60973
thestige80821242015-09-30 23:46:08467IN_PROC_BROWSER_TEST_F(BrowserFocusTest,
468 DISABLED_FocusTraversalOnInterstitial) {
[email protected]a6e602f2010-09-28 22:28:30469 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]cfd33792014-05-23 20:39:34470 const GURL url = embedded_test_server()->GetURL(kSimplePage);
[email protected]9e0c83a2009-05-06 19:44:37471 ui_test_utils::NavigateToURL(browser(), url);
[email protected]cfd33792014-05-23 20:39:34472 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]9e0c83a2009-05-06 19:44:37473
[email protected]cfd33792014-05-23 20:39:34474 // Create and show a test interstitial page.
[email protected]cc872372013-01-28 21:57:07475 TestInterstitialPage* interstitial_page = new TestInterstitialPage(
[email protected]cfd33792014-05-23 20:39:34476 browser()->tab_strip_model()->GetActiveWebContents());
477 content::RenderViewHost* host = interstitial_page->render_view_host();
[email protected]9e0c83a2009-05-06 19:44:37478
[email protected]cfd33792014-05-23 20:39:34479 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]a37d4b02012-06-25 21:56:10480 chrome::FocusLocationBar(browser());
[email protected]cfd33792014-05-23 20:39:34481 EXPECT_NO_FATAL_FAILURE(TestFocusTraversal(host, false));
482 EXPECT_NO_FATAL_FAILURE(TestFocusTraversal(host, true));
[email protected]9e0c83a2009-05-06 19:44:37483}
484
[email protected]cfd33792014-05-23 20:39:34485// Test the transfer of focus when an interstitial is shown and hidden.
486IN_PROC_BROWSER_TEST_F(BrowserFocusTest, InterstitialFocus) {
[email protected]a6e602f2010-09-28 22:28:30487 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]cfd33792014-05-23 20:39:34488 const GURL url = embedded_test_server()->GetURL(kSimplePage);
[email protected]e0ee4b42014-05-23 06:16:13489 ui_test_utils::NavigateToURL(browser(), url);
[email protected]cfd33792014-05-23 20:39:34490 WebContents* tab = browser()->tab_strip_model()->GetActiveWebContents();
491 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
avif9ab5d942015-10-15 14:05:44492 EXPECT_TRUE(tab->GetRenderViewHost()->GetWidget()->GetView()->HasFocus());
[email protected]e0ee4b42014-05-23 06:16:13493
[email protected]cfd33792014-05-23 20:39:34494 // Create and show a test interstitial page; it should gain focus.
495 TestInterstitialPage* interstitial_page = new TestInterstitialPage(tab);
496 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]9e0c83a2009-05-06 19:44:37497 EXPECT_TRUE(interstitial_page->HasFocus());
498
[email protected]cfd33792014-05-23 20:39:34499 // Hide the interstitial; the original page should gain focus.
[email protected]9e0c83a2009-05-06 19:44:37500 interstitial_page->DontProceed();
[email protected]cfd33792014-05-23 20:39:34501 content::RunAllPendingInMessageLoop();
502 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
avif9ab5d942015-10-15 14:05:44503 EXPECT_TRUE(tab->GetRenderViewHost()->GetWidget()->GetView()->HasFocus());
[email protected]9e0c83a2009-05-06 19:44:37504}
505
[email protected]cfd33792014-05-23 20:39:34506// Test that find-in-page UI can request focus, even when it is already open.
sky4df637772015-10-22 17:20:18507#if defined(OS_MACOSX)
John Abd-El-Malekf3fdd622014-09-09 04:04:23508#define MAYBE_FindFocusTest DISABLED_FindFocusTest
509#else
sky4df637772015-10-22 17:20:18510// If this flakes, disable and log details in http://crbug.com/523255.
John Abd-El-Malekf3fdd622014-09-09 04:04:23511#define MAYBE_FindFocusTest FindFocusTest
512#endif
513IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FindFocusTest) {
[email protected]cfd33792014-05-23 20:39:34514 chrome::DisableFindBarAnimationsDuringTesting(true);
[email protected]a6e602f2010-09-28 22:28:30515 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]cfd33792014-05-23 20:39:34516 const GURL url = embedded_test_server()->GetURL(kTypicalPage);
[email protected]e0ee4b42014-05-23 06:16:13517 ui_test_utils::NavigateToURL(browser(), url);
[email protected]cfd33792014-05-23 20:39:34518 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]e0ee4b42014-05-23 06:16:13519
[email protected]cfd33792014-05-23 20:39:34520 chrome::ShowFindBar(browser());
521 EXPECT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]9bd491ee2008-12-10 22:31:07522
[email protected]a37d4b02012-06-25 21:56:10523 chrome::FocusLocationBar(browser());
[email protected]cfd33792014-05-23 20:39:34524 EXPECT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
[email protected]9bd491ee2008-12-10 22:31:07525
[email protected]cfd33792014-05-23 20:39:34526 chrome::ShowFindBar(browser());
527 EXPECT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]9bd491ee2008-12-10 22:31:07528
[email protected]fc2e0872009-08-21 22:14:41529 ClickOnView(VIEW_ID_TAB_CONTAINER);
[email protected]cfd33792014-05-23 20:39:34530 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]9bd491ee2008-12-10 22:31:07531
[email protected]cfd33792014-05-23 20:39:34532 chrome::ShowFindBar(browser());
533 EXPECT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]9bd491ee2008-12-10 22:31:07534}
[email protected]401513c2009-03-12 00:21:28535
536// Makes sure the focus is in the right location when opening the different
537// types of tabs.
[email protected]c92928672010-11-09 18:31:07538// Flaky, http://crbug.com/62539.
[email protected]4d2451652012-02-14 23:54:26539IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_TabInitialFocus) {
[email protected]a6e602f2010-09-28 22:28:30540 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]9ba21ede2010-07-30 01:11:07541
[email protected]401513c2009-03-12 00:21:28542 // Open the history tab, focus should be on the tab contents.
[email protected]5d9cace72012-06-21 16:07:12543 chrome::ShowHistory(browser());
[email protected]6ddc6e3f22012-07-26 01:15:35544 ASSERT_NO_FATAL_FAILURE(content::WaitForLoadStop(
[email protected]cc872372013-01-28 21:57:07545 browser()->tab_strip_model()->GetActiveWebContents()));
[email protected]f2159ba2012-04-17 19:13:21546 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]401513c2009-03-12 00:21:28547
548 // Open the new tab, focus should be on the location bar.
[email protected]a37d4b02012-06-25 21:56:10549 chrome::NewTab(browser());
[email protected]6ddc6e3f22012-07-26 01:15:35550 ASSERT_NO_FATAL_FAILURE(content::WaitForLoadStop(
[email protected]cc872372013-01-28 21:57:07551 browser()->tab_strip_model()->GetActiveWebContents()));
[email protected]5c4e4482013-03-03 00:09:18552 EXPECT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
[email protected]401513c2009-03-12 00:21:28553
554 // Open the download tab, focus should be on the tab contents.
[email protected]5d9cace72012-06-21 16:07:12555 chrome::ShowDownloads(browser());
[email protected]6ddc6e3f22012-07-26 01:15:35556 ASSERT_NO_FATAL_FAILURE(content::WaitForLoadStop(
[email protected]cc872372013-01-28 21:57:07557 browser()->tab_strip_model()->GetActiveWebContents()));
[email protected]f2159ba2012-04-17 19:13:21558 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]3e3f0eb2009-06-22 18:33:43559
560 // Open about:blank, focus should be on the location bar.
[email protected]8e09c7af2014-06-10 11:46:17561 chrome::AddSelectedTabWithURL(
Sylvain Defresnec6ccc77d2014-09-19 10:19:35562 browser(), GURL(url::kAboutBlankURL), ui::PAGE_TRANSITION_LINK);
[email protected]6ddc6e3f22012-07-26 01:15:35563 ASSERT_NO_FATAL_FAILURE(content::WaitForLoadStop(
[email protected]cc872372013-01-28 21:57:07564 browser()->tab_strip_model()->GetActiveWebContents()));
[email protected]5c4e4482013-03-03 00:09:18565 EXPECT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
[email protected]401513c2009-03-12 00:21:28566}
[email protected]9d8a4642009-07-29 17:25:30567
[email protected]ddabf402013-10-22 22:43:08568#if defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(USE_AURA)
569// TODO(erg): http://crbug.com/163931
570#define MAYBE_FocusOnReload DISABLED_FocusOnReload
571#else
572#define MAYBE_FocusOnReload FocusOnReload
573#endif
574
[email protected]9d8a4642009-07-29 17:25:30575// Tests that focus goes where expected when using reload.
[email protected]ddabf402013-10-22 22:43:08576IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusOnReload) {
[email protected]a6e602f2010-09-28 22:28:30577 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]9d8a4642009-07-29 17:25:30578
[email protected]9d8a4642009-07-29 17:25:30579 // Open the new tab, reload.
[email protected]6b4e8e42011-08-17 19:36:06580 {
[email protected]a7fe9112012-07-20 02:34:45581 content::WindowedNotificationObserver observer(
[email protected]6b4e8e42011-08-17 19:36:06582 content::NOTIFICATION_LOAD_STOP,
[email protected]ad50def52011-10-19 23:17:07583 content::NotificationService::AllSources());
[email protected]a37d4b02012-06-25 21:56:10584 chrome::NewTab(browser());
[email protected]6b4e8e42011-08-17 19:36:06585 observer.Wait();
586 }
[email protected]b8deecd2012-07-30 21:09:44587 content::RunAllPendingInMessageLoop();
[email protected]f07d7bf2010-04-06 08:02:42588
[email protected]6b4e8e42011-08-17 19:36:06589 {
[email protected]a7fe9112012-07-20 02:34:45590 content::WindowedNotificationObserver observer(
[email protected]6b4e8e42011-08-17 19:36:06591 content::NOTIFICATION_LOAD_STOP,
[email protected]cfd33792014-05-23 20:39:34592 content::Source<content::NavigationController>(
[email protected]cc872372013-01-28 21:57:07593 &browser()->tab_strip_model()->GetActiveWebContents()->
594 GetController()));
[email protected]a37d4b02012-06-25 21:56:10595 chrome::Reload(browser(), CURRENT_TAB);
[email protected]6b4e8e42011-08-17 19:36:06596 observer.Wait();
597 }
[email protected]9d8a4642009-07-29 17:25:30598 // Focus should stay on the location bar.
[email protected]5c4e4482013-03-03 00:09:18599 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
[email protected]9d8a4642009-07-29 17:25:30600
601 // Open a regular page, focus the location bar, reload.
[email protected]c1dffe82013-06-26 20:59:05602 ui_test_utils::NavigateToURL(browser(),
603 embedded_test_server()->GetURL(kSimplePage));
[email protected]a37d4b02012-06-25 21:56:10604 chrome::FocusLocationBar(browser());
[email protected]5c4e4482013-03-03 00:09:18605 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
[email protected]6b4e8e42011-08-17 19:36:06606 {
[email protected]a7fe9112012-07-20 02:34:45607 content::WindowedNotificationObserver observer(
[email protected]6b4e8e42011-08-17 19:36:06608 content::NOTIFICATION_LOAD_STOP,
[email protected]cfd33792014-05-23 20:39:34609 content::Source<content::NavigationController>(
[email protected]cc872372013-01-28 21:57:07610 &browser()->tab_strip_model()->GetActiveWebContents()->
611 GetController()));
[email protected]a37d4b02012-06-25 21:56:10612 chrome::Reload(browser(), CURRENT_TAB);
[email protected]6b4e8e42011-08-17 19:36:06613 observer.Wait();
614 }
[email protected]9ba21ede2010-07-30 01:11:07615
[email protected]9d8a4642009-07-29 17:25:30616 // Focus should now be on the tab contents.
[email protected]5d9cace72012-06-21 16:07:12617 chrome::ShowDownloads(browser());
[email protected]f2159ba2012-04-17 19:13:21618 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]9d8a4642009-07-29 17:25:30619}
620
621// Tests that focus goes where expected when using reload on a crashed tab.
[email protected]160f29ad2010-10-28 15:43:27622IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_FocusOnReloadCrashedTab) {
[email protected]a6e602f2010-09-28 22:28:30623 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]9d8a4642009-07-29 17:25:30624
[email protected]9d8a4642009-07-29 17:25:30625 // Open a regular page, crash, reload.
[email protected]c1dffe82013-06-26 20:59:05626 ui_test_utils::NavigateToURL(browser(),
627 embedded_test_server()->GetURL(kSimplePage));
[email protected]cc872372013-01-28 21:57:07628 content::CrashTab(browser()->tab_strip_model()->GetActiveWebContents());
[email protected]6b4e8e42011-08-17 19:36:06629 {
[email protected]a7fe9112012-07-20 02:34:45630 content::WindowedNotificationObserver observer(
[email protected]6b4e8e42011-08-17 19:36:06631 content::NOTIFICATION_LOAD_STOP,
[email protected]cfd33792014-05-23 20:39:34632 content::Source<content::NavigationController>(
[email protected]cc872372013-01-28 21:57:07633 &browser()->tab_strip_model()->GetActiveWebContents()->
634 GetController()));
[email protected]a37d4b02012-06-25 21:56:10635 chrome::Reload(browser(), CURRENT_TAB);
[email protected]6b4e8e42011-08-17 19:36:06636 observer.Wait();
637 }
[email protected]9ba21ede2010-07-30 01:11:07638
[email protected]9d8a4642009-07-29 17:25:30639 // Focus should now be on the tab contents.
[email protected]5d9cace72012-06-21 16:07:12640 chrome::ShowDownloads(browser());
[email protected]f2159ba2012-04-17 19:13:21641 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]9d8a4642009-07-29 17:25:30642}
[email protected]629e0342010-07-27 23:30:13643
[email protected]09475062013-10-21 21:29:56644// Tests that focus goes to frame after crashed tab.
645// TODO(shrikant): Find out where the focus should be deterministically.
646// Currently focused_view after crash seem to be non null in debug mode
647// (invalidated pointer 0xcccccc).
648IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_FocusAfterCrashedTab) {
649 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]09475062013-10-21 21:29:56650
651 content::CrashTab(browser()->tab_strip_model()->GetActiveWebContents());
652
653 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
654}
655
[email protected]895a84722012-11-29 18:45:44656// Tests that when a new tab is opened from the omnibox, the focus is moved from
657// the omnibox for the current tab.
[email protected]cfd33792014-05-23 20:39:34658IN_PROC_BROWSER_TEST_F(BrowserFocusTest, NavigateFromOmniboxIntoNewTab) {
[email protected]895a84722012-11-29 18:45:44659 GURL url("http://www.google.com/");
660 GURL url2("http://maps.google.com/");
661
662 // Navigate to url.
Sylvain Defresnec6ccc77d2014-09-19 10:19:35663 chrome::NavigateParams p(browser(), url, ui::PAGE_TRANSITION_LINK);
[email protected]895a84722012-11-29 18:45:44664 p.window_action = chrome::NavigateParams::SHOW_WINDOW;
665 p.disposition = CURRENT_TAB;
666 chrome::Navigate(&p);
667
668 // Focus the omnibox.
669 chrome::FocusLocationBar(browser());
670
[email protected]cfd33792014-05-23 20:39:34671 OmniboxEditController* controller = browser()->window()->GetLocationBar()->
672 GetOmniboxView()->model()->controller();
[email protected]895a84722012-11-29 18:45:44673
674 // Simulate an alt-enter.
675 controller->OnAutocompleteAccept(url2, NEW_FOREGROUND_TAB,
Sylvain Defresnec6ccc77d2014-09-19 10:19:35676 ui::PAGE_TRANSITION_TYPED);
[email protected]895a84722012-11-29 18:45:44677
678 // Make sure the second tab is selected.
[email protected]04e3ead52013-01-14 01:09:20679 EXPECT_EQ(1, browser()->tab_strip_model()->active_index());
[email protected]895a84722012-11-29 18:45:44680
681 // The tab contents should have the focus in the second tab.
[email protected]cfd33792014-05-23 20:39:34682 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]895a84722012-11-29 18:45:44683
684 // Go back to the first tab. The focus should not be in the omnibox.
685 chrome::SelectPreviousTab(browser());
[email protected]04e3ead52013-01-14 01:09:20686 EXPECT_EQ(0, browser()->tab_strip_model()->active_index());
[email protected]cfd33792014-05-23 20:39:34687 EXPECT_FALSE(IsViewFocused(VIEW_ID_OMNIBOX));
[email protected]895a84722012-11-29 18:45:44688}
689
[email protected]19efce52013-11-14 00:42:28690// This functionality is currently broken. http://crbug.com/304865.
691//
692//#if defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(USE_AURA)
693//// TODO(erg): http://crbug.com/163931
694//#define MAYBE_FocusOnNavigate DISABLED_FocusOnNavigate
695//#else
696//#define MAYBE_FocusOnNavigate FocusOnNavigate
697//#endif
[email protected]ddabf402013-10-22 22:43:08698
[email protected]19efce52013-11-14 00:42:28699IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_FocusOnNavigate) {
[email protected]c744f7d22013-04-12 20:29:04700 // Needed on Mac.
701 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
702 // Load the NTP.
[email protected]19506d542013-10-15 23:11:06703 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUINewTabURL));
[email protected]c744f7d22013-04-12 20:29:04704 EXPECT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
705
706 // Navigate to another page.
707 const base::FilePath::CharType* kEmptyFile = FILE_PATH_LITERAL("empty.html");
708 GURL file_url(ui_test_utils::GetTestUrl(base::FilePath(
709 base::FilePath::kCurrentDirectory), base::FilePath(kEmptyFile)));
710 ui_test_utils::NavigateToURL(browser(), file_url);
711
712 ClickOnView(VIEW_ID_TAB_CONTAINER);
713
714 // Navigate back. Should focus the location bar.
715 {
716 content::WindowedNotificationObserver back_nav_observer(
717 content::NOTIFICATION_NAV_ENTRY_COMMITTED,
718 content::NotificationService::AllSources());
719 chrome::GoBack(browser(), CURRENT_TAB);
720 back_nav_observer.Wait();
721 }
722
723 EXPECT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
724
725 // Navigate forward. Shouldn't focus the location bar.
726 ClickOnView(VIEW_ID_TAB_CONTAINER);
727 {
728 content::WindowedNotificationObserver forward_nav_observer(
729 content::NOTIFICATION_NAV_ENTRY_COMMITTED,
730 content::NotificationService::AllSources());
731 chrome::GoForward(browser(), CURRENT_TAB);
732 forward_nav_observer.Wait();
733 }
734
735 EXPECT_FALSE(IsViewFocused(VIEW_ID_OMNIBOX));
736}
737
[email protected]629e0342010-07-27 23:30:13738} // namespace