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/motion_event_test_utils.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
),
21 MotionEvent::TOOL_TYPE_FINGER
,
28 gfx::RectF(kTouchX
- 1.f
, kTouchY
- 1.f
, 2.f
, 2.f
),
34 bool GestureEquals(const GestureEventData
& lhs
, const GestureEventData
& rhs
) {
35 return lhs
.type() == rhs
.type() &&
36 lhs
.motion_event_id
== rhs
.motion_event_id
&&
37 lhs
.primary_tool_type
== rhs
.primary_tool_type
&&
38 lhs
.time
== rhs
.time
&& lhs
.x
== rhs
.x
&& lhs
.y
== rhs
.y
&&
39 lhs
.raw_x
== rhs
.raw_x
&& lhs
.raw_y
== rhs
.raw_y
;
42 bool PacketEquals(const GestureEventDataPacket
& lhs
,
43 const GestureEventDataPacket
& rhs
) {
44 if (lhs
.timestamp() != rhs
.timestamp() ||
45 lhs
.gesture_count() != rhs
.gesture_count() ||
46 lhs
.timestamp() != rhs
.timestamp() ||
47 lhs
.gesture_source() != rhs
.gesture_source() ||
48 lhs
.touch_location() != rhs
.touch_location() ||
49 lhs
.raw_touch_location() != rhs
.raw_touch_location())
52 for (size_t i
= 0; i
< lhs
.gesture_count(); ++i
) {
53 if (!GestureEquals(lhs
.gesture(i
), rhs
.gesture(i
)))
60 class GestureEventDataPacketTest
: public testing::Test
{};
62 TEST_F(GestureEventDataPacketTest
, Basic
) {
63 base::TimeTicks touch_time
= base::TimeTicks::Now();
65 GestureEventDataPacket packet
;
66 EXPECT_EQ(0U, packet
.gesture_count());
67 EXPECT_EQ(GestureEventDataPacket::UNDEFINED
, packet
.gesture_source());
69 packet
= GestureEventDataPacket::FromTouch(
70 MockMotionEvent(MotionEvent::ACTION_DOWN
, touch_time
, kTouchX
, kTouchY
));
71 EXPECT_TRUE(touch_time
== packet
.timestamp());
72 EXPECT_EQ(0U, packet
.gesture_count());
73 EXPECT_EQ(gfx::PointF(kTouchX
, kTouchY
), packet
.touch_location());
75 for (size_t i
= ET_GESTURE_TYPE_START
; i
< ET_GESTURE_TYPE_END
; ++i
) {
76 const EventType type
= static_cast<EventType
>(i
);
77 GestureEventData gesture
= CreateGesture(type
);
79 const size_t index
= (i
- ET_GESTURE_TYPE_START
);
80 ASSERT_EQ(index
+ 1U, packet
.gesture_count());
81 EXPECT_TRUE(GestureEquals(gesture
, packet
.gesture(index
)));
85 TEST_F(GestureEventDataPacketTest
, Copy
) {
86 GestureEventDataPacket packet0
= GestureEventDataPacket::FromTouch(
87 MockMotionEvent(MotionEvent::ACTION_UP
));
88 packet0
.Push(CreateGesture(ET_GESTURE_TAP_DOWN
));
89 packet0
.Push(CreateGesture(ET_GESTURE_SCROLL_BEGIN
));
91 GestureEventDataPacket packet1
= packet0
;
92 EXPECT_TRUE(PacketEquals(packet0
, packet1
));
95 EXPECT_TRUE(PacketEquals(packet1
, packet0
));
98 TEST_F(GestureEventDataPacketTest
, GestureSource
) {
99 GestureEventDataPacket packet
= GestureEventDataPacket::FromTouch(
100 MockMotionEvent(MotionEvent::ACTION_DOWN
));
101 EXPECT_EQ(GestureEventDataPacket::TOUCH_SEQUENCE_START
,
102 packet
.gesture_source());
104 packet
= GestureEventDataPacket::FromTouch(
105 MockMotionEvent(MotionEvent::ACTION_UP
));
106 EXPECT_EQ(GestureEventDataPacket::TOUCH_SEQUENCE_END
,
107 packet
.gesture_source());
109 packet
= GestureEventDataPacket::FromTouch(
110 MockMotionEvent(MotionEvent::ACTION_CANCEL
));
111 EXPECT_EQ(GestureEventDataPacket::TOUCH_SEQUENCE_CANCEL
,
112 packet
.gesture_source());
114 packet
= GestureEventDataPacket::FromTouch(
115 MockMotionEvent(MotionEvent::ACTION_MOVE
));
116 EXPECT_EQ(GestureEventDataPacket::TOUCH_MOVE
, packet
.gesture_source());
118 packet
= GestureEventDataPacket::FromTouch(
119 MockMotionEvent(MotionEvent::ACTION_POINTER_DOWN
));
120 EXPECT_EQ(GestureEventDataPacket::TOUCH_START
, packet
.gesture_source());
122 packet
= GestureEventDataPacket::FromTouch(
123 MockMotionEvent(MotionEvent::ACTION_POINTER_UP
));
124 EXPECT_EQ(GestureEventDataPacket::TOUCH_END
, packet
.gesture_source());
126 GestureEventData gesture
= CreateGesture(ET_GESTURE_TAP
);
127 packet
= GestureEventDataPacket::FromTouchTimeout(gesture
);
128 EXPECT_EQ(GestureEventDataPacket::TOUCH_TIMEOUT
, packet
.gesture_source());
129 EXPECT_EQ(1U, packet
.gesture_count());
130 EXPECT_EQ(base::TimeTicks(), packet
.timestamp());
131 EXPECT_EQ(gfx::PointF(gesture
.x
, gesture
.y
), packet
.touch_location());