OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "athena/common/drag_handle.h" |
| 6 |
| 7 #include "testing/gtest/include/gtest/gtest.h" |
| 8 #include "ui/aura/test/aura_test_base.h" |
| 9 #include "ui/aura/window.h" |
| 10 #include "ui/views/view.h" |
| 11 #include "ui/views/widget/widget.h" |
| 12 |
| 13 namespace athena { |
| 14 |
| 15 class DragHandleDelegateTest : public DragHandle::ScrollDelegate { |
| 16 public: |
| 17 explicit DragHandleDelegateTest() |
| 18 : can_scroll_(true), |
| 19 begin_delta_(0), |
| 20 got_scroll_end_(false), |
| 21 update_delta_(0) {} |
| 22 |
| 23 virtual ~DragHandleDelegateTest() {} |
| 24 |
| 25 void Reset() { |
| 26 can_scroll_ = true; |
| 27 begin_delta_ = 0; |
| 28 got_scroll_end_ = false; |
| 29 update_delta_ = 0; |
| 30 } |
| 31 |
| 32 float begin_delta() { return begin_delta_; } |
| 33 bool got_scroll_end() { return got_scroll_end_; } |
| 34 float update_delta() { return update_delta_; } |
| 35 |
| 36 void set_can_scroll(bool can_scroll) { can_scroll_ = can_scroll; } |
| 37 |
| 38 private: |
| 39 // DragHandle::ScrollDelegate: |
| 40 virtual void HandleScrollBegin(float delta) OVERRIDE { |
| 41 begin_delta_ = delta; |
| 42 } |
| 43 |
| 44 virtual void HandleScrollEnd() OVERRIDE { |
| 45 got_scroll_end_ = true; |
| 46 } |
| 47 |
| 48 virtual void HandleScrollUpdate(float delta) OVERRIDE { |
| 49 update_delta_ = delta; |
| 50 } |
| 51 |
| 52 virtual bool HandleCanScroll() OVERRIDE { |
| 53 return can_scroll_; |
| 54 } |
| 55 |
| 56 bool can_scroll_; |
| 57 float begin_delta_; |
| 58 bool got_scroll_end_; |
| 59 float update_delta_; |
| 60 |
| 61 DISALLOW_COPY_AND_ASSIGN(DragHandleDelegateTest); |
| 62 }; |
| 63 |
| 64 typedef aura::test::AuraTestBase DragHandleTest; |
| 65 |
| 66 const int kDragHandleWidth = 10; |
| 67 const int kDragHandleHeight = 100; |
| 68 const int kDragHandleMargin = 2; |
| 69 |
| 70 ui::GestureEvent CreateGestureEvent(ui::EventType type, |
| 71 float x, |
| 72 float delta_x) { |
| 73 ui::GestureEvent event( |
| 74 x, |
| 75 kDragHandleMargin + 1, |
| 76 0, |
| 77 base::TimeDelta::FromInternalValue(base::Time::Now().ToInternalValue()), |
| 78 ui::GestureEventDetails(type, delta_x, 0)); |
| 79 event.set_root_location(gfx::PointF(x, kDragHandleMargin + 1)); |
| 80 return event; |
| 81 } |
| 82 |
| 83 TEST_F(DragHandleTest, ScrollTest) { |
| 84 DragHandleDelegateTest delegate; |
| 85 scoped_ptr<views::View> drag_handle( |
| 86 CreateDragHandleView(DragHandle::ScrollDirection::HORIZONTAL, |
| 87 &delegate, |
| 88 kDragHandleWidth, |
| 89 kDragHandleHeight, |
| 90 kDragHandleMargin)); |
| 91 |
| 92 views::Widget widget; |
| 93 views::Widget::InitParams params( |
| 94 views::Widget::InitParams::TYPE_WINDOW_FRAMELESS); |
| 95 params.parent = root_window(); |
| 96 params.accept_events = true; |
| 97 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; |
| 98 widget.Init(params); |
| 99 widget.SetContentsView(drag_handle.get()); |
| 100 const gfx::Size& size = gfx::Size(kDragHandleWidth, kDragHandleHeight); |
| 101 widget.SetSize(size); |
| 102 widget.SetBounds(gfx::Rect(0, 0, size.width(), size.height())); |
| 103 |
| 104 const float begin_x = 4.0; |
| 105 const float begin_delta = 10.0; |
| 106 const float update_delta = 15.0; |
| 107 |
| 108 ui::GestureEvent scroll_begin_model = |
| 109 CreateGestureEvent(ui::ET_GESTURE_SCROLL_BEGIN, begin_x, begin_delta); |
| 110 ui::GestureEvent scroll_update_model = |
| 111 CreateGestureEvent(ui::ET_GESTURE_SCROLL_UPDATE, |
| 112 begin_x + update_delta, |
| 113 update_delta - begin_delta); |
| 114 ui::GestureEvent scroll_end_model = |
| 115 CreateGestureEvent(ui::ET_GESTURE_SCROLL_END, begin_x + update_delta, 0); |
| 116 ui::GestureEvent fling_start_model = |
| 117 CreateGestureEvent(ui::ET_SCROLL_FLING_START, begin_x + update_delta, 0); |
| 118 |
| 119 // Normal scroll |
| 120 ui::GestureEvent e(scroll_begin_model); |
| 121 widget.OnGestureEvent(&e); |
| 122 EXPECT_EQ(begin_delta, delegate.begin_delta()); |
| 123 EXPECT_EQ(0, delegate.update_delta()); |
| 124 EXPECT_FALSE(delegate.got_scroll_end()); |
| 125 e = ui::GestureEvent(scroll_update_model); |
| 126 widget.OnGestureEvent(&e); |
| 127 EXPECT_EQ(update_delta, delegate.update_delta()); |
| 128 EXPECT_FALSE(delegate.got_scroll_end()); |
| 129 e = ui::GestureEvent(scroll_end_model); |
| 130 widget.OnGestureEvent(&e); |
| 131 EXPECT_EQ(update_delta, delegate.update_delta()); |
| 132 EXPECT_TRUE(delegate.got_scroll_end()); |
| 133 |
| 134 delegate.Reset(); |
| 135 |
| 136 // Scroll ending with a fling |
| 137 e = ui::GestureEvent(scroll_begin_model); |
| 138 widget.OnGestureEvent(&e); |
| 139 e = ui::GestureEvent(scroll_update_model); |
| 140 widget.OnGestureEvent(&e); |
| 141 e = ui::GestureEvent(fling_start_model); |
| 142 widget.OnGestureEvent(&e); |
| 143 EXPECT_TRUE(delegate.got_scroll_end()); |
| 144 |
| 145 delegate.Reset(); |
| 146 |
| 147 // Changing CanScroll mid-scroll should't prevent events for the current |
| 148 // scroll from coming through. |
| 149 e = ui::GestureEvent(scroll_begin_model); |
| 150 widget.OnGestureEvent(&e); |
| 151 EXPECT_EQ(begin_delta, delegate.begin_delta()); |
| 152 delegate.set_can_scroll(false); |
| 153 e = ui::GestureEvent(scroll_update_model); |
| 154 widget.OnGestureEvent(&e); |
| 155 EXPECT_EQ(update_delta, delegate.update_delta()); |
| 156 e = ui::GestureEvent(scroll_end_model); |
| 157 widget.OnGestureEvent(&e); |
| 158 EXPECT_TRUE(delegate.got_scroll_end()); |
| 159 |
| 160 delegate.Reset(); |
| 161 |
| 162 // When CanScroll is false, new scroll events should be ignored |
| 163 delegate.set_can_scroll(false); |
| 164 e = ui::GestureEvent(scroll_begin_model); |
| 165 widget.OnGestureEvent(&e); |
| 166 EXPECT_EQ(0, delegate.begin_delta()); |
| 167 e = ui::GestureEvent(scroll_update_model); |
| 168 widget.OnGestureEvent(&e); |
| 169 EXPECT_EQ(0, delegate.update_delta()); |
| 170 e = ui::GestureEvent(scroll_end_model); |
| 171 widget.OnGestureEvent(&e); |
| 172 |
| 173 drag_handle.reset(); |
| 174 } |
| 175 |
| 176 } // namespace athena |
OLD | NEW |