Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(327)

Side by Side Diff: chrome/browser/ui/views/omnibox/omnibox_view_views_browsertest.cc

Issue 55893004: Omnibox selects all text on tap. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix win aura test. Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « chrome/browser/ui/views/omnibox/omnibox_view_views.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/ui/views/omnibox/omnibox_view_views.h" 5 #include "chrome/browser/ui/views/omnibox/omnibox_view_views.h"
6 6
7 #include "chrome/browser/ui/browser.h" 7 #include "chrome/browser/ui/browser.h"
8 #include "chrome/browser/ui/browser_commands.h" 8 #include "chrome/browser/ui/browser_commands.h"
9 #include "chrome/browser/ui/browser_window.h" 9 #include "chrome/browser/ui/browser_window.h"
10 #include "chrome/browser/ui/omnibox/location_bar.h" 10 #include "chrome/browser/ui/omnibox/location_bar.h"
11 #include "chrome/browser/ui/omnibox/omnibox_popup_model.h" 11 #include "chrome/browser/ui/omnibox/omnibox_popup_model.h"
12 #include "chrome/browser/ui/view_ids.h" 12 #include "chrome/browser/ui/view_ids.h"
13 #include "chrome/browser/ui/views/frame/browser_view.h" 13 #include "chrome/browser/ui/views/frame/browser_view.h"
14 #include "chrome/browser/ui/views/omnibox/omnibox_views.h" 14 #include "chrome/browser/ui/views/omnibox/omnibox_views.h"
15 #include "chrome/test/base/in_process_browser_test.h" 15 #include "chrome/test/base/in_process_browser_test.h"
16 #include "chrome/test/base/interactive_test_utils.h" 16 #include "chrome/test/base/interactive_test_utils.h"
17 #include "grit/generated_resources.h" 17 #include "grit/generated_resources.h"
18 #include "ui/base/clipboard/clipboard.h" 18 #include "ui/base/clipboard/clipboard.h"
19 #include "ui/base/clipboard/scoped_clipboard_writer.h" 19 #include "ui/base/clipboard/scoped_clipboard_writer.h"
20 #include "ui/base/test/ui_controls.h" 20 #include "ui/base/test/ui_controls.h"
21 #include "ui/views/controls/textfield/native_textfield_wrapper.h" 21 #include "ui/views/controls/textfield/native_textfield_wrapper.h"
22 22
23 #if defined(USE_AURA)
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)
28
23 class OmniboxViewViewsTest : public InProcessBrowserTest { 29 class OmniboxViewViewsTest : public InProcessBrowserTest {
24 protected: 30 protected:
25 OmniboxViewViewsTest() {} 31 OmniboxViewViewsTest() {}
26 32
27 static void GetOmniboxViewForBrowser(const Browser* browser, 33 static void GetOmniboxViewForBrowser(const Browser* browser,
28 OmniboxView** omnibox_view) { 34 OmniboxView** omnibox_view) {
29 BrowserWindow* window = browser->window(); 35 BrowserWindow* window = browser->window();
30 ASSERT_TRUE(window); 36 ASSERT_TRUE(window);
31 LocationBar* location_bar = window->GetLocationBar(); 37 LocationBar* location_bar = window->GetLocationBar();
32 ASSERT_TRUE(location_bar); 38 ASSERT_TRUE(location_bar);
33 *omnibox_view = location_bar->GetLocationEntry(); 39 *omnibox_view = location_bar->GetLocationEntry();
34 ASSERT_TRUE(*omnibox_view); 40 ASSERT_TRUE(*omnibox_view);
35 } 41 }
36 42
37 // Move the mouse to the center of the browser window and left-click. 43 // Move the mouse to the center of the browser window and left-click.
38 void ClickBrowserWindowCenter() { 44 void ClickBrowserWindowCenter() {
39 ASSERT_TRUE(ui_test_utils::SendMouseMoveSync( 45 ASSERT_TRUE(ui_test_utils::SendMouseMoveSync(
40 BrowserView::GetBrowserViewForBrowser( 46 BrowserView::GetBrowserViewForBrowser(
41 browser())->GetBoundsInScreen().CenterPoint())); 47 browser())->GetBoundsInScreen().CenterPoint()));
42 ASSERT_TRUE(ui_test_utils::SendMouseEventsSync(ui_controls::LEFT, 48 ASSERT_TRUE(ui_test_utils::SendMouseEventsSync(ui_controls::LEFT,
43 ui_controls::DOWN)); 49 ui_controls::DOWN));
44 ASSERT_TRUE( 50 ASSERT_TRUE(
45 ui_test_utils::SendMouseEventsSync(ui_controls::LEFT, ui_controls::UP)); 51 ui_test_utils::SendMouseEventsSync(ui_controls::LEFT, ui_controls::UP));
46 } 52 }
47 53
48 // Press and release the mouse in the omnibox at an offset from its origin. 54 // Press and release the mouse at the specified locations. If
49 // If |release_offset| differs from |press_offset|, the mouse will be moved 55 // |release_offset| differs from |press_offset|, the mouse will be moved
50 // between the press and release. 56 // between the press and release.
51 void ClickOmnibox(ui_controls::MouseButton button, 57 void Click(ui_controls::MouseButton button,
52 const gfx::Vector2d& press_offset, 58 const gfx::Point& press_location,
53 const gfx::Vector2d& release_offset) { 59 const gfx::Point& release_location) {
54 const views::View* omnibox = BrowserView::GetBrowserViewForBrowser( 60 ASSERT_TRUE(ui_test_utils::SendMouseMoveSync(press_location));
55 browser())->GetViewByID(VIEW_ID_OMNIBOX);
56 gfx::Point omnibox_origin = omnibox->GetBoundsInScreen().origin();
57 gfx::Point press_point = omnibox_origin + press_offset;
58 ASSERT_TRUE(ui_test_utils::SendMouseMoveSync(press_point));
59 ASSERT_TRUE(ui_test_utils::SendMouseEventsSync(button, ui_controls::DOWN)); 61 ASSERT_TRUE(ui_test_utils::SendMouseEventsSync(button, ui_controls::DOWN));
60 62
61 gfx::Point release_point = omnibox_origin + release_offset; 63 if (press_location != release_location)
62 if (release_point != press_point) 64 ASSERT_TRUE(ui_test_utils::SendMouseMoveSync(release_location));
63 ASSERT_TRUE(ui_test_utils::SendMouseMoveSync(release_point));
64 ASSERT_TRUE(ui_test_utils::SendMouseEventsSync(button, ui_controls::UP)); 65 ASSERT_TRUE(ui_test_utils::SendMouseEventsSync(button, ui_controls::UP));
65 } 66 }
66 67
68 #if defined(USE_AURA)
69 // Tap the center of the browser window.
70 void TapBrowserWindowCenter() {
71 aura::RootWindowHostDelegate* rwhd =
72 browser()->window()->GetNativeWindow()->GetRootWindow()->
73 GetDispatcher()->AsRootWindowHostDelegate();
74
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);
80
81 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, center,
82 5, base::TimeDelta::FromMilliseconds(50));
83 rwhd->OnHostTouchEvent(&release);
84 }
85
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();
92
93 ui::TouchEvent press(ui::ET_TOUCH_PRESSED, press_location,
94 5, base::TimeDelta::FromMilliseconds(0));
95 rwhd->OnHostTouchEvent(&press);
96
97 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, release_location,
98 5, base::TimeDelta::FromMilliseconds(50));
99 rwhd->OnHostTouchEvent(&release);
100 }
101 #endif // defined(USE_AURA)
102
67 private: 103 private:
68 // InProcessBrowserTest: 104 // InProcessBrowserTest:
69 virtual void SetUpOnMainThread() OVERRIDE { 105 virtual void SetUpOnMainThread() OVERRIDE {
70 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); 106 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
71 chrome::FocusLocationBar(browser()); 107 chrome::FocusLocationBar(browser());
72 ASSERT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX)); 108 ASSERT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
73 } 109 }
74 110
75 DISALLOW_COPY_AND_ASSIGN(OmniboxViewViewsTest); 111 DISALLOW_COPY_AND_ASSIGN(OmniboxViewViewsTest);
76 }; 112 };
(...skipping 22 matching lines...) Expand all
99 native_textfield_wrapper->ExecuteTextCommand(IDS_PASTE_AND_GO); 135 native_textfield_wrapper->ExecuteTextCommand(IDS_PASTE_AND_GO);
100 136
101 // The popup should not be open. 137 // The popup should not be open.
102 EXPECT_FALSE(view->model()->popup_model()->IsOpen()); 138 EXPECT_FALSE(view->model()->popup_model()->IsOpen());
103 } 139 }
104 140
105 IN_PROC_BROWSER_TEST_F(OmniboxViewViewsTest, SelectAllOnClick) { 141 IN_PROC_BROWSER_TEST_F(OmniboxViewViewsTest, SelectAllOnClick) {
106 OmniboxView* omnibox_view = NULL; 142 OmniboxView* omnibox_view = NULL;
107 ASSERT_NO_FATAL_FAILURE(GetOmniboxViewForBrowser(browser(), &omnibox_view)); 143 ASSERT_NO_FATAL_FAILURE(GetOmniboxViewForBrowser(browser(), &omnibox_view));
108 omnibox_view->SetUserText(ASCIIToUTF16("http://www.google.com/")); 144 omnibox_view->SetUserText(ASCIIToUTF16("http://www.google.com/"));
109 const gfx::Vector2d click(40, 10);
110 145
111 // Take the focus away from the omnibox. 146 // Take the focus away from the omnibox.
112 ASSERT_NO_FATAL_FAILURE(ClickBrowserWindowCenter()); 147 ASSERT_NO_FATAL_FAILURE(ClickBrowserWindowCenter());
113 EXPECT_FALSE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX)); 148 EXPECT_FALSE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
114 EXPECT_FALSE(omnibox_view->IsSelectAll()); 149 EXPECT_FALSE(omnibox_view->IsSelectAll());
115 150
116 // Clicking in the omnibox should take focus and select all text. 151 // Clicking in the omnibox should take focus and select all text.
117 ASSERT_NO_FATAL_FAILURE(ClickOmnibox(ui_controls::LEFT, click, click)); 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));
118 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX)); 157 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
119 EXPECT_TRUE(omnibox_view->IsSelectAll()); 158 EXPECT_TRUE(omnibox_view->IsSelectAll());
120 159
121 // Clicking in another view should clear focus and the selection. 160 // Clicking in another view should clear focus and the selection.
122 ASSERT_NO_FATAL_FAILURE(ClickBrowserWindowCenter()); 161 ASSERT_NO_FATAL_FAILURE(ClickBrowserWindowCenter());
123 EXPECT_FALSE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX)); 162 EXPECT_FALSE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
124 EXPECT_FALSE(omnibox_view->IsSelectAll()); 163 EXPECT_FALSE(omnibox_view->IsSelectAll());
125 164
126 // Clicking in the omnibox again should take focus and select all text again. 165 // Clicking in the omnibox again should take focus and select all text again.
127 ASSERT_NO_FATAL_FAILURE(ClickOmnibox(ui_controls::LEFT, click, click)); 166 ASSERT_NO_FATAL_FAILURE(Click(ui_controls::LEFT,
167 click_location, click_location));
128 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX)); 168 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
129 EXPECT_TRUE(omnibox_view->IsSelectAll()); 169 EXPECT_TRUE(omnibox_view->IsSelectAll());
130 170
131 // Clicking another omnibox spot should keep focus but clear the selection. 171 // Clicking another omnibox spot should keep focus but clear the selection.
132 omnibox_view->SelectAll(false); 172 omnibox_view->SelectAll(false);
133 const gfx::Vector2d click_2(click.x() + 10, click.y()); 173 const gfx::Point click2_location = omnibox_bounds.origin() +
134 ASSERT_NO_FATAL_FAILURE(ClickOmnibox(ui_controls::LEFT, click_2, click_2)); 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));
135 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX)); 177 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
136 EXPECT_FALSE(omnibox_view->IsSelectAll()); 178 EXPECT_FALSE(omnibox_view->IsSelectAll());
137 179
138 // Take the focus away and click in the omnibox again, but drag a bit before 180 // Take the focus away and click in the omnibox again, but drag a bit before
139 // releasing. We should focus the omnibox but not select all of its text. 181 // releasing. We should focus the omnibox but not select all of its text.
140 ASSERT_NO_FATAL_FAILURE(ClickBrowserWindowCenter()); 182 ASSERT_NO_FATAL_FAILURE(ClickBrowserWindowCenter());
141 const gfx::Vector2d release(click.x() + 10, click.y()); 183 ASSERT_NO_FATAL_FAILURE(Click(ui_controls::LEFT,
142 ASSERT_NO_FATAL_FAILURE(ClickOmnibox(ui_controls::LEFT, click, release)); 184 click_location, click2_location));
143 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX)); 185 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
144 EXPECT_FALSE(omnibox_view->IsSelectAll()); 186 EXPECT_FALSE(omnibox_view->IsSelectAll());
145 187
146 // Middle-clicking should not be handled by the omnibox. 188 // Middle-clicking should not be handled by the omnibox.
147 ASSERT_NO_FATAL_FAILURE(ClickBrowserWindowCenter()); 189 ASSERT_NO_FATAL_FAILURE(ClickBrowserWindowCenter());
148 ASSERT_NO_FATAL_FAILURE(ClickOmnibox(ui_controls::MIDDLE, click, click)); 190 ASSERT_NO_FATAL_FAILURE(Click(ui_controls::MIDDLE,
191 click_location, click_location));
149 EXPECT_FALSE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX)); 192 EXPECT_FALSE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
150 EXPECT_FALSE(omnibox_view->IsSelectAll()); 193 EXPECT_FALSE(omnibox_view->IsSelectAll());
151 } 194 }
195
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/"));
201
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());
206
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());
214
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());
219
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());
224
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.
233
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());
240 }
241 #endif // defined(USE_AURA)
OLDNEW
« no previous file with comments | « chrome/browser/ui/views/omnibox/omnibox_view_views.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698