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 "testing/gtest/include/gtest/gtest.h"
6 #include "ui/events/event_constants.h"
7 #include "ui/events/gesture_detection/motion_event_generic.h"
8 #include "ui/events/test/motion_event_test_utils.h"
12 TEST(MotionEventGenericTest
, Basic
) {
13 base::TimeTicks event_time
= base::TimeTicks::Now();
14 MotionEventGeneric
event(
15 MotionEvent::ACTION_DOWN
, event_time
, PointerProperties());
16 EXPECT_EQ(1U, event
.GetPointerCount());
17 EXPECT_EQ(0U, event
.GetHistorySize());
18 EXPECT_EQ(event_time
, event
.GetEventTime());
20 event
.PushPointer(PointerProperties(8.3f
, 4.7f
, 0.9f
));
21 ASSERT_EQ(2U, event
.GetPointerCount());
22 EXPECT_EQ(8.3f
, event
.GetX(1));
23 EXPECT_EQ(4.7f
, event
.GetY(1));
25 event
.PushPointer(PointerProperties(2.3f
, -3.7f
, 5.8f
));
26 ASSERT_EQ(3U, event
.GetPointerCount());
27 EXPECT_EQ(2.3f
, event
.GetX(2));
28 EXPECT_EQ(-3.7f
, event
.GetY(2));
31 EXPECT_EQ(1, event
.GetId());
33 event
.set_action(MotionEvent::ACTION_POINTER_DOWN
);
34 EXPECT_EQ(MotionEvent::ACTION_POINTER_DOWN
, event
.GetAction());
36 event_time
+= base::TimeDelta::FromMilliseconds(5);
37 event
.set_event_time(event_time
);
38 EXPECT_EQ(event_time
, event
.GetEventTime());
40 event
.set_button_state(MotionEvent::BUTTON_PRIMARY
);
41 EXPECT_EQ(MotionEvent::BUTTON_PRIMARY
, event
.GetButtonState());
43 event
.set_flags(EF_ALT_DOWN
| EF_SHIFT_DOWN
);
44 EXPECT_EQ(EF_ALT_DOWN
| EF_SHIFT_DOWN
, event
.GetFlags());
46 event
.set_action_index(1);
47 EXPECT_EQ(1, event
.GetActionIndex());
49 event
.set_action(MotionEvent::ACTION_MOVE
);
50 EXPECT_EQ(MotionEvent::ACTION_MOVE
, event
.GetAction());
52 PointerProperties
historical_pointer0(1.2f
, 2.4f
, 1.f
);
53 PointerProperties
historical_pointer1(2.4f
, 4.8f
, 2.f
);
54 PointerProperties
historical_pointer2(4.8f
, 9.6f
, 3.f
);
55 MotionEventGeneric
historical_event(
56 MotionEvent::ACTION_MOVE
,
57 event_time
- base::TimeDelta::FromMilliseconds(5),
59 historical_event
.PushPointer(historical_pointer1
);
60 historical_event
.PushPointer(historical_pointer2
);
62 event
.PushHistoricalEvent(historical_event
.Clone());
63 EXPECT_EQ(1U, event
.GetHistorySize());
64 EXPECT_EQ(event_time
- base::TimeDelta::FromMilliseconds(5),
65 event
.GetHistoricalEventTime(0));
66 EXPECT_EQ(1.2f
, event
.GetHistoricalX(0, 0));
67 EXPECT_EQ(2.4f
, event
.GetHistoricalY(0, 0));
68 EXPECT_EQ(1.f
, event
.GetHistoricalTouchMajor(0, 0));
69 EXPECT_EQ(2.4f
, event
.GetHistoricalX(1, 0));
70 EXPECT_EQ(4.8f
, event
.GetHistoricalY(1, 0));
71 EXPECT_EQ(2.f
, event
.GetHistoricalTouchMajor(1, 0));
72 EXPECT_EQ(4.8f
, event
.GetHistoricalX(2, 0));
73 EXPECT_EQ(9.6f
, event
.GetHistoricalY(2, 0));
74 EXPECT_EQ(3.f
, event
.GetHistoricalTouchMajor(2, 0));
77 TEST(MotionEventGenericTest
, Clone
) {
78 MotionEventGeneric
event(MotionEvent::ACTION_DOWN
,
79 base::TimeTicks::Now(),
80 PointerProperties(8.3f
, 4.7f
, 2.f
));
82 event
.set_button_state(MotionEvent::BUTTON_PRIMARY
);
84 scoped_ptr
<MotionEvent
> clone
= event
.Clone();
86 EXPECT_EQ(test::ToString(event
), test::ToString(*clone
));
89 TEST(MotionEventGenericTest
, CloneWithHistory
) {
90 base::TimeTicks event_time
= base::TimeTicks::Now();
91 base::TimeTicks historical_event_time
=
92 event_time
- base::TimeDelta::FromMilliseconds(5);
94 PointerProperties
pointer(8.3f
, 4.7f
, 10.1f
);
95 MotionEventGeneric
event(MotionEvent::ACTION_MOVE
, event_time
, pointer
);
97 PointerProperties
historical_pointer(3.4f
, -4.3f
, 11.5);
98 scoped_ptr
<MotionEvent
> historical_event(new MotionEventGeneric(
99 MotionEvent::ACTION_MOVE
, historical_event_time
, historical_pointer
));
101 event
.PushHistoricalEvent(historical_event
.Pass());
102 EXPECT_EQ(1U, event
.GetHistorySize());
104 scoped_ptr
<MotionEvent
> clone
= event
.Clone();
106 EXPECT_EQ(test::ToString(event
), test::ToString(*clone
));
109 TEST(MotionEventGenericTest
, Cancel
) {
110 MotionEventGeneric
event(MotionEvent::ACTION_UP
,
111 base::TimeTicks::Now(),
112 PointerProperties(8.7f
, 4.3f
, 1.f
));
114 event
.set_button_state(MotionEvent::BUTTON_SECONDARY
);
116 scoped_ptr
<MotionEvent
> cancel
= event
.Cancel();
117 event
.set_action(MotionEvent::ACTION_CANCEL
);
119 EXPECT_EQ(test::ToString(event
), test::ToString(*cancel
));
122 TEST(MotionEventGenericTest
, FindPointerIndexOfId
) {
123 base::TimeTicks event_time
= base::TimeTicks::Now();
124 PointerProperties pointer
;
126 MotionEventGeneric
event0(MotionEvent::ACTION_DOWN
, event_time
, pointer
);
127 EXPECT_EQ(0, event0
.FindPointerIndexOfId(0));
128 EXPECT_EQ(-1, event0
.FindPointerIndexOfId(1));
129 EXPECT_EQ(-1, event0
.FindPointerIndexOfId(-1));
131 MotionEventGeneric
event1(event0
);
133 event1
.PushPointer(pointer
);
134 EXPECT_EQ(0, event1
.FindPointerIndexOfId(0));
135 EXPECT_EQ(1, event1
.FindPointerIndexOfId(7));
136 EXPECT_EQ(-1, event1
.FindPointerIndexOfId(6));
137 EXPECT_EQ(-1, event1
.FindPointerIndexOfId(1));
139 MotionEventGeneric
event2(event1
);
141 event2
.PushPointer(pointer
);
142 EXPECT_EQ(0, event2
.FindPointerIndexOfId(0));
143 EXPECT_EQ(1, event2
.FindPointerIndexOfId(7));
144 EXPECT_EQ(2, event2
.FindPointerIndexOfId(3));
145 EXPECT_EQ(-1, event2
.FindPointerIndexOfId(1));
146 EXPECT_EQ(-1, event2
.FindPointerIndexOfId(2));
149 TEST(MotionEventGenericTest
, ToString
) {
150 base::TimeTicks event_time
= base::TimeTicks::Now();
151 base::TimeTicks historical_event_time0
=
152 event_time
- base::TimeDelta::FromMilliseconds(10);
153 base::TimeTicks historical_event_time1
=
154 event_time
- base::TimeDelta::FromMilliseconds(5);
156 PointerProperties
pointer0(1, 2, 3);
158 pointer0
.pressure
= 10;
159 pointer0
.touch_minor
= 15;
160 pointer0
.touch_major
= 20;
161 pointer0
.orientation
= 1;
163 PointerProperties
pointer1(4, 5, 6);
165 pointer0
.pressure
= 25;
166 pointer0
.touch_minor
= 30;
167 pointer0
.touch_major
= 35;
168 pointer0
.orientation
= -1;
170 MotionEventGeneric
event(MotionEvent::ACTION_MOVE
, event_time
, pointer0
);
171 event
.PushPointer(pointer1
);
175 scoped_ptr
<MotionEventGeneric
> historical_event0(new MotionEventGeneric(
176 MotionEvent::ACTION_MOVE
, historical_event_time0
, pointer0
));
177 historical_event0
->PushPointer(pointer1
);
181 scoped_ptr
<MotionEventGeneric
> historical_event1(new MotionEventGeneric(
182 MotionEvent::ACTION_MOVE
, historical_event_time1
, pointer0
));
183 historical_event1
->PushPointer(pointer1
);
185 event
.PushHistoricalEvent(historical_event0
.Pass());
186 event
.PushHistoricalEvent(historical_event1
.Pass());
187 ASSERT_EQ(2U, event
.GetHistorySize());
188 ASSERT_EQ(2U, event
.GetPointerCount());
190 // Do a basic smoke exercise of event stringification to ensure things don't
191 // explode in the process.
192 std::string event_string
= test::ToString(event
);
193 EXPECT_FALSE(event_string
.empty());