blob: f034b13885b9537f3a2e79632fd801e1834c44b7 [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
avi655876a2015-12-25 07:18:155#include <stddef.h>
6
[email protected]a9c060ca2012-01-05 20:43:417#include "base/bind.h"
thestig18dfb7a52014-08-26 10:44:048#include "base/files/file_util.h"
[email protected]f07d7bf2010-04-06 08:02:429#include "base/format_macros.h"
avi655876a2015-12-25 07:18:1510#include "base/macros.h"
[email protected]3cc55ad2013-07-17 22:17:4111#include "base/message_loop/message_loop.h"
[email protected]3985ba82010-07-29 21:44:1212#include "base/path_service.h"
[email protected]11521182013-06-11 04:06:3613#include "base/strings/stringprintf.h"
[email protected]5846d582013-06-08 16:02:1214#include "base/strings/utf_string_conversions.h"
avi655876a2015-12-25 07:18:1515#include "build/build_config.h"
[email protected]dcc8fbc2013-07-12 00:54:0916#include "chrome/browser/chrome_notification_types.h"
[email protected]7b5dc002010-11-16 23:08:1017#include "chrome/browser/ui/browser.h"
[email protected]a37d4b02012-06-25 21:56:1018#include "chrome/browser/ui/browser_commands.h"
thestige80821242015-09-30 23:46:0819#include "chrome/browser/ui/browser_navigator_params.h"
[email protected]52877dbc62012-06-29 22:22:0320#include "chrome/browser/ui/browser_tabstrip.h"
[email protected]00070c732011-04-09 15:31:3321#include "chrome/browser/ui/browser_window.h"
[email protected]5d9cace72012-06-21 16:07:1222#include "chrome/browser/ui/chrome_pages.h"
[email protected]cfd33792014-05-23 20:39:3423#include "chrome/browser/ui/find_bar/find_bar_host_unittest_util.h"
[email protected]d57ce6a2014-07-03 15:39:2324#include "chrome/browser/ui/location_bar/location_bar.h"
[email protected]b56e2e32012-05-11 21:18:0425#include "chrome/browser/ui/tabs/tab_strip_model.h"
[email protected]6a3ec2312010-12-02 19:30:1926#include "chrome/browser/ui/view_ids.h"
[email protected]9e0c83a2009-05-06 19:44:3727#include "chrome/common/chrome_paths.h"
[email protected]cecc93a2010-10-05 15:58:5528#include "chrome/common/url_constants.h"
[email protected]af44e7fb2011-07-29 18:32:3229#include "chrome/test/base/in_process_browser_test.h"
[email protected]3ea1b182013-02-08 22:38:4130#include "chrome/test/base/interactive_test_utils.h"
[email protected]477ae052011-11-18 23:53:5731#include "chrome/test/base/ui_test_utils.h"
blundell3f0c8bb42015-08-03 18:55:0532#include "components/omnibox/browser/omnibox_edit_controller.h"
blundell7dbd3792015-08-05 15:14:1933#include "components/omnibox/browser/omnibox_edit_model.h"
34#include "components/omnibox/browser/omnibox_view.h"
[email protected]cadaec52012-02-08 21:53:1335#include "content/public/browser/interstitial_page.h"
[email protected]85f0a572012-02-07 22:20:1336#include "content/public/browser/interstitial_page_delegate.h"
[email protected]477ae052011-11-18 23:53:5737#include "content/public/browser/notification_service.h"
khannansf3b27192015-01-09 17:28:1738#include "content/public/browser/render_frame_host.h"
[email protected]9c1662b2012-03-06 15:44:3339#include "content/public/browser/render_view_host.h"
avif9ab5d942015-10-15 14:05:4440#include "content/public/browser/render_widget_host.h"
[email protected]5626b0892012-02-20 14:46:5841#include "content/public/browser/render_widget_host_view.h"
[email protected]6acde6352012-01-04 16:52:2042#include "content/public/browser/web_contents.h"
[email protected]7d478cb2012-07-24 17:19:4243#include "content/public/test/browser_test_utils.h"
[email protected]c1dffe82013-06-26 20:59:0544#include "net/test/embedded_test_server/embedded_test_server.h"
[email protected]853300a82010-07-27 21:17:5745
[email protected]e0ee4b42014-05-23 06:16:1346#if defined(OS_WIN)
[email protected]cfd33792014-05-23 20:39:3447#include "base/win/windows_version.h"
[email protected]e0ee4b42014-05-23 06:16:1348#endif
49
[email protected]eaabba22012-03-07 15:02:1150using content::RenderViewHost;
[email protected]4b19ea52012-01-02 20:15:2551using content::WebContents;
52
[email protected]3a3cf4d2011-12-09 00:39:3553#if defined(OS_LINUX) || defined(OS_MACOSX)
54// TODO(jcampan): http://crbug.com/23683 for linux.
55// TODO(suzhe): http://crbug.com/49737 for mac.
[email protected]e00ccc92012-11-01 17:32:3056#define MAYBE_TabsRememberFocusFindInPage DISABLED_TabsRememberFocusFindInPage
[email protected]853300a82010-07-27 21:17:5757#elif defined(OS_WIN)
[email protected]c92928672010-11-09 18:31:0758// Flaky, http://crbug.com/62537.
[email protected]4d2451652012-02-14 23:54:2659#define MAYBE_TabsRememberFocusFindInPage DISABLED_TabsRememberFocusFindInPage
[email protected]fc2e0872009-08-21 22:14:4160#endif
61
initial.commit09911bf2008-07-26 23:55:2962namespace {
63
mgiuca88867d32015-07-08 04:08:2464#if defined(OS_POSIX)
[email protected]8bcdec92009-02-25 16:15:1865// The delay waited in some cases where we don't have a notifications for an
66// action we take.
initial.commit09911bf2008-07-26 23:55:2967const int kActionDelayMs = 500;
mgiuca88867d32015-07-08 04:08:2468#endif
initial.commit09911bf2008-07-26 23:55:2969
[email protected]c1dffe82013-06-26 20:59:0570const char kSimplePage[] = "/focus/page_with_focus.html";
71const char kStealFocusPage[] = "/focus/page_steals_focus.html";
72const char kTypicalPage[] = "/focus/typical_page.html";
[email protected]a9c060ca2012-01-05 20:43:4173
[email protected]8bcdec92009-02-25 16:15:1874class BrowserFocusTest : public InProcessBrowserTest {
initial.commit09911bf2008-07-26 23:55:2975 public:
thestige80821242015-09-30 23:46:0876 // InProcessBrowserTest overrides:
dcheng5dd5ff62014-10-21 12:42:3877 void SetUpOnMainThread() override {
svaldeza01f7d92015-11-18 17:47:5678 ASSERT_TRUE(embedded_test_server()->Start());
thestige80821242015-09-30 23:46:0879 }
[email protected]cfd33792014-05-23 20:39:3480
[email protected]21abcc742009-10-23 02:52:0681 bool IsViewFocused(ViewID vid) {
82 return ui_test_utils::IsViewFocused(browser(), vid);
[email protected]b9821882009-08-17 22:25:1783 }
84
[email protected]fc2e0872009-08-21 22:14:4185 void ClickOnView(ViewID vid) {
[email protected]21abcc742009-10-23 02:52:0686 ui_test_utils::ClickOnView(browser(), vid);
[email protected]fc2e0872009-08-21 22:14:4187 }
[email protected]0b8fa8b2011-12-07 00:54:5288
[email protected]cfd33792014-05-23 20:39:3489 void TestFocusTraversal(RenderViewHost* render_view_host, bool reverse) {
90 const char kGetFocusedElementJS[] =
91 "window.domAutomationController.send(getFocusedElement());";
92 const char* kExpectedIDs[] = { "textEdit", "searchButton", "luckyButton",
93 "googleLink", "gmailLink", "gmapLink" };
94 SCOPED_TRACE(base::StringPrintf("TestFocusTraversal: reverse=%d", reverse));
95 ui::KeyboardCode key = ui::VKEY_TAB;
96#if defined(OS_MACOSX)
97 // TODO(msw): Mac requires ui::VKEY_BACKTAB for reverse cycling. Sigh...
98 key = reverse ? ui::VKEY_BACKTAB : ui::VKEY_TAB;
99#elif defined(OS_WIN)
100 // This loop times out on Windows XP with no output. http://crbug.com/376635
101 if (base::win::GetVersion() < base::win::VERSION_VISTA)
102 return;
103#endif
104
105 // Loop through the focus chain twice for good measure.
106 for (size_t i = 0; i < 2; ++i) {
107 SCOPED_TRACE(base::StringPrintf("focus outer loop: %" PRIuS, i));
108 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
miu5d0cfcac2014-10-20 20:46:42109
[email protected]cfd33792014-05-23 20:39:34110 // Mac requires an extra Tab key press to traverse the app menu button
miu5d0cfcac2014-10-20 20:46:42111 // iff "Full Keyboard Access" is enabled. In reverse, four Tab key presses
112 // are required to traverse the back/forward buttons and the tab strip.
113#if defined(OS_MACOSX)
114 if (ui_controls::IsFullKeyboardAccessEnabled()) {
115 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
116 browser(), key, false, reverse, false, false));
117 if (reverse) {
118 for (int j = 0; j < 3; ++j) {
119 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
120 browser(), key, false, reverse, false, false));
121 }
122 }
123 }
124#endif
[email protected]cfd33792014-05-23 20:39:34125
palmere67e1742015-12-11 18:54:55126 if (reverse) {
127 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait(
128 browser(), key, false, reverse, false, false,
129 content::NOTIFICATION_ALL,
130 content::NotificationService::AllSources()));
131 }
132
[email protected]cfd33792014-05-23 20:39:34133 for (size_t j = 0; j < arraysize(kExpectedIDs); ++j) {
134 SCOPED_TRACE(base::StringPrintf("focus inner loop %" PRIuS, j));
135 const size_t index = reverse ? arraysize(kExpectedIDs) - 1 - j : j;
136 // The details are the node's editable state, i.e. true for "textEdit".
137 bool is_editable_node = index == 0;
138
139 // Press Tab (or Shift+Tab) and check the focused element id.
140 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWaitWithDetails(
141 browser(), key, false, reverse, false, false,
142 content::NOTIFICATION_FOCUS_CHANGED_IN_PAGE,
143 content::Source<RenderViewHost>(render_view_host),
144 content::Details<bool>(&is_editable_node)));
145 std::string focused_id;
146 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
147 render_view_host, kGetFocusedElementJS, &focused_id));
148 EXPECT_STREQ(kExpectedIDs[index], focused_id.c_str());
149 }
150
151#if defined(OS_MACOSX)
152 // TODO(msw): Mac doesn't post NOTIFICATION_FOCUS_RETURNED_TO_BROWSER and
153 // would also apparently require extra Tab key presses here. Sigh...
154 chrome::FocusLocationBar(browser());
155#else
156 // On the last Tab key press, focus returns to the browser.
157 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait(
158 browser(), key, false, reverse, false, false,
159 chrome::NOTIFICATION_FOCUS_RETURNED_TO_BROWSER,
160 content::Source<Browser>(browser())));
palmere67e1742015-12-11 18:54:55161 EXPECT_TRUE(
162 IsViewFocused(reverse ? VIEW_ID_OMNIBOX : VIEW_ID_LOCATION_ICON));
163
164 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait(
165 browser(), key, false, reverse, false, false,
166 content::NOTIFICATION_ALL,
167 content::NotificationService::AllSources()));
[email protected]cfd33792014-05-23 20:39:34168#endif
169 content::RunAllPendingInMessageLoop();
palmere67e1742015-12-11 18:54:55170 EXPECT_TRUE(
171 IsViewFocused(reverse ? VIEW_ID_LOCATION_ICON : VIEW_ID_OMNIBOX));
172 if (reverse) {
173 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait(
174 browser(), key, false, false, false, false,
175 content::NOTIFICATION_ALL,
176 content::NotificationService::AllSources()));
177 }
[email protected]cfd33792014-05-23 20:39:34178 }
[email protected]a9c060ca2012-01-05 20:43:41179 }
initial.commit09911bf2008-07-26 23:55:29180};
181
[email protected]cfd33792014-05-23 20:39:34182// A test interstitial page with typical HTML contents.
183class TestInterstitialPage : public content::InterstitialPageDelegate {
184 public:
185 explicit TestInterstitialPage(WebContents* tab) {
186 base::FilePath file_path;
187 bool success = PathService::Get(chrome::DIR_TEST_DATA, &file_path);
188 EXPECT_TRUE(success);
189 file_path = file_path.AppendASCII("focus/typical_page.html");
190 success = base::ReadFileToString(file_path, &html_contents_);
191 EXPECT_TRUE(success);
192 interstitial_page_ = content::InterstitialPage::Create(
193 tab, true, GURL("http://interstitial.com"), this);
194
195 // Show the interstitial and delay return until it has attached.
196 interstitial_page_->Show();
meacerb47bc8782014-10-07 03:40:01197 content::WaitForInterstitialAttach(tab);
198
[email protected]cfd33792014-05-23 20:39:34199 EXPECT_TRUE(tab->ShowingInterstitialPage());
[email protected]e0ee4b42014-05-23 06:16:13200 }
[email protected]ca9077f2014-05-22 19:00:21201
dcheng5dd5ff62014-10-21 12:42:38202 std::string GetHTMLContents() override { return html_contents_; }
[email protected]cfd33792014-05-23 20:39:34203
[email protected]85f0a572012-02-07 22:20:13204 RenderViewHost* render_view_host() {
khannansf3b27192015-01-09 17:28:17205 return interstitial_page_->GetMainFrame()->GetRenderViewHost();
[email protected]9e0c83a2009-05-06 19:44:37206 }
207
[email protected]cfd33792014-05-23 20:39:34208 void DontProceed() { interstitial_page_->DontProceed(); }
[email protected]4e85c112011-01-26 22:27:21209
avif9ab5d942015-10-15 14:05:44210 bool HasFocus() {
211 return render_view_host()->GetWidget()->GetView()->HasFocus();
212 }
[email protected]9e0c83a2009-05-06 19:44:37213
214 private:
215 std::string html_contents_;
[email protected]cfd33792014-05-23 20:39:34216 content::InterstitialPage* interstitial_page_; // Owns this.
217 DISALLOW_COPY_AND_ASSIGN(TestInterstitialPage);
[email protected]9e0c83a2009-05-06 19:44:37218};
[email protected]b9821882009-08-17 22:25:17219
sky4df637772015-10-22 17:20:18220// Flaky on Mac (http://crbug.com/67301).
221#if defined(OS_MACOSX)
[email protected]839e0802012-05-18 14:45:23222#define MAYBE_ClickingMovesFocus DISABLED_ClickingMovesFocus
[email protected]c39e233d2012-05-18 13:24:12223#else
sky4df637772015-10-22 17:20:18224// If this flakes, disable and log details in http://crbug.com/523255.
[email protected]839e0802012-05-18 14:45:23225#define MAYBE_ClickingMovesFocus ClickingMovesFocus
[email protected]c39e233d2012-05-18 13:24:12226#endif
227IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_ClickingMovesFocus) {
[email protected]a6e602f2010-09-28 22:28:30228 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]0ff0ff32010-12-21 19:34:42229#if defined(OS_POSIX)
[email protected]fc2e0872009-08-21 22:14:41230 // It seems we have to wait a little bit for the widgets to spin up before
231 // we can start clicking on them.
skyostil380bb2222015-06-12 12:07:05232 base::MessageLoop::current()->task_runner()->PostDelayedTask(
ki.stfuc4f8e242015-10-09 20:40:20233 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure(),
[email protected]7e560102012-03-08 20:58:42234 base::TimeDelta::FromMilliseconds(kActionDelayMs));
[email protected]729eb632012-07-26 04:45:26235 content::RunMessageLoop();
[email protected]0ff0ff32010-12-21 19:34:42236#endif // defined(OS_POSIX)
[email protected]fc2e0872009-08-21 22:14:41237
[email protected]5c4e4482013-03-03 00:09:18238 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
[email protected]186f13f2009-08-19 20:34:00239
[email protected]fc2e0872009-08-21 22:14:41240 ClickOnView(VIEW_ID_TAB_CONTAINER);
[email protected]f2159ba2012-04-17 19:13:21241 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]186f13f2009-08-19 20:34:00242
[email protected]5c4e4482013-03-03 00:09:18243 ClickOnView(VIEW_ID_OMNIBOX);
244 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
[email protected]186f13f2009-08-19 20:34:00245}
[email protected]186f13f2009-08-19 20:34:00246
[email protected]320948e02011-01-10 08:21:12247// Flaky, http://crbug.com/69034.
[email protected]4d2451652012-02-14 23:54:26248IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_BrowsersRememberFocus) {
[email protected]a6e602f2010-09-28 22:28:30249 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]cfd33792014-05-23 20:39:34250 const GURL url = embedded_test_server()->GetURL(kSimplePage);
[email protected]8bcdec92009-02-25 16:15:18251 ui_test_utils::NavigateToURL(browser(), url);
initial.commit09911bf2008-07-26 23:55:29252
[email protected]90556dd2012-06-07 20:26:18253 gfx::NativeWindow window = browser()->window()->GetNativeWindow();
[email protected]186f13f2009-08-19 20:34:00254
initial.commit09911bf2008-07-26 23:55:29255 // The focus should be on the Tab contents.
[email protected]f2159ba2012-04-17 19:13:21256 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
initial.commit09911bf2008-07-26 23:55:29257 // Now hide the window, show it again, the focus should not have changed.
[email protected]853300a82010-07-27 21:17:57258 ui_test_utils::HideNativeWindow(window);
[email protected]7807088c2011-11-09 15:16:29259 ASSERT_TRUE(ui_test_utils::ShowAndFocusNativeWindow(window));
[email protected]f2159ba2012-04-17 19:13:21260 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
initial.commit09911bf2008-07-26 23:55:29261
[email protected]a37d4b02012-06-25 21:56:10262 chrome::FocusLocationBar(browser());
[email protected]5c4e4482013-03-03 00:09:18263 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
initial.commit09911bf2008-07-26 23:55:29264 // Hide the window, show it again, the focus should not have changed.
[email protected]853300a82010-07-27 21:17:57265 ui_test_utils::HideNativeWindow(window);
[email protected]7807088c2011-11-09 15:16:29266 ASSERT_TRUE(ui_test_utils::ShowAndFocusNativeWindow(window));
[email protected]5c4e4482013-03-03 00:09:18267 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
initial.commit09911bf2008-07-26 23:55:29268}
269
270// Tabs remember focus.
[email protected]26153732010-11-09 18:47:39271// Disabled, http://crbug.com/62542.
272IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_TabsRememberFocus) {
[email protected]a6e602f2010-09-28 22:28:30273 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]cfd33792014-05-23 20:39:34274 const GURL url = embedded_test_server()->GetURL(kSimplePage);
[email protected]8bcdec92009-02-25 16:15:18275 ui_test_utils::NavigateToURL(browser(), url);
276
initial.commit09911bf2008-07-26 23:55:29277 // Create several tabs.
[email protected]52877dbc62012-06-29 22:22:03278 for (int i = 0; i < 4; ++i) {
279 chrome::AddSelectedTabWithURL(browser(), url,
Sylvain Defresnec6ccc77d2014-09-19 10:19:35280 ui::PAGE_TRANSITION_TYPED);
[email protected]52877dbc62012-06-29 22:22:03281 }
initial.commit09911bf2008-07-26 23:55:29282
283 // Alternate focus for the tab.
284 const bool kFocusPage[3][5] = {
285 { true, true, true, true, false },
286 { false, false, false, false, false },
287 { false, true, false, true, false }
288 };
289
290 for (int i = 1; i < 3; i++) {
291 for (int j = 0; j < 5; j++) {
[email protected]8bcdec92009-02-25 16:15:18292 // Activate the tab.
[email protected]57892182012-12-03 19:15:39293 browser()->tab_strip_model()->ActivateTabAt(j, true);
initial.commit09911bf2008-07-26 23:55:29294
295 // Activate the location bar or the page.
[email protected]7e383692009-06-12 19:14:54296 if (kFocusPage[i][j]) {
[email protected]fc2b46b2014-05-03 16:33:45297 browser()->tab_strip_model()->GetWebContentsAt(j)->Focus();
[email protected]7e383692009-06-12 19:14:54298 } else {
[email protected]a37d4b02012-06-25 21:56:10299 chrome::FocusLocationBar(browser());
[email protected]7e383692009-06-12 19:14:54300 }
initial.commit09911bf2008-07-26 23:55:29301 }
302
303 // Now come back to the tab and check the right view is focused.
304 for (int j = 0; j < 5; j++) {
[email protected]8bcdec92009-02-25 16:15:18305 // Activate the tab.
[email protected]57892182012-12-03 19:15:39306 browser()->tab_strip_model()->ActivateTabAt(j, true);
initial.commit09911bf2008-07-26 23:55:29307
[email protected]5c4e4482013-03-03 00:09:18308 ViewID vid = kFocusPage[i][j] ? VIEW_ID_TAB_CONTAINER : VIEW_ID_OMNIBOX;
[email protected]21abcc742009-10-23 02:52:06309 ASSERT_TRUE(IsViewFocused(vid));
initial.commit09911bf2008-07-26 23:55:29310 }
[email protected]cb7e2542009-12-14 22:02:35311
[email protected]57892182012-12-03 19:15:39312 browser()->tab_strip_model()->ActivateTabAt(0, true);
[email protected]cb7e2542009-12-14 22:02:35313 // Try the above, but with ctrl+tab. Since tab normally changes focus,
314 // this has regressed in the past. Loop through several times to be sure.
315 for (int j = 0; j < 15; j++) {
[email protected]f2159ba2012-04-17 19:13:21316 ViewID vid = kFocusPage[i][j % 5] ? VIEW_ID_TAB_CONTAINER :
[email protected]5c4e4482013-03-03 00:09:18317 VIEW_ID_OMNIBOX;
[email protected]cb7e2542009-12-14 22:02:35318 ASSERT_TRUE(IsViewFocused(vid));
319
[email protected]1d000682010-08-23 16:21:28320 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09321 browser(), ui::VKEY_TAB, true, false, false, false));
[email protected]cb7e2542009-12-14 22:02:35322 }
323
324 // As above, but with ctrl+shift+tab.
[email protected]57892182012-12-03 19:15:39325 browser()->tab_strip_model()->ActivateTabAt(4, true);
[email protected]cb7e2542009-12-14 22:02:35326 for (int j = 14; j >= 0; --j) {
[email protected]f2159ba2012-04-17 19:13:21327 ViewID vid = kFocusPage[i][j % 5] ? VIEW_ID_TAB_CONTAINER :
[email protected]5c4e4482013-03-03 00:09:18328 VIEW_ID_OMNIBOX;
[email protected]cb7e2542009-12-14 22:02:35329 ASSERT_TRUE(IsViewFocused(vid));
330
[email protected]1d000682010-08-23 16:21:28331 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09332 browser(), ui::VKEY_TAB, true, true, false, false));
[email protected]cb7e2542009-12-14 22:02:35333 }
initial.commit09911bf2008-07-26 23:55:29334 }
335}
336
[email protected]ae40b572009-10-02 21:17:45337// Tabs remember focus with find-in-page box.
[email protected]cb7e2542009-12-14 22:02:35338IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_TabsRememberFocusFindInPage) {
[email protected]a6e602f2010-09-28 22:28:30339 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]cfd33792014-05-23 20:39:34340 const GURL url = embedded_test_server()->GetURL(kSimplePage);
[email protected]ae40b572009-10-02 21:17:45341 ui_test_utils::NavigateToURL(browser(), url);
342
[email protected]a37d4b02012-06-25 21:56:10343 chrome::Find(browser());
[email protected]cc872372013-01-28 21:57:07344 ui_test_utils::FindInPage(
345 browser()->tab_strip_model()->GetActiveWebContents(),
[email protected]1f136902013-12-24 23:26:45346 base::ASCIIToUTF16("a"), true, false, NULL, NULL);
[email protected]21abcc742009-10-23 02:52:06347 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]ae40b572009-10-02 21:17:45348
349 // Focus the location bar.
[email protected]a37d4b02012-06-25 21:56:10350 chrome::FocusLocationBar(browser());
[email protected]ae40b572009-10-02 21:17:45351
352 // Create a 2nd tab.
Sylvain Defresnec6ccc77d2014-09-19 10:19:35353 chrome::AddSelectedTabWithURL(browser(), url, ui::PAGE_TRANSITION_TYPED);
[email protected]ae40b572009-10-02 21:17:45354
355 // Focus should be on the recently opened tab page.
[email protected]f2159ba2012-04-17 19:13:21356 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]ae40b572009-10-02 21:17:45357
358 // Select 1st tab, focus should still be on the location-bar.
359 // (bug http://crbug.com/23296)
[email protected]57892182012-12-03 19:15:39360 browser()->tab_strip_model()->ActivateTabAt(0, true);
[email protected]5c4e4482013-03-03 00:09:18361 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
[email protected]ae40b572009-10-02 21:17:45362
363 // Now open the find box again, switch to another tab and come back, the focus
364 // should return to the find box.
[email protected]a37d4b02012-06-25 21:56:10365 chrome::Find(browser());
[email protected]21abcc742009-10-23 02:52:06366 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]57892182012-12-03 19:15:39367 browser()->tab_strip_model()->ActivateTabAt(1, true);
[email protected]f2159ba2012-04-17 19:13:21368 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]57892182012-12-03 19:15:39369 browser()->tab_strip_model()->ActivateTabAt(0, true);
[email protected]21abcc742009-10-23 02:52:06370 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]ae40b572009-10-02 21:17:45371}
372
initial.commit09911bf2008-07-26 23:55:29373// Background window does not steal focus.
[email protected]c92928672010-11-09 18:31:07374// Flaky, http://crbug.com/62538.
375IN_PROC_BROWSER_TEST_F(BrowserFocusTest,
[email protected]4d2451652012-02-14 23:54:26376 DISABLED_BackgroundBrowserDontStealFocus) {
[email protected]a6e602f2010-09-28 22:28:30377 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
initial.commit09911bf2008-07-26 23:55:29378
initial.commit09911bf2008-07-26 23:55:29379 // Open a new browser window.
[email protected]c125cdc2013-02-25 19:10:21380 Browser* browser2 =
381 new Browser(Browser::CreateParams(browser()->profile(),
382 browser()->host_desktop_type()));
[email protected]8bcdec92009-02-25 16:15:18383 ASSERT_TRUE(browser2);
[email protected]00509a32013-11-17 17:45:37384 chrome::AddTabAt(browser2, GURL(), -1, true);
[email protected]8bcdec92009-02-25 16:15:18385 browser2->window()->Show();
[email protected]186f13f2009-08-19 20:34:00386
[email protected]ed179ee2009-10-03 21:02:51387 Browser* focused_browser = NULL;
388 Browser* unfocused_browser = NULL;
[email protected]753efc42010-03-09 19:52:16389#if defined(USE_X11)
390 // On X11, calling Activate() is not guaranteed to move focus, so we have
[email protected]186f13f2009-08-19 20:34:00391 // to figure out which browser does have focus.
392 if (browser2->window()->IsActive()) {
393 focused_browser = browser2;
394 unfocused_browser = browser();
395 } else if (browser()->window()->IsActive()) {
396 focused_browser = browser();
397 unfocused_browser = browser2;
398 } else {
[email protected]579c3d82010-10-06 03:53:51399 FAIL() << "Could not determine which browser has focus";
[email protected]186f13f2009-08-19 20:34:00400 }
401#elif defined(OS_WIN)
402 focused_browser = browser();
403 unfocused_browser = browser2;
[email protected]853300a82010-07-27 21:17:57404#elif defined(OS_MACOSX)
405 // On Mac, the newly created window always gets the focus.
406 focused_browser = browser2;
407 unfocused_browser = browser();
[email protected]186f13f2009-08-19 20:34:00408#endif
409
[email protected]cfd33792014-05-23 20:39:34410 const GURL steal_focus_url = embedded_test_server()->GetURL(kStealFocusPage);
[email protected]186f13f2009-08-19 20:34:00411 ui_test_utils::NavigateToURL(unfocused_browser, steal_focus_url);
[email protected]1e187af2009-02-25 02:02:46412
[email protected]8bcdec92009-02-25 16:15:18413 // Activate the first browser.
[email protected]186f13f2009-08-19 20:34:00414 focused_browser->window()->Activate();
initial.commit09911bf2008-07-26 23:55:29415
[email protected]b6987e02013-01-04 18:30:43416 ASSERT_TRUE(content::ExecuteScript(
[email protected]da2bfa42013-01-29 23:47:05417 unfocused_browser->tab_strip_model()->GetActiveWebContents(),
[email protected]06bc5d92013-01-02 22:44:13418 "stealFocus();"));
initial.commit09911bf2008-07-26 23:55:29419
[email protected]8bcdec92009-02-25 16:15:18420 // Make sure the first browser is still active.
[email protected]186f13f2009-08-19 20:34:00421 EXPECT_TRUE(focused_browser->window()->IsActive());
initial.commit09911bf2008-07-26 23:55:29422}
423
[email protected]ddabf402013-10-22 22:43:08424#if defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(USE_AURA)
425// TODO(erg): http://crbug.com/163931
426#define MAYBE_LocationBarLockFocus DISABLED_LocationBarLockFocus
427#else
428#define MAYBE_LocationBarLockFocus LocationBarLockFocus
429#endif
430
initial.commit09911bf2008-07-26 23:55:29431// Page cannot steal focus when focus is on location bar.
[email protected]ddabf402013-10-22 22:43:08432IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_LocationBarLockFocus) {
[email protected]a6e602f2010-09-28 22:28:30433 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
initial.commit09911bf2008-07-26 23:55:29434
435 // Open the page that steals focus.
[email protected]cfd33792014-05-23 20:39:34436 const GURL url = embedded_test_server()->GetURL(kStealFocusPage);
[email protected]8bcdec92009-02-25 16:15:18437 ui_test_utils::NavigateToURL(browser(), url);
initial.commit09911bf2008-07-26 23:55:29438
[email protected]a37d4b02012-06-25 21:56:10439 chrome::FocusLocationBar(browser());
initial.commit09911bf2008-07-26 23:55:29440
[email protected]b6987e02013-01-04 18:30:43441 ASSERT_TRUE(content::ExecuteScript(
[email protected]cc872372013-01-28 21:57:07442 browser()->tab_strip_model()->GetActiveWebContents(),
[email protected]06bc5d92013-01-02 22:44:13443 "stealFocus();"));
initial.commit09911bf2008-07-26 23:55:29444
445 // Make sure the location bar is still focused.
[email protected]5c4e4482013-03-03 00:09:18446 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
initial.commit09911bf2008-07-26 23:55:29447}
448
[email protected]cfd33792014-05-23 20:39:34449// Test forward and reverse focus traversal on a typical page.
bnc0f8fe372014-11-24 19:42:51450// Disabled for Mac because it is flaky on "Mac10.9 Tests (dbg)",
451// see https://crbug.com/60973.
452#if defined(OS_MACOSX)
453#define MAYBE_FocusTraversal DISABLED_FocusTraversal
454#else
455#define MAYBE_FocusTraversal FocusTraversal
456#endif
457IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusTraversal) {
[email protected]a6e602f2010-09-28 22:28:30458 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]cfd33792014-05-23 20:39:34459 const GURL url = embedded_test_server()->GetURL(kTypicalPage);
[email protected]8bcdec92009-02-25 16:15:18460 ui_test_utils::NavigateToURL(browser(), url);
[email protected]cfd33792014-05-23 20:39:34461 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]a37d4b02012-06-25 21:56:10462 chrome::FocusLocationBar(browser());
initial.commit09911bf2008-07-26 23:55:29463
[email protected]cfd33792014-05-23 20:39:34464 WebContents* tab = browser()->tab_strip_model()->GetActiveWebContents();
465 EXPECT_NO_FATAL_FAILURE(TestFocusTraversal(tab->GetRenderViewHost(), false));
466 EXPECT_NO_FATAL_FAILURE(TestFocusTraversal(tab->GetRenderViewHost(), true));
initial.commit09911bf2008-07-26 23:55:29467}
468
[email protected]cfd33792014-05-23 20:39:34469// Test forward and reverse focus traversal while an interstitial is showing.
[email protected]6d731ed2014-06-02 14:53:29470// Disabled, see http://crbug.com/60973
thestige80821242015-09-30 23:46:08471IN_PROC_BROWSER_TEST_F(BrowserFocusTest,
472 DISABLED_FocusTraversalOnInterstitial) {
[email protected]a6e602f2010-09-28 22:28:30473 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]cfd33792014-05-23 20:39:34474 const GURL url = embedded_test_server()->GetURL(kSimplePage);
[email protected]9e0c83a2009-05-06 19:44:37475 ui_test_utils::NavigateToURL(browser(), url);
[email protected]cfd33792014-05-23 20:39:34476 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]9e0c83a2009-05-06 19:44:37477
[email protected]cfd33792014-05-23 20:39:34478 // Create and show a test interstitial page.
[email protected]cc872372013-01-28 21:57:07479 TestInterstitialPage* interstitial_page = new TestInterstitialPage(
[email protected]cfd33792014-05-23 20:39:34480 browser()->tab_strip_model()->GetActiveWebContents());
481 content::RenderViewHost* host = interstitial_page->render_view_host();
[email protected]9e0c83a2009-05-06 19:44:37482
[email protected]cfd33792014-05-23 20:39:34483 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]a37d4b02012-06-25 21:56:10484 chrome::FocusLocationBar(browser());
[email protected]cfd33792014-05-23 20:39:34485 EXPECT_NO_FATAL_FAILURE(TestFocusTraversal(host, false));
486 EXPECT_NO_FATAL_FAILURE(TestFocusTraversal(host, true));
[email protected]9e0c83a2009-05-06 19:44:37487}
488
[email protected]cfd33792014-05-23 20:39:34489// Test the transfer of focus when an interstitial is shown and hidden.
490IN_PROC_BROWSER_TEST_F(BrowserFocusTest, InterstitialFocus) {
[email protected]a6e602f2010-09-28 22:28:30491 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]cfd33792014-05-23 20:39:34492 const GURL url = embedded_test_server()->GetURL(kSimplePage);
[email protected]e0ee4b42014-05-23 06:16:13493 ui_test_utils::NavigateToURL(browser(), url);
[email protected]cfd33792014-05-23 20:39:34494 WebContents* tab = browser()->tab_strip_model()->GetActiveWebContents();
495 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
avif9ab5d942015-10-15 14:05:44496 EXPECT_TRUE(tab->GetRenderViewHost()->GetWidget()->GetView()->HasFocus());
[email protected]e0ee4b42014-05-23 06:16:13497
[email protected]cfd33792014-05-23 20:39:34498 // Create and show a test interstitial page; it should gain focus.
499 TestInterstitialPage* interstitial_page = new TestInterstitialPage(tab);
500 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]9e0c83a2009-05-06 19:44:37501 EXPECT_TRUE(interstitial_page->HasFocus());
502
[email protected]cfd33792014-05-23 20:39:34503 // Hide the interstitial; the original page should gain focus.
[email protected]9e0c83a2009-05-06 19:44:37504 interstitial_page->DontProceed();
[email protected]cfd33792014-05-23 20:39:34505 content::RunAllPendingInMessageLoop();
506 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
avif9ab5d942015-10-15 14:05:44507 EXPECT_TRUE(tab->GetRenderViewHost()->GetWidget()->GetView()->HasFocus());
[email protected]9e0c83a2009-05-06 19:44:37508}
509
[email protected]cfd33792014-05-23 20:39:34510// Test that find-in-page UI can request focus, even when it is already open.
sky4df637772015-10-22 17:20:18511#if defined(OS_MACOSX)
John Abd-El-Malekf3fdd622014-09-09 04:04:23512#define MAYBE_FindFocusTest DISABLED_FindFocusTest
513#else
sky4df637772015-10-22 17:20:18514// If this flakes, disable and log details in http://crbug.com/523255.
John Abd-El-Malekf3fdd622014-09-09 04:04:23515#define MAYBE_FindFocusTest FindFocusTest
516#endif
517IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FindFocusTest) {
[email protected]cfd33792014-05-23 20:39:34518 chrome::DisableFindBarAnimationsDuringTesting(true);
[email protected]a6e602f2010-09-28 22:28:30519 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]cfd33792014-05-23 20:39:34520 const GURL url = embedded_test_server()->GetURL(kTypicalPage);
[email protected]e0ee4b42014-05-23 06:16:13521 ui_test_utils::NavigateToURL(browser(), url);
[email protected]cfd33792014-05-23 20:39:34522 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]e0ee4b42014-05-23 06:16:13523
[email protected]cfd33792014-05-23 20:39:34524 chrome::ShowFindBar(browser());
525 EXPECT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]9bd491ee2008-12-10 22:31:07526
[email protected]a37d4b02012-06-25 21:56:10527 chrome::FocusLocationBar(browser());
[email protected]cfd33792014-05-23 20:39:34528 EXPECT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
[email protected]9bd491ee2008-12-10 22:31:07529
[email protected]cfd33792014-05-23 20:39:34530 chrome::ShowFindBar(browser());
531 EXPECT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]9bd491ee2008-12-10 22:31:07532
[email protected]fc2e0872009-08-21 22:14:41533 ClickOnView(VIEW_ID_TAB_CONTAINER);
[email protected]cfd33792014-05-23 20:39:34534 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]9bd491ee2008-12-10 22:31:07535
[email protected]cfd33792014-05-23 20:39:34536 chrome::ShowFindBar(browser());
537 EXPECT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]9bd491ee2008-12-10 22:31:07538}
[email protected]401513c2009-03-12 00:21:28539
540// Makes sure the focus is in the right location when opening the different
541// types of tabs.
[email protected]c92928672010-11-09 18:31:07542// Flaky, http://crbug.com/62539.
[email protected]4d2451652012-02-14 23:54:26543IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_TabInitialFocus) {
[email protected]a6e602f2010-09-28 22:28:30544 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]9ba21ede2010-07-30 01:11:07545
[email protected]401513c2009-03-12 00:21:28546 // Open the history tab, focus should be on the tab contents.
[email protected]5d9cace72012-06-21 16:07:12547 chrome::ShowHistory(browser());
[email protected]6ddc6e3f22012-07-26 01:15:35548 ASSERT_NO_FATAL_FAILURE(content::WaitForLoadStop(
[email protected]cc872372013-01-28 21:57:07549 browser()->tab_strip_model()->GetActiveWebContents()));
[email protected]f2159ba2012-04-17 19:13:21550 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]401513c2009-03-12 00:21:28551
552 // Open the new tab, focus should be on the location bar.
[email protected]a37d4b02012-06-25 21:56:10553 chrome::NewTab(browser());
[email protected]6ddc6e3f22012-07-26 01:15:35554 ASSERT_NO_FATAL_FAILURE(content::WaitForLoadStop(
[email protected]cc872372013-01-28 21:57:07555 browser()->tab_strip_model()->GetActiveWebContents()));
[email protected]5c4e4482013-03-03 00:09:18556 EXPECT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
[email protected]401513c2009-03-12 00:21:28557
558 // Open the download tab, focus should be on the tab contents.
[email protected]5d9cace72012-06-21 16:07:12559 chrome::ShowDownloads(browser());
[email protected]6ddc6e3f22012-07-26 01:15:35560 ASSERT_NO_FATAL_FAILURE(content::WaitForLoadStop(
[email protected]cc872372013-01-28 21:57:07561 browser()->tab_strip_model()->GetActiveWebContents()));
[email protected]f2159ba2012-04-17 19:13:21562 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]3e3f0eb2009-06-22 18:33:43563
564 // Open about:blank, focus should be on the location bar.
[email protected]8e09c7af2014-06-10 11:46:17565 chrome::AddSelectedTabWithURL(
Sylvain Defresnec6ccc77d2014-09-19 10:19:35566 browser(), GURL(url::kAboutBlankURL), ui::PAGE_TRANSITION_LINK);
[email protected]6ddc6e3f22012-07-26 01:15:35567 ASSERT_NO_FATAL_FAILURE(content::WaitForLoadStop(
[email protected]cc872372013-01-28 21:57:07568 browser()->tab_strip_model()->GetActiveWebContents()));
[email protected]5c4e4482013-03-03 00:09:18569 EXPECT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
[email protected]401513c2009-03-12 00:21:28570}
[email protected]9d8a4642009-07-29 17:25:30571
[email protected]ddabf402013-10-22 22:43:08572#if defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(USE_AURA)
573// TODO(erg): http://crbug.com/163931
574#define MAYBE_FocusOnReload DISABLED_FocusOnReload
575#else
576#define MAYBE_FocusOnReload FocusOnReload
577#endif
578
[email protected]9d8a4642009-07-29 17:25:30579// Tests that focus goes where expected when using reload.
[email protected]ddabf402013-10-22 22:43:08580IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusOnReload) {
[email protected]a6e602f2010-09-28 22:28:30581 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]9d8a4642009-07-29 17:25:30582
[email protected]9d8a4642009-07-29 17:25:30583 // Open the new tab, reload.
[email protected]6b4e8e42011-08-17 19:36:06584 {
[email protected]a7fe9112012-07-20 02:34:45585 content::WindowedNotificationObserver observer(
[email protected]6b4e8e42011-08-17 19:36:06586 content::NOTIFICATION_LOAD_STOP,
[email protected]ad50def52011-10-19 23:17:07587 content::NotificationService::AllSources());
[email protected]a37d4b02012-06-25 21:56:10588 chrome::NewTab(browser());
[email protected]6b4e8e42011-08-17 19:36:06589 observer.Wait();
590 }
[email protected]b8deecd2012-07-30 21:09:44591 content::RunAllPendingInMessageLoop();
[email protected]f07d7bf2010-04-06 08:02:42592
[email protected]6b4e8e42011-08-17 19:36:06593 {
[email protected]a7fe9112012-07-20 02:34:45594 content::WindowedNotificationObserver observer(
[email protected]6b4e8e42011-08-17 19:36:06595 content::NOTIFICATION_LOAD_STOP,
[email protected]cfd33792014-05-23 20:39:34596 content::Source<content::NavigationController>(
[email protected]cc872372013-01-28 21:57:07597 &browser()->tab_strip_model()->GetActiveWebContents()->
598 GetController()));
[email protected]a37d4b02012-06-25 21:56:10599 chrome::Reload(browser(), CURRENT_TAB);
[email protected]6b4e8e42011-08-17 19:36:06600 observer.Wait();
601 }
[email protected]9d8a4642009-07-29 17:25:30602 // Focus should stay on the location bar.
[email protected]5c4e4482013-03-03 00:09:18603 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
[email protected]9d8a4642009-07-29 17:25:30604
605 // Open a regular page, focus the location bar, reload.
[email protected]c1dffe82013-06-26 20:59:05606 ui_test_utils::NavigateToURL(browser(),
607 embedded_test_server()->GetURL(kSimplePage));
[email protected]a37d4b02012-06-25 21:56:10608 chrome::FocusLocationBar(browser());
[email protected]5c4e4482013-03-03 00:09:18609 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
[email protected]6b4e8e42011-08-17 19:36:06610 {
[email protected]a7fe9112012-07-20 02:34:45611 content::WindowedNotificationObserver observer(
[email protected]6b4e8e42011-08-17 19:36:06612 content::NOTIFICATION_LOAD_STOP,
[email protected]cfd33792014-05-23 20:39:34613 content::Source<content::NavigationController>(
[email protected]cc872372013-01-28 21:57:07614 &browser()->tab_strip_model()->GetActiveWebContents()->
615 GetController()));
[email protected]a37d4b02012-06-25 21:56:10616 chrome::Reload(browser(), CURRENT_TAB);
[email protected]6b4e8e42011-08-17 19:36:06617 observer.Wait();
618 }
[email protected]9ba21ede2010-07-30 01:11:07619
[email protected]9d8a4642009-07-29 17:25:30620 // Focus should now be on the tab contents.
[email protected]5d9cace72012-06-21 16:07:12621 chrome::ShowDownloads(browser());
[email protected]f2159ba2012-04-17 19:13:21622 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]9d8a4642009-07-29 17:25:30623}
624
625// Tests that focus goes where expected when using reload on a crashed tab.
[email protected]160f29ad2010-10-28 15:43:27626IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_FocusOnReloadCrashedTab) {
[email protected]a6e602f2010-09-28 22:28:30627 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]9d8a4642009-07-29 17:25:30628
[email protected]9d8a4642009-07-29 17:25:30629 // Open a regular page, crash, reload.
[email protected]c1dffe82013-06-26 20:59:05630 ui_test_utils::NavigateToURL(browser(),
631 embedded_test_server()->GetURL(kSimplePage));
[email protected]cc872372013-01-28 21:57:07632 content::CrashTab(browser()->tab_strip_model()->GetActiveWebContents());
[email protected]6b4e8e42011-08-17 19:36:06633 {
[email protected]a7fe9112012-07-20 02:34:45634 content::WindowedNotificationObserver observer(
[email protected]6b4e8e42011-08-17 19:36:06635 content::NOTIFICATION_LOAD_STOP,
[email protected]cfd33792014-05-23 20:39:34636 content::Source<content::NavigationController>(
[email protected]cc872372013-01-28 21:57:07637 &browser()->tab_strip_model()->GetActiveWebContents()->
638 GetController()));
[email protected]a37d4b02012-06-25 21:56:10639 chrome::Reload(browser(), CURRENT_TAB);
[email protected]6b4e8e42011-08-17 19:36:06640 observer.Wait();
641 }
[email protected]9ba21ede2010-07-30 01:11:07642
[email protected]9d8a4642009-07-29 17:25:30643 // Focus should now be on the tab contents.
[email protected]5d9cace72012-06-21 16:07:12644 chrome::ShowDownloads(browser());
[email protected]f2159ba2012-04-17 19:13:21645 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]9d8a4642009-07-29 17:25:30646}
[email protected]629e0342010-07-27 23:30:13647
[email protected]09475062013-10-21 21:29:56648// Tests that focus goes to frame after crashed tab.
649// TODO(shrikant): Find out where the focus should be deterministically.
650// Currently focused_view after crash seem to be non null in debug mode
651// (invalidated pointer 0xcccccc).
652IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_FocusAfterCrashedTab) {
653 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]09475062013-10-21 21:29:56654
655 content::CrashTab(browser()->tab_strip_model()->GetActiveWebContents());
656
657 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
658}
659
[email protected]895a84722012-11-29 18:45:44660// Tests that when a new tab is opened from the omnibox, the focus is moved from
661// the omnibox for the current tab.
[email protected]cfd33792014-05-23 20:39:34662IN_PROC_BROWSER_TEST_F(BrowserFocusTest, NavigateFromOmniboxIntoNewTab) {
[email protected]895a84722012-11-29 18:45:44663 GURL url("http://www.google.com/");
664 GURL url2("http://maps.google.com/");
665
666 // Navigate to url.
Sylvain Defresnec6ccc77d2014-09-19 10:19:35667 chrome::NavigateParams p(browser(), url, ui::PAGE_TRANSITION_LINK);
[email protected]895a84722012-11-29 18:45:44668 p.window_action = chrome::NavigateParams::SHOW_WINDOW;
669 p.disposition = CURRENT_TAB;
670 chrome::Navigate(&p);
671
672 // Focus the omnibox.
673 chrome::FocusLocationBar(browser());
674
[email protected]cfd33792014-05-23 20:39:34675 OmniboxEditController* controller = browser()->window()->GetLocationBar()->
676 GetOmniboxView()->model()->controller();
[email protected]895a84722012-11-29 18:45:44677
678 // Simulate an alt-enter.
679 controller->OnAutocompleteAccept(url2, NEW_FOREGROUND_TAB,
Sylvain Defresnec6ccc77d2014-09-19 10:19:35680 ui::PAGE_TRANSITION_TYPED);
[email protected]895a84722012-11-29 18:45:44681
682 // Make sure the second tab is selected.
[email protected]04e3ead52013-01-14 01:09:20683 EXPECT_EQ(1, browser()->tab_strip_model()->active_index());
[email protected]895a84722012-11-29 18:45:44684
685 // The tab contents should have the focus in the second tab.
[email protected]cfd33792014-05-23 20:39:34686 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]895a84722012-11-29 18:45:44687
688 // Go back to the first tab. The focus should not be in the omnibox.
689 chrome::SelectPreviousTab(browser());
[email protected]04e3ead52013-01-14 01:09:20690 EXPECT_EQ(0, browser()->tab_strip_model()->active_index());
[email protected]cfd33792014-05-23 20:39:34691 EXPECT_FALSE(IsViewFocused(VIEW_ID_OMNIBOX));
[email protected]895a84722012-11-29 18:45:44692}
693
[email protected]19efce52013-11-14 00:42:28694// This functionality is currently broken. http://crbug.com/304865.
695//
696//#if defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(USE_AURA)
697//// TODO(erg): http://crbug.com/163931
698//#define MAYBE_FocusOnNavigate DISABLED_FocusOnNavigate
699//#else
700//#define MAYBE_FocusOnNavigate FocusOnNavigate
701//#endif
[email protected]ddabf402013-10-22 22:43:08702
[email protected]19efce52013-11-14 00:42:28703IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_FocusOnNavigate) {
[email protected]c744f7d22013-04-12 20:29:04704 // Needed on Mac.
705 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
706 // Load the NTP.
[email protected]19506d542013-10-15 23:11:06707 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUINewTabURL));
[email protected]c744f7d22013-04-12 20:29:04708 EXPECT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
709
710 // Navigate to another page.
711 const base::FilePath::CharType* kEmptyFile = FILE_PATH_LITERAL("empty.html");
712 GURL file_url(ui_test_utils::GetTestUrl(base::FilePath(
713 base::FilePath::kCurrentDirectory), base::FilePath(kEmptyFile)));
714 ui_test_utils::NavigateToURL(browser(), file_url);
715
716 ClickOnView(VIEW_ID_TAB_CONTAINER);
717
718 // Navigate back. Should focus the location bar.
719 {
720 content::WindowedNotificationObserver back_nav_observer(
721 content::NOTIFICATION_NAV_ENTRY_COMMITTED,
722 content::NotificationService::AllSources());
723 chrome::GoBack(browser(), CURRENT_TAB);
724 back_nav_observer.Wait();
725 }
726
727 EXPECT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
728
729 // Navigate forward. Shouldn't focus the location bar.
730 ClickOnView(VIEW_ID_TAB_CONTAINER);
731 {
732 content::WindowedNotificationObserver forward_nav_observer(
733 content::NOTIFICATION_NAV_ENTRY_COMMITTED,
734 content::NotificationService::AllSources());
735 chrome::GoForward(browser(), CURRENT_TAB);
736 forward_nav_observer.Wait();
737 }
738
739 EXPECT_FALSE(IsViewFocused(VIEW_ID_OMNIBOX));
740}
741
[email protected]629e0342010-07-27 23:30:13742} // namespace