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 "base/command_line.h"
8 #include "chrome/browser/search_engines/template_url_service_factory.h"
9 #include "chrome/browser/ui/browser.h"
10 #include "chrome/browser/ui/browser_commands.h"
11 #include "chrome/browser/ui/browser_window.h"
12 #include "chrome/browser/ui/location_bar/location_bar.h"
13 #include "chrome/browser/ui/view_ids.h"
14 #include "chrome/browser/ui/views/frame/browser_view.h"
15 #include "chrome/browser/ui/views/location_bar/location_bar_view.h"
16 #include "chrome/browser/ui/views/toolbar/toolbar_view.h"
17 #include "chrome/grit/generated_resources.h"
18 #include "chrome/test/base/in_process_browser_test.h"
19 #include "chrome/test/base/interactive_test_utils.h"
20 #include "components/omnibox/browser/omnibox_popup_model.h"
21 #include "ui/base/clipboard/clipboard.h"
22 #include "ui/base/clipboard/scoped_clipboard_writer.h"
23 #include "ui/base/ime/input_method.h"
24 #include "ui/base/ime/text_input_client.h"
25 #include "ui/base/test/ui_controls.h"
26 #include "ui/base/ui_base_switches.h"
27 #include "ui/events/event_processor.h"
28 #include "ui/events/event_utils.h"
29 #include "ui/events/test/event_generator.h"
30 #include "ui/views/controls/textfield/textfield_test_api.h"
34 void SetClipboardText(ui::ClipboardType type
, const std::string
& text
) {
35 ui::ScopedClipboardWriter(type
).WriteText(base::ASCIIToUTF16(text
));
40 class OmniboxViewViewsTest
: public InProcessBrowserTest
{
42 OmniboxViewViewsTest() {}
44 static void GetOmniboxViewForBrowser(const Browser
* browser
,
45 OmniboxView
** omnibox_view
) {
46 BrowserWindow
* window
= browser
->window();
48 LocationBar
* location_bar
= window
->GetLocationBar();
49 ASSERT_TRUE(location_bar
);
50 *omnibox_view
= location_bar
->GetOmniboxView();
51 ASSERT_TRUE(*omnibox_view
);
54 // Move the mouse to the center of the browser window and left-click.
55 void ClickBrowserWindowCenter() {
56 ASSERT_TRUE(ui_test_utils::SendMouseMoveSync(
57 BrowserView::GetBrowserViewForBrowser(
58 browser())->GetBoundsInScreen().CenterPoint()));
59 ASSERT_TRUE(ui_test_utils::SendMouseEventsSync(ui_controls::LEFT
,
62 ui_test_utils::SendMouseEventsSync(ui_controls::LEFT
, ui_controls::UP
));
65 // Press and release the mouse at the specified locations. If
66 // |release_offset| differs from |press_offset|, the mouse will be moved
67 // between the press and release.
68 void Click(ui_controls::MouseButton button
,
69 const gfx::Point
& press_location
,
70 const gfx::Point
& release_location
) {
71 ASSERT_TRUE(ui_test_utils::SendMouseMoveSync(press_location
));
72 ASSERT_TRUE(ui_test_utils::SendMouseEventsSync(button
, ui_controls::DOWN
));
74 if (press_location
!= release_location
)
75 ASSERT_TRUE(ui_test_utils::SendMouseMoveSync(release_location
));
76 ASSERT_TRUE(ui_test_utils::SendMouseEventsSync(button
, ui_controls::UP
));
79 // Tap the center of the browser window.
80 void TapBrowserWindowCenter() {
81 gfx::Point center
= BrowserView::GetBrowserViewForBrowser(
82 browser())->GetBoundsInScreen().CenterPoint();
83 ui::test::EventGenerator
generator(browser()->window()->GetNativeWindow());
84 generator
.GestureTapAt(center
);
87 // Touch down and release at the specified locations.
88 void Tap(const gfx::Point
& press_location
,
89 const gfx::Point
& release_location
) {
90 ui::test::EventGenerator
generator(browser()->window()->GetNativeWindow());
91 if (press_location
== release_location
) {
92 generator
.GestureTapAt(press_location
);
94 generator
.GestureScrollSequence(press_location
,
96 base::TimeDelta::FromMilliseconds(10),
102 // InProcessBrowserTest:
103 void SetUpOnMainThread() override
{
104 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
105 chrome::FocusLocationBar(browser());
106 ASSERT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX
));
109 DISALLOW_COPY_AND_ASSIGN(OmniboxViewViewsTest
);
112 IN_PROC_BROWSER_TEST_F(OmniboxViewViewsTest
, PasteAndGoDoesNotLeavePopupOpen
) {
113 OmniboxView
* view
= NULL
;
114 ASSERT_NO_FATAL_FAILURE(GetOmniboxViewForBrowser(browser(), &view
));
115 OmniboxViewViews
* omnibox_view_views
= static_cast<OmniboxViewViews
*>(view
);
117 // Put an URL on the clipboard.
118 SetClipboardText(ui::CLIPBOARD_TYPE_COPY_PASTE
, "http://www.example.com/");
121 omnibox_view_views
->ExecuteCommand(IDS_PASTE_AND_GO
, ui::EF_NONE
);
123 // The popup should not be open.
124 EXPECT_FALSE(view
->model()->popup_model()->IsOpen());
127 IN_PROC_BROWSER_TEST_F(OmniboxViewViewsTest
, SelectAllOnClick
) {
128 OmniboxView
* omnibox_view
= NULL
;
129 ASSERT_NO_FATAL_FAILURE(GetOmniboxViewForBrowser(browser(), &omnibox_view
));
130 omnibox_view
->SetUserText(base::ASCIIToUTF16("http://www.google.com/"));
132 // Take the focus away from the omnibox.
133 ASSERT_NO_FATAL_FAILURE(ClickBrowserWindowCenter());
134 EXPECT_FALSE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX
));
135 EXPECT_FALSE(omnibox_view
->IsSelectAll());
137 // Clicking in the omnibox should take focus and select all text.
138 const gfx::Rect omnibox_bounds
= BrowserView::GetBrowserViewForBrowser(
139 browser())->GetViewByID(VIEW_ID_OMNIBOX
)->GetBoundsInScreen();
140 const gfx::Point click_location
= omnibox_bounds
.CenterPoint();
141 ASSERT_NO_FATAL_FAILURE(Click(ui_controls::LEFT
,
142 click_location
, click_location
));
143 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX
));
144 EXPECT_TRUE(omnibox_view
->IsSelectAll());
146 // Clicking in another view should clear focus and the selection.
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 again should take focus and select all text again.
152 ASSERT_NO_FATAL_FAILURE(Click(ui_controls::LEFT
,
153 click_location
, click_location
));
154 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX
));
155 EXPECT_TRUE(omnibox_view
->IsSelectAll());
157 // Clicking another omnibox spot should keep focus but clear the selection.
158 omnibox_view
->SelectAll(false);
159 const gfx::Point click2_location
= omnibox_bounds
.origin() +
160 gfx::Vector2d(omnibox_bounds
.width() / 4, omnibox_bounds
.height() / 4);
161 ASSERT_NO_FATAL_FAILURE(Click(ui_controls::LEFT
,
162 click2_location
, click2_location
));
163 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX
));
164 EXPECT_FALSE(omnibox_view
->IsSelectAll());
166 // Take the focus away and click in the omnibox again, but drag a bit before
167 // releasing. We should focus the omnibox but not select all of its text.
168 ASSERT_NO_FATAL_FAILURE(ClickBrowserWindowCenter());
169 ASSERT_NO_FATAL_FAILURE(Click(ui_controls::LEFT
,
170 click_location
, click2_location
));
171 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX
));
172 EXPECT_FALSE(omnibox_view
->IsSelectAll());
174 // Middle-click is only handled on Linux, by pasting the selection clipboard
175 // and moving the cursor after the pasted text instead of selecting-all.
176 ASSERT_NO_FATAL_FAILURE(ClickBrowserWindowCenter());
177 ASSERT_NO_FATAL_FAILURE(Click(ui_controls::MIDDLE
,
178 click_location
, click_location
));
179 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
180 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX
));
181 EXPECT_FALSE(omnibox_view
->IsSelectAll());
183 EXPECT_FALSE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX
));
184 EXPECT_FALSE(omnibox_view
->IsSelectAll());
185 #endif // OS_LINUX && !OS_CHROMEOS
188 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
189 IN_PROC_BROWSER_TEST_F(OmniboxViewViewsTest
, SelectionClipboard
) {
190 OmniboxView
* omnibox_view
= NULL
;
191 ASSERT_NO_FATAL_FAILURE(GetOmniboxViewForBrowser(browser(), &omnibox_view
));
192 omnibox_view
->SetUserText(base::ASCIIToUTF16("http://www.google.com/"));
193 OmniboxViewViews
* omnibox_view_views
=
194 static_cast<OmniboxViewViews
*>(omnibox_view
);
195 ASSERT_TRUE(omnibox_view_views
);
196 gfx::RenderText
* render_text
= omnibox_view_views
->GetRenderText();
198 // Take the focus away from the omnibox.
199 ASSERT_NO_FATAL_FAILURE(TapBrowserWindowCenter());
200 EXPECT_FALSE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX
));
201 EXPECT_FALSE(omnibox_view
->IsSelectAll());
203 size_t cursor_position
= 14;
204 int cursor_x
= render_text
->GetCursorBounds(
205 gfx::SelectionModel(cursor_position
, gfx::CURSOR_FORWARD
), false).x();
206 gfx::Point click_location
= omnibox_view_views
->GetBoundsInScreen().origin();
207 click_location
.Offset(cursor_x
+ render_text
->display_rect().x(),
208 omnibox_view_views
->height() / 2);
210 // Middle click focuses the omnibox, pastes, and sets a trailing cursor.
211 // Select-all on focus shouldn't alter the selection clipboard or cursor.
212 SetClipboardText(ui::CLIPBOARD_TYPE_SELECTION
, "123");
213 ASSERT_NO_FATAL_FAILURE(Click(ui_controls::MIDDLE
,
214 click_location
, click_location
));
215 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX
));
216 EXPECT_FALSE(omnibox_view
->IsSelectAll());
217 EXPECT_EQ(base::ASCIIToUTF16("http://www.goo123gle.com/"),
218 omnibox_view
->GetText());
219 EXPECT_EQ(17U, omnibox_view_views
->GetCursorPosition());
221 // Middle clicking again, with focus, pastes and updates the cursor.
222 SetClipboardText(ui::CLIPBOARD_TYPE_SELECTION
, "4567");
223 ASSERT_NO_FATAL_FAILURE(Click(ui_controls::MIDDLE
,
224 click_location
, click_location
));
225 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX
));
226 EXPECT_FALSE(omnibox_view
->IsSelectAll());
227 EXPECT_EQ(base::ASCIIToUTF16("http://www.goo4567123gle.com/"),
228 omnibox_view
->GetText());
229 EXPECT_EQ(18U, omnibox_view_views
->GetCursorPosition());
231 #endif // OS_LINUX && !OS_CHROMEOS
233 IN_PROC_BROWSER_TEST_F(OmniboxViewViewsTest
, SelectAllOnTap
) {
234 OmniboxView
* omnibox_view
= NULL
;
235 ASSERT_NO_FATAL_FAILURE(GetOmniboxViewForBrowser(browser(), &omnibox_view
));
236 omnibox_view
->SetUserText(base::ASCIIToUTF16("http://www.google.com/"));
238 // Take the focus away from the omnibox.
239 ASSERT_NO_FATAL_FAILURE(TapBrowserWindowCenter());
240 EXPECT_FALSE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX
));
241 EXPECT_FALSE(omnibox_view
->IsSelectAll());
243 // Tapping in the omnibox should take focus and select all text.
244 const gfx::Rect omnibox_bounds
= BrowserView::GetBrowserViewForBrowser(
245 browser())->GetViewByID(VIEW_ID_OMNIBOX
)->GetBoundsInScreen();
246 const gfx::Point tap_location
= omnibox_bounds
.CenterPoint();
247 ASSERT_NO_FATAL_FAILURE(Tap(tap_location
, tap_location
));
248 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX
));
249 EXPECT_TRUE(omnibox_view
->IsSelectAll());
251 // Tapping in another view should clear focus and the selection.
252 ASSERT_NO_FATAL_FAILURE(TapBrowserWindowCenter());
253 EXPECT_FALSE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX
));
254 EXPECT_FALSE(omnibox_view
->IsSelectAll());
256 // Tapping in the omnibox again should take focus and select all text again.
257 ASSERT_NO_FATAL_FAILURE(Tap(tap_location
, tap_location
));
258 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX
));
259 EXPECT_TRUE(omnibox_view
->IsSelectAll());
261 // Tapping another omnibox spot should keep focus and selection.
262 omnibox_view
->SelectAll(false);
263 const gfx::Point tap2_location
= omnibox_bounds
.origin() +
264 gfx::Vector2d(omnibox_bounds
.width() / 4, omnibox_bounds
.height() / 4);
265 ASSERT_NO_FATAL_FAILURE(Tap(tap2_location
, tap2_location
));
266 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX
));
267 // We don't test if the all text is selected because it depends on whether or
268 // not there was text under the tap, which appears to be flaky.
270 // Take the focus away and tap in the omnibox again, but drag a bit before
271 // releasing. We should focus the omnibox but not select all of its text.
272 ASSERT_NO_FATAL_FAILURE(TapBrowserWindowCenter());
273 ASSERT_NO_FATAL_FAILURE(Tap(tap_location
, tap2_location
));
274 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX
));
275 EXPECT_FALSE(omnibox_view
->IsSelectAll());
278 IN_PROC_BROWSER_TEST_F(OmniboxViewViewsTest
, SelectAllOnTabToFocus
) {
279 OmniboxView
* omnibox_view
= NULL
;
280 ASSERT_NO_FATAL_FAILURE(GetOmniboxViewForBrowser(browser(), &omnibox_view
));
281 ui_test_utils::NavigateToURL(browser(), GURL("http://www.google.com/"));
282 // RevertAll after navigation to invalidate the selection range saved on blur.
283 omnibox_view
->RevertAll();
284 EXPECT_FALSE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX
));
285 EXPECT_FALSE(omnibox_view
->IsSelectAll());
287 // Pressing tab to focus the omnibox should select all text.
288 while (!ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX
)) {
289 ASSERT_TRUE(ui_test_utils::SendKeyPressSync(browser(), ui::VKEY_TAB
,
290 false, false, false, false));
292 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX
));
293 EXPECT_TRUE(omnibox_view
->IsSelectAll());
296 IN_PROC_BROWSER_TEST_F(OmniboxViewViewsTest
, CloseOmniboxPopupOnTextDrag
) {
297 OmniboxView
* omnibox_view
= NULL
;
298 ASSERT_NO_FATAL_FAILURE(GetOmniboxViewForBrowser(browser(), &omnibox_view
));
299 OmniboxViewViews
* omnibox_view_views
=
300 static_cast<OmniboxViewViews
*>(omnibox_view
);
302 // Populate suggestions for the omnibox popup.
303 AutocompleteController
* autocomplete_controller
=
304 omnibox_view
->model()->popup_model()->autocomplete_controller();
305 AutocompleteResult
& results
= autocomplete_controller
->result_
;
307 AutocompleteMatch match
;
308 match
.destination_url
= GURL("http://autocomplete-result/");
309 match
.allowed_to_be_default_match
= true;
310 match
.type
= AutocompleteMatchType::HISTORY_TITLE
;
311 match
.relevance
= 500;
312 matches
.push_back(match
);
313 match
.destination_url
= GURL("http://autocomplete-result2/");
314 matches
.push_back(match
);
315 results
.AppendMatches(AutocompleteInput(), matches
);
319 TemplateURLServiceFactory::GetForProfile(browser()->profile()));
321 // The omnibox popup should open with suggestions displayed.
322 omnibox_view
->model()->popup_model()->OnResultChanged();
323 EXPECT_TRUE(omnibox_view
->model()->popup_model()->IsOpen());
325 // The omnibox text should be selected.
326 EXPECT_TRUE(omnibox_view
->IsSelectAll());
328 // Simulate a mouse click before dragging the mouse.
329 gfx::Point
point(omnibox_view_views
->x(), omnibox_view_views
->y());
330 ui::MouseEvent
pressed(ui::ET_MOUSE_PRESSED
, point
, point
,
331 ui::EventTimeForNow(), ui::EF_LEFT_MOUSE_BUTTON
,
332 ui::EF_LEFT_MOUSE_BUTTON
);
333 omnibox_view_views
->OnMousePressed(pressed
);
334 EXPECT_TRUE(omnibox_view
->model()->popup_model()->IsOpen());
336 // Simulate a mouse drag of the omnibox text, and the omnibox should close.
337 ui::MouseEvent
dragged(ui::ET_MOUSE_DRAGGED
, point
, point
,
338 ui::EventTimeForNow(), ui::EF_LEFT_MOUSE_BUTTON
, 0);
339 omnibox_view_views
->OnMouseDragged(dragged
);
341 EXPECT_FALSE(omnibox_view
->model()->popup_model()->IsOpen());
344 IN_PROC_BROWSER_TEST_F(OmniboxViewViewsTest
, BackgroundIsOpaque
) {
345 // The omnibox text should be rendered on an opaque background. Otherwise, we
346 // can't use subpixel rendering.
347 OmniboxViewViews
* view
= BrowserView::GetBrowserViewForBrowser(browser())->
348 toolbar()->location_bar()->omnibox_view();
350 EXPECT_FALSE(view
->GetRenderText()->subpixel_rendering_suppressed());
353 // Tests if executing a command hides touch editing handles.
354 IN_PROC_BROWSER_TEST_F(OmniboxViewViewsTest
,
355 DeactivateTouchEditingOnExecuteCommand
) {
356 base::CommandLine::ForCurrentProcess()->AppendSwitch(
357 switches::kEnableTouchEditing
);
359 OmniboxView
* view
= NULL
;
360 ASSERT_NO_FATAL_FAILURE(GetOmniboxViewForBrowser(browser(), &view
));
361 OmniboxViewViews
* omnibox_view_views
= static_cast<OmniboxViewViews
*>(view
);
362 views::TextfieldTestApi
textfield_test_api(omnibox_view_views
);
364 // Put a URL on the clipboard.
365 SetClipboardText(ui::CLIPBOARD_TYPE_COPY_PASTE
, "http://www.example.com/");
367 // Tap to activate touch editing.
368 gfx::Point omnibox_center
=
369 omnibox_view_views
->GetBoundsInScreen().CenterPoint();
370 Tap(omnibox_center
, omnibox_center
);
371 EXPECT_TRUE(textfield_test_api
.touch_selection_controller());
373 // Execute a command and check if it deactivate touch editing. Paste & Go is
374 // chosen since it is specific to Omnibox and its execution wouldn't be
375 // delgated to the base Textfield class.
376 omnibox_view_views
->ExecuteCommand(IDS_PASTE_AND_GO
, ui::EF_NONE
);
377 EXPECT_FALSE(textfield_test_api
.touch_selection_controller());
380 IN_PROC_BROWSER_TEST_F(OmniboxViewViewsTest
, FocusedTextInputClient
) {
381 chrome::FocusLocationBar(browser());
382 OmniboxView
* view
= NULL
;
383 ASSERT_NO_FATAL_FAILURE(GetOmniboxViewForBrowser(browser(), &view
));
384 OmniboxViewViews
* omnibox_view_views
= static_cast<OmniboxViewViews
*>(view
);
385 ui::InputMethod
* input_method
=
386 omnibox_view_views
->GetWidget()->GetInputMethod();
387 EXPECT_EQ(static_cast<ui::TextInputClient
*>(omnibox_view_views
),
388 input_method
->GetTextInputClient());