blob: f46298a5e1bfbdcdec318e1273bfc7605afca05e [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"
creis8f3a9a62017-01-12 20:19:2648#include "content/public/test/test_navigation_observer.h"
[email protected]c1dffe82013-06-26 20:59:0549#include "net/test/embedded_test_server/embedded_test_server.h"
[email protected]853300a82010-07-27 21:17:5750
[email protected]e0ee4b42014-05-23 06:16:1351#if defined(OS_WIN)
[email protected]cfd33792014-05-23 20:39:3452#include "base/win/windows_version.h"
[email protected]e0ee4b42014-05-23 06:16:1353#endif
54
[email protected]eaabba22012-03-07 15:02:1155using content::RenderViewHost;
[email protected]4b19ea52012-01-02 20:15:2556using content::WebContents;
57
initial.commit09911bf2008-07-26 23:55:2958namespace {
59
mgiuca88867d32015-07-08 04:08:2460#if defined(OS_POSIX)
[email protected]8bcdec92009-02-25 16:15:1861// The delay waited in some cases where we don't have a notifications for an
62// action we take.
initial.commit09911bf2008-07-26 23:55:2963const int kActionDelayMs = 500;
mgiuca88867d32015-07-08 04:08:2464#endif
initial.commit09911bf2008-07-26 23:55:2965
[email protected]c1dffe82013-06-26 20:59:0566const char kSimplePage[] = "/focus/page_with_focus.html";
67const char kStealFocusPage[] = "/focus/page_steals_focus.html";
68const char kTypicalPage[] = "/focus/typical_page.html";
[email protected]a9c060ca2012-01-05 20:43:4169
[email protected]8bcdec92009-02-25 16:15:1870class BrowserFocusTest : public InProcessBrowserTest {
initial.commit09911bf2008-07-26 23:55:2971 public:
thestige80821242015-09-30 23:46:0872 // InProcessBrowserTest overrides:
dcheng5dd5ff62014-10-21 12:42:3873 void SetUpOnMainThread() override {
svaldeza01f7d92015-11-18 17:47:5674 ASSERT_TRUE(embedded_test_server()->Start());
thestige80821242015-09-30 23:46:0875 }
[email protected]cfd33792014-05-23 20:39:3476
[email protected]21abcc742009-10-23 02:52:0677 bool IsViewFocused(ViewID vid) {
78 return ui_test_utils::IsViewFocused(browser(), vid);
[email protected]b9821882009-08-17 22:25:1779 }
80
[email protected]fc2e0872009-08-21 22:14:4181 void ClickOnView(ViewID vid) {
[email protected]21abcc742009-10-23 02:52:0682 ui_test_utils::ClickOnView(browser(), vid);
[email protected]fc2e0872009-08-21 22:14:4183 }
[email protected]0b8fa8b2011-12-07 00:54:5284
[email protected]cfd33792014-05-23 20:39:3485 void TestFocusTraversal(RenderViewHost* render_view_host, bool reverse) {
86 const char kGetFocusedElementJS[] =
87 "window.domAutomationController.send(getFocusedElement());";
88 const char* kExpectedIDs[] = { "textEdit", "searchButton", "luckyButton",
89 "googleLink", "gmailLink", "gmapLink" };
90 SCOPED_TRACE(base::StringPrintf("TestFocusTraversal: reverse=%d", reverse));
91 ui::KeyboardCode key = ui::VKEY_TAB;
92#if defined(OS_MACOSX)
93 // TODO(msw): Mac requires ui::VKEY_BACKTAB for reverse cycling. Sigh...
94 key = reverse ? ui::VKEY_BACKTAB : ui::VKEY_TAB;
95#elif defined(OS_WIN)
96 // This loop times out on Windows XP with no output. http://crbug.com/376635
97 if (base::win::GetVersion() < base::win::VERSION_VISTA)
98 return;
99#endif
100
101 // Loop through the focus chain twice for good measure.
102 for (size_t i = 0; i < 2; ++i) {
103 SCOPED_TRACE(base::StringPrintf("focus outer loop: %" PRIuS, i));
104 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
miu5d0cfcac2014-10-20 20:46:42105
[email protected]cfd33792014-05-23 20:39:34106 // Mac requires an extra Tab key press to traverse the app menu button
miu5d0cfcac2014-10-20 20:46:42107 // iff "Full Keyboard Access" is enabled. In reverse, four Tab key presses
108 // are required to traverse the back/forward buttons and the tab strip.
109#if defined(OS_MACOSX)
110 if (ui_controls::IsFullKeyboardAccessEnabled()) {
111 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
112 browser(), key, false, reverse, false, false));
113 if (reverse) {
114 for (int j = 0; j < 3; ++j) {
115 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
116 browser(), key, false, reverse, false, false));
117 }
118 }
119 }
120#endif
[email protected]cfd33792014-05-23 20:39:34121
palmere67e1742015-12-11 18:54:55122 if (reverse) {
123 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait(
124 browser(), key, false, reverse, false, false,
125 content::NOTIFICATION_ALL,
126 content::NotificationService::AllSources()));
127 }
128
[email protected]cfd33792014-05-23 20:39:34129 for (size_t j = 0; j < arraysize(kExpectedIDs); ++j) {
130 SCOPED_TRACE(base::StringPrintf("focus inner loop %" PRIuS, j));
131 const size_t index = reverse ? arraysize(kExpectedIDs) - 1 - j : j;
132 // The details are the node's editable state, i.e. true for "textEdit".
133 bool is_editable_node = index == 0;
134
135 // Press Tab (or Shift+Tab) and check the focused element id.
136 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWaitWithDetails(
137 browser(), key, false, reverse, false, false,
138 content::NOTIFICATION_FOCUS_CHANGED_IN_PAGE,
139 content::Source<RenderViewHost>(render_view_host),
140 content::Details<bool>(&is_editable_node)));
141 std::string focused_id;
142 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
143 render_view_host, kGetFocusedElementJS, &focused_id));
144 EXPECT_STREQ(kExpectedIDs[index], focused_id.c_str());
145 }
146
147#if defined(OS_MACOSX)
148 // TODO(msw): Mac doesn't post NOTIFICATION_FOCUS_RETURNED_TO_BROWSER and
149 // would also apparently require extra Tab key presses here. Sigh...
150 chrome::FocusLocationBar(browser());
151#else
152 // On the last Tab key press, focus returns to the browser.
153 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait(
154 browser(), key, false, reverse, false, false,
155 chrome::NOTIFICATION_FOCUS_RETURNED_TO_BROWSER,
156 content::Source<Browser>(browser())));
palmere67e1742015-12-11 18:54:55157 EXPECT_TRUE(
158 IsViewFocused(reverse ? VIEW_ID_OMNIBOX : VIEW_ID_LOCATION_ICON));
159
160 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait(
161 browser(), key, false, reverse, false, false,
162 content::NOTIFICATION_ALL,
163 content::NotificationService::AllSources()));
[email protected]cfd33792014-05-23 20:39:34164#endif
165 content::RunAllPendingInMessageLoop();
palmere67e1742015-12-11 18:54:55166 EXPECT_TRUE(
167 IsViewFocused(reverse ? VIEW_ID_LOCATION_ICON : VIEW_ID_OMNIBOX));
168 if (reverse) {
169 ASSERT_TRUE(ui_test_utils::SendKeyPressAndWait(
170 browser(), key, false, false, false, false,
171 content::NOTIFICATION_ALL,
172 content::NotificationService::AllSources()));
173 }
[email protected]cfd33792014-05-23 20:39:34174 }
[email protected]a9c060ca2012-01-05 20:43:41175 }
initial.commit09911bf2008-07-26 23:55:29176};
177
[email protected]cfd33792014-05-23 20:39:34178// A test interstitial page with typical HTML contents.
179class TestInterstitialPage : public content::InterstitialPageDelegate {
180 public:
181 explicit TestInterstitialPage(WebContents* tab) {
182 base::FilePath file_path;
183 bool success = PathService::Get(chrome::DIR_TEST_DATA, &file_path);
184 EXPECT_TRUE(success);
185 file_path = file_path.AppendASCII("focus/typical_page.html");
186 success = base::ReadFileToString(file_path, &html_contents_);
187 EXPECT_TRUE(success);
188 interstitial_page_ = content::InterstitialPage::Create(
189 tab, true, GURL("http://interstitial.com"), this);
190
191 // Show the interstitial and delay return until it has attached.
192 interstitial_page_->Show();
meacerb47bc8782014-10-07 03:40:01193 content::WaitForInterstitialAttach(tab);
194
[email protected]cfd33792014-05-23 20:39:34195 EXPECT_TRUE(tab->ShowingInterstitialPage());
[email protected]e0ee4b42014-05-23 06:16:13196 }
[email protected]ca9077f2014-05-22 19:00:21197
dcheng5dd5ff62014-10-21 12:42:38198 std::string GetHTMLContents() override { return html_contents_; }
[email protected]cfd33792014-05-23 20:39:34199
[email protected]85f0a572012-02-07 22:20:13200 RenderViewHost* render_view_host() {
khannansf3b27192015-01-09 17:28:17201 return interstitial_page_->GetMainFrame()->GetRenderViewHost();
[email protected]9e0c83a2009-05-06 19:44:37202 }
203
[email protected]cfd33792014-05-23 20:39:34204 void DontProceed() { interstitial_page_->DontProceed(); }
[email protected]4e85c112011-01-26 22:27:21205
avif9ab5d942015-10-15 14:05:44206 bool HasFocus() {
207 return render_view_host()->GetWidget()->GetView()->HasFocus();
208 }
[email protected]9e0c83a2009-05-06 19:44:37209
210 private:
211 std::string html_contents_;
[email protected]cfd33792014-05-23 20:39:34212 content::InterstitialPage* interstitial_page_; // Owns this.
213 DISALLOW_COPY_AND_ASSIGN(TestInterstitialPage);
[email protected]9e0c83a2009-05-06 19:44:37214};
[email protected]b9821882009-08-17 22:25:17215
sky4df637772015-10-22 17:20:18216// Flaky on Mac (http://crbug.com/67301).
217#if defined(OS_MACOSX)
[email protected]839e0802012-05-18 14:45:23218#define MAYBE_ClickingMovesFocus DISABLED_ClickingMovesFocus
[email protected]c39e233d2012-05-18 13:24:12219#else
sky4df637772015-10-22 17:20:18220// If this flakes, disable and log details in http://crbug.com/523255.
[email protected]839e0802012-05-18 14:45:23221#define MAYBE_ClickingMovesFocus ClickingMovesFocus
[email protected]c39e233d2012-05-18 13:24:12222#endif
223IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_ClickingMovesFocus) {
[email protected]a6e602f2010-09-28 22:28:30224 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]0ff0ff32010-12-21 19:34:42225#if defined(OS_POSIX)
[email protected]fc2e0872009-08-21 22:14:41226 // It seems we have to wait a little bit for the widgets to spin up before
227 // we can start clicking on them.
fdoraybd6c0982016-06-11 00:23:05228 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
ki.stfuc4f8e242015-10-09 20:40:20229 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure(),
[email protected]7e560102012-03-08 20:58:42230 base::TimeDelta::FromMilliseconds(kActionDelayMs));
[email protected]729eb632012-07-26 04:45:26231 content::RunMessageLoop();
[email protected]0ff0ff32010-12-21 19:34:42232#endif // defined(OS_POSIX)
[email protected]fc2e0872009-08-21 22:14:41233
[email protected]5c4e4482013-03-03 00:09:18234 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
[email protected]186f13f2009-08-19 20:34:00235
[email protected]fc2e0872009-08-21 22:14:41236 ClickOnView(VIEW_ID_TAB_CONTAINER);
[email protected]f2159ba2012-04-17 19:13:21237 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]186f13f2009-08-19 20:34:00238
[email protected]5c4e4482013-03-03 00:09:18239 ClickOnView(VIEW_ID_OMNIBOX);
240 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
[email protected]186f13f2009-08-19 20:34:00241}
[email protected]186f13f2009-08-19 20:34:00242
[email protected]320948e02011-01-10 08:21:12243// Flaky, http://crbug.com/69034.
[email protected]4d2451652012-02-14 23:54:26244IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_BrowsersRememberFocus) {
[email protected]a6e602f2010-09-28 22:28:30245 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]cfd33792014-05-23 20:39:34246 const GURL url = embedded_test_server()->GetURL(kSimplePage);
[email protected]8bcdec92009-02-25 16:15:18247 ui_test_utils::NavigateToURL(browser(), url);
initial.commit09911bf2008-07-26 23:55:29248
[email protected]90556dd2012-06-07 20:26:18249 gfx::NativeWindow window = browser()->window()->GetNativeWindow();
[email protected]186f13f2009-08-19 20:34:00250
initial.commit09911bf2008-07-26 23:55:29251 // The focus should be on the Tab contents.
[email protected]f2159ba2012-04-17 19:13:21252 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
initial.commit09911bf2008-07-26 23:55:29253 // Now hide the window, show it again, the focus should not have changed.
[email protected]853300a82010-07-27 21:17:57254 ui_test_utils::HideNativeWindow(window);
[email protected]7807088c2011-11-09 15:16:29255 ASSERT_TRUE(ui_test_utils::ShowAndFocusNativeWindow(window));
[email protected]f2159ba2012-04-17 19:13:21256 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
initial.commit09911bf2008-07-26 23:55:29257
[email protected]a37d4b02012-06-25 21:56:10258 chrome::FocusLocationBar(browser());
[email protected]5c4e4482013-03-03 00:09:18259 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
initial.commit09911bf2008-07-26 23:55:29260 // Hide the window, show it again, the focus should not have changed.
[email protected]853300a82010-07-27 21:17:57261 ui_test_utils::HideNativeWindow(window);
[email protected]7807088c2011-11-09 15:16:29262 ASSERT_TRUE(ui_test_utils::ShowAndFocusNativeWindow(window));
[email protected]5c4e4482013-03-03 00:09:18263 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
initial.commit09911bf2008-07-26 23:55:29264}
265
266// Tabs remember focus.
[email protected]26153732010-11-09 18:47:39267// Disabled, http://crbug.com/62542.
268IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_TabsRememberFocus) {
[email protected]a6e602f2010-09-28 22:28:30269 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]cfd33792014-05-23 20:39:34270 const GURL url = embedded_test_server()->GetURL(kSimplePage);
[email protected]8bcdec92009-02-25 16:15:18271 ui_test_utils::NavigateToURL(browser(), url);
272
initial.commit09911bf2008-07-26 23:55:29273 // Create several tabs.
[email protected]52877dbc62012-06-29 22:22:03274 for (int i = 0; i < 4; ++i) {
275 chrome::AddSelectedTabWithURL(browser(), url,
Sylvain Defresnec6ccc77d2014-09-19 10:19:35276 ui::PAGE_TRANSITION_TYPED);
[email protected]52877dbc62012-06-29 22:22:03277 }
initial.commit09911bf2008-07-26 23:55:29278
279 // Alternate focus for the tab.
280 const bool kFocusPage[3][5] = {
281 { true, true, true, true, false },
282 { false, false, false, false, false },
283 { false, true, false, true, false }
284 };
285
286 for (int i = 1; i < 3; i++) {
287 for (int j = 0; j < 5; j++) {
[email protected]8bcdec92009-02-25 16:15:18288 // Activate the tab.
[email protected]57892182012-12-03 19:15:39289 browser()->tab_strip_model()->ActivateTabAt(j, true);
initial.commit09911bf2008-07-26 23:55:29290
291 // Activate the location bar or the page.
[email protected]7e383692009-06-12 19:14:54292 if (kFocusPage[i][j]) {
[email protected]fc2b46b2014-05-03 16:33:45293 browser()->tab_strip_model()->GetWebContentsAt(j)->Focus();
[email protected]7e383692009-06-12 19:14:54294 } else {
[email protected]a37d4b02012-06-25 21:56:10295 chrome::FocusLocationBar(browser());
[email protected]7e383692009-06-12 19:14:54296 }
initial.commit09911bf2008-07-26 23:55:29297 }
298
299 // Now come back to the tab and check the right view is focused.
300 for (int j = 0; j < 5; j++) {
[email protected]8bcdec92009-02-25 16:15:18301 // Activate the tab.
[email protected]57892182012-12-03 19:15:39302 browser()->tab_strip_model()->ActivateTabAt(j, true);
initial.commit09911bf2008-07-26 23:55:29303
[email protected]5c4e4482013-03-03 00:09:18304 ViewID vid = kFocusPage[i][j] ? VIEW_ID_TAB_CONTAINER : VIEW_ID_OMNIBOX;
[email protected]21abcc742009-10-23 02:52:06305 ASSERT_TRUE(IsViewFocused(vid));
initial.commit09911bf2008-07-26 23:55:29306 }
[email protected]cb7e2542009-12-14 22:02:35307
[email protected]57892182012-12-03 19:15:39308 browser()->tab_strip_model()->ActivateTabAt(0, true);
[email protected]cb7e2542009-12-14 22:02:35309 // Try the above, but with ctrl+tab. Since tab normally changes focus,
310 // this has regressed in the past. Loop through several times to be sure.
311 for (int j = 0; j < 15; j++) {
[email protected]f2159ba2012-04-17 19:13:21312 ViewID vid = kFocusPage[i][j % 5] ? VIEW_ID_TAB_CONTAINER :
[email protected]5c4e4482013-03-03 00:09:18313 VIEW_ID_OMNIBOX;
[email protected]cb7e2542009-12-14 22:02:35314 ASSERT_TRUE(IsViewFocused(vid));
315
[email protected]1d000682010-08-23 16:21:28316 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09317 browser(), ui::VKEY_TAB, true, false, false, false));
[email protected]cb7e2542009-12-14 22:02:35318 }
319
320 // As above, but with ctrl+shift+tab.
[email protected]57892182012-12-03 19:15:39321 browser()->tab_strip_model()->ActivateTabAt(4, true);
[email protected]cb7e2542009-12-14 22:02:35322 for (int j = 14; j >= 0; --j) {
[email protected]f2159ba2012-04-17 19:13:21323 ViewID vid = kFocusPage[i][j % 5] ? VIEW_ID_TAB_CONTAINER :
[email protected]5c4e4482013-03-03 00:09:18324 VIEW_ID_OMNIBOX;
[email protected]cb7e2542009-12-14 22:02:35325 ASSERT_TRUE(IsViewFocused(vid));
326
[email protected]1d000682010-08-23 16:21:28327 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
[email protected]b6d81262011-01-13 17:36:09328 browser(), ui::VKEY_TAB, true, true, false, false));
[email protected]cb7e2542009-12-14 22:02:35329 }
initial.commit09911bf2008-07-26 23:55:29330 }
331}
332
[email protected]ae40b572009-10-02 21:17:45333// Tabs remember focus with find-in-page box.
warx9c88c582016-11-10 03:58:24334IN_PROC_BROWSER_TEST_F(BrowserFocusTest, TabsRememberFocusFindInPage) {
[email protected]cfd33792014-05-23 20:39:34335 const GURL url = embedded_test_server()->GetURL(kSimplePage);
[email protected]ae40b572009-10-02 21:17:45336 ui_test_utils::NavigateToURL(browser(), url);
337
[email protected]a37d4b02012-06-25 21:56:10338 chrome::Find(browser());
[email protected]cc872372013-01-28 21:57:07339 ui_test_utils::FindInPage(
340 browser()->tab_strip_model()->GetActiveWebContents(),
[email protected]1f136902013-12-24 23:26:45341 base::ASCIIToUTF16("a"), true, false, NULL, NULL);
[email protected]21abcc742009-10-23 02:52:06342 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]ae40b572009-10-02 21:17:45343
344 // Focus the location bar.
[email protected]a37d4b02012-06-25 21:56:10345 chrome::FocusLocationBar(browser());
[email protected]ae40b572009-10-02 21:17:45346
347 // Create a 2nd tab.
Sylvain Defresnec6ccc77d2014-09-19 10:19:35348 chrome::AddSelectedTabWithURL(browser(), url, ui::PAGE_TRANSITION_TYPED);
[email protected]ae40b572009-10-02 21:17:45349
350 // Focus should be on the recently opened tab page.
[email protected]f2159ba2012-04-17 19:13:21351 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]ae40b572009-10-02 21:17:45352
353 // Select 1st tab, focus should still be on the location-bar.
354 // (bug http://crbug.com/23296)
[email protected]57892182012-12-03 19:15:39355 browser()->tab_strip_model()->ActivateTabAt(0, true);
[email protected]5c4e4482013-03-03 00:09:18356 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
[email protected]ae40b572009-10-02 21:17:45357
358 // Now open the find box again, switch to another tab and come back, the focus
359 // should return to the find box.
[email protected]a37d4b02012-06-25 21:56:10360 chrome::Find(browser());
[email protected]21abcc742009-10-23 02:52:06361 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]57892182012-12-03 19:15:39362 browser()->tab_strip_model()->ActivateTabAt(1, true);
[email protected]f2159ba2012-04-17 19:13:21363 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]57892182012-12-03 19:15:39364 browser()->tab_strip_model()->ActivateTabAt(0, true);
[email protected]21abcc742009-10-23 02:52:06365 ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]ae40b572009-10-02 21:17:45366}
367
initial.commit09911bf2008-07-26 23:55:29368// Background window does not steal focus.
[email protected]c92928672010-11-09 18:31:07369// Flaky, http://crbug.com/62538.
370IN_PROC_BROWSER_TEST_F(BrowserFocusTest,
[email protected]4d2451652012-02-14 23:54:26371 DISABLED_BackgroundBrowserDontStealFocus) {
[email protected]a6e602f2010-09-28 22:28:30372 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
initial.commit09911bf2008-07-26 23:55:29373
initial.commit09911bf2008-07-26 23:55:29374 // Open a new browser window.
scottmg851949002016-02-09 20:09:44375 Browser* browser2 = new Browser(Browser::CreateParams(browser()->profile()));
376
[email protected]8bcdec92009-02-25 16:15:18377 ASSERT_TRUE(browser2);
[email protected]00509a32013-11-17 17:45:37378 chrome::AddTabAt(browser2, GURL(), -1, true);
[email protected]8bcdec92009-02-25 16:15:18379 browser2->window()->Show();
[email protected]186f13f2009-08-19 20:34:00380
[email protected]ed179ee2009-10-03 21:02:51381 Browser* focused_browser = NULL;
382 Browser* unfocused_browser = NULL;
[email protected]753efc42010-03-09 19:52:16383#if defined(USE_X11)
384 // On X11, calling Activate() is not guaranteed to move focus, so we have
[email protected]186f13f2009-08-19 20:34:00385 // to figure out which browser does have focus.
386 if (browser2->window()->IsActive()) {
387 focused_browser = browser2;
388 unfocused_browser = browser();
389 } else if (browser()->window()->IsActive()) {
390 focused_browser = browser();
391 unfocused_browser = browser2;
392 } else {
[email protected]579c3d82010-10-06 03:53:51393 FAIL() << "Could not determine which browser has focus";
[email protected]186f13f2009-08-19 20:34:00394 }
395#elif defined(OS_WIN)
396 focused_browser = browser();
397 unfocused_browser = browser2;
[email protected]853300a82010-07-27 21:17:57398#elif defined(OS_MACOSX)
399 // On Mac, the newly created window always gets the focus.
400 focused_browser = browser2;
401 unfocused_browser = browser();
[email protected]186f13f2009-08-19 20:34:00402#endif
403
[email protected]cfd33792014-05-23 20:39:34404 const GURL steal_focus_url = embedded_test_server()->GetURL(kStealFocusPage);
[email protected]186f13f2009-08-19 20:34:00405 ui_test_utils::NavigateToURL(unfocused_browser, steal_focus_url);
[email protected]1e187af2009-02-25 02:02:46406
[email protected]8bcdec92009-02-25 16:15:18407 // Activate the first browser.
[email protected]186f13f2009-08-19 20:34:00408 focused_browser->window()->Activate();
initial.commit09911bf2008-07-26 23:55:29409
[email protected]b6987e02013-01-04 18:30:43410 ASSERT_TRUE(content::ExecuteScript(
[email protected]da2bfa42013-01-29 23:47:05411 unfocused_browser->tab_strip_model()->GetActiveWebContents(),
[email protected]06bc5d92013-01-02 22:44:13412 "stealFocus();"));
initial.commit09911bf2008-07-26 23:55:29413
[email protected]8bcdec92009-02-25 16:15:18414 // Make sure the first browser is still active.
[email protected]186f13f2009-08-19 20:34:00415 EXPECT_TRUE(focused_browser->window()->IsActive());
initial.commit09911bf2008-07-26 23:55:29416}
417
418// Page cannot steal focus when focus is on location bar.
warx0faadcf2016-11-11 23:13:11419IN_PROC_BROWSER_TEST_F(BrowserFocusTest, LocationBarLockFocus) {
initial.commit09911bf2008-07-26 23:55:29420 // Open the page that steals focus.
[email protected]cfd33792014-05-23 20:39:34421 const GURL url = embedded_test_server()->GetURL(kStealFocusPage);
[email protected]8bcdec92009-02-25 16:15:18422 ui_test_utils::NavigateToURL(browser(), url);
initial.commit09911bf2008-07-26 23:55:29423
[email protected]a37d4b02012-06-25 21:56:10424 chrome::FocusLocationBar(browser());
initial.commit09911bf2008-07-26 23:55:29425
[email protected]b6987e02013-01-04 18:30:43426 ASSERT_TRUE(content::ExecuteScript(
[email protected]cc872372013-01-28 21:57:07427 browser()->tab_strip_model()->GetActiveWebContents(),
[email protected]06bc5d92013-01-02 22:44:13428 "stealFocus();"));
initial.commit09911bf2008-07-26 23:55:29429
430 // Make sure the location bar is still focused.
[email protected]5c4e4482013-03-03 00:09:18431 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
initial.commit09911bf2008-07-26 23:55:29432}
433
[email protected]cfd33792014-05-23 20:39:34434// Test forward and reverse focus traversal on a typical page.
bnc0f8fe372014-11-24 19:42:51435// Disabled for Mac because it is flaky on "Mac10.9 Tests (dbg)",
436// see https://crbug.com/60973.
437#if defined(OS_MACOSX)
438#define MAYBE_FocusTraversal DISABLED_FocusTraversal
439#else
440#define MAYBE_FocusTraversal FocusTraversal
441#endif
442IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusTraversal) {
[email protected]a6e602f2010-09-28 22:28:30443 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]cfd33792014-05-23 20:39:34444 const GURL url = embedded_test_server()->GetURL(kTypicalPage);
[email protected]8bcdec92009-02-25 16:15:18445 ui_test_utils::NavigateToURL(browser(), url);
[email protected]cfd33792014-05-23 20:39:34446 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]a37d4b02012-06-25 21:56:10447 chrome::FocusLocationBar(browser());
initial.commit09911bf2008-07-26 23:55:29448
[email protected]cfd33792014-05-23 20:39:34449 WebContents* tab = browser()->tab_strip_model()->GetActiveWebContents();
450 EXPECT_NO_FATAL_FAILURE(TestFocusTraversal(tab->GetRenderViewHost(), false));
451 EXPECT_NO_FATAL_FAILURE(TestFocusTraversal(tab->GetRenderViewHost(), true));
initial.commit09911bf2008-07-26 23:55:29452}
453
[email protected]cfd33792014-05-23 20:39:34454// Test forward and reverse focus traversal while an interstitial is showing.
[email protected]6d731ed2014-06-02 14:53:29455// Disabled, see http://crbug.com/60973
thestige80821242015-09-30 23:46:08456IN_PROC_BROWSER_TEST_F(BrowserFocusTest,
457 DISABLED_FocusTraversalOnInterstitial) {
[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(kSimplePage);
[email protected]9e0c83a2009-05-06 19:44:37460 ui_test_utils::NavigateToURL(browser(), url);
[email protected]cfd33792014-05-23 20:39:34461 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]9e0c83a2009-05-06 19:44:37462
[email protected]cfd33792014-05-23 20:39:34463 // Create and show a test interstitial page.
[email protected]cc872372013-01-28 21:57:07464 TestInterstitialPage* interstitial_page = new TestInterstitialPage(
[email protected]cfd33792014-05-23 20:39:34465 browser()->tab_strip_model()->GetActiveWebContents());
466 content::RenderViewHost* host = interstitial_page->render_view_host();
[email protected]9e0c83a2009-05-06 19:44:37467
[email protected]cfd33792014-05-23 20:39:34468 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]a37d4b02012-06-25 21:56:10469 chrome::FocusLocationBar(browser());
[email protected]cfd33792014-05-23 20:39:34470 EXPECT_NO_FATAL_FAILURE(TestFocusTraversal(host, false));
471 EXPECT_NO_FATAL_FAILURE(TestFocusTraversal(host, true));
[email protected]9e0c83a2009-05-06 19:44:37472}
473
[email protected]cfd33792014-05-23 20:39:34474// Test the transfer of focus when an interstitial is shown and hidden.
475IN_PROC_BROWSER_TEST_F(BrowserFocusTest, InterstitialFocus) {
[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]e0ee4b42014-05-23 06:16:13478 ui_test_utils::NavigateToURL(browser(), url);
[email protected]cfd33792014-05-23 20:39:34479 WebContents* tab = browser()->tab_strip_model()->GetActiveWebContents();
480 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
avif9ab5d942015-10-15 14:05:44481 EXPECT_TRUE(tab->GetRenderViewHost()->GetWidget()->GetView()->HasFocus());
[email protected]e0ee4b42014-05-23 06:16:13482
[email protected]cfd33792014-05-23 20:39:34483 // Create and show a test interstitial page; it should gain focus.
484 TestInterstitialPage* interstitial_page = new TestInterstitialPage(tab);
485 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]9e0c83a2009-05-06 19:44:37486 EXPECT_TRUE(interstitial_page->HasFocus());
487
[email protected]cfd33792014-05-23 20:39:34488 // Hide the interstitial; the original page should gain focus.
[email protected]9e0c83a2009-05-06 19:44:37489 interstitial_page->DontProceed();
[email protected]cfd33792014-05-23 20:39:34490 content::RunAllPendingInMessageLoop();
491 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
avif9ab5d942015-10-15 14:05:44492 EXPECT_TRUE(tab->GetRenderViewHost()->GetWidget()->GetView()->HasFocus());
[email protected]9e0c83a2009-05-06 19:44:37493}
494
[email protected]cfd33792014-05-23 20:39:34495// Test that find-in-page UI can request focus, even when it is already open.
sky4df637772015-10-22 17:20:18496#if defined(OS_MACOSX)
John Abd-El-Malekf3fdd622014-09-09 04:04:23497#define MAYBE_FindFocusTest DISABLED_FindFocusTest
498#else
sky4df637772015-10-22 17:20:18499// If this flakes, disable and log details in http://crbug.com/523255.
John Abd-El-Malekf3fdd622014-09-09 04:04:23500#define MAYBE_FindFocusTest FindFocusTest
501#endif
502IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FindFocusTest) {
[email protected]cfd33792014-05-23 20:39:34503 chrome::DisableFindBarAnimationsDuringTesting(true);
[email protected]a6e602f2010-09-28 22:28:30504 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]cfd33792014-05-23 20:39:34505 const GURL url = embedded_test_server()->GetURL(kTypicalPage);
[email protected]e0ee4b42014-05-23 06:16:13506 ui_test_utils::NavigateToURL(browser(), url);
[email protected]cfd33792014-05-23 20:39:34507 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]e0ee4b42014-05-23 06:16:13508
[email protected]cfd33792014-05-23 20:39:34509 chrome::ShowFindBar(browser());
510 EXPECT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]9bd491ee2008-12-10 22:31:07511
[email protected]a37d4b02012-06-25 21:56:10512 chrome::FocusLocationBar(browser());
[email protected]cfd33792014-05-23 20:39:34513 EXPECT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
[email protected]9bd491ee2008-12-10 22:31:07514
[email protected]cfd33792014-05-23 20:39:34515 chrome::ShowFindBar(browser());
516 EXPECT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]9bd491ee2008-12-10 22:31:07517
[email protected]fc2e0872009-08-21 22:14:41518 ClickOnView(VIEW_ID_TAB_CONTAINER);
[email protected]cfd33792014-05-23 20:39:34519 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]9bd491ee2008-12-10 22:31:07520
[email protected]cfd33792014-05-23 20:39:34521 chrome::ShowFindBar(browser());
522 EXPECT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD));
[email protected]9bd491ee2008-12-10 22:31:07523}
[email protected]401513c2009-03-12 00:21:28524
525// Makes sure the focus is in the right location when opening the different
526// types of tabs.
[email protected]c92928672010-11-09 18:31:07527// Flaky, http://crbug.com/62539.
[email protected]4d2451652012-02-14 23:54:26528IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_TabInitialFocus) {
[email protected]a6e602f2010-09-28 22:28:30529 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]9ba21ede2010-07-30 01:11:07530
[email protected]401513c2009-03-12 00:21:28531 // Open the history tab, focus should be on the tab contents.
[email protected]5d9cace72012-06-21 16:07:12532 chrome::ShowHistory(browser());
[email protected]6ddc6e3f22012-07-26 01:15:35533 ASSERT_NO_FATAL_FAILURE(content::WaitForLoadStop(
[email protected]cc872372013-01-28 21:57:07534 browser()->tab_strip_model()->GetActiveWebContents()));
[email protected]f2159ba2012-04-17 19:13:21535 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]401513c2009-03-12 00:21:28536
537 // Open the new tab, focus should be on the location bar.
[email protected]a37d4b02012-06-25 21:56:10538 chrome::NewTab(browser());
[email protected]6ddc6e3f22012-07-26 01:15:35539 ASSERT_NO_FATAL_FAILURE(content::WaitForLoadStop(
[email protected]cc872372013-01-28 21:57:07540 browser()->tab_strip_model()->GetActiveWebContents()));
[email protected]5c4e4482013-03-03 00:09:18541 EXPECT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
[email protected]401513c2009-03-12 00:21:28542
543 // Open the download tab, focus should be on the tab contents.
[email protected]5d9cace72012-06-21 16:07:12544 chrome::ShowDownloads(browser());
[email protected]6ddc6e3f22012-07-26 01:15:35545 ASSERT_NO_FATAL_FAILURE(content::WaitForLoadStop(
[email protected]cc872372013-01-28 21:57:07546 browser()->tab_strip_model()->GetActiveWebContents()));
[email protected]f2159ba2012-04-17 19:13:21547 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]3e3f0eb2009-06-22 18:33:43548
549 // Open about:blank, focus should be on the location bar.
[email protected]8e09c7af2014-06-10 11:46:17550 chrome::AddSelectedTabWithURL(
Sylvain Defresnec6ccc77d2014-09-19 10:19:35551 browser(), GURL(url::kAboutBlankURL), ui::PAGE_TRANSITION_LINK);
[email protected]6ddc6e3f22012-07-26 01:15:35552 ASSERT_NO_FATAL_FAILURE(content::WaitForLoadStop(
[email protected]cc872372013-01-28 21:57:07553 browser()->tab_strip_model()->GetActiveWebContents()));
[email protected]5c4e4482013-03-03 00:09:18554 EXPECT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
[email protected]401513c2009-03-12 00:21:28555}
[email protected]9d8a4642009-07-29 17:25:30556
557// Tests that focus goes where expected when using reload.
warx0faadcf2016-11-11 23:13:11558IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FocusOnReload) {
[email protected]9d8a4642009-07-29 17:25:30559 // Open the new tab, reload.
[email protected]6b4e8e42011-08-17 19:36:06560 {
[email protected]a7fe9112012-07-20 02:34:45561 content::WindowedNotificationObserver observer(
[email protected]6b4e8e42011-08-17 19:36:06562 content::NOTIFICATION_LOAD_STOP,
[email protected]ad50def52011-10-19 23:17:07563 content::NotificationService::AllSources());
[email protected]a37d4b02012-06-25 21:56:10564 chrome::NewTab(browser());
[email protected]6b4e8e42011-08-17 19:36:06565 observer.Wait();
566 }
[email protected]b8deecd2012-07-30 21:09:44567 content::RunAllPendingInMessageLoop();
[email protected]f07d7bf2010-04-06 08:02:42568
[email protected]6b4e8e42011-08-17 19:36:06569 {
[email protected]a7fe9112012-07-20 02:34:45570 content::WindowedNotificationObserver observer(
[email protected]6b4e8e42011-08-17 19:36:06571 content::NOTIFICATION_LOAD_STOP,
[email protected]cfd33792014-05-23 20:39:34572 content::Source<content::NavigationController>(
[email protected]cc872372013-01-28 21:57:07573 &browser()->tab_strip_model()->GetActiveWebContents()->
574 GetController()));
nick3b04f322016-08-31 19:29:19575 chrome::Reload(browser(), WindowOpenDisposition::CURRENT_TAB);
[email protected]6b4e8e42011-08-17 19:36:06576 observer.Wait();
577 }
[email protected]9d8a4642009-07-29 17:25:30578 // Focus should stay on the location bar.
[email protected]5c4e4482013-03-03 00:09:18579 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
[email protected]9d8a4642009-07-29 17:25:30580
581 // Open a regular page, focus the location bar, reload.
[email protected]c1dffe82013-06-26 20:59:05582 ui_test_utils::NavigateToURL(browser(),
583 embedded_test_server()->GetURL(kSimplePage));
[email protected]a37d4b02012-06-25 21:56:10584 chrome::FocusLocationBar(browser());
[email protected]5c4e4482013-03-03 00:09:18585 ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
[email protected]6b4e8e42011-08-17 19:36:06586 {
[email protected]a7fe9112012-07-20 02:34:45587 content::WindowedNotificationObserver observer(
[email protected]6b4e8e42011-08-17 19:36:06588 content::NOTIFICATION_LOAD_STOP,
[email protected]cfd33792014-05-23 20:39:34589 content::Source<content::NavigationController>(
[email protected]cc872372013-01-28 21:57:07590 &browser()->tab_strip_model()->GetActiveWebContents()->
591 GetController()));
nick3b04f322016-08-31 19:29:19592 chrome::Reload(browser(), WindowOpenDisposition::CURRENT_TAB);
[email protected]6b4e8e42011-08-17 19:36:06593 observer.Wait();
594 }
[email protected]9ba21ede2010-07-30 01:11:07595
[email protected]9d8a4642009-07-29 17:25:30596 // Focus should now be on the tab contents.
[email protected]5d9cace72012-06-21 16:07:12597 chrome::ShowDownloads(browser());
[email protected]f2159ba2012-04-17 19:13:21598 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]9d8a4642009-07-29 17:25:30599}
600
601// Tests that focus goes where expected when using reload on a crashed tab.
[email protected]160f29ad2010-10-28 15:43:27602IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_FocusOnReloadCrashedTab) {
[email protected]a6e602f2010-09-28 22:28:30603 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]9d8a4642009-07-29 17:25:30604
[email protected]9d8a4642009-07-29 17:25:30605 // Open a regular page, crash, reload.
[email protected]c1dffe82013-06-26 20:59:05606 ui_test_utils::NavigateToURL(browser(),
607 embedded_test_server()->GetURL(kSimplePage));
[email protected]cc872372013-01-28 21:57:07608 content::CrashTab(browser()->tab_strip_model()->GetActiveWebContents());
[email protected]6b4e8e42011-08-17 19:36:06609 {
[email protected]a7fe9112012-07-20 02:34:45610 content::WindowedNotificationObserver observer(
[email protected]6b4e8e42011-08-17 19:36:06611 content::NOTIFICATION_LOAD_STOP,
[email protected]cfd33792014-05-23 20:39:34612 content::Source<content::NavigationController>(
[email protected]cc872372013-01-28 21:57:07613 &browser()->tab_strip_model()->GetActiveWebContents()->
614 GetController()));
nick3b04f322016-08-31 19:29:19615 chrome::Reload(browser(), WindowOpenDisposition::CURRENT_TAB);
[email protected]6b4e8e42011-08-17 19:36:06616 observer.Wait();
617 }
[email protected]9ba21ede2010-07-30 01:11:07618
[email protected]9d8a4642009-07-29 17:25:30619 // Focus should now be on the tab contents.
[email protected]5d9cace72012-06-21 16:07:12620 chrome::ShowDownloads(browser());
[email protected]f2159ba2012-04-17 19:13:21621 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]9d8a4642009-07-29 17:25:30622}
[email protected]629e0342010-07-27 23:30:13623
[email protected]09475062013-10-21 21:29:56624// Tests that focus goes to frame after crashed tab.
625// TODO(shrikant): Find out where the focus should be deterministically.
626// Currently focused_view after crash seem to be non null in debug mode
627// (invalidated pointer 0xcccccc).
628IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_FocusAfterCrashedTab) {
629 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
[email protected]09475062013-10-21 21:29:56630
631 content::CrashTab(browser()->tab_strip_model()->GetActiveWebContents());
632
633 ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
634}
635
[email protected]895a84722012-11-29 18:45:44636// Tests that when a new tab is opened from the omnibox, the focus is moved from
637// the omnibox for the current tab.
[email protected]cfd33792014-05-23 20:39:34638IN_PROC_BROWSER_TEST_F(BrowserFocusTest, NavigateFromOmniboxIntoNewTab) {
[email protected]895a84722012-11-29 18:45:44639 GURL url("http://www.google.com/");
640 GURL url2("http://maps.google.com/");
641
642 // Navigate to url.
Sylvain Defresnec6ccc77d2014-09-19 10:19:35643 chrome::NavigateParams p(browser(), url, ui::PAGE_TRANSITION_LINK);
[email protected]895a84722012-11-29 18:45:44644 p.window_action = chrome::NavigateParams::SHOW_WINDOW;
nick3b04f322016-08-31 19:29:19645 p.disposition = WindowOpenDisposition::CURRENT_TAB;
[email protected]895a84722012-11-29 18:45:44646 chrome::Navigate(&p);
647
648 // Focus the omnibox.
649 chrome::FocusLocationBar(browser());
650
[email protected]cfd33792014-05-23 20:39:34651 OmniboxEditController* controller = browser()->window()->GetLocationBar()->
652 GetOmniboxView()->model()->controller();
[email protected]895a84722012-11-29 18:45:44653
654 // Simulate an alt-enter.
nick3b04f322016-08-31 19:29:19655 controller->OnAutocompleteAccept(
656 url2, WindowOpenDisposition::NEW_FOREGROUND_TAB,
657 ui::PAGE_TRANSITION_TYPED, AutocompleteMatchType::URL_WHAT_YOU_TYPED);
[email protected]895a84722012-11-29 18:45:44658
659 // Make sure the second tab is selected.
[email protected]04e3ead52013-01-14 01:09:20660 EXPECT_EQ(1, browser()->tab_strip_model()->active_index());
[email protected]895a84722012-11-29 18:45:44661
662 // The tab contents should have the focus in the second tab.
[email protected]cfd33792014-05-23 20:39:34663 EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER));
[email protected]895a84722012-11-29 18:45:44664
665 // Go back to the first tab. The focus should not be in the omnibox.
666 chrome::SelectPreviousTab(browser());
[email protected]04e3ead52013-01-14 01:09:20667 EXPECT_EQ(0, browser()->tab_strip_model()->active_index());
[email protected]cfd33792014-05-23 20:39:34668 EXPECT_FALSE(IsViewFocused(VIEW_ID_OMNIBOX));
[email protected]895a84722012-11-29 18:45:44669}
670
tsergeantecbf1e112016-11-22 03:40:12671// Flaky on Windows and Mac (http://crbug.com/665296).
672#if defined(OS_WIN) || defined(OS_MACOSX)
Tsuyoshi Horoc868c2f2016-11-15 05:21:38673#define MAYBE_FocusOnNavigate DISABLED_FocusOnNavigate
674#else
675#define MAYBE_FocusOnNavigate FocusOnNavigate
676#endif
677IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusOnNavigate) {
warx3e256ad2016-11-16 19:14:04678 // Needed on Mac.
679 // TODO(warx): check why it is needed on Mac.
[email protected]c744f7d22013-04-12 20:29:04680 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
681 // Load the NTP.
[email protected]19506d542013-10-15 23:11:06682 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUINewTabURL));
[email protected]c744f7d22013-04-12 20:29:04683 EXPECT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
684
685 // Navigate to another page.
686 const base::FilePath::CharType* kEmptyFile = FILE_PATH_LITERAL("empty.html");
687 GURL file_url(ui_test_utils::GetTestUrl(base::FilePath(
688 base::FilePath::kCurrentDirectory), base::FilePath(kEmptyFile)));
689 ui_test_utils::NavigateToURL(browser(), file_url);
690
691 ClickOnView(VIEW_ID_TAB_CONTAINER);
692
693 // Navigate back. Should focus the location bar.
694 {
695 content::WindowedNotificationObserver back_nav_observer(
696 content::NOTIFICATION_NAV_ENTRY_COMMITTED,
697 content::NotificationService::AllSources());
nick3b04f322016-08-31 19:29:19698 chrome::GoBack(browser(), WindowOpenDisposition::CURRENT_TAB);
[email protected]c744f7d22013-04-12 20:29:04699 back_nav_observer.Wait();
700 }
701
702 EXPECT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX));
703
704 // Navigate forward. Shouldn't focus the location bar.
705 ClickOnView(VIEW_ID_TAB_CONTAINER);
706 {
707 content::WindowedNotificationObserver forward_nav_observer(
708 content::NOTIFICATION_NAV_ENTRY_COMMITTED,
709 content::NotificationService::AllSources());
nick3b04f322016-08-31 19:29:19710 chrome::GoForward(browser(), WindowOpenDisposition::CURRENT_TAB);
[email protected]c744f7d22013-04-12 20:29:04711 forward_nav_observer.Wait();
712 }
713
714 EXPECT_FALSE(IsViewFocused(VIEW_ID_OMNIBOX));
715}
716
palmerc70cb1f2016-03-04 23:41:26717// Ensure that crbug.com/567445 does not regress. This test checks that the
718// Omnibox does not get focused when loading about:blank in a case where it's
719// not the startup URL, e.g. when a page opens a popup to about:blank, with a
720// null opener, and then navigates it. This is a potential security issue; see
721// comments in |WebContentsImpl::FocusLocationBarByDefault|.
722IN_PROC_BROWSER_TEST_F(BrowserFocusTest, AboutBlankNavigationLocationTest) {
creis8f3a9a62017-01-12 20:19:26723 const GURL url1 = embedded_test_server()->GetURL("/title1.html");
palmerc70cb1f2016-03-04 23:41:26724 ui_test_utils::NavigateToURL(browser(), url1);
725
726 TabStripModel* tab_strip = browser()->tab_strip_model();
727 WebContents* web_contents = tab_strip->GetActiveWebContents();
728
creis8f3a9a62017-01-12 20:19:26729 const GURL url2 = embedded_test_server()->GetURL("/title2.html");
730 const std::string spoof =
731 "var w = window.open('about:blank'); w.opener = null;"
732 "w.document.location = '" + url2.spec() + "';";
palmerc70cb1f2016-03-04 23:41:26733
734 ASSERT_TRUE(content::ExecuteScript(web_contents, spoof));
735 EXPECT_EQ(url1, web_contents->GetVisibleURL());
736 // After running the spoof code, |GetActiveWebContents| returns the new tab,
737 // not the same as |web_contents|.
738 ASSERT_NO_FATAL_FAILURE(content::WaitForLoadStop(
739 browser()->tab_strip_model()->GetActiveWebContents()));
740 EXPECT_FALSE(IsViewFocused(VIEW_ID_OMNIBOX));
741}
742
creis8f3a9a62017-01-12 20:19:26743// Regression test for https://crbug.com/677716. This ensures that the omnibox
744// does not get focused if another tab in the same window navigates to the New
745// Tab Page, since that can scroll the origin of the selected tab out of view.
746IN_PROC_BROWSER_TEST_F(BrowserFocusTest, NoFocusForBackgroundNTP) {
747 // Start at the NTP and navigate to a test page. We will later go back to the
748 // NTP, which gives the omnibox focus in some cases.
749 chrome::NewTab(browser());
750 ui_test_utils::NavigateToURL(browser(),
751 embedded_test_server()->GetURL("/title1.html"));
752
753 TabStripModel* tab_strip = browser()->tab_strip_model();
754 WebContents* opener_web_contents = tab_strip->GetActiveWebContents();
755
756 // Open a second tab from the test page.
757 const GURL new_url = embedded_test_server()->GetURL("/title2.html");
758 const std::string open_script = "window.open('" + new_url.spec() + "');";
759 content::WebContentsAddedObserver open_observer;
760 ASSERT_TRUE(content::ExecuteScript(opener_web_contents, open_script));
761 WebContents* new_web_contents = open_observer.GetWebContents();
762
763 // Tell the first (non-selected) tab to go back. This should not give the
764 // omnibox focus, since the navigation occurred in a different tab. Otherwise
765 // the focus may scroll the origin out of view, making a spoof possible.
766 const std::string go_back_script = "window.opener.history.back();";
767 content::TestNavigationObserver back_observer(opener_web_contents);
768 ASSERT_TRUE(content::ExecuteScript(new_web_contents, go_back_script));
769 back_observer.Wait();
770 EXPECT_FALSE(IsViewFocused(VIEW_ID_OMNIBOX));
771}
772
[email protected]629e0342010-07-27 23:30:13773} // namespace