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

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

Issue 927743002: Focus and trail cursor on Views Textfield selection clipboard paste. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Use SetClipboardText on other platforms too. Created 5 years, 10 months 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 | « no previous file | ui/views/controls/textfield/textfield.cc » ('j') | 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 "base/command_line.h" 7 #include "base/command_line.h"
8 #include "chrome/browser/search_engines/template_url_service_factory.h" 8 #include "chrome/browser/search_engines/template_url_service_factory.h"
9 #include "chrome/browser/ui/browser.h" 9 #include "chrome/browser/ui/browser.h"
10 #include "chrome/browser/ui/browser_commands.h" 10 #include "chrome/browser/ui/browser_commands.h"
(...skipping 10 matching lines...) Expand all
21 #include "ui/base/clipboard/clipboard.h" 21 #include "ui/base/clipboard/clipboard.h"
22 #include "ui/base/clipboard/scoped_clipboard_writer.h" 22 #include "ui/base/clipboard/scoped_clipboard_writer.h"
23 #include "ui/base/ime/text_input_focus_manager.h" 23 #include "ui/base/ime/text_input_focus_manager.h"
24 #include "ui/base/test/ui_controls.h" 24 #include "ui/base/test/ui_controls.h"
25 #include "ui/base/ui_base_switches.h" 25 #include "ui/base/ui_base_switches.h"
26 #include "ui/events/event_processor.h" 26 #include "ui/events/event_processor.h"
27 #include "ui/events/event_utils.h" 27 #include "ui/events/event_utils.h"
28 #include "ui/events/test/event_generator.h" 28 #include "ui/events/test/event_generator.h"
29 #include "ui/views/controls/textfield/textfield_test_api.h" 29 #include "ui/views/controls/textfield/textfield_test_api.h"
30 30
31 namespace {
32
33 void SetClipboardText(ui::ClipboardType type, const std::string& text) {
34 ui::ScopedClipboardWriter(type).WriteText(base::ASCIIToUTF16(text));
35 }
36
37 } // namespace
38
31 class OmniboxViewViewsTest : public InProcessBrowserTest { 39 class OmniboxViewViewsTest : public InProcessBrowserTest {
32 protected: 40 protected:
33 OmniboxViewViewsTest() {} 41 OmniboxViewViewsTest() {}
34 42
35 static void GetOmniboxViewForBrowser(const Browser* browser, 43 static void GetOmniboxViewForBrowser(const Browser* browser,
36 OmniboxView** omnibox_view) { 44 OmniboxView** omnibox_view) {
37 BrowserWindow* window = browser->window(); 45 BrowserWindow* window = browser->window();
38 ASSERT_TRUE(window); 46 ASSERT_TRUE(window);
39 LocationBar* location_bar = window->GetLocationBar(); 47 LocationBar* location_bar = window->GetLocationBar();
40 ASSERT_TRUE(location_bar); 48 ASSERT_TRUE(location_bar);
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
99 107
100 DISALLOW_COPY_AND_ASSIGN(OmniboxViewViewsTest); 108 DISALLOW_COPY_AND_ASSIGN(OmniboxViewViewsTest);
101 }; 109 };
102 110
103 IN_PROC_BROWSER_TEST_F(OmniboxViewViewsTest, PasteAndGoDoesNotLeavePopupOpen) { 111 IN_PROC_BROWSER_TEST_F(OmniboxViewViewsTest, PasteAndGoDoesNotLeavePopupOpen) {
104 OmniboxView* view = NULL; 112 OmniboxView* view = NULL;
105 ASSERT_NO_FATAL_FAILURE(GetOmniboxViewForBrowser(browser(), &view)); 113 ASSERT_NO_FATAL_FAILURE(GetOmniboxViewForBrowser(browser(), &view));
106 OmniboxViewViews* omnibox_view_views = static_cast<OmniboxViewViews*>(view); 114 OmniboxViewViews* omnibox_view_views = static_cast<OmniboxViewViews*>(view);
107 115
108 // Put an URL on the clipboard. 116 // Put an URL on the clipboard.
109 { 117 SetClipboardText(ui::CLIPBOARD_TYPE_COPY_PASTE, "http://www.example.com/");
110 ui::ScopedClipboardWriter clipboard_writer(ui::CLIPBOARD_TYPE_COPY_PASTE);
111 clipboard_writer.WriteURL(base::ASCIIToUTF16("http://www.example.com/"));
112 }
113 118
114 // Paste and go. 119 // Paste and go.
115 omnibox_view_views->ExecuteCommand(IDS_PASTE_AND_GO, ui::EF_NONE); 120 omnibox_view_views->ExecuteCommand(IDS_PASTE_AND_GO, ui::EF_NONE);
116 121
117 // The popup should not be open. 122 // The popup should not be open.
118 EXPECT_FALSE(view->model()->popup_model()->IsOpen()); 123 EXPECT_FALSE(view->model()->popup_model()->IsOpen());
119 } 124 }
120 125
121 IN_PROC_BROWSER_TEST_F(OmniboxViewViewsTest, SelectAllOnClick) { 126 IN_PROC_BROWSER_TEST_F(OmniboxViewViewsTest, SelectAllOnClick) {
122 OmniboxView* omnibox_view = NULL; 127 OmniboxView* omnibox_view = NULL;
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
158 EXPECT_FALSE(omnibox_view->IsSelectAll()); 163 EXPECT_FALSE(omnibox_view->IsSelectAll());
159 164
160 // Take the focus away and click in the omnibox again, but drag a bit before 165 // Take the focus away and click in the omnibox again, but drag a bit before
161 // releasing. We should focus the omnibox but not select all of its text. 166 // releasing. We should focus the omnibox but not select all of its text.
162 ASSERT_NO_FATAL_FAILURE(ClickBrowserWindowCenter()); 167 ASSERT_NO_FATAL_FAILURE(ClickBrowserWindowCenter());
163 ASSERT_NO_FATAL_FAILURE(Click(ui_controls::LEFT, 168 ASSERT_NO_FATAL_FAILURE(Click(ui_controls::LEFT,
164 click_location, click2_location)); 169 click_location, click2_location));
165 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX)); 170 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
166 EXPECT_FALSE(omnibox_view->IsSelectAll()); 171 EXPECT_FALSE(omnibox_view->IsSelectAll());
167 172
168 // Middle-clicking should not be handled by the omnibox. 173 // Middle-click is only handled on Linux, by pasting the selection clipboard
174 // and moving the cursor after the pasted text instead of selecting-all.
169 ASSERT_NO_FATAL_FAILURE(ClickBrowserWindowCenter()); 175 ASSERT_NO_FATAL_FAILURE(ClickBrowserWindowCenter());
170 ASSERT_NO_FATAL_FAILURE(Click(ui_controls::MIDDLE, 176 ASSERT_NO_FATAL_FAILURE(Click(ui_controls::MIDDLE,
171 click_location, click_location)); 177 click_location, click_location));
178 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
179 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
180 EXPECT_FALSE(omnibox_view->IsSelectAll());
181 #else
172 EXPECT_FALSE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX)); 182 EXPECT_FALSE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
173 EXPECT_FALSE(omnibox_view->IsSelectAll()); 183 EXPECT_FALSE(omnibox_view->IsSelectAll());
184 #endif // OS_LINUX && !OS_CHROMEOS
174 } 185 }
175 186
187 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
188 IN_PROC_BROWSER_TEST_F(OmniboxViewViewsTest, SelectionClipboard) {
189 OmniboxView* omnibox_view = NULL;
190 ASSERT_NO_FATAL_FAILURE(GetOmniboxViewForBrowser(browser(), &omnibox_view));
191 omnibox_view->SetUserText(base::ASCIIToUTF16("http://www.google.com/"));
192 OmniboxViewViews* omnibox_view_views =
193 static_cast<OmniboxViewViews*>(omnibox_view);
194 ASSERT_TRUE(omnibox_view_views);
195 gfx::RenderText* render_text = omnibox_view_views->GetRenderText();
196
197 // Take the focus away from the omnibox.
198 ASSERT_NO_FATAL_FAILURE(TapBrowserWindowCenter());
199 EXPECT_FALSE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
200 EXPECT_FALSE(omnibox_view->IsSelectAll());
201
202 size_t cursor_position = 14;
203 int cursor_x = render_text->GetCursorBounds(
204 gfx::SelectionModel(cursor_position, gfx::CURSOR_FORWARD), false).x();
205 gfx::Point click_location = omnibox_view_views->GetBoundsInScreen().origin();
206 click_location.Offset(cursor_x + render_text->display_rect().x(),
207 omnibox_view_views->height() / 2);
208
209 // Middle click focuses the omnibox, pastes, and sets a trailing cursor.
210 // Select-all on focus shouldn't alter the selection clipboard or cursor.
211 SetClipboardText(ui::CLIPBOARD_TYPE_SELECTION, "123");
212 ASSERT_NO_FATAL_FAILURE(Click(ui_controls::MIDDLE,
213 click_location, click_location));
214 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
215 EXPECT_FALSE(omnibox_view->IsSelectAll());
216 EXPECT_EQ(base::ASCIIToUTF16("http://www.goo123gle.com/"),
217 omnibox_view->GetText());
218 EXPECT_EQ(17U, omnibox_view_views->GetCursorPosition());
219
220 // Middle clicking again, with focus, pastes and updates the cursor.
221 SetClipboardText(ui::CLIPBOARD_TYPE_SELECTION, "4567");
222 ASSERT_NO_FATAL_FAILURE(Click(ui_controls::MIDDLE,
223 click_location, click_location));
224 EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
225 EXPECT_FALSE(omnibox_view->IsSelectAll());
226 EXPECT_EQ(base::ASCIIToUTF16("http://www.goo4567123gle.com/"),
227 omnibox_view->GetText());
228 EXPECT_EQ(18U, omnibox_view_views->GetCursorPosition());
229 }
230 #endif // OS_LINUX && !OS_CHROMEOS
231
176 IN_PROC_BROWSER_TEST_F(OmniboxViewViewsTest, SelectAllOnTap) { 232 IN_PROC_BROWSER_TEST_F(OmniboxViewViewsTest, SelectAllOnTap) {
177 OmniboxView* omnibox_view = NULL; 233 OmniboxView* omnibox_view = NULL;
178 ASSERT_NO_FATAL_FAILURE(GetOmniboxViewForBrowser(browser(), &omnibox_view)); 234 ASSERT_NO_FATAL_FAILURE(GetOmniboxViewForBrowser(browser(), &omnibox_view));
179 omnibox_view->SetUserText(base::ASCIIToUTF16("http://www.google.com/")); 235 omnibox_view->SetUserText(base::ASCIIToUTF16("http://www.google.com/"));
180 236
181 // Take the focus away from the omnibox. 237 // Take the focus away from the omnibox.
182 ASSERT_NO_FATAL_FAILURE(TapBrowserWindowCenter()); 238 ASSERT_NO_FATAL_FAILURE(TapBrowserWindowCenter());
183 EXPECT_FALSE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX)); 239 EXPECT_FALSE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
184 EXPECT_FALSE(omnibox_view->IsSelectAll()); 240 EXPECT_FALSE(omnibox_view->IsSelectAll());
185 241
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after
295 IN_PROC_BROWSER_TEST_F(OmniboxViewViewsTest, 351 IN_PROC_BROWSER_TEST_F(OmniboxViewViewsTest,
296 DeactivateTouchEditingOnExecuteCommand) { 352 DeactivateTouchEditingOnExecuteCommand) {
297 base::CommandLine::ForCurrentProcess()->AppendSwitch( 353 base::CommandLine::ForCurrentProcess()->AppendSwitch(
298 switches::kEnableTouchEditing); 354 switches::kEnableTouchEditing);
299 355
300 OmniboxView* view = NULL; 356 OmniboxView* view = NULL;
301 ASSERT_NO_FATAL_FAILURE(GetOmniboxViewForBrowser(browser(), &view)); 357 ASSERT_NO_FATAL_FAILURE(GetOmniboxViewForBrowser(browser(), &view));
302 OmniboxViewViews* omnibox_view_views = static_cast<OmniboxViewViews*>(view); 358 OmniboxViewViews* omnibox_view_views = static_cast<OmniboxViewViews*>(view);
303 views::TextfieldTestApi textfield_test_api(omnibox_view_views); 359 views::TextfieldTestApi textfield_test_api(omnibox_view_views);
304 360
305 // Put a URL on the clipboard. It is written to the clipboard upon destruction 361 // Put a URL on the clipboard.
306 // of the writer. 362 SetClipboardText(ui::CLIPBOARD_TYPE_COPY_PASTE, "http://www.example.com/");
307 {
308 ui::ScopedClipboardWriter clipboard_writer(
309 ui::CLIPBOARD_TYPE_COPY_PASTE);
310 clipboard_writer.WriteURL(base::ASCIIToUTF16("http://www.example.com/"));
311 }
312 363
313 // Tap to activate touch editing. 364 // Tap to activate touch editing.
314 gfx::Point omnibox_center = 365 gfx::Point omnibox_center =
315 omnibox_view_views->GetBoundsInScreen().CenterPoint(); 366 omnibox_view_views->GetBoundsInScreen().CenterPoint();
316 Tap(omnibox_center, omnibox_center); 367 Tap(omnibox_center, omnibox_center);
317 EXPECT_TRUE(textfield_test_api.touch_selection_controller()); 368 EXPECT_TRUE(textfield_test_api.touch_selection_controller());
318 369
319 // Execute a command and check if it deactivate touch editing. Paste & Go is 370 // Execute a command and check if it deactivate touch editing. Paste & Go is
320 // chosen since it is specific to Omnibox and its execution wouldn't be 371 // chosen since it is specific to Omnibox and its execution wouldn't be
321 // delgated to the base Textfield class. 372 // delgated to the base Textfield class.
(...skipping 10 matching lines...) Expand all
332 // TextInputFocusManager completes. 383 // TextInputFocusManager completes.
333 chrome::FocusLocationBar(browser()); 384 chrome::FocusLocationBar(browser());
334 OmniboxView* view = NULL; 385 OmniboxView* view = NULL;
335 ASSERT_NO_FATAL_FAILURE(GetOmniboxViewForBrowser(browser(), &view)); 386 ASSERT_NO_FATAL_FAILURE(GetOmniboxViewForBrowser(browser(), &view));
336 OmniboxViewViews* omnibox_view_views = static_cast<OmniboxViewViews*>(view); 387 OmniboxViewViews* omnibox_view_views = static_cast<OmniboxViewViews*>(view);
337 ui::TextInputFocusManager* text_input_focus_manager = 388 ui::TextInputFocusManager* text_input_focus_manager =
338 ui::TextInputFocusManager::GetInstance(); 389 ui::TextInputFocusManager::GetInstance();
339 EXPECT_EQ(omnibox_view_views->GetTextInputClient(), 390 EXPECT_EQ(omnibox_view_views->GetTextInputClient(),
340 text_input_focus_manager->GetFocusedTextInputClient()); 391 text_input_focus_manager->GetFocusedTextInputClient());
341 } 392 }
OLDNEW
« no previous file with comments | « no previous file | ui/views/controls/textfield/textfield.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698