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/util/drag_handle.h" |
| 6 |
| 7 #include "ui/aura/test/aura_test_base.h" |
| 8 #include "ui/aura/window.h" |
| 9 #include "ui/views/view.h" |
| 10 #include "ui/views/widget/widget.h" |
| 11 |
| 12 namespace athena { |
| 13 |
| 14 class DragHandleDelegateTest : public DragHandleScrollDelegate { |
| 15 public: |
| 16 explicit DragHandleDelegateTest() |
| 17 : begin_delta_(0), |
| 18 got_scroll_end_(false), |
| 19 update_delta_(0) {} |
| 20 |
| 21 virtual ~DragHandleDelegateTest() {} |
| 22 |
| 23 void Reset() { |
| 24 begin_delta_ = 0; |
| 25 got_scroll_end_ = false; |
| 26 update_delta_ = 0; |
| 27 } |
| 28 |
| 29 float begin_delta() { return begin_delta_; } |
| 30 bool got_scroll_end() { return got_scroll_end_; } |
| 31 float update_delta() { return update_delta_; } |
| 32 |
| 33 private: |
| 34 // DragHandleScrollDelegate: |
| 35 virtual void HandleScrollBegin(float delta) OVERRIDE { |
| 36 begin_delta_ = delta; |
| 37 } |
| 38 |
| 39 virtual void HandleScrollEnd() OVERRIDE { |
| 40 got_scroll_end_ = true; |
| 41 } |
| 42 |
| 43 virtual void HandleScrollUpdate(float delta) OVERRIDE { |
| 44 update_delta_ = delta; |
| 45 } |
| 46 |
| 47 float begin_delta_; |
| 48 bool got_scroll_end_; |
| 49 float update_delta_; |
| 50 |
| 51 DISALLOW_COPY_AND_ASSIGN(DragHandleDelegateTest); |
| 52 }; |
| 53 |
| 54 typedef aura::test::AuraTestBase DragHandleTest; |
| 55 |
| 56 const int kDragHandleWidth = 10; |
| 57 const int kDragHandleHeight = 100; |
| 58 |
| 59 ui::GestureEvent CreateGestureEvent(ui::EventType type, |
| 60 float x, |
| 61 float delta_x) { |
| 62 ui::GestureEvent event( |
| 63 x, |
| 64 1, |
| 65 0, |
| 66 base::TimeDelta::FromInternalValue(base::Time::Now().ToInternalValue()), |
| 67 type == ui::ET_GESTURE_SCROLL_END |
| 68 ? ui::GestureEventDetails(type) |
| 69 : ui::GestureEventDetails(type, delta_x, 0)); |
| 70 event.set_root_location(gfx::PointF(x, 1)); |
| 71 return event; |
| 72 } |
| 73 |
| 74 TEST_F(DragHandleTest, ScrollTest) { |
| 75 DragHandleDelegateTest delegate; |
| 76 scoped_ptr<views::View> drag_handle( |
| 77 CreateDragHandleView(DragHandleScrollDirection::DRAG_HANDLE_HORIZONTAL, |
| 78 &delegate, |
| 79 kDragHandleWidth, |
| 80 kDragHandleHeight)); |
| 81 |
| 82 views::Widget widget; |
| 83 views::Widget::InitParams params( |
| 84 views::Widget::InitParams::TYPE_WINDOW_FRAMELESS); |
| 85 params.parent = root_window(); |
| 86 params.accept_events = true; |
| 87 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; |
| 88 widget.Init(params); |
| 89 widget.SetContentsView(drag_handle.get()); |
| 90 const gfx::Size& size = gfx::Size(kDragHandleWidth, kDragHandleHeight); |
| 91 widget.SetSize(size); |
| 92 widget.SetBounds(gfx::Rect(0, 0, size.width(), size.height())); |
| 93 |
| 94 const float begin_x = 4.0; |
| 95 const float begin_delta = 10.0; |
| 96 const float update_delta = 15.0; |
| 97 |
| 98 ui::GestureEvent scroll_begin_model = |
| 99 CreateGestureEvent(ui::ET_GESTURE_SCROLL_BEGIN, begin_x, begin_delta); |
| 100 ui::GestureEvent scroll_update_model = |
| 101 CreateGestureEvent(ui::ET_GESTURE_SCROLL_UPDATE, |
| 102 begin_x + update_delta, |
| 103 update_delta - begin_delta); |
| 104 ui::GestureEvent scroll_end_model = |
| 105 CreateGestureEvent(ui::ET_GESTURE_SCROLL_END, begin_x + update_delta, 0); |
| 106 ui::GestureEvent fling_start_model = |
| 107 CreateGestureEvent(ui::ET_SCROLL_FLING_START, begin_x + update_delta, 0); |
| 108 |
| 109 // Normal scroll |
| 110 ui::GestureEvent e(scroll_begin_model); |
| 111 widget.OnGestureEvent(&e); |
| 112 EXPECT_EQ(begin_delta, delegate.begin_delta()); |
| 113 EXPECT_EQ(0, delegate.update_delta()); |
| 114 EXPECT_FALSE(delegate.got_scroll_end()); |
| 115 e = ui::GestureEvent(scroll_update_model); |
| 116 widget.OnGestureEvent(&e); |
| 117 EXPECT_EQ(update_delta, delegate.update_delta()); |
| 118 EXPECT_FALSE(delegate.got_scroll_end()); |
| 119 e = ui::GestureEvent(scroll_end_model); |
| 120 widget.OnGestureEvent(&e); |
| 121 EXPECT_EQ(update_delta, delegate.update_delta()); |
| 122 EXPECT_TRUE(delegate.got_scroll_end()); |
| 123 |
| 124 delegate.Reset(); |
| 125 |
| 126 // Scroll ending with a fling |
| 127 e = ui::GestureEvent(scroll_begin_model); |
| 128 widget.OnGestureEvent(&e); |
| 129 e = ui::GestureEvent(scroll_update_model); |
| 130 widget.OnGestureEvent(&e); |
| 131 e = ui::GestureEvent(fling_start_model); |
| 132 widget.OnGestureEvent(&e); |
| 133 EXPECT_TRUE(delegate.got_scroll_end()); |
| 134 |
| 135 delegate.Reset(); |
| 136 |
| 137 drag_handle.reset(); |
| 138 } |
| 139 |
| 140 } // namespace athena |
OLD | NEW |