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