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 DCHECK_NE(0U, event
.GetUniqueEventId());
21 event
.set_unique_event_id(123456U);
22 EXPECT_EQ(123456U, event
.GetUniqueEventId());
24 event
.PushPointer(PointerProperties(8.3f
, 4.7f
, 0.9f
));
25 ASSERT_EQ(2U, event
.GetPointerCount());
26 EXPECT_EQ(8.3f
, event
.GetX(1));
27 EXPECT_EQ(4.7f
, event
.GetY(1));
29 event
.PushPointer(PointerProperties(2.3f
, -3.7f
, 5.8f
));
30 ASSERT_EQ(3U, event
.GetPointerCount());
31 EXPECT_EQ(2.3f
, event
.GetX(2));
32 EXPECT_EQ(-3.7f
, event
.GetY(2));
34 event
.pointer(0).id
= 3;
35 EXPECT_EQ(3, event
.GetPointerId(0));
37 event
.set_action(MotionEvent::ACTION_POINTER_DOWN
);
38 EXPECT_EQ(MotionEvent::ACTION_POINTER_DOWN
, event
.GetAction());
40 event_time
+= base::TimeDelta::FromMilliseconds(5);
41 event
.set_event_time(event_time
);
42 EXPECT_EQ(event_time
, event
.GetEventTime());
44 event
.set_button_state(MotionEvent::BUTTON_PRIMARY
);
45 EXPECT_EQ(MotionEvent::BUTTON_PRIMARY
, event
.GetButtonState());
47 event
.set_flags(EF_ALT_DOWN
| EF_SHIFT_DOWN
);
48 EXPECT_EQ(EF_ALT_DOWN
| EF_SHIFT_DOWN
, event
.GetFlags());
50 event
.set_action_index(1);
51 EXPECT_EQ(1, event
.GetActionIndex());
53 event
.set_action(MotionEvent::ACTION_MOVE
);
54 EXPECT_EQ(MotionEvent::ACTION_MOVE
, event
.GetAction());
56 PointerProperties
historical_pointer0(1.2f
, 2.4f
, 1.f
);
57 PointerProperties
historical_pointer1(2.4f
, 4.8f
, 2.f
);
58 PointerProperties
historical_pointer2(4.8f
, 9.6f
, 3.f
);
59 MotionEventGeneric
historical_event(
60 MotionEvent::ACTION_MOVE
,
61 event_time
- base::TimeDelta::FromMilliseconds(5),
63 historical_event
.PushPointer(historical_pointer1
);
64 historical_event
.PushPointer(historical_pointer2
);
66 event
.PushHistoricalEvent(historical_event
.Clone());
67 EXPECT_EQ(1U, event
.GetHistorySize());
68 EXPECT_EQ(event_time
- base::TimeDelta::FromMilliseconds(5),
69 event
.GetHistoricalEventTime(0));
70 EXPECT_EQ(1.2f
, event
.GetHistoricalX(0, 0));
71 EXPECT_EQ(2.4f
, event
.GetHistoricalY(0, 0));
72 EXPECT_EQ(1.f
, event
.GetHistoricalTouchMajor(0, 0));
73 EXPECT_EQ(2.4f
, event
.GetHistoricalX(1, 0));
74 EXPECT_EQ(4.8f
, event
.GetHistoricalY(1, 0));
75 EXPECT_EQ(2.f
, event
.GetHistoricalTouchMajor(1, 0));
76 EXPECT_EQ(4.8f
, event
.GetHistoricalX(2, 0));
77 EXPECT_EQ(9.6f
, event
.GetHistoricalY(2, 0));
78 EXPECT_EQ(3.f
, event
.GetHistoricalTouchMajor(2, 0));
81 TEST(MotionEventGenericTest
, Clone
) {
82 MotionEventGeneric
event(MotionEvent::ACTION_DOWN
,
83 base::TimeTicks::Now(),
84 PointerProperties(8.3f
, 4.7f
, 2.f
));
85 event
.set_button_state(MotionEvent::BUTTON_PRIMARY
);
87 scoped_ptr
<MotionEvent
> clone
= event
.Clone();
89 EXPECT_EQ(event
.GetUniqueEventId(), clone
->GetUniqueEventId());
90 EXPECT_EQ(test::ToString(event
), test::ToString(*clone
));
93 TEST(MotionEventGenericTest
, CloneWithHistory
) {
94 base::TimeTicks event_time
= base::TimeTicks::Now();
95 base::TimeTicks historical_event_time
=
96 event_time
- base::TimeDelta::FromMilliseconds(5);
98 PointerProperties
pointer(8.3f
, 4.7f
, 10.1f
);
99 MotionEventGeneric
event(MotionEvent::ACTION_MOVE
, event_time
, pointer
);
101 PointerProperties
historical_pointer(3.4f
, -4.3f
, 11.5);
102 scoped_ptr
<MotionEvent
> historical_event(new MotionEventGeneric(
103 MotionEvent::ACTION_MOVE
, historical_event_time
, historical_pointer
));
105 event
.PushHistoricalEvent(historical_event
.Pass());
106 EXPECT_EQ(1U, event
.GetHistorySize());
108 scoped_ptr
<MotionEvent
> clone
= event
.Clone();
110 EXPECT_EQ(event
.GetUniqueEventId(), clone
->GetUniqueEventId());
111 EXPECT_EQ(test::ToString(event
), test::ToString(*clone
));
114 TEST(MotionEventGenericTest
, Cancel
) {
115 MotionEventGeneric
event(MotionEvent::ACTION_UP
,
116 base::TimeTicks::Now(),
117 PointerProperties(8.7f
, 4.3f
, 1.f
));
118 event
.set_button_state(MotionEvent::BUTTON_SECONDARY
);
120 scoped_ptr
<MotionEvent
> cancel
= event
.Cancel();
121 event
.set_action(MotionEvent::ACTION_CANCEL
);
123 EXPECT_NE(event
.GetUniqueEventId(), cancel
->GetUniqueEventId());
124 EXPECT_EQ(test::ToString(event
), test::ToString(*cancel
));
127 TEST(MotionEventGenericTest
, FindPointerIndexOfId
) {
128 base::TimeTicks event_time
= base::TimeTicks::Now();
129 PointerProperties pointer
;
131 MotionEventGeneric
event0(MotionEvent::ACTION_DOWN
, event_time
, pointer
);
132 EXPECT_EQ(0, event0
.FindPointerIndexOfId(0));
133 EXPECT_EQ(-1, event0
.FindPointerIndexOfId(1));
134 EXPECT_EQ(-1, event0
.FindPointerIndexOfId(-1));
136 MotionEventGeneric
event1(event0
);
138 EXPECT_EQ(1u, event1
.PushPointer(pointer
));
139 EXPECT_EQ(0, event1
.FindPointerIndexOfId(0));
140 EXPECT_EQ(1, event1
.FindPointerIndexOfId(7));
141 EXPECT_EQ(-1, event1
.FindPointerIndexOfId(6));
142 EXPECT_EQ(-1, event1
.FindPointerIndexOfId(1));
144 MotionEventGeneric
event2(event1
);
146 EXPECT_EQ(2u, event2
.PushPointer(pointer
));
147 EXPECT_EQ(0, event2
.FindPointerIndexOfId(0));
148 EXPECT_EQ(1, event2
.FindPointerIndexOfId(7));
149 EXPECT_EQ(2, event2
.FindPointerIndexOfId(3));
150 EXPECT_EQ(-1, event2
.FindPointerIndexOfId(1));
151 EXPECT_EQ(-1, event2
.FindPointerIndexOfId(2));
154 TEST(MotionEventGenericTest
, RemovePointerAt
) {
155 base::TimeTicks event_time
= base::TimeTicks::Now();
156 PointerProperties pointer
;
158 MotionEventGeneric
event(MotionEvent::ACTION_DOWN
, event_time
, pointer
);
161 EXPECT_EQ(1u, event
.PushPointer(pointer
));
162 EXPECT_EQ(2u, event
.GetPointerCount());
164 // Remove from the end.
165 event
.RemovePointerAt(1);
166 EXPECT_EQ(1u, event
.GetPointerCount());
167 EXPECT_EQ(-1, event
.FindPointerIndexOfId(7));
168 EXPECT_EQ(0, event
.FindPointerIndexOfId(0));
170 EXPECT_EQ(1u, event
.PushPointer(pointer
));
171 EXPECT_EQ(2u, event
.GetPointerCount());
173 // Remove from the beginning.
174 event
.RemovePointerAt(0);
175 EXPECT_EQ(1u, event
.GetPointerCount());
176 EXPECT_EQ(0, event
.FindPointerIndexOfId(7));
177 EXPECT_EQ(-1, event
.FindPointerIndexOfId(0));
180 TEST(MotionEventGenericTest
, ToString
) {
181 base::TimeTicks event_time
= base::TimeTicks::Now();
182 base::TimeTicks historical_event_time0
=
183 event_time
- base::TimeDelta::FromMilliseconds(10);
184 base::TimeTicks historical_event_time1
=
185 event_time
- base::TimeDelta::FromMilliseconds(5);
187 PointerProperties
pointer0(1, 2, 3);
189 pointer0
.pressure
= 10;
190 pointer0
.touch_minor
= 15;
191 pointer0
.touch_major
= 20;
192 pointer0
.orientation
= 1;
194 PointerProperties
pointer1(4, 5, 6);
196 pointer0
.pressure
= 25;
197 pointer0
.touch_minor
= 30;
198 pointer0
.touch_major
= 35;
199 pointer0
.orientation
= -1;
201 MotionEventGeneric
event(MotionEvent::ACTION_MOVE
, event_time
, pointer0
);
202 event
.PushPointer(pointer1
);
206 scoped_ptr
<MotionEventGeneric
> historical_event0(new MotionEventGeneric(
207 MotionEvent::ACTION_MOVE
, historical_event_time0
, pointer0
));
208 historical_event0
->PushPointer(pointer1
);
212 scoped_ptr
<MotionEventGeneric
> historical_event1(new MotionEventGeneric(
213 MotionEvent::ACTION_MOVE
, historical_event_time1
, pointer0
));
214 historical_event1
->PushPointer(pointer1
);
216 event
.PushHistoricalEvent(historical_event0
.Pass());
217 event
.PushHistoricalEvent(historical_event1
.Pass());
218 ASSERT_EQ(2U, event
.GetHistorySize());
219 ASSERT_EQ(2U, event
.GetPointerCount());
221 // Do a basic smoke exercise of event stringification to ensure things don't
222 // explode in the process.
223 std::string event_string
= test::ToString(event
);
224 EXPECT_FALSE(event_string
.empty());