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 "base/basictypes.h" |
| 6 #include "testing/gtest/include/gtest/gtest.h" |
| 7 #include "ui/events/gesture_detection/gesture_event_data_packet.h" |
| 8 #include "ui/events/test/mock_motion_event.h" |
| 9 |
| 10 using ui::test::MockMotionEvent; |
| 11 |
| 12 namespace ui { |
| 13 namespace { |
| 14 |
| 15 const float kTouchX = 13.7f; |
| 16 const float kTouchY = 14.2f; |
| 17 |
| 18 GestureEventData CreateGesture(EventType type) { |
| 19 return GestureEventData(GestureEventDetails(type, 0, 0), |
| 20 0, |
| 21 base::TimeTicks(), |
| 22 kTouchX, |
| 23 kTouchY, |
| 24 kTouchX + 5.f, |
| 25 kTouchY + 10.f, |
| 26 1, |
| 27 gfx::RectF(kTouchX - 1.f, kTouchY - 1.f, 2.f, 2.f)); |
| 28 } |
| 29 |
| 30 } // namespace |
| 31 |
| 32 bool GestureEquals(const GestureEventData& lhs, const GestureEventData& rhs) { |
| 33 return lhs.type() == rhs.type() && |
| 34 lhs.motion_event_id == rhs.motion_event_id && lhs.time == rhs.time && |
| 35 lhs.x == rhs.x && lhs.y == rhs.y && lhs.raw_x == rhs.raw_x && |
| 36 lhs.raw_y == rhs.raw_y; |
| 37 } |
| 38 |
| 39 bool PacketEquals(const GestureEventDataPacket& lhs, |
| 40 const GestureEventDataPacket& rhs) { |
| 41 if (lhs.timestamp() != rhs.timestamp() || |
| 42 lhs.gesture_count() != rhs.gesture_count() || |
| 43 lhs.timestamp() != rhs.timestamp() || |
| 44 lhs.gesture_source() != rhs.gesture_source() || |
| 45 lhs.touch_location() != rhs.touch_location() || |
| 46 lhs.raw_touch_location() != rhs.raw_touch_location()) |
| 47 return false; |
| 48 |
| 49 for (size_t i = 0; i < lhs.gesture_count(); ++i) { |
| 50 if (!GestureEquals(lhs.gesture(i), rhs.gesture(i))) |
| 51 return false; |
| 52 } |
| 53 |
| 54 return true; |
| 55 } |
| 56 |
| 57 class GestureEventDataPacketTest : public testing::Test {}; |
| 58 |
| 59 TEST_F(GestureEventDataPacketTest, Basic) { |
| 60 base::TimeTicks touch_time = base::TimeTicks::Now(); |
| 61 |
| 62 GestureEventDataPacket packet; |
| 63 EXPECT_EQ(0U, packet.gesture_count()); |
| 64 EXPECT_EQ(GestureEventDataPacket::UNDEFINED, packet.gesture_source()); |
| 65 |
| 66 packet = GestureEventDataPacket::FromTouch( |
| 67 MockMotionEvent(MotionEvent::ACTION_DOWN, touch_time, kTouchX, kTouchY)); |
| 68 EXPECT_TRUE(touch_time == packet.timestamp()); |
| 69 EXPECT_EQ(0U, packet.gesture_count()); |
| 70 EXPECT_EQ(gfx::PointF(kTouchX, kTouchY), packet.touch_location()); |
| 71 |
| 72 for (size_t i = ET_GESTURE_TYPE_START; i < ET_GESTURE_TYPE_END; ++i) { |
| 73 const EventType type = static_cast<EventType>(i); |
| 74 GestureEventData gesture = CreateGesture(type); |
| 75 packet.Push(gesture); |
| 76 const size_t index = (i - ET_GESTURE_TYPE_START); |
| 77 ASSERT_EQ(index + 1U, packet.gesture_count()); |
| 78 EXPECT_TRUE(GestureEquals(gesture, packet.gesture(index))); |
| 79 } |
| 80 } |
| 81 |
| 82 TEST_F(GestureEventDataPacketTest, Copy) { |
| 83 GestureEventDataPacket packet0 = GestureEventDataPacket::FromTouch( |
| 84 MockMotionEvent(MotionEvent::ACTION_UP)); |
| 85 packet0.Push(CreateGesture(ET_GESTURE_TAP_DOWN)); |
| 86 packet0.Push(CreateGesture(ET_GESTURE_SCROLL_BEGIN)); |
| 87 |
| 88 GestureEventDataPacket packet1 = packet0; |
| 89 EXPECT_TRUE(PacketEquals(packet0, packet1)); |
| 90 |
| 91 packet0 = packet1; |
| 92 EXPECT_TRUE(PacketEquals(packet1, packet0)); |
| 93 } |
| 94 |
| 95 TEST_F(GestureEventDataPacketTest, GestureSource) { |
| 96 GestureEventDataPacket packet = GestureEventDataPacket::FromTouch( |
| 97 MockMotionEvent(MotionEvent::ACTION_DOWN)); |
| 98 EXPECT_EQ(GestureEventDataPacket::TOUCH_SEQUENCE_START, |
| 99 packet.gesture_source()); |
| 100 |
| 101 packet = GestureEventDataPacket::FromTouch( |
| 102 MockMotionEvent(MotionEvent::ACTION_UP)); |
| 103 EXPECT_EQ(GestureEventDataPacket::TOUCH_SEQUENCE_END, |
| 104 packet.gesture_source()); |
| 105 |
| 106 packet = GestureEventDataPacket::FromTouch( |
| 107 MockMotionEvent(MotionEvent::ACTION_CANCEL)); |
| 108 EXPECT_EQ(GestureEventDataPacket::TOUCH_SEQUENCE_CANCEL, |
| 109 packet.gesture_source()); |
| 110 |
| 111 packet = GestureEventDataPacket::FromTouch( |
| 112 MockMotionEvent(MotionEvent::ACTION_MOVE)); |
| 113 EXPECT_EQ(GestureEventDataPacket::TOUCH_MOVE, packet.gesture_source()); |
| 114 |
| 115 packet = GestureEventDataPacket::FromTouch( |
| 116 MockMotionEvent(MotionEvent::ACTION_POINTER_DOWN)); |
| 117 EXPECT_EQ(GestureEventDataPacket::TOUCH_START, packet.gesture_source()); |
| 118 |
| 119 packet = GestureEventDataPacket::FromTouch( |
| 120 MockMotionEvent(MotionEvent::ACTION_POINTER_UP)); |
| 121 EXPECT_EQ(GestureEventDataPacket::TOUCH_END, packet.gesture_source()); |
| 122 |
| 123 GestureEventData gesture = CreateGesture(ET_GESTURE_TAP); |
| 124 packet = GestureEventDataPacket::FromTouchTimeout(gesture); |
| 125 EXPECT_EQ(GestureEventDataPacket::TOUCH_TIMEOUT, packet.gesture_source()); |
| 126 EXPECT_EQ(1U, packet.gesture_count()); |
| 127 EXPECT_EQ(base::TimeTicks(), packet.timestamp()); |
| 128 EXPECT_EQ(gfx::PointF(gesture.x, gesture.y), packet.touch_location()); |
| 129 } |
| 130 |
| 131 } // namespace ui |
OLD | NEW |