| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "ui/touch_selection/touch_selection_controller.h" | 5 #include "ui/touch_selection/touch_selection_controller.h" |
| 6 | 6 |
| 7 #include "testing/gtest/include/gtest/gtest.h" | 7 #include "testing/gtest/include/gtest/gtest.h" |
| 8 #include "ui/events/test/motion_event_test_utils.h" | 8 #include "ui/events/test/motion_event_test_utils.h" |
| 9 | 9 |
| 10 using ui::test::MockMotionEvent; | 10 using ui::test::MockMotionEvent; |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 44 selection_moved_(false), | 44 selection_moved_(false), |
| 45 selection_points_swapped_(false), | 45 selection_points_swapped_(false), |
| 46 needs_animate_(false), | 46 needs_animate_(false), |
| 47 animation_enabled_(true), | 47 animation_enabled_(true), |
| 48 dragging_enabled_(false) {} | 48 dragging_enabled_(false) {} |
| 49 | 49 |
| 50 ~TouchSelectionControllerTest() override {} | 50 ~TouchSelectionControllerTest() override {} |
| 51 | 51 |
| 52 // testing::Test implementation. | 52 // testing::Test implementation. |
| 53 void SetUp() override { | 53 void SetUp() override { |
| 54 controller_.reset(new TouchSelectionController( | 54 Reset(false); |
| 55 this, | |
| 56 base::TimeDelta::FromMilliseconds(kDefaultTapTimeoutMs), | |
| 57 kDefaulTapSlop)); | |
| 58 } | 55 } |
| 59 | 56 |
| 60 void TearDown() override { controller_.reset(); } | 57 void TearDown() override { controller_.reset(); } |
| 61 | 58 |
| 62 // TouchSelectionControllerClient implementation. | 59 // TouchSelectionControllerClient implementation. |
| 63 | 60 |
| 64 bool SupportsAnimation() const override { return animation_enabled_; } | 61 bool SupportsAnimation() const override { return animation_enabled_; } |
| 65 | 62 |
| 66 void SetNeedsAnimate() override { needs_animate_ = true; } | 63 void SetNeedsAnimate() override { needs_animate_ = true; } |
| 67 | 64 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 88 const gfx::PointF& end_position) override { | 85 const gfx::PointF& end_position) override { |
| 89 last_event_ = event; | 86 last_event_ = event; |
| 90 last_event_start_ = end_position; | 87 last_event_start_ = end_position; |
| 91 } | 88 } |
| 92 | 89 |
| 93 scoped_ptr<TouchHandleDrawable> CreateDrawable() override { | 90 scoped_ptr<TouchHandleDrawable> CreateDrawable() override { |
| 94 return scoped_ptr<TouchHandleDrawable>( | 91 return scoped_ptr<TouchHandleDrawable>( |
| 95 new MockTouchHandleDrawable(&dragging_enabled_)); | 92 new MockTouchHandleDrawable(&dragging_enabled_)); |
| 96 } | 93 } |
| 97 | 94 |
| 95 void Reset(bool show_on_tap_for_empty_editable) { |
| 96 controller_.reset(new TouchSelectionController( |
| 97 this, |
| 98 base::TimeDelta::FromMilliseconds(kDefaultTapTimeoutMs), |
| 99 kDefaulTapSlop, |
| 100 show_on_tap_for_empty_editable)); |
| 101 last_event_start_ = gfx::PointF(); |
| 102 caret_position_ = gfx::PointF(); |
| 103 selection_start_ = gfx::PointF(); |
| 104 selection_end_ = gfx::PointF(); |
| 105 last_event_ = SELECTION_CLEARED; |
| 106 caret_moved_ = false; |
| 107 selection_moved_ = false; |
| 108 selection_points_swapped_ = false; |
| 109 needs_animate_ = false; |
| 110 animation_enabled_ = true; |
| 111 dragging_enabled_ = false; |
| 112 } |
| 113 |
| 98 void SetAnimationEnabled(bool enabled) { animation_enabled_ = enabled; } | 114 void SetAnimationEnabled(bool enabled) { animation_enabled_ = enabled; } |
| 99 void SetDraggingEnabled(bool enabled) { dragging_enabled_ = enabled; } | 115 void SetDraggingEnabled(bool enabled) { dragging_enabled_ = enabled; } |
| 100 | 116 |
| 101 void ClearSelection() { | 117 void ClearSelection() { |
| 102 controller_->OnSelectionBoundsChanged(SelectionBound(), | 118 controller_->OnSelectionBoundsUpdated(SelectionBound(), |
| 103 SelectionBound()); | 119 SelectionBound()); |
| 104 } | 120 } |
| 105 | 121 |
| 106 void ClearInsertion() { ClearSelection(); } | 122 void ClearInsertion() { ClearSelection(); } |
| 107 | 123 |
| 108 void ChangeInsertion(const gfx::RectF& rect, bool visible) { | 124 void ChangeInsertion(const gfx::RectF& rect, bool visible) { |
| 109 SelectionBound bound; | 125 SelectionBound bound; |
| 110 bound.set_type(SelectionBound::CENTER); | 126 bound.set_type(SelectionBound::CENTER); |
| 111 bound.SetEdge(rect.origin(), rect.bottom_left()); | 127 bound.SetEdge(rect.origin(), rect.bottom_left()); |
| 112 bound.set_visible(visible); | 128 bound.set_visible(visible); |
| 113 controller_->OnSelectionBoundsChanged(bound, bound); | 129 controller_->OnSelectionBoundsUpdated(bound, bound); |
| 114 } | 130 } |
| 115 | 131 |
| 116 void ChangeSelection(const gfx::RectF& start_rect, | 132 void ChangeSelection(const gfx::RectF& start_rect, |
| 117 bool start_visible, | 133 bool start_visible, |
| 118 const gfx::RectF& end_rect, | 134 const gfx::RectF& end_rect, |
| 119 bool end_visible) { | 135 bool end_visible) { |
| 120 SelectionBound start_bound, end_bound; | 136 SelectionBound start_bound, end_bound; |
| 121 start_bound.set_type(SelectionBound::LEFT); | 137 start_bound.set_type(SelectionBound::LEFT); |
| 122 end_bound.set_type(SelectionBound::RIGHT); | 138 end_bound.set_type(SelectionBound::RIGHT); |
| 123 start_bound.SetEdge(start_rect.origin(), start_rect.bottom_left()); | 139 start_bound.SetEdge(start_rect.origin(), start_rect.bottom_left()); |
| 124 end_bound.SetEdge(end_rect.origin(), end_rect.bottom_left()); | 140 end_bound.SetEdge(end_rect.origin(), end_rect.bottom_left()); |
| 125 start_bound.set_visible(start_visible); | 141 start_bound.set_visible(start_visible); |
| 126 end_bound.set_visible(end_visible); | 142 end_bound.set_visible(end_visible); |
| 127 controller_->OnSelectionBoundsChanged(start_bound, end_bound); | 143 controller_->OnSelectionBoundsUpdated(start_bound, end_bound); |
| 128 } | 144 } |
| 129 | 145 |
| 130 void Animate() { | 146 void Animate() { |
| 131 base::TimeTicks now = base::TimeTicks::Now(); | 147 base::TimeTicks now = base::TimeTicks::Now(); |
| 132 while (needs_animate_) { | 148 while (needs_animate_) { |
| 133 needs_animate_ = controller_->Animate(now); | 149 needs_animate_ = controller_->Animate(now); |
| 134 now += base::TimeDelta::FromMilliseconds(16); | 150 now += base::TimeDelta::FromMilliseconds(16); |
| 135 } | 151 } |
| 136 } | 152 } |
| 137 | 153 |
| (...skipping 18 matching lines...) Expand all Loading... |
| 156 bool GetAndResetSelectionPointsSwapped() { | 172 bool GetAndResetSelectionPointsSwapped() { |
| 157 bool swapped = selection_points_swapped_; | 173 bool swapped = selection_points_swapped_; |
| 158 selection_points_swapped_ = false; | 174 selection_points_swapped_ = false; |
| 159 return swapped; | 175 return swapped; |
| 160 } | 176 } |
| 161 | 177 |
| 162 const gfx::PointF& GetLastCaretPosition() const { return caret_position_; } | 178 const gfx::PointF& GetLastCaretPosition() const { return caret_position_; } |
| 163 const gfx::PointF& GetLastSelectionStart() const { return selection_start_; } | 179 const gfx::PointF& GetLastSelectionStart() const { return selection_start_; } |
| 164 const gfx::PointF& GetLastSelectionEnd() const { return selection_end_; } | 180 const gfx::PointF& GetLastSelectionEnd() const { return selection_end_; } |
| 165 SelectionEventType GetLastEventType() const { return last_event_; } | 181 SelectionEventType GetLastEventType() const { return last_event_; } |
| 166 const gfx::PointF& GetLastEventAnchor() const { return last_event_start_; } | 182 const gfx::PointF& GetLastEventAnchor() { return last_event_start_; } |
| 167 | 183 |
| 168 TouchSelectionController& controller() { return *controller_; } | 184 TouchSelectionController& controller() { return *controller_; } |
| 169 | 185 |
| 170 private: | 186 private: |
| 171 gfx::PointF last_event_start_; | 187 gfx::PointF last_event_start_; |
| 172 gfx::PointF caret_position_; | 188 gfx::PointF caret_position_; |
| 173 gfx::PointF selection_start_; | 189 gfx::PointF selection_start_; |
| 174 gfx::PointF selection_end_; | 190 gfx::PointF selection_end_; |
| 175 SelectionEventType last_event_; | 191 SelectionEventType last_event_; |
| 176 bool caret_moved_; | 192 bool caret_moved_; |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 222 controller().OnSelectionEditable(true); | 238 controller().OnSelectionEditable(true); |
| 223 | 239 |
| 224 ChangeInsertion(insertion_rect, visible); | 240 ChangeInsertion(insertion_rect, visible); |
| 225 EXPECT_EQ(INSERTION_SHOWN, GetLastEventType()); | 241 EXPECT_EQ(INSERTION_SHOWN, GetLastEventType()); |
| 226 EXPECT_EQ(insertion_rect.bottom_left(), GetLastEventAnchor()); | 242 EXPECT_EQ(insertion_rect.bottom_left(), GetLastEventAnchor()); |
| 227 | 243 |
| 228 controller().OnSelectionEditable(false); | 244 controller().OnSelectionEditable(false); |
| 229 EXPECT_EQ(INSERTION_CLEARED, GetLastEventType()); | 245 EXPECT_EQ(INSERTION_CLEARED, GetLastEventType()); |
| 230 } | 246 } |
| 231 | 247 |
| 232 TEST_F(TouchSelectionControllerTest, InsertionStaysHiddenIfEmptyRegionTapped) { | 248 TEST_F(TouchSelectionControllerTest, EmptyRegionTapped) { |
| 233 gfx::RectF insertion_rect(5, 5, 0, 10); | 249 gfx::RectF insertion_rect(5, 5, 0, 10); |
| 234 bool visible = true; | 250 bool visible = true; |
| 251 |
| 252 // Taps on an empty editable region should show the insertion handle if the |
| 253 // controller is created with show_on_tap_for_empty_editable set to true. |
| 254 Reset(true); |
| 235 controller().OnSelectionEditable(true); | 255 controller().OnSelectionEditable(true); |
| 256 controller().OnTapEvent(); |
| 257 controller().OnSelectionEmpty(true); |
| 258 ChangeInsertion(insertion_rect, visible); |
| 259 EXPECT_EQ(INSERTION_SHOWN, GetLastEventType()); |
| 260 EXPECT_EQ(insertion_rect.bottom_left(), GetLastEventAnchor()); |
| 236 | 261 |
| 237 // Taps should be ignored if they're in an empty editable region. | 262 // Taps on an empty editable region should be ignored if the controller is |
| 263 // created with show_on_tap_for_empty_editable set to false. |
| 264 Reset(false); |
| 265 controller().OnSelectionEditable(true); |
| 238 controller().OnTapEvent(); | 266 controller().OnTapEvent(); |
| 239 controller().OnSelectionEmpty(true); | 267 controller().OnSelectionEmpty(true); |
| 240 ChangeInsertion(insertion_rect, visible); | 268 ChangeInsertion(insertion_rect, visible); |
| 241 EXPECT_EQ(gfx::PointF(), GetLastEventAnchor()); | 269 EXPECT_EQ(gfx::PointF(), GetLastEventAnchor()); |
| 242 | 270 |
| 243 // Once the region becomes editable, taps should show the insertion handle. | 271 // Once the region becomes non-empty, taps should show the insertion handle. |
| 244 controller().OnTapEvent(); | 272 controller().OnTapEvent(); |
| 245 controller().OnSelectionEmpty(false); | 273 controller().OnSelectionEmpty(false); |
| 246 ChangeInsertion(insertion_rect, visible); | 274 ChangeInsertion(insertion_rect, visible); |
| 247 EXPECT_EQ(INSERTION_SHOWN, GetLastEventType()); | 275 EXPECT_EQ(INSERTION_SHOWN, GetLastEventType()); |
| 248 EXPECT_EQ(insertion_rect.bottom_left(), GetLastEventAnchor()); | 276 EXPECT_EQ(insertion_rect.bottom_left(), GetLastEventAnchor()); |
| 249 | 277 |
| 250 // Reset the selection. | 278 // Reset the selection. |
| 251 controller().HideAndDisallowShowingAutomatically(); | 279 controller().HideAndDisallowShowingAutomatically(); |
| 252 EXPECT_EQ(INSERTION_CLEARED, GetLastEventType()); | 280 EXPECT_EQ(INSERTION_CLEARED, GetLastEventType()); |
| 253 | 281 |
| (...skipping 580 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 834 controller().OnSelectionEmpty(false); | 862 controller().OnSelectionEmpty(false); |
| 835 controller().HideAndDisallowShowingAutomatically(); | 863 controller().HideAndDisallowShowingAutomatically(); |
| 836 gfx::RectF insertion_rect(5, 5, 0, 10); | 864 gfx::RectF insertion_rect(5, 5, 0, 10); |
| 837 ChangeInsertion(insertion_rect, visible); | 865 ChangeInsertion(insertion_rect, visible); |
| 838 controller().AllowShowingFromCurrentSelection(); | 866 controller().AllowShowingFromCurrentSelection(); |
| 839 EXPECT_EQ(INSERTION_SHOWN, GetLastEventType()); | 867 EXPECT_EQ(INSERTION_SHOWN, GetLastEventType()); |
| 840 EXPECT_EQ(insertion_rect.bottom_left(), GetLastEventAnchor()); | 868 EXPECT_EQ(insertion_rect.bottom_left(), GetLastEventAnchor()); |
| 841 } | 869 } |
| 842 | 870 |
| 843 } // namespace ui | 871 } // namespace ui |
| OLD | NEW |