OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "base/command_line.h" | 5 #include "base/command_line.h" |
6 #include "base/strings/utf_string_conversions.h" | 6 #include "base/strings/utf_string_conversions.h" |
| 7 #include "grit/ui_resources.h" |
| 8 #include "ui/base/resource/resource_bundle.h" |
7 #include "ui/base/touch/touch_editing_controller.h" | 9 #include "ui/base/touch/touch_editing_controller.h" |
8 #include "ui/base/ui_base_switches.h" | 10 #include "ui/base/ui_base_switches.h" |
9 #include "ui/gfx/point.h" | 11 #include "ui/gfx/point.h" |
10 #include "ui/gfx/rect.h" | 12 #include "ui/gfx/rect.h" |
11 #include "ui/gfx/render_text.h" | 13 #include "ui/gfx/render_text.h" |
12 #include "ui/views/controls/textfield/native_textfield_views.h" | 14 #include "ui/views/controls/textfield/native_textfield_views.h" |
13 #include "ui/views/controls/textfield/textfield.h" | 15 #include "ui/views/controls/textfield/textfield.h" |
14 #include "ui/views/test/views_test_base.h" | 16 #include "ui/views/test/views_test_base.h" |
15 #include "ui/views/touchui/touch_selection_controller_impl.h" | 17 #include "ui/views/touchui/touch_selection_controller_impl.h" |
16 #include "ui/views/widget/widget.h" | 18 #include "ui/views/widget/widget.h" |
17 | 19 |
18 #if defined(USE_AURA) | 20 #if defined(USE_AURA) |
19 #include "ui/aura/test/event_generator.h" | 21 #include "ui/aura/test/event_generator.h" |
20 #include "ui/aura/window.h" | 22 #include "ui/aura/window.h" |
21 #endif | 23 #endif |
22 | 24 |
| 25 namespace { |
| 26 // Should match kSelectionHandlePadding in touch_selection_controller. |
| 27 const int kPadding = 10; |
| 28 |
| 29 gfx::Image* GetHandleImage() { |
| 30 static gfx::Image* handle_image = NULL; |
| 31 if (!handle_image) { |
| 32 handle_image = &ui::ResourceBundle::GetSharedInstance().GetImageNamed( |
| 33 IDR_TEXT_SELECTION_HANDLE); |
| 34 } |
| 35 return handle_image; |
| 36 } |
| 37 |
| 38 gfx::Size GetHandleImageSize() { |
| 39 return GetHandleImage()->Size(); |
| 40 } |
| 41 } // namespace |
| 42 |
23 namespace views { | 43 namespace views { |
24 | 44 |
25 class TouchSelectionControllerImplTest : public ViewsTestBase { | 45 class TouchSelectionControllerImplTest : public ViewsTestBase { |
26 public: | 46 public: |
27 TouchSelectionControllerImplTest() | 47 TouchSelectionControllerImplTest() |
28 : widget_(NULL), | 48 : widget_(NULL), |
29 textfield_(NULL), | 49 textfield_(NULL), |
30 textfield_view_(NULL), | 50 textfield_view_(NULL), |
31 views_tsc_factory_(new ViewsTouchSelectionControllerFactory) { | 51 views_tsc_factory_(new ViewsTouchSelectionControllerFactory) { |
32 CommandLine::ForCurrentProcess()->AppendSwitch( | 52 CommandLine::ForCurrentProcess()->AppendSwitch( |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
80 void SimulateSelectionHandleDrag(gfx::Point p, int selection_handle) { | 100 void SimulateSelectionHandleDrag(gfx::Point p, int selection_handle) { |
81 TouchSelectionControllerImpl* controller = GetSelectionController(); | 101 TouchSelectionControllerImpl* controller = GetSelectionController(); |
82 // Do the work of OnMousePressed(). | 102 // Do the work of OnMousePressed(). |
83 if (selection_handle == 1) | 103 if (selection_handle == 1) |
84 controller->SetDraggingHandle(controller->selection_handle_1_.get()); | 104 controller->SetDraggingHandle(controller->selection_handle_1_.get()); |
85 else | 105 else |
86 controller->SetDraggingHandle(controller->selection_handle_2_.get()); | 106 controller->SetDraggingHandle(controller->selection_handle_2_.get()); |
87 | 107 |
88 // Offset the drag position by the selection handle radius since it is | 108 // Offset the drag position by the selection handle radius since it is |
89 // supposed to be in the coordinate system of the handle. | 109 // supposed to be in the coordinate system of the handle. |
90 p.Offset(10, 0); | 110 p.Offset(GetHandleImageSize().width() / 2 + kPadding, 0); |
91 controller->SelectionHandleDragged(p); | 111 controller->SelectionHandleDragged(p); |
92 | 112 |
93 // Do the work of OnMouseReleased(). | 113 // Do the work of OnMouseReleased(). |
94 controller->dragging_handle_ = NULL; | 114 controller->dragging_handle_ = NULL; |
95 } | 115 } |
96 | 116 |
97 gfx::Point GetSelectionHandle1Position() { | 117 gfx::Point GetSelectionHandle1Position() { |
98 return GetSelectionController()->GetSelectionHandle1Position(); | 118 return GetSelectionController()->GetSelectionHandle1Position(); |
99 } | 119 } |
100 | 120 |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
142 if (textfield_->HasSelection()) { \ | 162 if (textfield_->HasSelection()) { \ |
143 EXPECT_TRUE(IsSelectionHandle1Visible()); \ | 163 EXPECT_TRUE(IsSelectionHandle1Visible()); \ |
144 EXPECT_TRUE(IsSelectionHandle2Visible()); \ | 164 EXPECT_TRUE(IsSelectionHandle2Visible()); \ |
145 EXPECT_FALSE(IsCursorHandleVisible()); \ | 165 EXPECT_FALSE(IsCursorHandleVisible()); \ |
146 gfx::SelectionModel sel_start = GetRenderText()-> \ | 166 gfx::SelectionModel sel_start = GetRenderText()-> \ |
147 GetSelectionModelForSelectionStart(); \ | 167 GetSelectionModelForSelectionStart(); \ |
148 gfx::Point selection_start = GetCursorPosition(sel_start); \ | 168 gfx::Point selection_start = GetCursorPosition(sel_start); \ |
149 gfx::Point selection_end = GetCursorPosition(sel); \ | 169 gfx::Point selection_end = GetCursorPosition(sel); \ |
150 gfx::Point sh1 = GetSelectionHandle1Position(); \ | 170 gfx::Point sh1 = GetSelectionHandle1Position(); \ |
151 gfx::Point sh2 = GetSelectionHandle2Position(); \ | 171 gfx::Point sh2 = GetSelectionHandle2Position(); \ |
152 sh1.Offset(10, 0); \ | 172 sh1.Offset(GetHandleImageSize().width() / 2 + kPadding, 0); \ |
153 sh2.Offset(10, 0); \ | 173 sh2.Offset(GetHandleImageSize().width() / 2 + kPadding, 0); \ |
154 if (cursor_at_selection_handle_1) { \ | 174 if (cursor_at_selection_handle_1) { \ |
155 EXPECT_EQ(sh1, selection_end); \ | 175 EXPECT_EQ(sh1, selection_end); \ |
156 EXPECT_EQ(sh2, selection_start); \ | 176 EXPECT_EQ(sh2, selection_start); \ |
157 } else { \ | 177 } else { \ |
158 EXPECT_EQ(sh1, selection_start); \ | 178 EXPECT_EQ(sh1, selection_start); \ |
159 EXPECT_EQ(sh2, selection_end); \ | 179 EXPECT_EQ(sh2, selection_end); \ |
160 } \ | 180 } \ |
161 } else { \ | 181 } else { \ |
162 EXPECT_FALSE(IsSelectionHandle1Visible()); \ | 182 EXPECT_FALSE(IsSelectionHandle1Visible()); \ |
163 EXPECT_FALSE(IsSelectionHandle2Visible()); \ | 183 EXPECT_FALSE(IsSelectionHandle2Visible()); \ |
164 EXPECT_TRUE(IsCursorHandleVisible()); \ | 184 EXPECT_TRUE(IsCursorHandleVisible()); \ |
165 gfx::Point cursor_pos = GetCursorPosition(sel); \ | 185 gfx::Point cursor_pos = GetCursorPosition(sel); \ |
166 gfx::Point ch_pos = GetCursorHandlePosition(); \ | 186 gfx::Point ch_pos = GetCursorHandlePosition(); \ |
167 ch_pos.Offset(10, 0); \ | 187 ch_pos.Offset(GetHandleImageSize().width() / 2 + kPadding, 0); \ |
168 EXPECT_EQ(ch_pos, cursor_pos); \ | 188 EXPECT_EQ(ch_pos, cursor_pos); \ |
169 } \ | 189 } \ |
170 } | 190 } |
171 | 191 |
172 // Tests that the selection handles are placed appropriately when selection in | 192 // Tests that the selection handles are placed appropriately when selection in |
173 // a Textfield changes. | 193 // a Textfield changes. |
174 TEST_F(TouchSelectionControllerImplTest, SelectionInTextfieldTest) { | 194 TEST_F(TouchSelectionControllerImplTest, SelectionInTextfieldTest) { |
175 CreateTextfield(); | 195 CreateTextfield(); |
176 textfield_->SetText(ASCIIToUTF16("some text")); | 196 textfield_->SetText(ASCIIToUTF16("some text")); |
177 // Tap the textfield to invoke touch selection. | 197 // Tap the textfield to invoke touch selection. |
(...skipping 244 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
422 generator.GestureTapAt(gfx::Point(10, 10)); | 442 generator.GestureTapAt(gfx::Point(10, 10)); |
423 | 443 |
424 // Cursor handle should be visible. | 444 // Cursor handle should be visible. |
425 EXPECT_FALSE(textfield_->HasSelection()); | 445 EXPECT_FALSE(textfield_->HasSelection()); |
426 VERIFY_HANDLE_POSITIONS(false); | 446 VERIFY_HANDLE_POSITIONS(false); |
427 | 447 |
428 // Double tap on the cursor handle position. We want to check that the cursor | 448 // Double tap on the cursor handle position. We want to check that the cursor |
429 // handle is not eating the event and that the event is falling through to the | 449 // handle is not eating the event and that the event is falling through to the |
430 // textfield. | 450 // textfield. |
431 gfx::Point cursor_pos = GetCursorHandlePosition(); | 451 gfx::Point cursor_pos = GetCursorHandlePosition(); |
| 452 cursor_pos.Offset(GetHandleImageSize().width() / 2 + kPadding, 0); |
432 generator.GestureTapAt(cursor_pos); | 453 generator.GestureTapAt(cursor_pos); |
433 generator.GestureTapAt(cursor_pos); | 454 generator.GestureTapAt(cursor_pos); |
434 EXPECT_TRUE(textfield_->HasSelection()); | 455 EXPECT_TRUE(textfield_->HasSelection()); |
435 VERIFY_HANDLE_POSITIONS(false); | 456 VERIFY_HANDLE_POSITIONS(false); |
436 } | 457 } |
437 #endif | 458 #endif |
438 | 459 |
439 } // namespace views | 460 } // namespace views |
OLD | NEW |