blob: fd032793b3d0bd87e543124fce25c2e2ed689551 [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"
fdoraybd6c0982016-06-11 00:23:0510#include "base/location.h"
avi655876a2015-12-25 07:18:1511#include "base/macros.h"
[email protected]3cc55ad2013-07-17 22:17:4112#include "base/message_loop/message_loop.h"
[email protected]3985ba82010-07-29 21:44:1213#include "base/path_service.h"
fdoraybd6c0982016-06-11 00:23:0514#include "base/single_thread_task_runner.h"
[email protected]11521182013-06-11 04:06:3615#include "base/strings/stringprintf.h"
[email protected]5846d582013-06-08 16:02:1216#include "base/strings/utf_string_conversions.h"
fdoraybd6c0982016-06-11 00:23:0517#include "base/threading/thread_task_runner_handle.h"
avi655876a2015-12-25 07:18:1518#include "build/build_config.h"
[email protected]dcc8fbc2013-07-12 00:54:0919#include "chrome/browser/chrome_notification_types.h"
[email protected]7b5dc002010-11-16 23:08:1020#include "chrome/browser/ui/browser.h"
[email protected]a37d4b02012-06-25 21:56:1021#include "chrome/browser/ui/browser_commands.h"
thestige80821242015-09-30 23:46:0822#include "chrome/browser/ui/browser_navigator_params.h"
[email protected]52877dbc62012-06-29 22:22:0323#include "chrome/browser/ui/browser_tabstrip.h"
[email protected]00070c732011-04-09 15:31:3324#include "chrome/browser/ui/browser_window.h"
[email protected]5d9cace72012-06-21 16:07:1225#include "chrome/browser/ui/chrome_pages.h"
[email protected]cfd33792014-05-23 20:39:3426#include "chrome/browser/ui/find_bar/find_bar_host_unittest_util.h"
[email protected]d57ce6a2014-07-03 15:39:2327#include "chrome/browser/ui/location_bar/location_bar.h"
[email protected]b56e2e32012-05-11 21:18:0428#include "chrome/browser/ui/tabs/tab_strip_model.h"
[email protected]6a3ec2312010-12-02 19:30:1929#include "chrome/browser/ui/view_ids.h"
[email protected]9e0c83a2009-05-06 19:44:3730#include "chrome/common/chrome_paths.h"
[email protected]cecc93a2010-10-05 15:58:5531#include "chrome/common/url_constants.h"
[email protected]af44e7fb2011-07-29 18:32:3232#include "chrome/test/base/in_process_browser_test.h"
[email protected]3ea1b182013-02-08 22:38:4133#include "chrome/test/base/interactive_test_utils.h"
[email protected]477ae052011-11-18 23:53:5734#include "chrome/test/base/ui_test_utils.h"
rdevlin.croninb2aa4f42016-07-02 02:23:3435#include "components/omnibox/browser/autocomplete_match_type.h"
blundell3f0c8bb42015-08-03 18:55:0536#include "components/omnibox/browser/omnibox_edit_controller.h"
blundell7dbd3792015-08-05 15:14:1937#include "components/omnibox/browser/omnibox_edit_model.h"
38#include "components/omnibox/browser/omnibox_view.h"
[email protected]cadaec52012-02-08 21:53:1339#include "content/public/browser/interstitial_page.h"
[email protected]85f0a572012-02-07 22:20:1340#include "content/public/browser/interstitial_page_delegate.h"
[email protected]477ae052011-11-18 23:53:5741#include "content/public/browser/notification_service.h"
khannansf3b27192015-01-09 17:28:1742#include "content/public/browser/render_frame_host.h"
[email protected]9c1662b2012-03-06 15:44:3343#include "content/public/browser/render_view_host.h"
avif9ab5d942015-10-15 14:05:4444#include "content/public/browser/render_widget_host.h"
[email protected]5626b0892012-02-20 14:46:5845#include "content/public/browser/render_widget_host_view.h"
[email protected]6acde6352012-01-04 16:52:2046#include "content/public/browser/web_contents.h"
[email protected]7d478cb2012-07-24 17:19:4247#include "content/public/test/browser_test_utils.h"
[email protected]c1dffe82013-06-26 20:59:0548#include "net/test/embedded_test_server/embedded_test_server.h"
[email protected]853300a82010-07-27 21:17:5749
[email protected]e0ee4b42014-05-23 06:16:1350#if defined(OS_WIN)
[email protected]cfd33792014-05-23 20:39:3451#include "base/win/windows_version.h"
[email protected]e0ee4b42014-05-23 06:16:1352#endif
53
[email protected]eaabba22012-03-07 15:02:1154using content::RenderViewHost;
[email protected]4b19ea52012-01-02 20:15:2555using content::WebContents;
56
[email protected]3a3cf4d2011-12-09 00:39:3557#if defined(OS_LINUX) || defined(OS_MACOSX)
58// TODO(jcampan): http://crbug.com/23683 for linux.
59// TODO(suzhe): http://crbug.com/49737 for mac.
[email protected]e00ccc92012-11-01 17:32:3060#define MAYBE_TabsRememberFocusFindInPage DISABLED_TabsRememberFocusFindInPage
[email protected]853300a82010-07-27 21:17:5761#elif defined(OS_WIN)
[email protected]c92928672010-11-09 18:31:0762// Flaky, http://crbug.com/62537.
[email protected]4d2451652012-02-14 23:54:2663#define MAYBE_TabsRememberFocusFindInPage DISABLED_TabsRememberFocusFindInPage
[email protected]fc2e0872009-08-21 22:14:4164#endif
65
initial.commit09911bf2008-07-26 23:55:2966namespace {
67
mgiuca88867d32015-07-08 04:08:2468#if defined(OS_POSIX)
[email protected]8bcdec92009-02-25 16:15:1869// The delay waited in some cases where we don't have a notifications for an
70// action we take.
initial.commit09911bf2008-07-26 23:55:2971const int kActionDelayMs = 500;
mgiuca88867d32015-07-08 04:08:2472#endif
initial.commit09911bf2008-07-26 23:55:2973
[email protected]c1dffe82013-06-26 20:59:0574const char kSimplePage[] = "/focus/page_with_focus.html";
75const char kStealFocusPage[] = "/focus/page_steals_focus.html";
76const char kTypicalPage[] = "/focus/typical_page.html";
[email protected]a9c060ca2012-01-05 20:43:4177
[email protected]8bcdec92009-02-25 16:15:1878class BrowserFocusTest : public InProcessBrowserTest {
initial.commit09911bf2008-07-26 23:55:2979 public:
thestige80821242015-09-30 23:46:0880 // InProcessBrowserTest overrides:
dcheng5dd5ff62014-10-21 12:42:3881 void SetUpOnMainThread() override {
svaldeza01f7d92015-11-18 17:47:5682 ASSERT_TRUE(embedded_test_server()->Start());
thestige80821242015-09-30 23:46:0883 }
[email protected]cfd33792014-05-23 20:39:3484
[email protected]21abcc742009-10-23 02:52:0685 bool IsViewFocused(ViewID vid) {
86 return ui_test_utils::IsViewFocused(browser(), vid);
[email protected]b9821882009-08-17 22:25:1787 }
88
[email protected]fc2e0872009-08-21 22:14:4189 void ClickOnView(ViewID vid) {
[email protected]21abcc742009-10-23 02:52:0690 ui_test_utils::ClickOnView(browser(), vid);
[email protected]fc2e0872009-08-21 22:14:4191 }
[email protected]0b8fa8b2011-12-07 00:54:5292
[email protected]cfd33792014-05-23 20:39:3493 void TestFocusTraversal(RenderViewHost* render_view_host, bool reverse) {
94 const char kGetFocusedElementJS[] =
95 "window.domAutomationController.send(getFocusedElement());";
96 const char* kExpectedIDs[] = { "textEdit", "searchButton", "luckyButton",
97 "googleLink", "gmailLink", "gmapLink" };
98 SCOPED_TRACE(base::StringPrintf("TestFocusTraversal: reverse=%d", reverse));
99 ui::KeyboardCode key = ui::VKEY_TAB;
100#if defined(OS_MACOSX)
101 // TODO(msw): Mac requires ui::VKEY_BACKTAB for reverse cycling. Sigh...
102 key = reverse ? ui::VKEY_BACKTAB : ui::VKEY_TAB;
103#elif defined(OS_WIN)
104 // This loop times out on Windows XP with no output. http://crbug.com/376635
105 if (base::win::GetVersion() < base::win::VERSION_VISTA)
106 return;
107#endif
108
109 // Loop through the focus chain twice for good measure.
110 for (size_t i = 0; i < 2; ++i) {
111 SCOPED_TRACE(base::StringPrintf("focus outer loop: %" PRIuS, i));
112 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
miu5d0cfcac2014-10-20 20:46:42113
[email protected]cfd33792014-05-23 20:39:34114 // Mac requires an extra Tab key press to traverse the app menu button
miu5d0cfcac2014-10-20 20:46:42115 // iff "Full Keyboard Access" is enabled. In reverse, four Tab key presses
116 // are required to traverse the back/forward buttons and the tab strip.
117#if defined(OS_MACOSX)
118 if (ui_controls::IsFullKeyboardAccessEnabled()) {
119 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
120 browser(), key, false, reverse, false, false));
121 if (reverse) {
122 for (int j = 0; j < 3; ++j) {
123 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
124 browser(), key, false, reverse, false, false));
125 }
126 }
127 }
128#endif
[email protected]cfd33792014-05-23 20:39:34129
palmere67e1742015-12-11 18:54:55130 if (reverse) {
131 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait(
132 browser(), key, false, reverse, false, false,
133 content::NOTIFICATION_ALL,
134 content::NotificationService::AllSources()));
135 }
136
[email protected]cfd33792014-05-23 20:39:34137 for (size_t j = 0; j < arraysize(kExpectedIDs); ++j) {
138 SCOPED_TRACE(base::StringPrintf("focus inner loop %" PRIuS, j));
139 const size_t index = reverse ? arraysize(kExpectedIDs) - 1 - j : j;
140 // The details are the node's editable state, i.e. true for "textEdit".
141 bool is_editable_node = index == 0;
142
143 // Press Tab (or Shift+Tab) and check the focused element id.
144 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWaitWithDetails(
145 browser(), key, false, reverse, false, false,
146 content::NOTIFICATION_FOCUS_CHANGED_IN_PAGE,
147 content::Source<RenderViewHost>(render_view_host),
148 content::Details<bool>(&is_editable_node)));
149 std::string focused_id;
150 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
151 render_view_host, kGetFocusedElementJS, &focused_id));
152 EXPECT_STREQ(kExpectedIDs[index], focused_id.c_str());
153 }
154
155#if defined(OS_MACOSX)
156 // TODO(msw): Mac doesn't post NOTIFICATION_FOCUS_RETURNED_TO_BROWSER and
157 // would also apparently require extra Tab key presses here. Sigh...
158 chrome::FocusLocationBar(browser());
159#else
160 // On the last Tab key press, focus returns to the browser.
161 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait(
162 browser(), key, false, reverse, false, false,
163 chrome::NOTIFICATION_FOCUS_RETURNED_TO_BROWSER,
164 content::Source<Browser>(browser())));
palmere67e1742015-12-11 18:54:55165 EXPECT_TRUE(
166 IsViewFocused(reverse ? VIEW_ID_OMNIBOX : VIEW_ID_LOCATION_ICON));
167
168 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait(
169 browser(), key, false, reverse, false, false,
170 content::NOTIFICATION_ALL,
171 content::NotificationService::AllSources()));
[email protected]cfd33792014-05-23 20:39:34172#endif
173 content::RunAllPendingInMessageLoop();
palmere67e1742015-12-11 18:54:55174 EXPECT_TRUE(
175 IsViewFocused(reverse ? VIEW_ID_LOCATION_ICON : VIEW_ID_OMNIBOX));
176 if (reverse) {
177 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait(
178 browser(), key, false, false, false, false,
179 content::NOTIFICATION_ALL,
180 content::NotificationService::AllSources()));
181 }
[email protected]cfd33792014-05-23 20:39:34182 }
[email protected]a9c060ca2012-01-05 20:43:41183 }
initial.commit09911bf2008-07-26 23:55:29184};
185
[email protected]cfd33792014-05-23 20:39:34186// A test interstitial page with typical HTML contents.
187class TestInterstitialPage : public content::InterstitialPageDelegate {
188 public:
189 explicit TestInterstitialPage(WebContents* tab) {
190 base::FilePath file_path;
191 bool success = PathService::Get(chrome::DIR_TEST_DATA, &file_path);
192 EXPECT_TRUE(success);
193 file_path = file_path.AppendASCII("focus/typical_page.html");
194 success = base::ReadFileToString(file_path, &html_contents_);
195 EXPECT_TRUE(success);
196 interstitial_page_ = content::InterstitialPage::Create(
197 tab, true, GURL("http://interstitial.com"), this);
198
199 // Show the interstitial and delay return until it has attached.
200 interstitial_page_->Show();
meacerb47bc8782014-10-07 03:40:01201 content::WaitForInterstitialAttach(tab);
202
[email protected]cfd33792014-05-23 20:39:34203 EXPECT_TRUE(tab->ShowingInterstitialPage());
[email protected]e0ee4b42014-05-23 06:16:13204 }
[email protected]ca9077f2014-05-22 19:00:21205
dcheng5dd5ff62014-10-21 12:42:38206 std::string GetHTMLContents() override { return html_contents_; }
[email protected]cfd33792014-05-23 20:39:34207
[email protected]85f0a572012-02-07 22:20:13208 RenderViewHost* render_view_host() {
khannansf3b27192015-01-09 17:28:17209 return interstitial_page_->GetMainFrame()->GetRenderViewHost();
[email protected]9e0c83a2009-05-06 19:44:37210 }
211
[email protected]cfd33792014-05-23 20:39:34212 void DontProceed() { interstitial_page_->DontProceed(); }
[email protected]4e85c112011-01-26 22:27:21213
avif9ab5d942015-10-15 14:05:44214 bool HasFocus() {
215 return render_view_host()->GetWidget()->GetView()->HasFocus();
216 }
[email protected]9e0c83a2009-05-06 19:44:37217
218 private:
219 std::string html_contents_;
[email protected]cfd33792014-05-23 20:39:34220 content::InterstitialPage* interstitial_page_; // Owns this.
221 DISALLOW_COPY_AND_ASSIGN(TestInterstitialPage);
[email protected]9e0c83a2009-05-06 19:44:37222};
[email protected]b9821882009-08-17 22:25:17223
sky4df637772015-10-22 17:20:18224// Flaky on Mac (http://crbug.com/67301).
225#if defined(OS_MACOSX)
[email protected]839e0802012-05-18 14:45:23226#define MAYBE_ClickingMovesFocus DISABLED_ClickingMovesFocus
[email protected]c39e233d2012-05-18 13:24:12227#else
sky4df637772015-10-22 17:20:18228// If this flakes, disable and log details in http://crbug.com/523255.
[email protected]839e0802012-05-18 14:45:23229#define MAYBE_ClickingMovesFocus ClickingMovesFocus
[email protected]c39e233d2012-05-18 13:24:12230#endif
231IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_ClickingMovesFocus) {
[email protected]a6e602f2010-09-28 22:28:30232 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]0ff0ff32010-12-21 19:34:42233#if defined(OS_POSIX)
[email protected]fc2e0872009-08-21 22:14:41234 // It seems we have to wait a little bit for the widgets to spin up before
235 // we can start clicking on them.
fdoraybd6c0982016-06-11 00:23:05236 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
ki.stfuc4f8e242015-10-09 20:40:20237 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure(),
[email protected]7e560102012-03-08 20:58:42238 base::TimeDelta::FromMilliseconds(kActionDelayMs));
[email protected]729eb632012-07-26 04:45:26239 content::RunMessageLoop();
[email protected]0ff0ff32010-12-21 19:34:42240#endif // defined(OS_POSIX)
[email protected]fc2e0872009-08-21 22:14:41241
[email protected]5c4e4482013-03-03 00:09:18242 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
[email protected]186f13f2009-08-19 20:34:00243
[email protected]fc2e0872009-08-21 22:14:41244 ClickOnView(VIEW_ID_TAB_CONTAINER);
[email protected]f2159ba2012-04-17 19:13:21245 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]186f13f2009-08-19 20:34:00246
[email protected]5c4e4482013-03-03 00:09:18247 ClickOnView(VIEW_ID_OMNIBOX);
248 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
[email protected]186f13f2009-08-19 20:34:00249}
[email protected]186f13f2009-08-19 20:34:00250
[email protected]320948e02011-01-10 08:21:12251// Flaky, http://crbug.com/69034.
[email protected]4d2451652012-02-14 23:54:26252IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_BrowsersRememberFocus) {
[email protected]a6e602f2010-09-28 22:28:30253 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]cfd33792014-05-23 20:39:34254 const GURL url = embedded_test_server()->GetURL(kSimplePage);
[email protected]8bcdec92009-02-25 16:15:18255 ui_test_utils::NavigateToURL(browser(), url);
initial.commit09911bf2008-07-26 23:55:29256
[email protected]90556dd2012-06-07 20:26:18257 gfx::NativeWindow window = browser()->window()->GetNativeWindow();
[email protected]186f13f2009-08-19 20:34:00258
initial.commit09911bf2008-07-26 23:55:29259 // The focus should be on the Tab contents.
[email protected]f2159ba2012-04-17 19:13:21260 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
initial.commit09911bf2008-07-26 23:55:29261 // Now hide the window, show it again, the focus should not have changed.
[email protected]853300a82010-07-27 21:17:57262 ui_test_utils::HideNativeWindow(window);
[email protected]7807088c2011-11-09 15:16:29263 ASSERT_TRUE(ui_test_utils::ShowAndFocusNativeWindow(window));
[email protected]f2159ba2012-04-17 19:13:21264 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
initial.commit09911bf2008-07-26 23:55:29265
[email protected]a37d4b02012-06-25 21:56:10266 chrome::FocusLocationBar(browser());
[email protected]5c4e4482013-03-03 00:09:18267 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
initial.commit09911bf2008-07-26 23:55:29268 // Hide the window, show it again, the focus should not have changed.
[email protected]853300a82010-07-27 21:17:57269 ui_test_utils::HideNativeWindow(window);
[email protected]7807088c2011-11-09 15:16:29270 ASSERT_TRUE(ui_test_utils::ShowAndFocusNativeWindow(window));
[email protected]5c4e4482013-03-03 00:09:18271 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
initial.commit09911bf2008-07-26 23:55:29272}
273
274// Tabs remember focus.
[email protected]26153732010-11-09 18:47:39275// Disabled, http://crbug.com/62542.
276IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_TabsRememberFocus) {
[email protected]a6e602f2010-09-28 22:28:30277 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]cfd33792014-05-23 20:39:34278 const GURL url = embedded_test_server()->GetURL(kSimplePage);
[email protected]8bcdec92009-02-25 16:15:18279 ui_test_utils::NavigateToURL(browser(), url);
280
initial.commit09911bf2008-07-26 23:55:29281 // Create several tabs.
[email protected]52877dbc62012-06-29 22:22:03282 for (int i = 0; i < 4; ++i) {
283 chrome::AddSelectedTabWithURL(browser(), url,
Sylvain Defresnec6ccc77d2014-09-19 10:19:35284 ui::PAGE_TRANSITION_TYPED);
[email protected]52877dbc62012-06-29 22:22:03285 }
initial.commit09911bf2008-07-26 23:55:29286
287 // Alternate focus for the tab.
288 const bool kFocusPage[3][5] = {
289 { true, true, true, true, false },
290 { false, false, false, false, false },
291 { false, true, false, true, false }
292 };
293
294 for (int i = 1; i < 3; i++) {
295 for (int j = 0; j < 5; j++) {
[email protected]8bcdec92009-02-25 16:15:18296 // Activate the tab.
[email protected]57892182012-12-03 19:15:39297 browser()->tab_strip_model()->ActivateTabAt(j, true);
initial.commit09911bf2008-07-26 23:55:29298
299 // Activate the location bar or the page.
[email protected]7e383692009-06-12 19:14:54300 if (kFocusPage[i][j]) {
[email protected]fc2b46b2014-05-03 16:33:45301 browser()->tab_strip_model()->GetWebContentsAt(j)->Focus();
[email protected]7e383692009-06-12 19:14:54302 } else {
[email protected]a37d4b02012-06-25 21:56:10303 chrome::FocusLocationBar(browser());
[email protected]7e383692009-06-12 19:14:54304 }
initial.commit09911bf2008-07-26 23:55:29305 }
306
307 // Now come back to the tab and check the right view is focused.
308 for (int j = 0; j < 5; j++) {
[email protected]8bcdec92009-02-25 16:15:18309 // Activate the tab.
[email protected]57892182012-12-03 19:15:39310 browser()->tab_strip_model()->ActivateTabAt(j, true);
initial.commit09911bf2008-07-26 23:55:29311
[email protected]5c4e4482013-03-03 00:09:18312 ViewID vid = kFocusPage[i][j] ? VIEW_ID_TAB_CONTAINER : VIEW_ID_OMNIBOX;
[email protected]21abcc742009-10-23 02:52:06313 ASSERT_TRUE(IsViewFocused(vid));
initial.commit09911bf2008-07-26 23:55:29314 }
[email protected]cb7e2542009-12-14 22:02:35315
[email protected]57892182012-12-03 19:15:39316 browser()->tab_strip_model()->ActivateTabAt(0, true);
[email protected]cb7e2542009-12-14 22:02:35317 // Try the above, but with ctrl+tab. Since tab normally changes focus,
318 // this has regressed in the past. Loop through several times to be sure.
319 for (int j = 0; j < 15; j++) {
[email protected]f2159ba2012-04-17 19:13:21320 ViewID vid = kFocusPage[i][j % 5] ? VIEW_ID_TAB_CONTAINER :
[email protected]5c4e4482013-03-03 00:09:18321 VIEW_ID_OMNIBOX;
[email protected]cb7e2542009-12-14 22:02:35322 ASSERT_TRUE(IsViewFocused(vid));
323
[email protected]1d000682010-08-23 16:21:28324 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09325 browser(), ui::VKEY_TAB, true, false, false, false));
[email protected]cb7e2542009-12-14 22:02:35326 }
327
328 // As above, but with ctrl+shift+tab.
[email protected]57892182012-12-03 19:15:39329 browser()->tab_strip_model()->ActivateTabAt(4, true);
[email protected]cb7e2542009-12-14 22:02:35330 for (int j = 14; j >= 0; --j) {
[email protected]f2159ba2012-04-17 19:13:21331 ViewID vid = kFocusPage[i][j % 5] ? VIEW_ID_TAB_CONTAINER :
[email protected]5c4e4482013-03-03 00:09:18332 VIEW_ID_OMNIBOX;
[email protected]cb7e2542009-12-14 22:02:35333 ASSERT_TRUE(IsViewFocused(vid));
334
[email protected]1d000682010-08-23 16:21:28335 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09336 browser(), ui::VKEY_TAB, true, true, false, false));
[email protected]cb7e2542009-12-14 22:02:35337 }
initial.commit09911bf2008-07-26 23:55:29338 }
339}
340
[email protected]ae40b572009-10-02 21:17:45341// Tabs remember focus with find-in-page box.
[email protected]cb7e2542009-12-14 22:02:35342IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_TabsRememberFocusFindInPage) {
[email protected]a6e602f2010-09-28 22:28:30343 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]cfd33792014-05-23 20:39:34344 const GURL url = embedded_test_server()->GetURL(kSimplePage);
[email protected]ae40b572009-10-02 21:17:45345 ui_test_utils::NavigateToURL(browser(), url);
346
[email protected]a37d4b02012-06-25 21:56:10347 chrome::Find(browser());
[email protected]cc872372013-01-28 21:57:07348 ui_test_utils::FindInPage(
349 browser()->tab_strip_model()->GetActiveWebContents(),
[email protected]1f136902013-12-24 23:26:45350 base::ASCIIToUTF16("a"), true, false, NULL, NULL);
[email protected]21abcc742009-10-23 02:52:06351 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]ae40b572009-10-02 21:17:45352
353 // Focus the location bar.
[email protected]a37d4b02012-06-25 21:56:10354 chrome::FocusLocationBar(browser());
[email protected]ae40b572009-10-02 21:17:45355
356 // Create a 2nd tab.
Sylvain Defresnec6ccc77d2014-09-19 10:19:35357 chrome::AddSelectedTabWithURL(browser(), url, ui::PAGE_TRANSITION_TYPED);
[email protected]ae40b572009-10-02 21:17:45358
359 // Focus should be on the recently opened tab page.
[email protected]f2159ba2012-04-17 19:13:21360 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]ae40b572009-10-02 21:17:45361
362 // Select 1st tab, focus should still be on the location-bar.
363 // (bug http://crbug.com/23296)
[email protected]57892182012-12-03 19:15:39364 browser()->tab_strip_model()->ActivateTabAt(0, true);
[email protected]5c4e4482013-03-03 00:09:18365 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
[email protected]ae40b572009-10-02 21:17:45366
367 // Now open the find box again, switch to another tab and come back, the focus
368 // should return to the find box.
[email protected]a37d4b02012-06-25 21:56:10369 chrome::Find(browser());
[email protected]21abcc742009-10-23 02:52:06370 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]57892182012-12-03 19:15:39371 browser()->tab_strip_model()->ActivateTabAt(1, true);
[email protected]f2159ba2012-04-17 19:13:21372 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]57892182012-12-03 19:15:39373 browser()->tab_strip_model()->ActivateTabAt(0, true);
[email protected]21abcc742009-10-23 02:52:06374 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]ae40b572009-10-02 21:17:45375}
376
initial.commit09911bf2008-07-26 23:55:29377// Background window does not steal focus.
[email protected]c92928672010-11-09 18:31:07378// Flaky, http://crbug.com/62538.
379IN_PROC_BROWSER_TEST_F(BrowserFocusTest,
[email protected]4d2451652012-02-14 23:54:26380 DISABLED_BackgroundBrowserDontStealFocus) {
[email protected]a6e602f2010-09-28 22:28:30381 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
initial.commit09911bf2008-07-26 23:55:29382
initial.commit09911bf2008-07-26 23:55:29383 // Open a new browser window.
scottmg851949002016-02-09 20:09:44384 Browser* browser2 = new Browser(Browser::CreateParams(browser()->profile()));
385
[email protected]8bcdec92009-02-25 16:15:18386 ASSERT_TRUE(browser2);
[email protected]00509a32013-11-17 17:45:37387 chrome::AddTabAt(browser2, GURL(), -1, true);
[email protected]8bcdec92009-02-25 16:15:18388 browser2->window()->Show();
[email protected]186f13f2009-08-19 20:34:00389
[email protected]ed179ee2009-10-03 21:02:51390 Browser* focused_browser = NULL;
391 Browser* unfocused_browser = NULL;
[email protected]753efc42010-03-09 19:52:16392#if defined(USE_X11)
393 // On X11, calling Activate() is not guaranteed to move focus, so we have
[email protected]186f13f2009-08-19 20:34:00394 // to figure out which browser does have focus.
395 if (browser2->window()->IsActive()) {
396 focused_browser = browser2;
397 unfocused_browser = browser();
398 } else if (browser()->window()->IsActive()) {
399 focused_browser = browser();
400 unfocused_browser = browser2;
401 } else {
[email protected]579c3d82010-10-06 03:53:51402 FAIL() << "Could not determine which browser has focus";
[email protected]186f13f2009-08-19 20:34:00403 }
404#elif defined(OS_WIN)
405 focused_browser = browser();
406 unfocused_browser = browser2;
[email protected]853300a82010-07-27 21:17:57407#elif defined(OS_MACOSX)
408 // On Mac, the newly created window always gets the focus.
409 focused_browser = browser2;
410 unfocused_browser = browser();
[email protected]186f13f2009-08-19 20:34:00411#endif
412
[email protected]cfd33792014-05-23 20:39:34413 const GURL steal_focus_url = embedded_test_server()->GetURL(kStealFocusPage);
[email protected]186f13f2009-08-19 20:34:00414 ui_test_utils::NavigateToURL(unfocused_browser, steal_focus_url);
[email protected]1e187af2009-02-25 02:02:46415
[email protected]8bcdec92009-02-25 16:15:18416 // Activate the first browser.
[email protected]186f13f2009-08-19 20:34:00417 focused_browser->window()->Activate();
initial.commit09911bf2008-07-26 23:55:29418
[email protected]b6987e02013-01-04 18:30:43419 ASSERT_TRUE(content::ExecuteScript(
[email protected]da2bfa42013-01-29 23:47:05420 unfocused_browser->tab_strip_model()->GetActiveWebContents(),
[email protected]06bc5d92013-01-02 22:44:13421 "stealFocus();"));
initial.commit09911bf2008-07-26 23:55:29422
[email protected]8bcdec92009-02-25 16:15:18423 // Make sure the first browser is still active.
[email protected]186f13f2009-08-19 20:34:00424 EXPECT_TRUE(focused_browser->window()->IsActive());
initial.commit09911bf2008-07-26 23:55:29425}
426
[email protected]ddabf402013-10-22 22:43:08427#if defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(USE_AURA)
428// TODO(erg): http://crbug.com/163931
429#define MAYBE_LocationBarLockFocus DISABLED_LocationBarLockFocus
430#else
431#define MAYBE_LocationBarLockFocus LocationBarLockFocus
432#endif
433
initial.commit09911bf2008-07-26 23:55:29434// Page cannot steal focus when focus is on location bar.
[email protected]ddabf402013-10-22 22:43:08435IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_LocationBarLockFocus) {
[email protected]a6e602f2010-09-28 22:28:30436 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
initial.commit09911bf2008-07-26 23:55:29437
438 // Open the page that steals focus.
[email protected]cfd33792014-05-23 20:39:34439 const GURL url = embedded_test_server()->GetURL(kStealFocusPage);
[email protected]8bcdec92009-02-25 16:15:18440 ui_test_utils::NavigateToURL(browser(), url);
initial.commit09911bf2008-07-26 23:55:29441
[email protected]a37d4b02012-06-25 21:56:10442 chrome::FocusLocationBar(browser());
initial.commit09911bf2008-07-26 23:55:29443
[email protected]b6987e02013-01-04 18:30:43444 ASSERT_TRUE(content::ExecuteScript(
[email protected]cc872372013-01-28 21:57:07445 browser()->tab_strip_model()->GetActiveWebContents(),
[email protected]06bc5d92013-01-02 22:44:13446 "stealFocus();"));
initial.commit09911bf2008-07-26 23:55:29447
448 // Make sure the location bar is still focused.
[email protected]5c4e4482013-03-03 00:09:18449 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
initial.commit09911bf2008-07-26 23:55:29450}
451
[email protected]cfd33792014-05-23 20:39:34452// Test forward and reverse focus traversal on a typical page.
bnc0f8fe372014-11-24 19:42:51453// Disabled for Mac because it is flaky on "Mac10.9 Tests (dbg)",
454// see https://crbug.com/60973.
455#if defined(OS_MACOSX)
456#define MAYBE_FocusTraversal DISABLED_FocusTraversal
457#else
458#define MAYBE_FocusTraversal FocusTraversal
459#endif
460IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusTraversal) {
[email protected]a6e602f2010-09-28 22:28:30461 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]cfd33792014-05-23 20:39:34462 const GURL url = embedded_test_server()->GetURL(kTypicalPage);
[email protected]8bcdec92009-02-25 16:15:18463 ui_test_utils::NavigateToURL(browser(), url);
[email protected]cfd33792014-05-23 20:39:34464 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]a37d4b02012-06-25 21:56:10465 chrome::FocusLocationBar(browser());
initial.commit09911bf2008-07-26 23:55:29466
[email protected]cfd33792014-05-23 20:39:34467 WebContents* tab = browser()->tab_strip_model()->GetActiveWebContents();
468 EXPECT_NO_FATAL_FAILURE(TestFocusTraversal(tab->GetRenderViewHost(), false));
469 EXPECT_NO_FATAL_FAILURE(TestFocusTraversal(tab->GetRenderViewHost(), true));
initial.commit09911bf2008-07-26 23:55:29470}
471
[email protected]cfd33792014-05-23 20:39:34472// Test forward and reverse focus traversal while an interstitial is showing.
[email protected]6d731ed2014-06-02 14:53:29473// Disabled, see http://crbug.com/60973
thestige80821242015-09-30 23:46:08474IN_PROC_BROWSER_TEST_F(BrowserFocusTest,
475 DISABLED_FocusTraversalOnInterstitial) {
[email protected]a6e602f2010-09-28 22:28:30476 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]cfd33792014-05-23 20:39:34477 const GURL url = embedded_test_server()->GetURL(kSimplePage);
[email protected]9e0c83a2009-05-06 19:44:37478 ui_test_utils::NavigateToURL(browser(), url);
[email protected]cfd33792014-05-23 20:39:34479 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]9e0c83a2009-05-06 19:44:37480
[email protected]cfd33792014-05-23 20:39:34481 // Create and show a test interstitial page.
[email protected]cc872372013-01-28 21:57:07482 TestInterstitialPage* interstitial_page = new TestInterstitialPage(
[email protected]cfd33792014-05-23 20:39:34483 browser()->tab_strip_model()->GetActiveWebContents());
484 content::RenderViewHost* host = interstitial_page->render_view_host();
[email protected]9e0c83a2009-05-06 19:44:37485
[email protected]cfd33792014-05-23 20:39:34486 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]a37d4b02012-06-25 21:56:10487 chrome::FocusLocationBar(browser());
[email protected]cfd33792014-05-23 20:39:34488 EXPECT_NO_FATAL_FAILURE(TestFocusTraversal(host, false));
489 EXPECT_NO_FATAL_FAILURE(TestFocusTraversal(host, true));
[email protected]9e0c83a2009-05-06 19:44:37490}
491
[email protected]cfd33792014-05-23 20:39:34492// Test the transfer of focus when an interstitial is shown and hidden.
493IN_PROC_BROWSER_TEST_F(BrowserFocusTest, InterstitialFocus) {
[email protected]a6e602f2010-09-28 22:28:30494 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]cfd33792014-05-23 20:39:34495 const GURL url = embedded_test_server()->GetURL(kSimplePage);
[email protected]e0ee4b42014-05-23 06:16:13496 ui_test_utils::NavigateToURL(browser(), url);
[email protected]cfd33792014-05-23 20:39:34497 WebContents* tab = browser()->tab_strip_model()->GetActiveWebContents();
498 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
avif9ab5d942015-10-15 14:05:44499 EXPECT_TRUE(tab->GetRenderViewHost()->GetWidget()->GetView()->HasFocus());
[email protected]e0ee4b42014-05-23 06:16:13500
[email protected]cfd33792014-05-23 20:39:34501 // Create and show a test interstitial page; it should gain focus.
502 TestInterstitialPage* interstitial_page = new TestInterstitialPage(tab);
503 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]9e0c83a2009-05-06 19:44:37504 EXPECT_TRUE(interstitial_page->HasFocus());
505
[email protected]cfd33792014-05-23 20:39:34506 // Hide the interstitial; the original page should gain focus.
[email protected]9e0c83a2009-05-06 19:44:37507 interstitial_page->DontProceed();
[email protected]cfd33792014-05-23 20:39:34508 content::RunAllPendingInMessageLoop();
509 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
avif9ab5d942015-10-15 14:05:44510 EXPECT_TRUE(tab->GetRenderViewHost()->GetWidget()->GetView()->HasFocus());
[email protected]9e0c83a2009-05-06 19:44:37511}
512
[email protected]cfd33792014-05-23 20:39:34513// Test that find-in-page UI can request focus, even when it is already open.
sky4df637772015-10-22 17:20:18514#if defined(OS_MACOSX)
John Abd-El-Malekf3fdd622014-09-09 04:04:23515#define MAYBE_FindFocusTest DISABLED_FindFocusTest
516#else
sky4df637772015-10-22 17:20:18517// If this flakes, disable and log details in http://crbug.com/523255.
John Abd-El-Malekf3fdd622014-09-09 04:04:23518#define MAYBE_FindFocusTest FindFocusTest
519#endif
520IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FindFocusTest) {
[email protected]cfd33792014-05-23 20:39:34521 chrome::DisableFindBarAnimationsDuringTesting(true);
[email protected]a6e602f2010-09-28 22:28:30522 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]cfd33792014-05-23 20:39:34523 const GURL url = embedded_test_server()->GetURL(kTypicalPage);
[email protected]e0ee4b42014-05-23 06:16:13524 ui_test_utils::NavigateToURL(browser(), url);
[email protected]cfd33792014-05-23 20:39:34525 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]e0ee4b42014-05-23 06:16:13526
[email protected]cfd33792014-05-23 20:39:34527 chrome::ShowFindBar(browser());
528 EXPECT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]9bd491ee2008-12-10 22:31:07529
[email protected]a37d4b02012-06-25 21:56:10530 chrome::FocusLocationBar(browser());
[email protected]cfd33792014-05-23 20:39:34531 EXPECT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
[email protected]9bd491ee2008-12-10 22:31:07532
[email protected]cfd33792014-05-23 20:39:34533 chrome::ShowFindBar(browser());
534 EXPECT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]9bd491ee2008-12-10 22:31:07535
[email protected]fc2e0872009-08-21 22:14:41536 ClickOnView(VIEW_ID_TAB_CONTAINER);
[email protected]cfd33792014-05-23 20:39:34537 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]9bd491ee2008-12-10 22:31:07538
[email protected]cfd33792014-05-23 20:39:34539 chrome::ShowFindBar(browser());
540 EXPECT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]9bd491ee2008-12-10 22:31:07541}
[email protected]401513c2009-03-12 00:21:28542
543// Makes sure the focus is in the right location when opening the different
544// types of tabs.
[email protected]c92928672010-11-09 18:31:07545// Flaky, http://crbug.com/62539.
[email protected]4d2451652012-02-14 23:54:26546IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_TabInitialFocus) {
[email protected]a6e602f2010-09-28 22:28:30547 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]9ba21ede2010-07-30 01:11:07548
[email protected]401513c2009-03-12 00:21:28549 // Open the history tab, focus should be on the tab contents.
[email protected]5d9cace72012-06-21 16:07:12550 chrome::ShowHistory(browser());
[email protected]6ddc6e3f22012-07-26 01:15:35551 ASSERT_NO_FATAL_FAILURE(content::WaitForLoadStop(
[email protected]cc872372013-01-28 21:57:07552 browser()->tab_strip_model()->GetActiveWebContents()));
[email protected]f2159ba2012-04-17 19:13:21553 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]401513c2009-03-12 00:21:28554
555 // Open the new tab, focus should be on the location bar.
[email protected]a37d4b02012-06-25 21:56:10556 chrome::NewTab(browser());
[email protected]6ddc6e3f22012-07-26 01:15:35557 ASSERT_NO_FATAL_FAILURE(content::WaitForLoadStop(
[email protected]cc872372013-01-28 21:57:07558 browser()->tab_strip_model()->GetActiveWebContents()));
[email protected]5c4e4482013-03-03 00:09:18559 EXPECT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
[email protected]401513c2009-03-12 00:21:28560
561 // Open the download tab, focus should be on the tab contents.
[email protected]5d9cace72012-06-21 16:07:12562 chrome::ShowDownloads(browser());
[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]f2159ba2012-04-17 19:13:21565 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]3e3f0eb2009-06-22 18:33:43566
567 // Open about:blank, focus should be on the location bar.
[email protected]8e09c7af2014-06-10 11:46:17568 chrome::AddSelectedTabWithURL(
Sylvain Defresnec6ccc77d2014-09-19 10:19:35569 browser(), GURL(url::kAboutBlankURL), ui::PAGE_TRANSITION_LINK);
[email protected]6ddc6e3f22012-07-26 01:15:35570 ASSERT_NO_FATAL_FAILURE(content::WaitForLoadStop(
[email protected]cc872372013-01-28 21:57:07571 browser()->tab_strip_model()->GetActiveWebContents()));
[email protected]5c4e4482013-03-03 00:09:18572 EXPECT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
[email protected]401513c2009-03-12 00:21:28573}
[email protected]9d8a4642009-07-29 17:25:30574
[email protected]ddabf402013-10-22 22:43:08575#if defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(USE_AURA)
576// TODO(erg): http://crbug.com/163931
577#define MAYBE_FocusOnReload DISABLED_FocusOnReload
578#else
579#define MAYBE_FocusOnReload FocusOnReload
580#endif
581
[email protected]9d8a4642009-07-29 17:25:30582// Tests that focus goes where expected when using reload.
[email protected]ddabf402013-10-22 22:43:08583IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusOnReload) {
[email protected]a6e602f2010-09-28 22:28:30584 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]9d8a4642009-07-29 17:25:30585
[email protected]9d8a4642009-07-29 17:25:30586 // Open the new tab, reload.
[email protected]6b4e8e42011-08-17 19:36:06587 {
[email protected]a7fe9112012-07-20 02:34:45588 content::WindowedNotificationObserver observer(
[email protected]6b4e8e42011-08-17 19:36:06589 content::NOTIFICATION_LOAD_STOP,
[email protected]ad50def52011-10-19 23:17:07590 content::NotificationService::AllSources());
[email protected]a37d4b02012-06-25 21:56:10591 chrome::NewTab(browser());
[email protected]6b4e8e42011-08-17 19:36:06592 observer.Wait();
593 }
[email protected]b8deecd2012-07-30 21:09:44594 content::RunAllPendingInMessageLoop();
[email protected]f07d7bf2010-04-06 08:02:42595
[email protected]6b4e8e42011-08-17 19:36:06596 {
[email protected]a7fe9112012-07-20 02:34:45597 content::WindowedNotificationObserver observer(
[email protected]6b4e8e42011-08-17 19:36:06598 content::NOTIFICATION_LOAD_STOP,
[email protected]cfd33792014-05-23 20:39:34599 content::Source<content::NavigationController>(
[email protected]cc872372013-01-28 21:57:07600 &browser()->tab_strip_model()->GetActiveWebContents()->
601 GetController()));
[email protected]a37d4b02012-06-25 21:56:10602 chrome::Reload(browser(), CURRENT_TAB);
[email protected]6b4e8e42011-08-17 19:36:06603 observer.Wait();
604 }
[email protected]9d8a4642009-07-29 17:25:30605 // Focus should stay on the location bar.
[email protected]5c4e4482013-03-03 00:09:18606 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
[email protected]9d8a4642009-07-29 17:25:30607
608 // Open a regular page, focus the location bar, reload.
[email protected]c1dffe82013-06-26 20:59:05609 ui_test_utils::NavigateToURL(browser(),
610 embedded_test_server()->GetURL(kSimplePage));
[email protected]a37d4b02012-06-25 21:56:10611 chrome::FocusLocationBar(browser());
[email protected]5c4e4482013-03-03 00:09:18612 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
[email protected]6b4e8e42011-08-17 19:36:06613 {
[email protected]a7fe9112012-07-20 02:34:45614 content::WindowedNotificationObserver observer(
[email protected]6b4e8e42011-08-17 19:36:06615 content::NOTIFICATION_LOAD_STOP,
[email protected]cfd33792014-05-23 20:39:34616 content::Source<content::NavigationController>(
[email protected]cc872372013-01-28 21:57:07617 &browser()->tab_strip_model()->GetActiveWebContents()->
618 GetController()));
[email protected]a37d4b02012-06-25 21:56:10619 chrome::Reload(browser(), CURRENT_TAB);
[email protected]6b4e8e42011-08-17 19:36:06620 observer.Wait();
621 }
[email protected]9ba21ede2010-07-30 01:11:07622
[email protected]9d8a4642009-07-29 17:25:30623 // Focus should now be on the tab contents.
[email protected]5d9cace72012-06-21 16:07:12624 chrome::ShowDownloads(browser());
[email protected]f2159ba2012-04-17 19:13:21625 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]9d8a4642009-07-29 17:25:30626}
627
628// Tests that focus goes where expected when using reload on a crashed tab.
[email protected]160f29ad2010-10-28 15:43:27629IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_FocusOnReloadCrashedTab) {
[email protected]a6e602f2010-09-28 22:28:30630 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]9d8a4642009-07-29 17:25:30631
[email protected]9d8a4642009-07-29 17:25:30632 // Open a regular page, crash, reload.
[email protected]c1dffe82013-06-26 20:59:05633 ui_test_utils::NavigateToURL(browser(),
634 embedded_test_server()->GetURL(kSimplePage));
[email protected]cc872372013-01-28 21:57:07635 content::CrashTab(browser()->tab_strip_model()->GetActiveWebContents());
[email protected]6b4e8e42011-08-17 19:36:06636 {
[email protected]a7fe9112012-07-20 02:34:45637 content::WindowedNotificationObserver observer(
[email protected]6b4e8e42011-08-17 19:36:06638 content::NOTIFICATION_LOAD_STOP,
[email protected]cfd33792014-05-23 20:39:34639 content::Source<content::NavigationController>(
[email protected]cc872372013-01-28 21:57:07640 &browser()->tab_strip_model()->GetActiveWebContents()->
641 GetController()));
[email protected]a37d4b02012-06-25 21:56:10642 chrome::Reload(browser(), CURRENT_TAB);
[email protected]6b4e8e42011-08-17 19:36:06643 observer.Wait();
644 }
[email protected]9ba21ede2010-07-30 01:11:07645
[email protected]9d8a4642009-07-29 17:25:30646 // Focus should now be on the tab contents.
[email protected]5d9cace72012-06-21 16:07:12647 chrome::ShowDownloads(browser());
[email protected]f2159ba2012-04-17 19:13:21648 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]9d8a4642009-07-29 17:25:30649}
[email protected]629e0342010-07-27 23:30:13650
[email protected]09475062013-10-21 21:29:56651// Tests that focus goes to frame after crashed tab.
652// TODO(shrikant): Find out where the focus should be deterministically.
653// Currently focused_view after crash seem to be non null in debug mode
654// (invalidated pointer 0xcccccc).
655IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_FocusAfterCrashedTab) {
656 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]09475062013-10-21 21:29:56657
658 content::CrashTab(browser()->tab_strip_model()->GetActiveWebContents());
659
660 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
661}
662
[email protected]895a84722012-11-29 18:45:44663// Tests that when a new tab is opened from the omnibox, the focus is moved from
664// the omnibox for the current tab.
[email protected]cfd33792014-05-23 20:39:34665IN_PROC_BROWSER_TEST_F(BrowserFocusTest, NavigateFromOmniboxIntoNewTab) {
[email protected]895a84722012-11-29 18:45:44666 GURL url("http://www.google.com/");
667 GURL url2("http://maps.google.com/");
668
669 // Navigate to url.
Sylvain Defresnec6ccc77d2014-09-19 10:19:35670 chrome::NavigateParams p(browser(), url, ui::PAGE_TRANSITION_LINK);
[email protected]895a84722012-11-29 18:45:44671 p.window_action = chrome::NavigateParams::SHOW_WINDOW;
672 p.disposition = CURRENT_TAB;
673 chrome::Navigate(&p);
674
675 // Focus the omnibox.
676 chrome::FocusLocationBar(browser());
677
[email protected]cfd33792014-05-23 20:39:34678 OmniboxEditController* controller = browser()->window()->GetLocationBar()->
679 GetOmniboxView()->model()->controller();
[email protected]895a84722012-11-29 18:45:44680
681 // Simulate an alt-enter.
682 controller->OnAutocompleteAccept(url2, NEW_FOREGROUND_TAB,
rdevlin.croninb2aa4f42016-07-02 02:23:34683 ui::PAGE_TRANSITION_TYPED,
684 AutocompleteMatchType::URL_WHAT_YOU_TYPED);
[email protected]895a84722012-11-29 18:45:44685
686 // Make sure the second tab is selected.
[email protected]04e3ead52013-01-14 01:09:20687 EXPECT_EQ(1, browser()->tab_strip_model()->active_index());
[email protected]895a84722012-11-29 18:45:44688
689 // The tab contents should have the focus in the second tab.
[email protected]cfd33792014-05-23 20:39:34690 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]895a84722012-11-29 18:45:44691
692 // Go back to the first tab. The focus should not be in the omnibox.
693 chrome::SelectPreviousTab(browser());
[email protected]04e3ead52013-01-14 01:09:20694 EXPECT_EQ(0, browser()->tab_strip_model()->active_index());
[email protected]cfd33792014-05-23 20:39:34695 EXPECT_FALSE(IsViewFocused(VIEW_ID_OMNIBOX));
[email protected]895a84722012-11-29 18:45:44696}
697
[email protected]19efce52013-11-14 00:42:28698// This functionality is currently broken. http://crbug.com/304865.
699//
fdoraybd6c0982016-06-11 00:23:05700// #if defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(USE_AURA)
701// // TODO(erg): http://crbug.com/163931
702// #define MAYBE_FocusOnNavigate DISABLED_FocusOnNavigate
703// #else
704// #define MAYBE_FocusOnNavigate FocusOnNavigate
705// #endif
[email protected]ddabf402013-10-22 22:43:08706
[email protected]19efce52013-11-14 00:42:28707IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_FocusOnNavigate) {
[email protected]c744f7d22013-04-12 20:29:04708 // Needed on Mac.
709 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
710 // Load the NTP.
[email protected]19506d542013-10-15 23:11:06711 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUINewTabURL));
[email protected]c744f7d22013-04-12 20:29:04712 EXPECT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
713
714 // Navigate to another page.
715 const base::FilePath::CharType* kEmptyFile = FILE_PATH_LITERAL("empty.html");
716 GURL file_url(ui_test_utils::GetTestUrl(base::FilePath(
717 base::FilePath::kCurrentDirectory), base::FilePath(kEmptyFile)));
718 ui_test_utils::NavigateToURL(browser(), file_url);
719
720 ClickOnView(VIEW_ID_TAB_CONTAINER);
721
722 // Navigate back. Should focus the location bar.
723 {
724 content::WindowedNotificationObserver back_nav_observer(
725 content::NOTIFICATION_NAV_ENTRY_COMMITTED,
726 content::NotificationService::AllSources());
727 chrome::GoBack(browser(), CURRENT_TAB);
728 back_nav_observer.Wait();
729 }
730
731 EXPECT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
732
733 // Navigate forward. Shouldn't focus the location bar.
734 ClickOnView(VIEW_ID_TAB_CONTAINER);
735 {
736 content::WindowedNotificationObserver forward_nav_observer(
737 content::NOTIFICATION_NAV_ENTRY_COMMITTED,
738 content::NotificationService::AllSources());
739 chrome::GoForward(browser(), CURRENT_TAB);
740 forward_nav_observer.Wait();
741 }
742
743 EXPECT_FALSE(IsViewFocused(VIEW_ID_OMNIBOX));
744}
745
palmerc70cb1f2016-03-04 23:41:26746// Ensure that crbug.com/567445 does not regress. This test checks that the
747// Omnibox does not get focused when loading about:blank in a case where it's
748// not the startup URL, e.g. when a page opens a popup to about:blank, with a
749// null opener, and then navigates it. This is a potential security issue; see
750// comments in |WebContentsImpl::FocusLocationBarByDefault|.
751IN_PROC_BROWSER_TEST_F(BrowserFocusTest, AboutBlankNavigationLocationTest) {
752 const GURL url1(embedded_test_server()->GetURL("/title1.html"));
753 ui_test_utils::NavigateToURL(browser(), url1);
754
755 TabStripModel* tab_strip = browser()->tab_strip_model();
756 WebContents* web_contents = tab_strip->GetActiveWebContents();
757
758 const GURL url2(embedded_test_server()->GetURL("/title2.html"));
759 const std::string spoof("var w = window.open('about:blank'); w.opener = null;"
760 "w.document.location = '" + url2.spec() + "';");
761
762 ASSERT_TRUE(content::ExecuteScript(web_contents, spoof));
763 EXPECT_EQ(url1, web_contents->GetVisibleURL());
764 // After running the spoof code, |GetActiveWebContents| returns the new tab,
765 // not the same as |web_contents|.
766 ASSERT_NO_FATAL_FAILURE(content::WaitForLoadStop(
767 browser()->tab_strip_model()->GetActiveWebContents()));
768 EXPECT_FALSE(IsViewFocused(VIEW_ID_OMNIBOX));
769}
770
[email protected]629e0342010-07-27 23:30:13771} // namespace