1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/ui/views/omnibox/omnibox_view_views.h"
7 #include "chrome/browser/ui/browser.h"
8 #include "chrome/browser/ui/browser_commands.h"
9 #include "chrome/browser/ui/browser_window.h"
10 #include "chrome/browser/ui/omnibox/location_bar.h"
11 #include "chrome/browser/ui/omnibox/omnibox_popup_model.h"
12 #include "chrome/browser/ui/view_ids.h"
13 #include "chrome/browser/ui/views/frame/browser_view.h"
14 #include "chrome/browser/ui/views/omnibox/omnibox_views.h"
15 #include "chrome/test/base/in_process_browser_test.h"
16 #include "chrome/test/base/interactive_test_utils.h"
17 #include "grit/generated_resources.h"
18 #include "ui/base/clipboard/clipboard.h"
19 #include "ui/base/clipboard/scoped_clipboard_writer.h"
20 #include "ui/base/test/ui_controls.h"
21 #include "ui/views/controls/textfield/native_textfield_wrapper.h"
24 #include "ui/aura/root_window.h"
25 #include "ui/aura/root_window_host_delegate.h"
26 #include "ui/aura/window.h"
27 #endif // defined(USE_AURA)
29 class OmniboxViewViewsTest : public InProcessBrowserTest {
31 OmniboxViewViewsTest() {}
33 static void GetOmniboxViewForBrowser(const Browser* browser,
34 OmniboxView** omnibox_view) {
35 BrowserWindow* window = browser->window();
37 LocationBar* location_bar = window->GetLocationBar();
38 ASSERT_TRUE(location_bar);
39 *omnibox_view = location_bar->GetLocationEntry();
40 ASSERT_TRUE(*omnibox_view);
43 // Move the mouse to the center of the browser window and left-click.
44 void ClickBrowserWindowCenter() {
45 ASSERT_TRUE(ui_test_utils::SendMouseMoveSync(
46 BrowserView::GetBrowserViewForBrowser(
47 browser())->GetBoundsInScreen().CenterPoint()));
48 ASSERT_TRUE(ui_test_utils::SendMouseEventsSync(ui_controls::LEFT,
51 ui_test_utils::SendMouseEventsSync(ui_controls::LEFT, ui_controls::UP));
54 // Press and release the mouse at the specified locations. If
55 // |release_offset| differs from |press_offset|, the mouse will be moved
56 // between the press and release.
57 void Click(ui_controls::MouseButton button,
58 const gfx::Point& press_location,
59 const gfx::Point& release_location) {
60 ASSERT_TRUE(ui_test_utils::SendMouseMoveSync(press_location));
61 ASSERT_TRUE(ui_test_utils::SendMouseEventsSync(button, ui_controls::DOWN));
63 if (press_location != release_location)
64 ASSERT_TRUE(ui_test_utils::SendMouseMoveSync(release_location));
65 ASSERT_TRUE(ui_test_utils::SendMouseEventsSync(button, ui_controls::UP));
69 // Tap the center of the browser window.
70 void TapBrowserWindowCenter() {
71 aura::RootWindowHostDelegate* rwhd =
72 browser()->window()->GetNativeWindow()->GetRootWindow()->
73 GetDispatcher()->AsRootWindowHostDelegate();
75 gfx::Point center = BrowserView::GetBrowserViewForBrowser(
76 browser())->GetBoundsInScreen().CenterPoint();
77 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, center,
78 5, base::TimeDelta::FromMilliseconds(0));
79 rwhd->OnHostTouchEvent(&press);
81 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, center,
82 5, base::TimeDelta::FromMilliseconds(50));
83 rwhd->OnHostTouchEvent(&release);
86 // Touch down and release at the specified locations.
87 void Tap(const gfx::Point& press_location,
88 const gfx::Point& release_location) {
89 aura::RootWindowHostDelegate* rwhd =
90 browser()->window()->GetNativeWindow()->GetRootWindow()->
91 GetDispatcher()->AsRootWindowHostDelegate();
93 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, press_location,
94 5, base::TimeDelta::FromMilliseconds(0));
95 rwhd->OnHostTouchEvent(&press);
97 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, release_location,
98 5, base::TimeDelta::FromMilliseconds(50));
99 rwhd->OnHostTouchEvent(&release);
101 #endif // defined(USE_AURA)
104 // InProcessBrowserTest:
105 virtual void SetUpOnMainThread() OVERRIDE {
106 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
107 chrome::FocusLocationBar(browser());
108 ASSERT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
111 DISALLOW_COPY_AND_ASSIGN(OmniboxViewViewsTest);
114 IN_PROC_BROWSER_TEST_F(OmniboxViewViewsTest, PasteAndGoDoesNotLeavePopupOpen) {
115 OmniboxView* view = browser()->window()->GetLocationBar()->GetLocationEntry();
116 OmniboxViewViews* omnibox_view_views = GetOmniboxViewViews(view);
117 // This test is only relevant when OmniboxViewViews is present and is using
118 // the native textfield wrapper.
119 if (!omnibox_view_views)
121 views::NativeTextfieldWrapper* native_textfield_wrapper =
122 static_cast<views::NativeTextfieldWrapper*>(
123 omnibox_view_views->GetNativeWrapperForTesting());
124 if (!native_textfield_wrapper)
127 // Put an URL on the clipboard.
129 ui::ScopedClipboardWriter clipboard_writer(
130 ui::Clipboard::GetForCurrentThread(), ui::CLIPBOARD_TYPE_COPY_PASTE);
131 clipboard_writer.WriteURL(ASCIIToUTF16("http://www.example.com/"));
135 native_textfield_wrapper->ExecuteTextCommand(IDS_PASTE_AND_GO);
137 // The popup should not be open.
138 EXPECT_FALSE(view->model()->popup_model()->IsOpen());
141 IN_PROC_BROWSER_TEST_F(OmniboxViewViewsTest, SelectAllOnClick) {
142 OmniboxView* omnibox_view = NULL;
143 ASSERT_NO_FATAL_FAILURE(GetOmniboxViewForBrowser(browser(), &omnibox_view));
144 omnibox_view->SetUserText(ASCIIToUTF16("http://www.google.com/"));
146 // Take the focus away from the omnibox.
147 ASSERT_NO_FATAL_FAILURE(ClickBrowserWindowCenter());
148 EXPECT_FALSE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
149 EXPECT_FALSE(omnibox_view->IsSelectAll());
151 // Clicking in the omnibox should take focus and select all text.
152 const gfx::Rect omnibox_bounds = BrowserView::GetBrowserViewForBrowser(
153 browser())->GetViewByID(VIEW_ID_OMNIBOX)->GetBoundsInScreen();
154 const gfx::Point click_location = omnibox_bounds.CenterPoint();
155 ASSERT_NO_FATAL_FAILURE(Click(ui_controls::LEFT,
156 click_location, click_location));
157 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
158 EXPECT_TRUE(omnibox_view->IsSelectAll());
160 // Clicking in another view should clear focus and the selection.
161 ASSERT_NO_FATAL_FAILURE(ClickBrowserWindowCenter());
162 EXPECT_FALSE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
163 EXPECT_FALSE(omnibox_view->IsSelectAll());
165 // Clicking in the omnibox again should take focus and select all text again.
166 ASSERT_NO_FATAL_FAILURE(Click(ui_controls::LEFT,
167 click_location, click_location));
168 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
169 EXPECT_TRUE(omnibox_view->IsSelectAll());
171 // Clicking another omnibox spot should keep focus but clear the selection.
172 omnibox_view->SelectAll(false);
173 const gfx::Point click2_location = omnibox_bounds.origin() +
174 gfx::Vector2d(omnibox_bounds.width() / 4, omnibox_bounds.height() / 4);
175 ASSERT_NO_FATAL_FAILURE(Click(ui_controls::LEFT,
176 click2_location, click2_location));
177 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
178 EXPECT_FALSE(omnibox_view->IsSelectAll());
180 // Take the focus away and click in the omnibox again, but drag a bit before
181 // releasing. We should focus the omnibox but not select all of its text.
182 ASSERT_NO_FATAL_FAILURE(ClickBrowserWindowCenter());
183 ASSERT_NO_FATAL_FAILURE(Click(ui_controls::LEFT,
184 click_location, click2_location));
185 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
186 EXPECT_FALSE(omnibox_view->IsSelectAll());
188 // Middle-clicking should not be handled by the omnibox.
189 ASSERT_NO_FATAL_FAILURE(ClickBrowserWindowCenter());
190 ASSERT_NO_FATAL_FAILURE(Click(ui_controls::MIDDLE,
191 click_location, click_location));
192 EXPECT_FALSE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
193 EXPECT_FALSE(omnibox_view->IsSelectAll());
196 #if defined(USE_AURA)
197 IN_PROC_BROWSER_TEST_F(OmniboxViewViewsTest, SelectAllOnTap) {
198 OmniboxView* omnibox_view = NULL;
199 ASSERT_NO_FATAL_FAILURE(GetOmniboxViewForBrowser(browser(), &omnibox_view));
200 omnibox_view->SetUserText(ASCIIToUTF16("http://www.google.com/"));
202 // Take the focus away from the omnibox.
203 ASSERT_NO_FATAL_FAILURE(TapBrowserWindowCenter());
204 EXPECT_FALSE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
205 EXPECT_FALSE(omnibox_view->IsSelectAll());
207 // Tapping in the omnibox should take focus and select all text.
208 const gfx::Rect omnibox_bounds = BrowserView::GetBrowserViewForBrowser(
209 browser())->GetViewByID(VIEW_ID_OMNIBOX)->GetBoundsInScreen();
210 const gfx::Point tap_location = omnibox_bounds.CenterPoint();
211 ASSERT_NO_FATAL_FAILURE(Tap(tap_location, tap_location));
212 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
213 EXPECT_TRUE(omnibox_view->IsSelectAll());
215 // Tapping in another view should clear focus and the selection.
216 ASSERT_NO_FATAL_FAILURE(TapBrowserWindowCenter());
217 EXPECT_FALSE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
218 EXPECT_FALSE(omnibox_view->IsSelectAll());
220 // Tapping in the omnibox again should take focus and select all text again.
221 ASSERT_NO_FATAL_FAILURE(Tap(tap_location, tap_location));
222 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
223 EXPECT_TRUE(omnibox_view->IsSelectAll());
225 // Tapping another omnibox spot should keep focus and selection.
226 omnibox_view->SelectAll(false);
227 const gfx::Point tap2_location = omnibox_bounds.origin() +
228 gfx::Vector2d(omnibox_bounds.width() / 4, omnibox_bounds.height() / 4);
229 ASSERT_NO_FATAL_FAILURE(Tap(tap2_location, tap2_location));
230 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
231 // We don't test if the all text is selected because it depends on whether or
232 // not there was text under the tap, which appears to be flaky.
234 // Take the focus away and tap in the omnibox again, but drag a bit before
235 // releasing. We should focus the omnibox but not select all of its text.
236 ASSERT_NO_FATAL_FAILURE(TapBrowserWindowCenter());
237 ASSERT_NO_FATAL_FAILURE(Tap(tap_location, tap2_location));
238 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
239 EXPECT_FALSE(omnibox_view->IsSelectAll());
241 #endif // defined(USE_AURA)