[email protected] | a9c060ca | 2012-01-05 20:43:41 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
avi | 655876a | 2015-12-25 07:18:15 | [diff] [blame] | 5 | #include <stddef.h> |
| 6 | |
[email protected] | a9c060ca | 2012-01-05 20:43:41 | [diff] [blame] | 7 | #include "base/bind.h" |
thestig | 18dfb7a5 | 2014-08-26 10:44:04 | [diff] [blame] | 8 | #include "base/files/file_util.h" |
[email protected] | f07d7bf | 2010-04-06 08:02:42 | [diff] [blame] | 9 | #include "base/format_macros.h" |
fdoray | bd6c098 | 2016-06-11 00:23:05 | [diff] [blame] | 10 | #include "base/location.h" |
avi | 655876a | 2015-12-25 07:18:15 | [diff] [blame] | 11 | #include "base/macros.h" |
[email protected] | 3cc55ad | 2013-07-17 22:17:41 | [diff] [blame] | 12 | #include "base/message_loop/message_loop.h" |
[email protected] | 3985ba8 | 2010-07-29 21:44:12 | [diff] [blame] | 13 | #include "base/path_service.h" |
fdoray | bd6c098 | 2016-06-11 00:23:05 | [diff] [blame] | 14 | #include "base/single_thread_task_runner.h" |
[email protected] | 1152118 | 2013-06-11 04:06:36 | [diff] [blame] | 15 | #include "base/strings/stringprintf.h" |
[email protected] | 5846d58 | 2013-06-08 16:02:12 | [diff] [blame] | 16 | #include "base/strings/utf_string_conversions.h" |
fdoray | bd6c098 | 2016-06-11 00:23:05 | [diff] [blame] | 17 | #include "base/threading/thread_task_runner_handle.h" |
avi | 655876a | 2015-12-25 07:18:15 | [diff] [blame] | 18 | #include "build/build_config.h" |
[email protected] | dcc8fbc | 2013-07-12 00:54:09 | [diff] [blame] | 19 | #include "chrome/browser/chrome_notification_types.h" |
[email protected] | 7b5dc00 | 2010-11-16 23:08:10 | [diff] [blame] | 20 | #include "chrome/browser/ui/browser.h" |
[email protected] | a37d4b0 | 2012-06-25 21:56:10 | [diff] [blame] | 21 | #include "chrome/browser/ui/browser_commands.h" |
thestig | e8082124 | 2015-09-30 23:46:08 | [diff] [blame] | 22 | #include "chrome/browser/ui/browser_navigator_params.h" |
[email protected] | 52877dbc6 | 2012-06-29 22:22:03 | [diff] [blame] | 23 | #include "chrome/browser/ui/browser_tabstrip.h" |
[email protected] | 00070c73 | 2011-04-09 15:31:33 | [diff] [blame] | 24 | #include "chrome/browser/ui/browser_window.h" |
[email protected] | 5d9cace7 | 2012-06-21 16:07:12 | [diff] [blame] | 25 | #include "chrome/browser/ui/chrome_pages.h" |
[email protected] | cfd3379 | 2014-05-23 20:39:34 | [diff] [blame] | 26 | #include "chrome/browser/ui/find_bar/find_bar_host_unittest_util.h" |
[email protected] | d57ce6a | 2014-07-03 15:39:23 | [diff] [blame] | 27 | #include "chrome/browser/ui/location_bar/location_bar.h" |
[email protected] | b56e2e3 | 2012-05-11 21:18:04 | [diff] [blame] | 28 | #include "chrome/browser/ui/tabs/tab_strip_model.h" |
[email protected] | 6a3ec231 | 2010-12-02 19:30:19 | [diff] [blame] | 29 | #include "chrome/browser/ui/view_ids.h" |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 30 | #include "chrome/common/chrome_paths.h" |
[email protected] | cecc93a | 2010-10-05 15:58:55 | [diff] [blame] | 31 | #include "chrome/common/url_constants.h" |
[email protected] | af44e7fb | 2011-07-29 18:32:32 | [diff] [blame] | 32 | #include "chrome/test/base/in_process_browser_test.h" |
[email protected] | 3ea1b18 | 2013-02-08 22:38:41 | [diff] [blame] | 33 | #include "chrome/test/base/interactive_test_utils.h" |
[email protected] | 477ae05 | 2011-11-18 23:53:57 | [diff] [blame] | 34 | #include "chrome/test/base/ui_test_utils.h" |
rdevlin.cronin | b2aa4f4 | 2016-07-02 02:23:34 | [diff] [blame] | 35 | #include "components/omnibox/browser/autocomplete_match_type.h" |
blundell | 3f0c8bb4 | 2015-08-03 18:55:05 | [diff] [blame] | 36 | #include "components/omnibox/browser/omnibox_edit_controller.h" |
blundell | 7dbd379 | 2015-08-05 15:14:19 | [diff] [blame] | 37 | #include "components/omnibox/browser/omnibox_edit_model.h" |
| 38 | #include "components/omnibox/browser/omnibox_view.h" |
[email protected] | cadaec5 | 2012-02-08 21:53:13 | [diff] [blame] | 39 | #include "content/public/browser/interstitial_page.h" |
[email protected] | 85f0a57 | 2012-02-07 22:20:13 | [diff] [blame] | 40 | #include "content/public/browser/interstitial_page_delegate.h" |
[email protected] | 477ae05 | 2011-11-18 23:53:57 | [diff] [blame] | 41 | #include "content/public/browser/notification_service.h" |
khannans | f3b2719 | 2015-01-09 17:28:17 | [diff] [blame] | 42 | #include "content/public/browser/render_frame_host.h" |
[email protected] | 9c1662b | 2012-03-06 15:44:33 | [diff] [blame] | 43 | #include "content/public/browser/render_view_host.h" |
avi | f9ab5d94 | 2015-10-15 14:05:44 | [diff] [blame] | 44 | #include "content/public/browser/render_widget_host.h" |
[email protected] | 5626b089 | 2012-02-20 14:46:58 | [diff] [blame] | 45 | #include "content/public/browser/render_widget_host_view.h" |
[email protected] | 6acde635 | 2012-01-04 16:52:20 | [diff] [blame] | 46 | #include "content/public/browser/web_contents.h" |
[email protected] | 7d478cb | 2012-07-24 17:19:42 | [diff] [blame] | 47 | #include "content/public/test/browser_test_utils.h" |
creis | 8f3a9a6 | 2017-01-12 20:19:26 | [diff] [blame] | 48 | #include "content/public/test/test_navigation_observer.h" |
[email protected] | c1dffe8 | 2013-06-26 20:59:05 | [diff] [blame] | 49 | #include "net/test/embedded_test_server/embedded_test_server.h" |
[email protected] | 853300a8 | 2010-07-27 21:17:57 | [diff] [blame] | 50 | |
[email protected] | e0ee4b4 | 2014-05-23 06:16:13 | [diff] [blame] | 51 | #if defined(OS_WIN) |
[email protected] | cfd3379 | 2014-05-23 20:39:34 | [diff] [blame] | 52 | #include "base/win/windows_version.h" |
[email protected] | e0ee4b4 | 2014-05-23 06:16:13 | [diff] [blame] | 53 | #endif |
| 54 | |
[email protected] | eaabba2 | 2012-03-07 15:02:11 | [diff] [blame] | 55 | using content::RenderViewHost; |
[email protected] | 4b19ea5 | 2012-01-02 20:15:25 | [diff] [blame] | 56 | using content::WebContents; |
| 57 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 58 | namespace { |
| 59 | |
mgiuca | 88867d3 | 2015-07-08 04:08:24 | [diff] [blame] | 60 | #if defined(OS_POSIX) |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 61 | // The delay waited in some cases where we don't have a notifications for an |
| 62 | // action we take. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 63 | const int kActionDelayMs = 500; |
mgiuca | 88867d3 | 2015-07-08 04:08:24 | [diff] [blame] | 64 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 65 | |
[email protected] | c1dffe8 | 2013-06-26 20:59:05 | [diff] [blame] | 66 | const char kSimplePage[] = "/focus/page_with_focus.html"; |
| 67 | const char kStealFocusPage[] = "/focus/page_steals_focus.html"; |
| 68 | const char kTypicalPage[] = "/focus/typical_page.html"; |
[email protected] | a9c060ca | 2012-01-05 20:43:41 | [diff] [blame] | 69 | |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 70 | class BrowserFocusTest : public InProcessBrowserTest { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 71 | public: |
thestig | e8082124 | 2015-09-30 23:46:08 | [diff] [blame] | 72 | // InProcessBrowserTest overrides: |
dcheng | 5dd5ff6 | 2014-10-21 12:42:38 | [diff] [blame] | 73 | void SetUpOnMainThread() override { |
svaldez | a01f7d9 | 2015-11-18 17:47:56 | [diff] [blame] | 74 | ASSERT_TRUE(embedded_test_server()->Start()); |
thestig | e8082124 | 2015-09-30 23:46:08 | [diff] [blame] | 75 | } |
[email protected] | cfd3379 | 2014-05-23 20:39:34 | [diff] [blame] | 76 | |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 77 | bool IsViewFocused(ViewID vid) { |
| 78 | return ui_test_utils::IsViewFocused(browser(), vid); |
[email protected] | b982188 | 2009-08-17 22:25:17 | [diff] [blame] | 79 | } |
| 80 | |
[email protected] | fc2e087 | 2009-08-21 22:14:41 | [diff] [blame] | 81 | void ClickOnView(ViewID vid) { |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 82 | ui_test_utils::ClickOnView(browser(), vid); |
[email protected] | fc2e087 | 2009-08-21 22:14:41 | [diff] [blame] | 83 | } |
[email protected] | 0b8fa8b | 2011-12-07 00:54:52 | [diff] [blame] | 84 | |
[email protected] | cfd3379 | 2014-05-23 20:39:34 | [diff] [blame] | 85 | 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)); |
miu | 5d0cfcac | 2014-10-20 20:46:42 | [diff] [blame] | 105 | |
[email protected] | cfd3379 | 2014-05-23 20:39:34 | [diff] [blame] | 106 | // Mac requires an extra Tab key press to traverse the app menu button |
miu | 5d0cfcac | 2014-10-20 20:46:42 | [diff] [blame] | 107 | // 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] | cfd3379 | 2014-05-23 20:39:34 | [diff] [blame] | 121 | |
palmer | e67e174 | 2015-12-11 18:54:55 | [diff] [blame] | 122 | 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] | cfd3379 | 2014-05-23 20:39:34 | [diff] [blame] | 129 | 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()))); |
palmer | e67e174 | 2015-12-11 18:54:55 | [diff] [blame] | 157 | 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] | cfd3379 | 2014-05-23 20:39:34 | [diff] [blame] | 164 | #endif |
| 165 | content::RunAllPendingInMessageLoop(); |
palmer | e67e174 | 2015-12-11 18:54:55 | [diff] [blame] | 166 | 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] | cfd3379 | 2014-05-23 20:39:34 | [diff] [blame] | 174 | } |
[email protected] | a9c060ca | 2012-01-05 20:43:41 | [diff] [blame] | 175 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 176 | }; |
| 177 | |
[email protected] | cfd3379 | 2014-05-23 20:39:34 | [diff] [blame] | 178 | // A test interstitial page with typical HTML contents. |
| 179 | class 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(); |
meacer | b47bc878 | 2014-10-07 03:40:01 | [diff] [blame] | 193 | content::WaitForInterstitialAttach(tab); |
| 194 | |
[email protected] | cfd3379 | 2014-05-23 20:39:34 | [diff] [blame] | 195 | EXPECT_TRUE(tab->ShowingInterstitialPage()); |
[email protected] | e0ee4b4 | 2014-05-23 06:16:13 | [diff] [blame] | 196 | } |
[email protected] | ca9077f | 2014-05-22 19:00:21 | [diff] [blame] | 197 | |
dcheng | 5dd5ff6 | 2014-10-21 12:42:38 | [diff] [blame] | 198 | std::string GetHTMLContents() override { return html_contents_; } |
[email protected] | cfd3379 | 2014-05-23 20:39:34 | [diff] [blame] | 199 | |
[email protected] | 85f0a57 | 2012-02-07 22:20:13 | [diff] [blame] | 200 | RenderViewHost* render_view_host() { |
khannans | f3b2719 | 2015-01-09 17:28:17 | [diff] [blame] | 201 | return interstitial_page_->GetMainFrame()->GetRenderViewHost(); |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 202 | } |
| 203 | |
[email protected] | cfd3379 | 2014-05-23 20:39:34 | [diff] [blame] | 204 | void DontProceed() { interstitial_page_->DontProceed(); } |
[email protected] | 4e85c11 | 2011-01-26 22:27:21 | [diff] [blame] | 205 | |
avi | f9ab5d94 | 2015-10-15 14:05:44 | [diff] [blame] | 206 | bool HasFocus() { |
| 207 | return render_view_host()->GetWidget()->GetView()->HasFocus(); |
| 208 | } |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 209 | |
| 210 | private: |
| 211 | std::string html_contents_; |
[email protected] | cfd3379 | 2014-05-23 20:39:34 | [diff] [blame] | 212 | content::InterstitialPage* interstitial_page_; // Owns this. |
| 213 | DISALLOW_COPY_AND_ASSIGN(TestInterstitialPage); |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 214 | }; |
[email protected] | b982188 | 2009-08-17 22:25:17 | [diff] [blame] | 215 | |
sky | 4df63777 | 2015-10-22 17:20:18 | [diff] [blame] | 216 | // Flaky on Mac (http://crbug.com/67301). |
| 217 | #if defined(OS_MACOSX) |
[email protected] | 839e080 | 2012-05-18 14:45:23 | [diff] [blame] | 218 | #define MAYBE_ClickingMovesFocus DISABLED_ClickingMovesFocus |
[email protected] | c39e233d | 2012-05-18 13:24:12 | [diff] [blame] | 219 | #else |
sky | 4df63777 | 2015-10-22 17:20:18 | [diff] [blame] | 220 | // If this flakes, disable and log details in http://crbug.com/523255. |
[email protected] | 839e080 | 2012-05-18 14:45:23 | [diff] [blame] | 221 | #define MAYBE_ClickingMovesFocus ClickingMovesFocus |
[email protected] | c39e233d | 2012-05-18 13:24:12 | [diff] [blame] | 222 | #endif |
| 223 | IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_ClickingMovesFocus) { |
[email protected] | a6e602f | 2010-09-28 22:28:30 | [diff] [blame] | 224 | ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); |
[email protected] | 0ff0ff3 | 2010-12-21 19:34:42 | [diff] [blame] | 225 | #if defined(OS_POSIX) |
[email protected] | fc2e087 | 2009-08-21 22:14:41 | [diff] [blame] | 226 | // It seems we have to wait a little bit for the widgets to spin up before |
| 227 | // we can start clicking on them. |
fdoray | bd6c098 | 2016-06-11 00:23:05 | [diff] [blame] | 228 | base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
ki.stfu | c4f8e24 | 2015-10-09 20:40:20 | [diff] [blame] | 229 | FROM_HERE, base::MessageLoop::QuitWhenIdleClosure(), |
[email protected] | 7e56010 | 2012-03-08 20:58:42 | [diff] [blame] | 230 | base::TimeDelta::FromMilliseconds(kActionDelayMs)); |
[email protected] | 729eb63 | 2012-07-26 04:45:26 | [diff] [blame] | 231 | content::RunMessageLoop(); |
[email protected] | 0ff0ff3 | 2010-12-21 19:34:42 | [diff] [blame] | 232 | #endif // defined(OS_POSIX) |
[email protected] | fc2e087 | 2009-08-21 22:14:41 | [diff] [blame] | 233 | |
[email protected] | 5c4e448 | 2013-03-03 00:09:18 | [diff] [blame] | 234 | ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX)); |
[email protected] | 186f13f | 2009-08-19 20:34:00 | [diff] [blame] | 235 | |
[email protected] | fc2e087 | 2009-08-21 22:14:41 | [diff] [blame] | 236 | ClickOnView(VIEW_ID_TAB_CONTAINER); |
[email protected] | f2159ba | 2012-04-17 19:13:21 | [diff] [blame] | 237 | ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER)); |
[email protected] | 186f13f | 2009-08-19 20:34:00 | [diff] [blame] | 238 | |
[email protected] | 5c4e448 | 2013-03-03 00:09:18 | [diff] [blame] | 239 | ClickOnView(VIEW_ID_OMNIBOX); |
| 240 | ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX)); |
[email protected] | 186f13f | 2009-08-19 20:34:00 | [diff] [blame] | 241 | } |
[email protected] | 186f13f | 2009-08-19 20:34:00 | [diff] [blame] | 242 | |
[email protected] | 320948e0 | 2011-01-10 08:21:12 | [diff] [blame] | 243 | // Flaky, http://crbug.com/69034. |
[email protected] | 4d245165 | 2012-02-14 23:54:26 | [diff] [blame] | 244 | IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_BrowsersRememberFocus) { |
[email protected] | a6e602f | 2010-09-28 22:28:30 | [diff] [blame] | 245 | ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); |
[email protected] | cfd3379 | 2014-05-23 20:39:34 | [diff] [blame] | 246 | const GURL url = embedded_test_server()->GetURL(kSimplePage); |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 247 | ui_test_utils::NavigateToURL(browser(), url); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 248 | |
[email protected] | 90556dd | 2012-06-07 20:26:18 | [diff] [blame] | 249 | gfx::NativeWindow window = browser()->window()->GetNativeWindow(); |
[email protected] | 186f13f | 2009-08-19 20:34:00 | [diff] [blame] | 250 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 251 | // The focus should be on the Tab contents. |
[email protected] | f2159ba | 2012-04-17 19:13:21 | [diff] [blame] | 252 | ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 253 | // Now hide the window, show it again, the focus should not have changed. |
[email protected] | 853300a8 | 2010-07-27 21:17:57 | [diff] [blame] | 254 | ui_test_utils::HideNativeWindow(window); |
[email protected] | 7807088c | 2011-11-09 15:16:29 | [diff] [blame] | 255 | ASSERT_TRUE(ui_test_utils::ShowAndFocusNativeWindow(window)); |
[email protected] | f2159ba | 2012-04-17 19:13:21 | [diff] [blame] | 256 | ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 257 | |
[email protected] | a37d4b0 | 2012-06-25 21:56:10 | [diff] [blame] | 258 | chrome::FocusLocationBar(browser()); |
[email protected] | 5c4e448 | 2013-03-03 00:09:18 | [diff] [blame] | 259 | ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 260 | // Hide the window, show it again, the focus should not have changed. |
[email protected] | 853300a8 | 2010-07-27 21:17:57 | [diff] [blame] | 261 | ui_test_utils::HideNativeWindow(window); |
[email protected] | 7807088c | 2011-11-09 15:16:29 | [diff] [blame] | 262 | ASSERT_TRUE(ui_test_utils::ShowAndFocusNativeWindow(window)); |
[email protected] | 5c4e448 | 2013-03-03 00:09:18 | [diff] [blame] | 263 | ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | // Tabs remember focus. |
[email protected] | 2615373 | 2010-11-09 18:47:39 | [diff] [blame] | 267 | // Disabled, http://crbug.com/62542. |
| 268 | IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_TabsRememberFocus) { |
[email protected] | a6e602f | 2010-09-28 22:28:30 | [diff] [blame] | 269 | ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); |
[email protected] | cfd3379 | 2014-05-23 20:39:34 | [diff] [blame] | 270 | const GURL url = embedded_test_server()->GetURL(kSimplePage); |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 271 | ui_test_utils::NavigateToURL(browser(), url); |
| 272 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 273 | // Create several tabs. |
[email protected] | 52877dbc6 | 2012-06-29 22:22:03 | [diff] [blame] | 274 | for (int i = 0; i < 4; ++i) { |
| 275 | chrome::AddSelectedTabWithURL(browser(), url, |
Sylvain Defresne | c6ccc77d | 2014-09-19 10:19:35 | [diff] [blame] | 276 | ui::PAGE_TRANSITION_TYPED); |
[email protected] | 52877dbc6 | 2012-06-29 22:22:03 | [diff] [blame] | 277 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 278 | |
| 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] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 288 | // Activate the tab. |
[email protected] | 5789218 | 2012-12-03 19:15:39 | [diff] [blame] | 289 | browser()->tab_strip_model()->ActivateTabAt(j, true); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 290 | |
| 291 | // Activate the location bar or the page. |
[email protected] | 7e38369 | 2009-06-12 19:14:54 | [diff] [blame] | 292 | if (kFocusPage[i][j]) { |
[email protected] | fc2b46b | 2014-05-03 16:33:45 | [diff] [blame] | 293 | browser()->tab_strip_model()->GetWebContentsAt(j)->Focus(); |
[email protected] | 7e38369 | 2009-06-12 19:14:54 | [diff] [blame] | 294 | } else { |
[email protected] | a37d4b0 | 2012-06-25 21:56:10 | [diff] [blame] | 295 | chrome::FocusLocationBar(browser()); |
[email protected] | 7e38369 | 2009-06-12 19:14:54 | [diff] [blame] | 296 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 297 | } |
| 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] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 301 | // Activate the tab. |
[email protected] | 5789218 | 2012-12-03 19:15:39 | [diff] [blame] | 302 | browser()->tab_strip_model()->ActivateTabAt(j, true); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 303 | |
[email protected] | 5c4e448 | 2013-03-03 00:09:18 | [diff] [blame] | 304 | ViewID vid = kFocusPage[i][j] ? VIEW_ID_TAB_CONTAINER : VIEW_ID_OMNIBOX; |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 305 | ASSERT_TRUE(IsViewFocused(vid)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 306 | } |
[email protected] | cb7e254 | 2009-12-14 22:02:35 | [diff] [blame] | 307 | |
[email protected] | 5789218 | 2012-12-03 19:15:39 | [diff] [blame] | 308 | browser()->tab_strip_model()->ActivateTabAt(0, true); |
[email protected] | cb7e254 | 2009-12-14 22:02:35 | [diff] [blame] | 309 | // 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] | f2159ba | 2012-04-17 19:13:21 | [diff] [blame] | 312 | ViewID vid = kFocusPage[i][j % 5] ? VIEW_ID_TAB_CONTAINER : |
[email protected] | 5c4e448 | 2013-03-03 00:09:18 | [diff] [blame] | 313 | VIEW_ID_OMNIBOX; |
[email protected] | cb7e254 | 2009-12-14 22:02:35 | [diff] [blame] | 314 | ASSERT_TRUE(IsViewFocused(vid)); |
| 315 | |
[email protected] | 1d00068 | 2010-08-23 16:21:28 | [diff] [blame] | 316 | ASSERT_TRUE(ui_test_utils::SendKeyPressSync( |
[email protected] | b6d8126 | 2011-01-13 17:36:09 | [diff] [blame] | 317 | browser(), ui::VKEY_TAB, true, false, false, false)); |
[email protected] | cb7e254 | 2009-12-14 22:02:35 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | // As above, but with ctrl+shift+tab. |
[email protected] | 5789218 | 2012-12-03 19:15:39 | [diff] [blame] | 321 | browser()->tab_strip_model()->ActivateTabAt(4, true); |
[email protected] | cb7e254 | 2009-12-14 22:02:35 | [diff] [blame] | 322 | for (int j = 14; j >= 0; --j) { |
[email protected] | f2159ba | 2012-04-17 19:13:21 | [diff] [blame] | 323 | ViewID vid = kFocusPage[i][j % 5] ? VIEW_ID_TAB_CONTAINER : |
[email protected] | 5c4e448 | 2013-03-03 00:09:18 | [diff] [blame] | 324 | VIEW_ID_OMNIBOX; |
[email protected] | cb7e254 | 2009-12-14 22:02:35 | [diff] [blame] | 325 | ASSERT_TRUE(IsViewFocused(vid)); |
| 326 | |
[email protected] | 1d00068 | 2010-08-23 16:21:28 | [diff] [blame] | 327 | ASSERT_TRUE(ui_test_utils::SendKeyPressSync( |
[email protected] | b6d8126 | 2011-01-13 17:36:09 | [diff] [blame] | 328 | browser(), ui::VKEY_TAB, true, true, false, false)); |
[email protected] | cb7e254 | 2009-12-14 22:02:35 | [diff] [blame] | 329 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 330 | } |
| 331 | } |
| 332 | |
[email protected] | ae40b57 | 2009-10-02 21:17:45 | [diff] [blame] | 333 | // Tabs remember focus with find-in-page box. |
warx | 9c88c58 | 2016-11-10 03:58:24 | [diff] [blame] | 334 | IN_PROC_BROWSER_TEST_F(BrowserFocusTest, TabsRememberFocusFindInPage) { |
[email protected] | cfd3379 | 2014-05-23 20:39:34 | [diff] [blame] | 335 | const GURL url = embedded_test_server()->GetURL(kSimplePage); |
[email protected] | ae40b57 | 2009-10-02 21:17:45 | [diff] [blame] | 336 | ui_test_utils::NavigateToURL(browser(), url); |
| 337 | |
[email protected] | a37d4b0 | 2012-06-25 21:56:10 | [diff] [blame] | 338 | chrome::Find(browser()); |
[email protected] | cc87237 | 2013-01-28 21:57:07 | [diff] [blame] | 339 | ui_test_utils::FindInPage( |
| 340 | browser()->tab_strip_model()->GetActiveWebContents(), |
[email protected] | 1f13690 | 2013-12-24 23:26:45 | [diff] [blame] | 341 | base::ASCIIToUTF16("a"), true, false, NULL, NULL); |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 342 | ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); |
[email protected] | ae40b57 | 2009-10-02 21:17:45 | [diff] [blame] | 343 | |
| 344 | // Focus the location bar. |
[email protected] | a37d4b0 | 2012-06-25 21:56:10 | [diff] [blame] | 345 | chrome::FocusLocationBar(browser()); |
[email protected] | ae40b57 | 2009-10-02 21:17:45 | [diff] [blame] | 346 | |
| 347 | // Create a 2nd tab. |
Sylvain Defresne | c6ccc77d | 2014-09-19 10:19:35 | [diff] [blame] | 348 | chrome::AddSelectedTabWithURL(browser(), url, ui::PAGE_TRANSITION_TYPED); |
[email protected] | ae40b57 | 2009-10-02 21:17:45 | [diff] [blame] | 349 | |
| 350 | // Focus should be on the recently opened tab page. |
[email protected] | f2159ba | 2012-04-17 19:13:21 | [diff] [blame] | 351 | ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER)); |
[email protected] | ae40b57 | 2009-10-02 21:17:45 | [diff] [blame] | 352 | |
| 353 | // Select 1st tab, focus should still be on the location-bar. |
| 354 | // (bug http://crbug.com/23296) |
[email protected] | 5789218 | 2012-12-03 19:15:39 | [diff] [blame] | 355 | browser()->tab_strip_model()->ActivateTabAt(0, true); |
[email protected] | 5c4e448 | 2013-03-03 00:09:18 | [diff] [blame] | 356 | ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX)); |
[email protected] | ae40b57 | 2009-10-02 21:17:45 | [diff] [blame] | 357 | |
| 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] | a37d4b0 | 2012-06-25 21:56:10 | [diff] [blame] | 360 | chrome::Find(browser()); |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 361 | ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); |
[email protected] | 5789218 | 2012-12-03 19:15:39 | [diff] [blame] | 362 | browser()->tab_strip_model()->ActivateTabAt(1, true); |
[email protected] | f2159ba | 2012-04-17 19:13:21 | [diff] [blame] | 363 | ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER)); |
[email protected] | 5789218 | 2012-12-03 19:15:39 | [diff] [blame] | 364 | browser()->tab_strip_model()->ActivateTabAt(0, true); |
[email protected] | 21abcc74 | 2009-10-23 02:52:06 | [diff] [blame] | 365 | ASSERT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); |
[email protected] | ae40b57 | 2009-10-02 21:17:45 | [diff] [blame] | 366 | } |
| 367 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 368 | // Background window does not steal focus. |
[email protected] | c9292867 | 2010-11-09 18:31:07 | [diff] [blame] | 369 | // Flaky, http://crbug.com/62538. |
| 370 | IN_PROC_BROWSER_TEST_F(BrowserFocusTest, |
[email protected] | 4d245165 | 2012-02-14 23:54:26 | [diff] [blame] | 371 | DISABLED_BackgroundBrowserDontStealFocus) { |
[email protected] | a6e602f | 2010-09-28 22:28:30 | [diff] [blame] | 372 | ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 373 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 374 | // Open a new browser window. |
scottmg | 85194900 | 2016-02-09 20:09:44 | [diff] [blame] | 375 | Browser* browser2 = new Browser(Browser::CreateParams(browser()->profile())); |
| 376 | |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 377 | ASSERT_TRUE(browser2); |
[email protected] | 00509a3 | 2013-11-17 17:45:37 | [diff] [blame] | 378 | chrome::AddTabAt(browser2, GURL(), -1, true); |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 379 | browser2->window()->Show(); |
[email protected] | 186f13f | 2009-08-19 20:34:00 | [diff] [blame] | 380 | |
[email protected] | ed179ee | 2009-10-03 21:02:51 | [diff] [blame] | 381 | Browser* focused_browser = NULL; |
| 382 | Browser* unfocused_browser = NULL; |
[email protected] | 753efc4 | 2010-03-09 19:52:16 | [diff] [blame] | 383 | #if defined(USE_X11) |
| 384 | // On X11, calling Activate() is not guaranteed to move focus, so we have |
[email protected] | 186f13f | 2009-08-19 20:34:00 | [diff] [blame] | 385 | // 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] | 579c3d8 | 2010-10-06 03:53:51 | [diff] [blame] | 393 | FAIL() << "Could not determine which browser has focus"; |
[email protected] | 186f13f | 2009-08-19 20:34:00 | [diff] [blame] | 394 | } |
| 395 | #elif defined(OS_WIN) |
| 396 | focused_browser = browser(); |
| 397 | unfocused_browser = browser2; |
[email protected] | 853300a8 | 2010-07-27 21:17:57 | [diff] [blame] | 398 | #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] | 186f13f | 2009-08-19 20:34:00 | [diff] [blame] | 402 | #endif |
| 403 | |
[email protected] | cfd3379 | 2014-05-23 20:39:34 | [diff] [blame] | 404 | const GURL steal_focus_url = embedded_test_server()->GetURL(kStealFocusPage); |
[email protected] | 186f13f | 2009-08-19 20:34:00 | [diff] [blame] | 405 | ui_test_utils::NavigateToURL(unfocused_browser, steal_focus_url); |
[email protected] | 1e187af | 2009-02-25 02:02:46 | [diff] [blame] | 406 | |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 407 | // Activate the first browser. |
[email protected] | 186f13f | 2009-08-19 20:34:00 | [diff] [blame] | 408 | focused_browser->window()->Activate(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 409 | |
[email protected] | b6987e0 | 2013-01-04 18:30:43 | [diff] [blame] | 410 | ASSERT_TRUE(content::ExecuteScript( |
[email protected] | da2bfa4 | 2013-01-29 23:47:05 | [diff] [blame] | 411 | unfocused_browser->tab_strip_model()->GetActiveWebContents(), |
[email protected] | 06bc5d9 | 2013-01-02 22:44:13 | [diff] [blame] | 412 | "stealFocus();")); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 413 | |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 414 | // Make sure the first browser is still active. |
[email protected] | 186f13f | 2009-08-19 20:34:00 | [diff] [blame] | 415 | EXPECT_TRUE(focused_browser->window()->IsActive()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | // Page cannot steal focus when focus is on location bar. |
warx | 0faadcf | 2016-11-11 23:13:11 | [diff] [blame] | 419 | IN_PROC_BROWSER_TEST_F(BrowserFocusTest, LocationBarLockFocus) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 420 | // Open the page that steals focus. |
[email protected] | cfd3379 | 2014-05-23 20:39:34 | [diff] [blame] | 421 | const GURL url = embedded_test_server()->GetURL(kStealFocusPage); |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 422 | ui_test_utils::NavigateToURL(browser(), url); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 423 | |
[email protected] | a37d4b0 | 2012-06-25 21:56:10 | [diff] [blame] | 424 | chrome::FocusLocationBar(browser()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 425 | |
[email protected] | b6987e0 | 2013-01-04 18:30:43 | [diff] [blame] | 426 | ASSERT_TRUE(content::ExecuteScript( |
[email protected] | cc87237 | 2013-01-28 21:57:07 | [diff] [blame] | 427 | browser()->tab_strip_model()->GetActiveWebContents(), |
[email protected] | 06bc5d9 | 2013-01-02 22:44:13 | [diff] [blame] | 428 | "stealFocus();")); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 429 | |
| 430 | // Make sure the location bar is still focused. |
[email protected] | 5c4e448 | 2013-03-03 00:09:18 | [diff] [blame] | 431 | ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 432 | } |
| 433 | |
[email protected] | cfd3379 | 2014-05-23 20:39:34 | [diff] [blame] | 434 | // Test forward and reverse focus traversal on a typical page. |
bnc | 0f8fe37 | 2014-11-24 19:42:51 | [diff] [blame] | 435 | // 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 |
| 442 | IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusTraversal) { |
[email protected] | a6e602f | 2010-09-28 22:28:30 | [diff] [blame] | 443 | ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); |
[email protected] | cfd3379 | 2014-05-23 20:39:34 | [diff] [blame] | 444 | const GURL url = embedded_test_server()->GetURL(kTypicalPage); |
[email protected] | 8bcdec9 | 2009-02-25 16:15:18 | [diff] [blame] | 445 | ui_test_utils::NavigateToURL(browser(), url); |
[email protected] | cfd3379 | 2014-05-23 20:39:34 | [diff] [blame] | 446 | EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER)); |
[email protected] | a37d4b0 | 2012-06-25 21:56:10 | [diff] [blame] | 447 | chrome::FocusLocationBar(browser()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 448 | |
[email protected] | cfd3379 | 2014-05-23 20:39:34 | [diff] [blame] | 449 | 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.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 452 | } |
| 453 | |
[email protected] | cfd3379 | 2014-05-23 20:39:34 | [diff] [blame] | 454 | // Test forward and reverse focus traversal while an interstitial is showing. |
[email protected] | 6d731ed | 2014-06-02 14:53:29 | [diff] [blame] | 455 | // Disabled, see http://crbug.com/60973 |
thestig | e8082124 | 2015-09-30 23:46:08 | [diff] [blame] | 456 | IN_PROC_BROWSER_TEST_F(BrowserFocusTest, |
| 457 | DISABLED_FocusTraversalOnInterstitial) { |
[email protected] | a6e602f | 2010-09-28 22:28:30 | [diff] [blame] | 458 | ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); |
[email protected] | cfd3379 | 2014-05-23 20:39:34 | [diff] [blame] | 459 | const GURL url = embedded_test_server()->GetURL(kSimplePage); |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 460 | ui_test_utils::NavigateToURL(browser(), url); |
[email protected] | cfd3379 | 2014-05-23 20:39:34 | [diff] [blame] | 461 | EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER)); |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 462 | |
[email protected] | cfd3379 | 2014-05-23 20:39:34 | [diff] [blame] | 463 | // Create and show a test interstitial page. |
[email protected] | cc87237 | 2013-01-28 21:57:07 | [diff] [blame] | 464 | TestInterstitialPage* interstitial_page = new TestInterstitialPage( |
[email protected] | cfd3379 | 2014-05-23 20:39:34 | [diff] [blame] | 465 | browser()->tab_strip_model()->GetActiveWebContents()); |
| 466 | content::RenderViewHost* host = interstitial_page->render_view_host(); |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 467 | |
[email protected] | cfd3379 | 2014-05-23 20:39:34 | [diff] [blame] | 468 | EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER)); |
[email protected] | a37d4b0 | 2012-06-25 21:56:10 | [diff] [blame] | 469 | chrome::FocusLocationBar(browser()); |
[email protected] | cfd3379 | 2014-05-23 20:39:34 | [diff] [blame] | 470 | EXPECT_NO_FATAL_FAILURE(TestFocusTraversal(host, false)); |
| 471 | EXPECT_NO_FATAL_FAILURE(TestFocusTraversal(host, true)); |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 472 | } |
| 473 | |
[email protected] | cfd3379 | 2014-05-23 20:39:34 | [diff] [blame] | 474 | // Test the transfer of focus when an interstitial is shown and hidden. |
| 475 | IN_PROC_BROWSER_TEST_F(BrowserFocusTest, InterstitialFocus) { |
[email protected] | a6e602f | 2010-09-28 22:28:30 | [diff] [blame] | 476 | ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); |
[email protected] | cfd3379 | 2014-05-23 20:39:34 | [diff] [blame] | 477 | const GURL url = embedded_test_server()->GetURL(kSimplePage); |
[email protected] | e0ee4b4 | 2014-05-23 06:16:13 | [diff] [blame] | 478 | ui_test_utils::NavigateToURL(browser(), url); |
[email protected] | cfd3379 | 2014-05-23 20:39:34 | [diff] [blame] | 479 | WebContents* tab = browser()->tab_strip_model()->GetActiveWebContents(); |
| 480 | EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER)); |
avi | f9ab5d94 | 2015-10-15 14:05:44 | [diff] [blame] | 481 | EXPECT_TRUE(tab->GetRenderViewHost()->GetWidget()->GetView()->HasFocus()); |
[email protected] | e0ee4b4 | 2014-05-23 06:16:13 | [diff] [blame] | 482 | |
[email protected] | cfd3379 | 2014-05-23 20:39:34 | [diff] [blame] | 483 | // 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] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 486 | EXPECT_TRUE(interstitial_page->HasFocus()); |
| 487 | |
[email protected] | cfd3379 | 2014-05-23 20:39:34 | [diff] [blame] | 488 | // Hide the interstitial; the original page should gain focus. |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 489 | interstitial_page->DontProceed(); |
[email protected] | cfd3379 | 2014-05-23 20:39:34 | [diff] [blame] | 490 | content::RunAllPendingInMessageLoop(); |
| 491 | EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER)); |
avi | f9ab5d94 | 2015-10-15 14:05:44 | [diff] [blame] | 492 | EXPECT_TRUE(tab->GetRenderViewHost()->GetWidget()->GetView()->HasFocus()); |
[email protected] | 9e0c83a | 2009-05-06 19:44:37 | [diff] [blame] | 493 | } |
| 494 | |
[email protected] | cfd3379 | 2014-05-23 20:39:34 | [diff] [blame] | 495 | // Test that find-in-page UI can request focus, even when it is already open. |
sky | 4df63777 | 2015-10-22 17:20:18 | [diff] [blame] | 496 | #if defined(OS_MACOSX) |
John Abd-El-Malek | f3fdd62 | 2014-09-09 04:04:23 | [diff] [blame] | 497 | #define MAYBE_FindFocusTest DISABLED_FindFocusTest |
| 498 | #else |
sky | 4df63777 | 2015-10-22 17:20:18 | [diff] [blame] | 499 | // If this flakes, disable and log details in http://crbug.com/523255. |
John Abd-El-Malek | f3fdd62 | 2014-09-09 04:04:23 | [diff] [blame] | 500 | #define MAYBE_FindFocusTest FindFocusTest |
| 501 | #endif |
| 502 | IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FindFocusTest) { |
[email protected] | cfd3379 | 2014-05-23 20:39:34 | [diff] [blame] | 503 | chrome::DisableFindBarAnimationsDuringTesting(true); |
[email protected] | a6e602f | 2010-09-28 22:28:30 | [diff] [blame] | 504 | ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); |
[email protected] | cfd3379 | 2014-05-23 20:39:34 | [diff] [blame] | 505 | const GURL url = embedded_test_server()->GetURL(kTypicalPage); |
[email protected] | e0ee4b4 | 2014-05-23 06:16:13 | [diff] [blame] | 506 | ui_test_utils::NavigateToURL(browser(), url); |
[email protected] | cfd3379 | 2014-05-23 20:39:34 | [diff] [blame] | 507 | EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER)); |
[email protected] | e0ee4b4 | 2014-05-23 06:16:13 | [diff] [blame] | 508 | |
[email protected] | cfd3379 | 2014-05-23 20:39:34 | [diff] [blame] | 509 | chrome::ShowFindBar(browser()); |
| 510 | EXPECT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); |
[email protected] | 9bd491ee | 2008-12-10 22:31:07 | [diff] [blame] | 511 | |
[email protected] | a37d4b0 | 2012-06-25 21:56:10 | [diff] [blame] | 512 | chrome::FocusLocationBar(browser()); |
[email protected] | cfd3379 | 2014-05-23 20:39:34 | [diff] [blame] | 513 | EXPECT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX)); |
[email protected] | 9bd491ee | 2008-12-10 22:31:07 | [diff] [blame] | 514 | |
[email protected] | cfd3379 | 2014-05-23 20:39:34 | [diff] [blame] | 515 | chrome::ShowFindBar(browser()); |
| 516 | EXPECT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); |
[email protected] | 9bd491ee | 2008-12-10 22:31:07 | [diff] [blame] | 517 | |
[email protected] | fc2e087 | 2009-08-21 22:14:41 | [diff] [blame] | 518 | ClickOnView(VIEW_ID_TAB_CONTAINER); |
[email protected] | cfd3379 | 2014-05-23 20:39:34 | [diff] [blame] | 519 | EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER)); |
[email protected] | 9bd491ee | 2008-12-10 22:31:07 | [diff] [blame] | 520 | |
[email protected] | cfd3379 | 2014-05-23 20:39:34 | [diff] [blame] | 521 | chrome::ShowFindBar(browser()); |
| 522 | EXPECT_TRUE(IsViewFocused(VIEW_ID_FIND_IN_PAGE_TEXT_FIELD)); |
[email protected] | 9bd491ee | 2008-12-10 22:31:07 | [diff] [blame] | 523 | } |
[email protected] | 401513c | 2009-03-12 00:21:28 | [diff] [blame] | 524 | |
| 525 | // Makes sure the focus is in the right location when opening the different |
| 526 | // types of tabs. |
[email protected] | c9292867 | 2010-11-09 18:31:07 | [diff] [blame] | 527 | // Flaky, http://crbug.com/62539. |
[email protected] | 4d245165 | 2012-02-14 23:54:26 | [diff] [blame] | 528 | IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_TabInitialFocus) { |
[email protected] | a6e602f | 2010-09-28 22:28:30 | [diff] [blame] | 529 | ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); |
[email protected] | 9ba21ede | 2010-07-30 01:11:07 | [diff] [blame] | 530 | |
[email protected] | 401513c | 2009-03-12 00:21:28 | [diff] [blame] | 531 | // Open the history tab, focus should be on the tab contents. |
[email protected] | 5d9cace7 | 2012-06-21 16:07:12 | [diff] [blame] | 532 | chrome::ShowHistory(browser()); |
[email protected] | 6ddc6e3f2 | 2012-07-26 01:15:35 | [diff] [blame] | 533 | ASSERT_NO_FATAL_FAILURE(content::WaitForLoadStop( |
[email protected] | cc87237 | 2013-01-28 21:57:07 | [diff] [blame] | 534 | browser()->tab_strip_model()->GetActiveWebContents())); |
[email protected] | f2159ba | 2012-04-17 19:13:21 | [diff] [blame] | 535 | EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER)); |
[email protected] | 401513c | 2009-03-12 00:21:28 | [diff] [blame] | 536 | |
| 537 | // Open the new tab, focus should be on the location bar. |
[email protected] | a37d4b0 | 2012-06-25 21:56:10 | [diff] [blame] | 538 | chrome::NewTab(browser()); |
[email protected] | 6ddc6e3f2 | 2012-07-26 01:15:35 | [diff] [blame] | 539 | ASSERT_NO_FATAL_FAILURE(content::WaitForLoadStop( |
[email protected] | cc87237 | 2013-01-28 21:57:07 | [diff] [blame] | 540 | browser()->tab_strip_model()->GetActiveWebContents())); |
[email protected] | 5c4e448 | 2013-03-03 00:09:18 | [diff] [blame] | 541 | EXPECT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX)); |
[email protected] | 401513c | 2009-03-12 00:21:28 | [diff] [blame] | 542 | |
| 543 | // Open the download tab, focus should be on the tab contents. |
[email protected] | 5d9cace7 | 2012-06-21 16:07:12 | [diff] [blame] | 544 | chrome::ShowDownloads(browser()); |
[email protected] | 6ddc6e3f2 | 2012-07-26 01:15:35 | [diff] [blame] | 545 | ASSERT_NO_FATAL_FAILURE(content::WaitForLoadStop( |
[email protected] | cc87237 | 2013-01-28 21:57:07 | [diff] [blame] | 546 | browser()->tab_strip_model()->GetActiveWebContents())); |
[email protected] | f2159ba | 2012-04-17 19:13:21 | [diff] [blame] | 547 | EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER)); |
[email protected] | 3e3f0eb | 2009-06-22 18:33:43 | [diff] [blame] | 548 | |
| 549 | // Open about:blank, focus should be on the location bar. |
[email protected] | 8e09c7af | 2014-06-10 11:46:17 | [diff] [blame] | 550 | chrome::AddSelectedTabWithURL( |
Sylvain Defresne | c6ccc77d | 2014-09-19 10:19:35 | [diff] [blame] | 551 | browser(), GURL(url::kAboutBlankURL), ui::PAGE_TRANSITION_LINK); |
[email protected] | 6ddc6e3f2 | 2012-07-26 01:15:35 | [diff] [blame] | 552 | ASSERT_NO_FATAL_FAILURE(content::WaitForLoadStop( |
[email protected] | cc87237 | 2013-01-28 21:57:07 | [diff] [blame] | 553 | browser()->tab_strip_model()->GetActiveWebContents())); |
[email protected] | 5c4e448 | 2013-03-03 00:09:18 | [diff] [blame] | 554 | EXPECT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX)); |
[email protected] | 401513c | 2009-03-12 00:21:28 | [diff] [blame] | 555 | } |
[email protected] | 9d8a464 | 2009-07-29 17:25:30 | [diff] [blame] | 556 | |
| 557 | // Tests that focus goes where expected when using reload. |
warx | 0faadcf | 2016-11-11 23:13:11 | [diff] [blame] | 558 | IN_PROC_BROWSER_TEST_F(BrowserFocusTest, FocusOnReload) { |
[email protected] | 9d8a464 | 2009-07-29 17:25:30 | [diff] [blame] | 559 | // Open the new tab, reload. |
[email protected] | 6b4e8e4 | 2011-08-17 19:36:06 | [diff] [blame] | 560 | { |
[email protected] | a7fe911 | 2012-07-20 02:34:45 | [diff] [blame] | 561 | content::WindowedNotificationObserver observer( |
[email protected] | 6b4e8e4 | 2011-08-17 19:36:06 | [diff] [blame] | 562 | content::NOTIFICATION_LOAD_STOP, |
[email protected] | ad50def5 | 2011-10-19 23:17:07 | [diff] [blame] | 563 | content::NotificationService::AllSources()); |
[email protected] | a37d4b0 | 2012-06-25 21:56:10 | [diff] [blame] | 564 | chrome::NewTab(browser()); |
[email protected] | 6b4e8e4 | 2011-08-17 19:36:06 | [diff] [blame] | 565 | observer.Wait(); |
| 566 | } |
[email protected] | b8deecd | 2012-07-30 21:09:44 | [diff] [blame] | 567 | content::RunAllPendingInMessageLoop(); |
[email protected] | f07d7bf | 2010-04-06 08:02:42 | [diff] [blame] | 568 | |
[email protected] | 6b4e8e4 | 2011-08-17 19:36:06 | [diff] [blame] | 569 | { |
[email protected] | a7fe911 | 2012-07-20 02:34:45 | [diff] [blame] | 570 | content::WindowedNotificationObserver observer( |
[email protected] | 6b4e8e4 | 2011-08-17 19:36:06 | [diff] [blame] | 571 | content::NOTIFICATION_LOAD_STOP, |
[email protected] | cfd3379 | 2014-05-23 20:39:34 | [diff] [blame] | 572 | content::Source<content::NavigationController>( |
[email protected] | cc87237 | 2013-01-28 21:57:07 | [diff] [blame] | 573 | &browser()->tab_strip_model()->GetActiveWebContents()-> |
| 574 | GetController())); |
nick | 3b04f32 | 2016-08-31 19:29:19 | [diff] [blame] | 575 | chrome::Reload(browser(), WindowOpenDisposition::CURRENT_TAB); |
[email protected] | 6b4e8e4 | 2011-08-17 19:36:06 | [diff] [blame] | 576 | observer.Wait(); |
| 577 | } |
[email protected] | 9d8a464 | 2009-07-29 17:25:30 | [diff] [blame] | 578 | // Focus should stay on the location bar. |
[email protected] | 5c4e448 | 2013-03-03 00:09:18 | [diff] [blame] | 579 | ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX)); |
[email protected] | 9d8a464 | 2009-07-29 17:25:30 | [diff] [blame] | 580 | |
| 581 | // Open a regular page, focus the location bar, reload. |
[email protected] | c1dffe8 | 2013-06-26 20:59:05 | [diff] [blame] | 582 | ui_test_utils::NavigateToURL(browser(), |
| 583 | embedded_test_server()->GetURL(kSimplePage)); |
[email protected] | a37d4b0 | 2012-06-25 21:56:10 | [diff] [blame] | 584 | chrome::FocusLocationBar(browser()); |
[email protected] | 5c4e448 | 2013-03-03 00:09:18 | [diff] [blame] | 585 | ASSERT_TRUE(IsViewFocused(VIEW_ID_OMNIBOX)); |
[email protected] | 6b4e8e4 | 2011-08-17 19:36:06 | [diff] [blame] | 586 | { |
[email protected] | a7fe911 | 2012-07-20 02:34:45 | [diff] [blame] | 587 | content::WindowedNotificationObserver observer( |
[email protected] | 6b4e8e4 | 2011-08-17 19:36:06 | [diff] [blame] | 588 | content::NOTIFICATION_LOAD_STOP, |
[email protected] | cfd3379 | 2014-05-23 20:39:34 | [diff] [blame] | 589 | content::Source<content::NavigationController>( |
[email protected] | cc87237 | 2013-01-28 21:57:07 | [diff] [blame] | 590 | &browser()->tab_strip_model()->GetActiveWebContents()-> |
| 591 | GetController())); |
nick | 3b04f32 | 2016-08-31 19:29:19 | [diff] [blame] | 592 | chrome::Reload(browser(), WindowOpenDisposition::CURRENT_TAB); |
[email protected] | 6b4e8e4 | 2011-08-17 19:36:06 | [diff] [blame] | 593 | observer.Wait(); |
| 594 | } |
[email protected] | 9ba21ede | 2010-07-30 01:11:07 | [diff] [blame] | 595 | |
[email protected] | 9d8a464 | 2009-07-29 17:25:30 | [diff] [blame] | 596 | // Focus should now be on the tab contents. |
[email protected] | 5d9cace7 | 2012-06-21 16:07:12 | [diff] [blame] | 597 | chrome::ShowDownloads(browser()); |
[email protected] | f2159ba | 2012-04-17 19:13:21 | [diff] [blame] | 598 | ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER)); |
[email protected] | 9d8a464 | 2009-07-29 17:25:30 | [diff] [blame] | 599 | } |
| 600 | |
| 601 | // Tests that focus goes where expected when using reload on a crashed tab. |
[email protected] | 160f29ad | 2010-10-28 15:43:27 | [diff] [blame] | 602 | IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_FocusOnReloadCrashedTab) { |
[email protected] | a6e602f | 2010-09-28 22:28:30 | [diff] [blame] | 603 | ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); |
[email protected] | 9d8a464 | 2009-07-29 17:25:30 | [diff] [blame] | 604 | |
[email protected] | 9d8a464 | 2009-07-29 17:25:30 | [diff] [blame] | 605 | // Open a regular page, crash, reload. |
[email protected] | c1dffe8 | 2013-06-26 20:59:05 | [diff] [blame] | 606 | ui_test_utils::NavigateToURL(browser(), |
| 607 | embedded_test_server()->GetURL(kSimplePage)); |
[email protected] | cc87237 | 2013-01-28 21:57:07 | [diff] [blame] | 608 | content::CrashTab(browser()->tab_strip_model()->GetActiveWebContents()); |
[email protected] | 6b4e8e4 | 2011-08-17 19:36:06 | [diff] [blame] | 609 | { |
[email protected] | a7fe911 | 2012-07-20 02:34:45 | [diff] [blame] | 610 | content::WindowedNotificationObserver observer( |
[email protected] | 6b4e8e4 | 2011-08-17 19:36:06 | [diff] [blame] | 611 | content::NOTIFICATION_LOAD_STOP, |
[email protected] | cfd3379 | 2014-05-23 20:39:34 | [diff] [blame] | 612 | content::Source<content::NavigationController>( |
[email protected] | cc87237 | 2013-01-28 21:57:07 | [diff] [blame] | 613 | &browser()->tab_strip_model()->GetActiveWebContents()-> |
| 614 | GetController())); |
nick | 3b04f32 | 2016-08-31 19:29:19 | [diff] [blame] | 615 | chrome::Reload(browser(), WindowOpenDisposition::CURRENT_TAB); |
[email protected] | 6b4e8e4 | 2011-08-17 19:36:06 | [diff] [blame] | 616 | observer.Wait(); |
| 617 | } |
[email protected] | 9ba21ede | 2010-07-30 01:11:07 | [diff] [blame] | 618 | |
[email protected] | 9d8a464 | 2009-07-29 17:25:30 | [diff] [blame] | 619 | // Focus should now be on the tab contents. |
[email protected] | 5d9cace7 | 2012-06-21 16:07:12 | [diff] [blame] | 620 | chrome::ShowDownloads(browser()); |
[email protected] | f2159ba | 2012-04-17 19:13:21 | [diff] [blame] | 621 | ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER)); |
[email protected] | 9d8a464 | 2009-07-29 17:25:30 | [diff] [blame] | 622 | } |
[email protected] | 629e034 | 2010-07-27 23:30:13 | [diff] [blame] | 623 | |
[email protected] | 0947506 | 2013-10-21 21:29:56 | [diff] [blame] | 624 | // 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). |
| 628 | IN_PROC_BROWSER_TEST_F(BrowserFocusTest, DISABLED_FocusAfterCrashedTab) { |
| 629 | ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); |
[email protected] | 0947506 | 2013-10-21 21:29:56 | [diff] [blame] | 630 | |
| 631 | content::CrashTab(browser()->tab_strip_model()->GetActiveWebContents()); |
| 632 | |
| 633 | ASSERT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER)); |
| 634 | } |
| 635 | |
[email protected] | 895a8472 | 2012-11-29 18:45:44 | [diff] [blame] | 636 | // 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] | cfd3379 | 2014-05-23 20:39:34 | [diff] [blame] | 638 | IN_PROC_BROWSER_TEST_F(BrowserFocusTest, NavigateFromOmniboxIntoNewTab) { |
[email protected] | 895a8472 | 2012-11-29 18:45:44 | [diff] [blame] | 639 | GURL url("http://www.google.com/"); |
| 640 | GURL url2("http://maps.google.com/"); |
| 641 | |
| 642 | // Navigate to url. |
Sylvain Defresne | c6ccc77d | 2014-09-19 10:19:35 | [diff] [blame] | 643 | chrome::NavigateParams p(browser(), url, ui::PAGE_TRANSITION_LINK); |
[email protected] | 895a8472 | 2012-11-29 18:45:44 | [diff] [blame] | 644 | p.window_action = chrome::NavigateParams::SHOW_WINDOW; |
nick | 3b04f32 | 2016-08-31 19:29:19 | [diff] [blame] | 645 | p.disposition = WindowOpenDisposition::CURRENT_TAB; |
[email protected] | 895a8472 | 2012-11-29 18:45:44 | [diff] [blame] | 646 | chrome::Navigate(&p); |
| 647 | |
| 648 | // Focus the omnibox. |
| 649 | chrome::FocusLocationBar(browser()); |
| 650 | |
[email protected] | cfd3379 | 2014-05-23 20:39:34 | [diff] [blame] | 651 | OmniboxEditController* controller = browser()->window()->GetLocationBar()-> |
| 652 | GetOmniboxView()->model()->controller(); |
[email protected] | 895a8472 | 2012-11-29 18:45:44 | [diff] [blame] | 653 | |
| 654 | // Simulate an alt-enter. |
nick | 3b04f32 | 2016-08-31 19:29:19 | [diff] [blame] | 655 | controller->OnAutocompleteAccept( |
| 656 | url2, WindowOpenDisposition::NEW_FOREGROUND_TAB, |
| 657 | ui::PAGE_TRANSITION_TYPED, AutocompleteMatchType::URL_WHAT_YOU_TYPED); |
[email protected] | 895a8472 | 2012-11-29 18:45:44 | [diff] [blame] | 658 | |
| 659 | // Make sure the second tab is selected. |
[email protected] | 04e3ead5 | 2013-01-14 01:09:20 | [diff] [blame] | 660 | EXPECT_EQ(1, browser()->tab_strip_model()->active_index()); |
[email protected] | 895a8472 | 2012-11-29 18:45:44 | [diff] [blame] | 661 | |
| 662 | // The tab contents should have the focus in the second tab. |
[email protected] | cfd3379 | 2014-05-23 20:39:34 | [diff] [blame] | 663 | EXPECT_TRUE(IsViewFocused(VIEW_ID_TAB_CONTAINER)); |
[email protected] | 895a8472 | 2012-11-29 18:45:44 | [diff] [blame] | 664 | |
| 665 | // Go back to the first tab. The focus should not be in the omnibox. |
| 666 | chrome::SelectPreviousTab(browser()); |
[email protected] | 04e3ead5 | 2013-01-14 01:09:20 | [diff] [blame] | 667 | EXPECT_EQ(0, browser()->tab_strip_model()->active_index()); |
[email protected] | cfd3379 | 2014-05-23 20:39:34 | [diff] [blame] | 668 | EXPECT_FALSE(IsViewFocused(VIEW_ID_OMNIBOX)); |
[email protected] | 895a8472 | 2012-11-29 18:45:44 | [diff] [blame] | 669 | } |
| 670 | |
tsergeant | ecbf1e11 | 2016-11-22 03:40:12 | [diff] [blame] | 671 | // Flaky on Windows and Mac (http://crbug.com/665296). |
| 672 | #if defined(OS_WIN) || defined(OS_MACOSX) |
Tsuyoshi Horo | c868c2f | 2016-11-15 05:21:38 | [diff] [blame] | 673 | #define MAYBE_FocusOnNavigate DISABLED_FocusOnNavigate |
| 674 | #else |
| 675 | #define MAYBE_FocusOnNavigate FocusOnNavigate |
| 676 | #endif |
| 677 | IN_PROC_BROWSER_TEST_F(BrowserFocusTest, MAYBE_FocusOnNavigate) { |
warx | 3e256ad | 2016-11-16 19:14:04 | [diff] [blame] | 678 | // Needed on Mac. |
| 679 | // TODO(warx): check why it is needed on Mac. |
[email protected] | c744f7d2 | 2013-04-12 20:29:04 | [diff] [blame] | 680 | ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); |
| 681 | // Load the NTP. |
[email protected] | 19506d54 | 2013-10-15 23:11:06 | [diff] [blame] | 682 | ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUINewTabURL)); |
[email protected] | c744f7d2 | 2013-04-12 20:29:04 | [diff] [blame] | 683 | 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()); |
nick | 3b04f32 | 2016-08-31 19:29:19 | [diff] [blame] | 698 | chrome::GoBack(browser(), WindowOpenDisposition::CURRENT_TAB); |
[email protected] | c744f7d2 | 2013-04-12 20:29:04 | [diff] [blame] | 699 | 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()); |
nick | 3b04f32 | 2016-08-31 19:29:19 | [diff] [blame] | 710 | chrome::GoForward(browser(), WindowOpenDisposition::CURRENT_TAB); |
[email protected] | c744f7d2 | 2013-04-12 20:29:04 | [diff] [blame] | 711 | forward_nav_observer.Wait(); |
| 712 | } |
| 713 | |
| 714 | EXPECT_FALSE(IsViewFocused(VIEW_ID_OMNIBOX)); |
| 715 | } |
| 716 | |
palmer | c70cb1f | 2016-03-04 23:41:26 | [diff] [blame] | 717 | // 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|. |
| 722 | IN_PROC_BROWSER_TEST_F(BrowserFocusTest, AboutBlankNavigationLocationTest) { |
creis | 8f3a9a6 | 2017-01-12 20:19:26 | [diff] [blame] | 723 | const GURL url1 = embedded_test_server()->GetURL("/title1.html"); |
palmer | c70cb1f | 2016-03-04 23:41:26 | [diff] [blame] | 724 | ui_test_utils::NavigateToURL(browser(), url1); |
| 725 | |
| 726 | TabStripModel* tab_strip = browser()->tab_strip_model(); |
| 727 | WebContents* web_contents = tab_strip->GetActiveWebContents(); |
| 728 | |
creis | 8f3a9a6 | 2017-01-12 20:19:26 | [diff] [blame] | 729 | 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() + "';"; |
palmer | c70cb1f | 2016-03-04 23:41:26 | [diff] [blame] | 733 | |
| 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 | |
creis | 8f3a9a6 | 2017-01-12 20:19:26 | [diff] [blame] | 743 | // 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. |
| 746 | IN_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] | 629e034 | 2010-07-27 23:30:13 | [diff] [blame] | 773 | } // namespace |