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.
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"
10 using ui::test::MockMotionEvent
;
15 const float kTouchX
= 13.7f
;
16 const float kTouchY
= 14.2f
;
18 GestureEventData
CreateGesture(EventType type
) {
19 return GestureEventData(GestureEventDetails(type
, 0, 0),
21 MotionEvent::TOOL_TYPE_FINGER
,
28 gfx::RectF(kTouchX
- 1.f
, kTouchY
- 1.f
, 2.f
, 2.f
));
33 bool GestureEquals(const GestureEventData
& lhs
, const GestureEventData
& rhs
) {
34 return lhs
.type() == rhs
.type() &&
35 lhs
.motion_event_id
== rhs
.motion_event_id
&&
36 lhs
.primary_tool_type
== rhs
.primary_tool_type
&&
37 lhs
.time
== rhs
.time
&& lhs
.x
== rhs
.x
&& lhs
.y
== rhs
.y
&&
38 lhs
.raw_x
== rhs
.raw_x
&& lhs
.raw_y
== rhs
.raw_y
;
41 bool PacketEquals(const GestureEventDataPacket
& lhs
,
42 const GestureEventDataPacket
& rhs
) {
43 if (lhs
.timestamp() != rhs
.timestamp() ||
44 lhs
.gesture_count() != rhs
.gesture_count() ||
45 lhs
.timestamp() != rhs
.timestamp() ||
46 lhs
.gesture_source() != rhs
.gesture_source() ||
47 lhs
.touch_location() != rhs
.touch_location() ||
48 lhs
.raw_touch_location() != rhs
.raw_touch_location())
51 for (size_t i
= 0; i
< lhs
.gesture_count(); ++i
) {
52 if (!GestureEquals(lhs
.gesture(i
), rhs
.gesture(i
)))
59 class GestureEventDataPacketTest
: public testing::Test
{};
61 TEST_F(GestureEventDataPacketTest
, Basic
) {
62 base::TimeTicks touch_time
= base::TimeTicks::Now();
64 GestureEventDataPacket packet
;
65 EXPECT_EQ(0U, packet
.gesture_count());
66 EXPECT_EQ(GestureEventDataPacket::UNDEFINED
, packet
.gesture_source());
68 packet
= GestureEventDataPacket::FromTouch(
69 MockMotionEvent(MotionEvent::ACTION_DOWN
, touch_time
, kTouchX
, kTouchY
));
70 EXPECT_TRUE(touch_time
== packet
.timestamp());
71 EXPECT_EQ(0U, packet
.gesture_count());
72 EXPECT_EQ(gfx::PointF(kTouchX
, kTouchY
), packet
.touch_location());
74 for (size_t i
= ET_GESTURE_TYPE_START
; i
< ET_GESTURE_TYPE_END
; ++i
) {
75 const EventType type
= static_cast<EventType
>(i
);
76 GestureEventData gesture
= CreateGesture(type
);
78 const size_t index
= (i
- ET_GESTURE_TYPE_START
);
79 ASSERT_EQ(index
+ 1U, packet
.gesture_count());
80 EXPECT_TRUE(GestureEquals(gesture
, packet
.gesture(index
)));
84 TEST_F(GestureEventDataPacketTest
, Copy
) {
85 GestureEventDataPacket packet0
= GestureEventDataPacket::FromTouch(
86 MockMotionEvent(MotionEvent::ACTION_UP
));
87 packet0
.Push(CreateGesture(ET_GESTURE_TAP_DOWN
));
88 packet0
.Push(CreateGesture(ET_GESTURE_SCROLL_BEGIN
));
90 GestureEventDataPacket packet1
= packet0
;
91 EXPECT_TRUE(PacketEquals(packet0
, packet1
));
94 EXPECT_TRUE(PacketEquals(packet1
, packet0
));
97 TEST_F(GestureEventDataPacketTest
, GestureSource
) {
98 GestureEventDataPacket packet
= GestureEventDataPacket::FromTouch(
99 MockMotionEvent(MotionEvent::ACTION_DOWN
));
100 EXPECT_EQ(GestureEventDataPacket::TOUCH_SEQUENCE_START
,
101 packet
.gesture_source());
103 packet
= GestureEventDataPacket::FromTouch(
104 MockMotionEvent(MotionEvent::ACTION_UP
));
105 EXPECT_EQ(GestureEventDataPacket::TOUCH_SEQUENCE_END
,
106 packet
.gesture_source());
108 packet
= GestureEventDataPacket::FromTouch(
109 MockMotionEvent(MotionEvent::ACTION_CANCEL
));
110 EXPECT_EQ(GestureEventDataPacket::TOUCH_SEQUENCE_CANCEL
,
111 packet
.gesture_source());
113 packet
= GestureEventDataPacket::FromTouch(
114 MockMotionEvent(MotionEvent::ACTION_MOVE
));
115 EXPECT_EQ(GestureEventDataPacket::TOUCH_MOVE
, packet
.gesture_source());
117 packet
= GestureEventDataPacket::FromTouch(
118 MockMotionEvent(MotionEvent::ACTION_POINTER_DOWN
));
119 EXPECT_EQ(GestureEventDataPacket::TOUCH_START
, packet
.gesture_source());
121 packet
= GestureEventDataPacket::FromTouch(
122 MockMotionEvent(MotionEvent::ACTION_POINTER_UP
));
123 EXPECT_EQ(GestureEventDataPacket::TOUCH_END
, packet
.gesture_source());
125 GestureEventData gesture
= CreateGesture(ET_GESTURE_TAP
);
126 packet
= GestureEventDataPacket::FromTouchTimeout(gesture
);
127 EXPECT_EQ(GestureEventDataPacket::TOUCH_TIMEOUT
, packet
.gesture_source());
128 EXPECT_EQ(1U, packet
.gesture_count());
129 EXPECT_EQ(base::TimeTicks(), packet
.timestamp());
130 EXPECT_EQ(gfx::PointF(gesture
.x
, gesture
.y
), packet
.touch_location());