Chromium Code Reviews| Index: ui/touch_selection/touch_handle_unittest.cc |
| diff --git a/ui/touch_selection/touch_handle_unittest.cc b/ui/touch_selection/touch_handle_unittest.cc |
| index e86c3d3881b245953f17aa8621166e934718b1dc..90337df6ba5b22d6d7616f8311a4c0bacf59ece5 100644 |
| --- a/ui/touch_selection/touch_handle_unittest.cc |
| +++ b/ui/touch_selection/touch_handle_unittest.cc |
| @@ -17,6 +17,7 @@ namespace { |
| const int kDefaultTapTimeoutMs = 200; |
| const float kDefaultTapSlop = 10.f; |
| const float kDefaultDrawableSize = 10.f; |
| +const gfx::RectF kDefaultViewportRect(0, 0, 560, 1200); |
| struct MockDrawableData { |
| MockDrawableData() |
| @@ -27,6 +28,8 @@ struct MockDrawableData { |
| rect(0, 0, kDefaultDrawableSize, kDefaultDrawableSize) {} |
| TouchHandleOrientation orientation; |
| float alpha; |
| + bool mirror_horizontal; |
| + bool mirror_vertical; |
| bool enabled; |
| bool visible; |
| gfx::RectF rect; |
| @@ -39,8 +42,14 @@ class MockTouchHandleDrawable : public TouchHandleDrawable { |
| void SetEnabled(bool enabled) override { data_->enabled = enabled; } |
| - void SetOrientation(TouchHandleOrientation orientation) override { |
| + void SetLayout(const gfx::PointF& position, |
| + TouchHandleOrientation orientation, |
| + bool mirror_vertical, |
| + bool mirror_horizontal) override { |
| + data_->rect.set_origin(position); |
| data_->orientation = orientation; |
| + data_->mirror_horizontal = mirror_horizontal; |
| + data_->mirror_vertical = mirror_vertical; |
| } |
| void SetAlpha(float alpha) override { |
| @@ -48,10 +57,7 @@ class MockTouchHandleDrawable : public TouchHandleDrawable { |
| data_->visible = alpha > 0; |
| } |
| - void SetFocus(const gfx::PointF& position) override { |
| - // Anchor focus to the top left of the rect (regardless of orientation). |
| - data_->rect.set_origin(position); |
| - } |
| + gfx::SizeF GetDrawablePadding() const override { return gfx::SizeF(); } |
|
mfomitchev
2015/06/18 16:16:31
Once we have the implementation nailed down, we sh
AviD
2015/06/19 07:20:47
Acknowledged.
|
| gfx::RectF GetVisibleBounds() const override { |
| return data_->rect; |
| @@ -144,7 +150,8 @@ class TouchHandleTest : public testing::Test, public TouchHandleClient { |
| }; |
| TEST_F(TouchHandleTest, Visibility) { |
| - TouchHandle handle(this, TouchHandleOrientation::CENTER); |
| + TouchHandle handle(this, TouchHandleOrientation::CENTER, |
| + kDefaultViewportRect); |
| EXPECT_FALSE(drawable().visible); |
| handle.SetVisible(true, TouchHandle::ANIMATION_NONE); |
| @@ -160,7 +167,8 @@ TEST_F(TouchHandleTest, Visibility) { |
| } |
| TEST_F(TouchHandleTest, VisibilityAnimation) { |
| - TouchHandle handle(this, TouchHandleOrientation::CENTER); |
| + TouchHandle handle(this, TouchHandleOrientation::CENTER, |
| + kDefaultViewportRect); |
| ASSERT_FALSE(NeedsAnimate()); |
| ASSERT_FALSE(drawable().visible); |
| ASSERT_EQ(0.f, drawable().alpha); |
| @@ -196,9 +204,11 @@ TEST_F(TouchHandleTest, VisibilityAnimation) { |
| } |
| TEST_F(TouchHandleTest, Orientation) { |
| - TouchHandle handle(this, TouchHandleOrientation::CENTER); |
| + TouchHandle handle(this, TouchHandleOrientation::CENTER, |
| + kDefaultViewportRect); |
| EXPECT_EQ(TouchHandleOrientation::CENTER, drawable().orientation); |
| + handle.SetVisible(true, TouchHandle::ANIMATION_NONE); |
| handle.SetOrientation(TouchHandleOrientation::LEFT); |
| EXPECT_EQ(TouchHandleOrientation::LEFT, drawable().orientation); |
| @@ -210,58 +220,66 @@ TEST_F(TouchHandleTest, Orientation) { |
| } |
| TEST_F(TouchHandleTest, Position) { |
| - TouchHandle handle(this, TouchHandleOrientation::CENTER); |
| + TouchHandle handle(this, TouchHandleOrientation::CENTER, |
| + kDefaultViewportRect); |
| handle.SetVisible(true, TouchHandle::ANIMATION_NONE); |
| - gfx::PointF position; |
| + gfx::PointF focus_top; |
| + gfx::PointF focus_bottom; |
| EXPECT_EQ(gfx::PointF(), drawable().rect.origin()); |
| - position = gfx::PointF(7.3f, -3.7f); |
| - handle.SetPosition(position); |
| - EXPECT_EQ(position, drawable().rect.origin()); |
| + focus_top = gfx::PointF(7.3f, -3.7f); |
| + focus_bottom = gfx::PointF(7.3f, -2.7f); |
| + handle.SetFocus(focus_top, focus_bottom); |
| + EXPECT_EQ(focus_bottom, drawable().rect.origin()); |
| - position = gfx::PointF(-7.3f, 3.7f); |
| - handle.SetPosition(position); |
| - EXPECT_EQ(position, drawable().rect.origin()); |
| + focus_top = gfx::PointF(-7.3f, 3.7f); |
| + focus_bottom = gfx::PointF(-7.3f, 4.7f); |
| + handle.SetFocus(focus_top, focus_bottom); |
| + EXPECT_EQ(focus_bottom, drawable().rect.origin()); |
| } |
| TEST_F(TouchHandleTest, PositionNotUpdatedWhileFadingOrInvisible) { |
| - TouchHandle handle(this, TouchHandleOrientation::CENTER); |
| + TouchHandle handle(this, TouchHandleOrientation::CENTER, |
| + kDefaultViewportRect); |
| handle.SetVisible(true, TouchHandle::ANIMATION_NONE); |
| ASSERT_TRUE(drawable().visible); |
| ASSERT_FALSE(NeedsAnimate()); |
| - gfx::PointF old_position(7.3f, -3.7f); |
| - handle.SetPosition(old_position); |
| - ASSERT_EQ(old_position, drawable().rect.origin()); |
| + gfx::PointF old_focus_top(7.3f, -3.7f); |
| + gfx::PointF old_focus_bottom(7.3f, -2.7f); |
| + handle.SetFocus(old_focus_top, old_focus_bottom); |
| + ASSERT_EQ(old_focus_bottom, drawable().rect.origin()); |
| handle.SetVisible(false, TouchHandle::ANIMATION_SMOOTH); |
| ASSERT_TRUE(NeedsAnimate()); |
| - gfx::PointF new_position(3.7f, -3.7f); |
| - handle.SetPosition(new_position); |
| - EXPECT_EQ(old_position, drawable().rect.origin()); |
| + gfx::PointF new_position_top(3.7f, -3.7f); |
| + gfx::PointF new_position_bottom(3.7f, -2.7f); |
| + handle.SetFocus(new_position_top, new_position_bottom); |
| + EXPECT_EQ(old_focus_bottom, drawable().rect.origin()); |
| EXPECT_TRUE(NeedsAnimate()); |
| // While the handle is fading, the new position should not take affect. |
| base::TimeTicks now = base::TimeTicks::Now(); |
| while (handle.Animate(now)) { |
| - EXPECT_EQ(old_position, drawable().rect.origin()); |
| + EXPECT_EQ(old_focus_bottom, drawable().rect.origin()); |
| now += base::TimeDelta::FromMilliseconds(16); |
| } |
| // Even after the animation terminates, the new position will not be pushed. |
| - EXPECT_EQ(old_position, drawable().rect.origin()); |
| + EXPECT_EQ(old_focus_bottom, drawable().rect.origin()); |
| // As soon as the handle becomes visible, the new position will be pushed. |
| handle.SetVisible(true, TouchHandle::ANIMATION_SMOOTH); |
| - EXPECT_EQ(new_position, drawable().rect.origin()); |
| + EXPECT_EQ(new_position_bottom, drawable().rect.origin()); |
| } |
| TEST_F(TouchHandleTest, Enabled) { |
| // A newly created handle defaults to enabled. |
| - TouchHandle handle(this, TouchHandleOrientation::CENTER); |
| + TouchHandle handle(this, TouchHandleOrientation::CENTER, |
| + kDefaultViewportRect); |
| EXPECT_TRUE(drawable().enabled); |
| handle.SetVisible(true, TouchHandle::ANIMATION_SMOOTH); |
| @@ -298,7 +316,8 @@ TEST_F(TouchHandleTest, Enabled) { |
| } |
| TEST_F(TouchHandleTest, Drag) { |
| - TouchHandle handle(this, TouchHandleOrientation::CENTER); |
| + TouchHandle handle(this, TouchHandleOrientation::CENTER, |
| + kDefaultViewportRect); |
| base::TimeTicks event_time = base::TimeTicks::Now(); |
| const float kOffset = kDefaultDrawableSize / 2.f; |
| @@ -352,7 +371,7 @@ TEST_F(TouchHandleTest, Drag) { |
| } |
| TEST_F(TouchHandleTest, DragDefersOrientationChange) { |
| - TouchHandle handle(this, TouchHandleOrientation::RIGHT); |
| + TouchHandle handle(this, TouchHandleOrientation::RIGHT, kDefaultViewportRect); |
| ASSERT_EQ(drawable().orientation, TouchHandleOrientation::RIGHT); |
| handle.SetVisible(true, TouchHandle::ANIMATION_NONE); |
| @@ -378,7 +397,8 @@ TEST_F(TouchHandleTest, DragDefersOrientationChange) { |
| } |
| TEST_F(TouchHandleTest, DragDefersFade) { |
| - TouchHandle handle(this, TouchHandleOrientation::CENTER); |
| + TouchHandle handle(this, TouchHandleOrientation::CENTER, |
| + kDefaultViewportRect); |
| handle.SetVisible(true, TouchHandle::ANIMATION_NONE); |
| MockMotionEvent event(MockMotionEvent::ACTION_DOWN); |
| @@ -407,7 +427,8 @@ TEST_F(TouchHandleTest, DragDefersFade) { |
| } |
| TEST_F(TouchHandleTest, DragTargettingUsesTouchSize) { |
| - TouchHandle handle(this, TouchHandleOrientation::CENTER); |
| + TouchHandle handle(this, TouchHandleOrientation::CENTER, |
| + kDefaultViewportRect); |
| handle.SetVisible(true, TouchHandle::ANIMATION_NONE); |
| base::TimeTicks event_time = base::TimeTicks::Now(); |
| @@ -458,7 +479,8 @@ TEST_F(TouchHandleTest, DragTargettingUsesTouchSize) { |
| } |
| TEST_F(TouchHandleTest, Tap) { |
| - TouchHandle handle(this, TouchHandleOrientation::CENTER); |
| + TouchHandle handle(this, TouchHandleOrientation::CENTER, |
| + kDefaultViewportRect); |
| handle.SetVisible(true, TouchHandle::ANIMATION_NONE); |
| base::TimeTicks event_time = base::TimeTicks::Now(); |
| @@ -504,4 +526,112 @@ TEST_F(TouchHandleTest, Tap) { |
| EXPECT_FALSE(GetAndResetHandleTapped()); |
| } |
| +TEST_F(TouchHandleTest, MirrorFocusChange) { |
| + TouchHandle handle(this, TouchHandleOrientation::LEFT, kDefaultViewportRect); |
| + handle.SetVisible(true, TouchHandle::ANIMATION_NONE); |
| + |
| + gfx::PointF focus_top; |
| + gfx::PointF focus_bottom; |
| + EXPECT_EQ(gfx::PointF(), drawable().rect.origin()); |
| + |
| + // Moving the selection to the bottom of the screen |
| + // should mirror the handle vertically. |
| + focus_top = gfx::PointF(17.3f, 1199.0f); |
| + focus_bottom = gfx::PointF(17.3f, 1200.0f); |
| + handle.SetFocus(focus_top, focus_bottom); |
| + EXPECT_TRUE(drawable().mirror_vertical); |
| + EXPECT_FALSE(drawable().mirror_horizontal); |
| + |
| + // Moving the left handle to the left edge of the viewport |
| + // should mirror the handle horizontally as well. |
| + focus_top = gfx::PointF(2.3f, 1199.0f); |
| + focus_bottom = gfx::PointF(2.3f, 1200.0f); |
| + handle.SetFocus(focus_top, focus_bottom); |
| + EXPECT_TRUE(drawable().mirror_vertical); |
| + EXPECT_TRUE(drawable().mirror_horizontal); |
| + |
| + // When the selection is not at the bottom, only the |
| + // horizontal mirror flag should be true. |
| + focus_top = gfx::PointF(2.3f, 7.3f); |
| + focus_bottom = gfx::PointF(2.3f, 8.3f); |
| + handle.SetFocus(focus_top, focus_bottom); |
| + EXPECT_FALSE(drawable().mirror_vertical); |
| + EXPECT_TRUE(drawable().mirror_horizontal); |
| + |
| + // When selection handles intersects the viewport fully, |
| + // both mirror values should be false. |
| + focus_top = gfx::PointF(23.3f, 7.3f); |
| + focus_bottom = gfx::PointF(23.3f, 8.3f); |
| + handle.SetFocus(focus_top, focus_bottom); |
| + EXPECT_FALSE(drawable().mirror_vertical); |
| + EXPECT_FALSE(drawable().mirror_horizontal); |
| + |
| + // Horizontal mirror should be true for Right handle when |
| + // the handle is at theright edge of the viewport. |
| + handle.SetOrientation(TouchHandleOrientation::RIGHT); |
| + focus_top = gfx::PointF(560.0f, 7.3f); |
| + focus_bottom = gfx::PointF(560.0f, 8.3f); |
| + handle.SetFocus(focus_top, focus_bottom); |
| + EXPECT_FALSE(drawable().mirror_vertical); |
| + EXPECT_TRUE(drawable().mirror_horizontal); |
| +} |
| + |
| +TEST_F(TouchHandleTest, DragDefersMirrorChange) { |
| + TouchHandle handle(this, TouchHandleOrientation::RIGHT, kDefaultViewportRect); |
| + ASSERT_EQ(drawable().orientation, TouchHandleOrientation::RIGHT); |
| + handle.SetVisible(true, TouchHandle::ANIMATION_NONE); |
| + |
| + base::TimeTicks event_time = base::TimeTicks::Now(); |
| + const float kOffset = kDefaultDrawableSize / 2.f; |
| + |
| + // Start the drag. |
| + MockMotionEvent event(MockMotionEvent::ACTION_DOWN, event_time, kOffset, |
| + kOffset); |
| + EXPECT_TRUE(handle.WillHandleTouchEvent(event)); |
| + EXPECT_TRUE(IsDragging()); |
| + |
| + handle.SetOrientation(TouchHandleOrientation::LEFT); |
| + gfx::PointF focus_top(17.3f, 1199.0f); |
| + gfx::PointF focus_bottom(17.3f, 1200.0f); |
| + handle.SetFocus(focus_top, focus_bottom); |
| + EXPECT_FALSE(drawable().mirror_vertical); |
| + EXPECT_FALSE(drawable().mirror_horizontal); |
| + |
| + // Mirror flag changes will be deferred until the drag ends. |
| + event = MockMotionEvent(MockMotionEvent::ACTION_UP); |
| + EXPECT_TRUE(handle.WillHandleTouchEvent(event)); |
| + EXPECT_FALSE(GetAndResetHandleDragged()); |
| + EXPECT_FALSE(IsDragging()); |
| + EXPECT_TRUE(drawable().mirror_vertical); |
| + EXPECT_FALSE(drawable().mirror_horizontal); |
| +} |
| + |
| +TEST_F(TouchHandleTest, ViewportSizeChange) { |
| + TouchHandle handle(this, TouchHandleOrientation::RIGHT, kDefaultViewportRect); |
| + ASSERT_EQ(drawable().orientation, TouchHandleOrientation::RIGHT); |
| + handle.SetVisible(true, TouchHandle::ANIMATION_NONE); |
| + |
| + gfx::PointF focus_top; |
| + gfx::PointF focus_bottom; |
| + EXPECT_EQ(gfx::PointF(), drawable().rect.origin()); |
| + |
| + focus_top = gfx::PointF(230.0f, 599.0f); |
| + focus_bottom = gfx::PointF(230.0f, 600.0f); |
| + handle.SetFocus(focus_top, focus_bottom); |
| + EXPECT_FALSE(drawable().mirror_vertical); |
| + EXPECT_FALSE(drawable().mirror_horizontal); |
| + |
| + handle.SetViewportRect(gfx::RectF(0, 0, 560, 600)); |
| + EXPECT_TRUE(drawable().mirror_vertical); |
| + EXPECT_FALSE(drawable().mirror_horizontal); |
| + |
| + handle.SetViewportRect(gfx::RectF(0, 0, 230, 600)); |
| + EXPECT_TRUE(drawable().mirror_vertical); |
| + EXPECT_TRUE(drawable().mirror_horizontal); |
| + |
| + handle.SetViewportRect(kDefaultViewportRect); |
| + EXPECT_FALSE(drawable().mirror_vertical); |
| + EXPECT_FALSE(drawable().mirror_horizontal); |
| +} |
| + |
| } // namespace ui |