OLD | NEW |
---|---|
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)); | |
Peter Kasting
2013/11/05 19:54:07
Nit: You interleaved the press/release calls in Ta
tdresser
2013/11/05 20:18:20
Done.
| |
79 ui::TouchEvent release(ui::ET_TOUCH_RELEASED, center, | |
80 5, base::TimeDelta::FromMilliseconds(50)); | |
81 | |
82 rwhd->OnHostTouchEvent(&press); | |
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 Loading... | |
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); | 145 |
146 const gfx::Rect omnibox_bounds = BrowserView::GetBrowserViewForBrowser( | |
147 browser())->GetViewByID(VIEW_ID_OMNIBOX)->GetBoundsInScreen(); | |
148 | |
149 const gfx::Point click_location = omnibox_bounds.CenterPoint(); | |
Peter Kasting
2013/11/05 19:54:07
Nit: Define this just above first use.
I'd probab
tdresser
2013/11/05 20:18:20
Done.
| |
110 | 150 |
111 // Take the focus away from the omnibox. | 151 // Take the focus away from the omnibox. |
112 ASSERT_NO_FATAL_FAILURE(ClickBrowserWindowCenter()); | 152 ASSERT_NO_FATAL_FAILURE(ClickBrowserWindowCenter()); |
113 EXPECT_FALSE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX)); | 153 EXPECT_FALSE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX)); |
114 EXPECT_FALSE(omnibox_view->IsSelectAll()); | 154 EXPECT_FALSE(omnibox_view->IsSelectAll()); |
115 | 155 |
116 // Clicking in the omnibox should take focus and select all text. | 156 // Clicking in the omnibox should take focus and select all text. |
117 ASSERT_NO_FATAL_FAILURE(ClickOmnibox(ui_controls::LEFT, click, click)); | 157 ASSERT_NO_FATAL_FAILURE(Click(ui_controls::LEFT, |
158 click_location, click_location)); | |
118 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX)); | 159 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX)); |
119 EXPECT_TRUE(omnibox_view->IsSelectAll()); | 160 EXPECT_TRUE(omnibox_view->IsSelectAll()); |
120 | 161 |
121 // Clicking in another view should clear focus and the selection. | 162 // Clicking in another view should clear focus and the selection. |
122 ASSERT_NO_FATAL_FAILURE(ClickBrowserWindowCenter()); | 163 ASSERT_NO_FATAL_FAILURE(ClickBrowserWindowCenter()); |
123 EXPECT_FALSE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX)); | 164 EXPECT_FALSE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX)); |
124 EXPECT_FALSE(omnibox_view->IsSelectAll()); | 165 EXPECT_FALSE(omnibox_view->IsSelectAll()); |
125 | 166 |
126 // Clicking in the omnibox again should take focus and select all text again. | 167 // Clicking in the omnibox again should take focus and select all text again. |
127 ASSERT_NO_FATAL_FAILURE(ClickOmnibox(ui_controls::LEFT, click, click)); | 168 ASSERT_NO_FATAL_FAILURE(Click(ui_controls::LEFT, |
169 click_location, click_location)); | |
128 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX)); | 170 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX)); |
129 EXPECT_TRUE(omnibox_view->IsSelectAll()); | 171 EXPECT_TRUE(omnibox_view->IsSelectAll()); |
130 | 172 |
131 // Clicking another omnibox spot should keep focus but clear the selection. | 173 // Clicking another omnibox spot should keep focus but clear the selection. |
132 omnibox_view->SelectAll(false); | 174 omnibox_view->SelectAll(false); |
133 const gfx::Vector2d click_2(click.x() + 10, click.y()); | 175 const gfx::Point click2_location = omnibox_bounds.origin() + |
134 ASSERT_NO_FATAL_FAILURE(ClickOmnibox(ui_controls::LEFT, click_2, click_2)); | 176 gfx::Vector2d(omnibox_bounds.width() / 4, omnibox_bounds.height() / 4); |
177 ASSERT_NO_FATAL_FAILURE(Click(ui_controls::LEFT, | |
178 click2_location, click2_location)); | |
135 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX)); | 179 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX)); |
136 EXPECT_FALSE(omnibox_view->IsSelectAll()); | 180 EXPECT_FALSE(omnibox_view->IsSelectAll()); |
137 | 181 |
138 // Take the focus away and click in the omnibox again, but drag a bit before | 182 // 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. | 183 // releasing. We should focus the omnibox but not select all of its text. |
140 ASSERT_NO_FATAL_FAILURE(ClickBrowserWindowCenter()); | 184 ASSERT_NO_FATAL_FAILURE(ClickBrowserWindowCenter()); |
141 const gfx::Vector2d release(click.x() + 10, click.y()); | 185 ASSERT_NO_FATAL_FAILURE(Click(ui_controls::LEFT, |
142 ASSERT_NO_FATAL_FAILURE(ClickOmnibox(ui_controls::LEFT, click, release)); | 186 click_location, click2_location)); |
143 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX)); | 187 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX)); |
144 EXPECT_FALSE(omnibox_view->IsSelectAll()); | 188 EXPECT_FALSE(omnibox_view->IsSelectAll()); |
145 | 189 |
146 // Middle-clicking should not be handled by the omnibox. | 190 // Middle-clicking should not be handled by the omnibox. |
147 ASSERT_NO_FATAL_FAILURE(ClickBrowserWindowCenter()); | 191 ASSERT_NO_FATAL_FAILURE(ClickBrowserWindowCenter()); |
148 ASSERT_NO_FATAL_FAILURE(ClickOmnibox(ui_controls::MIDDLE, click, click)); | 192 ASSERT_NO_FATAL_FAILURE(Click(ui_controls::MIDDLE, |
193 click_location, click_location)); | |
149 EXPECT_FALSE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX)); | 194 EXPECT_FALSE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX)); |
150 EXPECT_FALSE(omnibox_view->IsSelectAll()); | 195 EXPECT_FALSE(omnibox_view->IsSelectAll()); |
151 } | 196 } |
197 | |
198 #if defined(USE_AURA) | |
199 IN_PROC_BROWSER_TEST_F(OmniboxViewViewsTest, SelectAllOnTap) { | |
200 OmniboxView* omnibox_view = NULL; | |
201 ASSERT_NO_FATAL_FAILURE(GetOmniboxViewForBrowser(browser(), &omnibox_view)); | |
202 omnibox_view->SetUserText(ASCIIToUTF16("http://www.google.com/")); | |
203 | |
204 const gfx::Rect omnibox_bounds = BrowserView::GetBrowserViewForBrowser( | |
205 browser())->GetViewByID(VIEW_ID_OMNIBOX)->GetBoundsInScreen(); | |
206 | |
207 const gfx::Point tap_location = omnibox_bounds.CenterPoint(); | |
Peter Kasting
2013/11/05 19:54:07
Nit: Same comments as above.
tdresser
2013/11/05 20:18:20
Done.
| |
208 | |
209 // Take the focus away from the omnibox. | |
210 ASSERT_NO_FATAL_FAILURE(TapBrowserWindowCenter()); | |
211 EXPECT_FALSE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX)); | |
212 EXPECT_FALSE(omnibox_view->IsSelectAll()); | |
213 | |
214 // Tapping in the omnibox should take focus and select all text. | |
215 ASSERT_NO_FATAL_FAILURE(Tap(tap_location, tap_location)); | |
216 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX)); | |
217 EXPECT_TRUE(omnibox_view->IsSelectAll()); | |
218 | |
219 // Tapping in another view should clear focus and the selection. | |
220 ASSERT_NO_FATAL_FAILURE(TapBrowserWindowCenter()); | |
221 EXPECT_FALSE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX)); | |
222 EXPECT_FALSE(omnibox_view->IsSelectAll()); | |
223 | |
224 // Tapping in the omnibox again should take focus and select all text again. | |
225 ASSERT_NO_FATAL_FAILURE(Tap(tap_location, tap_location)); | |
226 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX)); | |
227 EXPECT_TRUE(omnibox_view->IsSelectAll()); | |
228 | |
229 // Tapping another omnibox spot should keep focus and selection. | |
230 omnibox_view->SelectAll(false); | |
231 | |
Peter Kasting
2013/11/05 19:54:07
Nit: This blank line and the one below aren't nece
tdresser
2013/11/05 20:18:20
Done.
| |
232 const gfx::Point tap2_location = omnibox_bounds.origin() + | |
233 gfx::Vector2d(omnibox_bounds.width() / 4, omnibox_bounds.height() / 4); | |
234 | |
235 ASSERT_NO_FATAL_FAILURE(Tap(tap2_location, tap2_location)); | |
236 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX)); | |
237 EXPECT_TRUE(omnibox_view->IsSelectAll()); | |
238 | |
239 // Take the focus away and tap in the omnibox again, but drag a bit before | |
240 // releasing. We should focus the omnibox but not select all of its text. | |
241 ASSERT_NO_FATAL_FAILURE(TapBrowserWindowCenter()); | |
242 ASSERT_NO_FATAL_FAILURE(Tap(tap_location, tap2_location)); | |
243 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX)); | |
244 EXPECT_FALSE(omnibox_view->IsSelectAll()); | |
245 } | |
246 #endif // defined(USE_AURA) | |
OLD | NEW |