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.h"
7 #include "ui/events/gestures/motion_event_aura.h"
11 ui::TouchEvent
TouchWithType(ui::EventType type
, int id
) {
12 return ui::TouchEvent(
13 type
, gfx::PointF(0, 0), id
, base::TimeDelta::FromMilliseconds(0));
16 ui::TouchEvent
TouchWithPosition(ui::EventType type
,
24 ui::TouchEvent
event(type
,
28 base::TimeDelta::FromMilliseconds(0),
33 event
.set_root_location(gfx::PointF(raw_x
, raw_y
));
37 ui::TouchEvent
TouchWithTime(ui::EventType type
, int id
, int ms
) {
38 return ui::TouchEvent(
39 type
, gfx::PointF(0, 0), id
, base::TimeDelta::FromMilliseconds(ms
));
42 base::TimeTicks
MsToTicks(int ms
) {
43 return base::TimeTicks() + base::TimeDelta::FromMilliseconds(ms
);
50 TEST(MotionEventAuraTest
, PointerCountAndIds
) {
51 // Test that |PointerCount()| returns the correct number of pointers, and ids
52 // are assigned correctly.
53 int ids
[] = {4, 6, 1};
55 MotionEventAura event
;
56 EXPECT_EQ(0U, event
.GetPointerCount());
58 TouchEvent press0
= TouchWithType(ET_TOUCH_PRESSED
, ids
[0]);
59 event
.OnTouch(press0
);
60 EXPECT_EQ(1U, event
.GetPointerCount());
62 EXPECT_EQ(ids
[0], event
.GetPointerId(0));
64 TouchEvent press1
= TouchWithType(ET_TOUCH_PRESSED
, ids
[1]);
65 event
.OnTouch(press1
);
66 EXPECT_EQ(2U, event
.GetPointerCount());
68 EXPECT_EQ(ids
[0], event
.GetPointerId(0));
69 EXPECT_EQ(ids
[1], event
.GetPointerId(1));
71 TouchEvent press2
= TouchWithType(ET_TOUCH_PRESSED
, ids
[2]);
72 event
.OnTouch(press2
);
73 EXPECT_EQ(3U, event
.GetPointerCount());
75 EXPECT_EQ(ids
[0], event
.GetPointerId(0));
76 EXPECT_EQ(ids
[1], event
.GetPointerId(1));
77 EXPECT_EQ(ids
[2], event
.GetPointerId(2));
79 TouchEvent release1
= TouchWithType(ET_TOUCH_RELEASED
, ids
[1]);
80 event
.OnTouch(release1
);
81 event
.CleanupRemovedTouchPoints(release1
);
82 EXPECT_EQ(2U, event
.GetPointerCount());
84 EXPECT_EQ(ids
[0], event
.GetPointerId(0));
85 EXPECT_EQ(ids
[2], event
.GetPointerId(1));
87 // Test cloning of pointer count and id information.
88 scoped_ptr
<MotionEvent
> clone
= event
.Clone();
89 EXPECT_EQ(2U, clone
->GetPointerCount());
90 EXPECT_EQ(ids
[0], clone
->GetPointerId(0));
91 EXPECT_EQ(ids
[2], clone
->GetPointerId(1));
93 TouchEvent release0
= TouchWithType(ET_TOUCH_RELEASED
, ids
[0]);
94 event
.OnTouch(release0
);
95 event
.CleanupRemovedTouchPoints(release0
);
96 EXPECT_EQ(1U, event
.GetPointerCount());
98 EXPECT_EQ(ids
[2], event
.GetPointerId(0));
100 TouchEvent release2
= TouchWithType(ET_TOUCH_RELEASED
, ids
[2]);
101 event
.OnTouch(release2
);
102 event
.CleanupRemovedTouchPoints(release2
);
103 EXPECT_EQ(0U, event
.GetPointerCount());
106 TEST(MotionEventAuraTest
, GetActionIndexAfterRemoval
) {
107 // Test that |GetActionIndex()| returns the correct index when points have
109 int ids
[] = {4, 6, 9};
111 MotionEventAura event
;
112 EXPECT_EQ(0U, event
.GetPointerCount());
114 TouchEvent press0
= TouchWithType(ET_TOUCH_PRESSED
, ids
[0]);
115 event
.OnTouch(press0
);
116 TouchEvent press1
= TouchWithType(ET_TOUCH_PRESSED
, ids
[1]);
117 event
.OnTouch(press1
);
118 TouchEvent press2
= TouchWithType(ET_TOUCH_PRESSED
, ids
[2]);
119 event
.OnTouch(press2
);
120 EXPECT_EQ(3U, event
.GetPointerCount());
122 TouchEvent release1
= TouchWithType(ET_TOUCH_RELEASED
, ids
[1]);
123 event
.OnTouch(release1
);
124 event
.CleanupRemovedTouchPoints(release1
);
125 EXPECT_EQ(1, event
.GetActionIndex());
126 EXPECT_EQ(2U, event
.GetPointerCount());
128 TouchEvent release2
= TouchWithType(ET_TOUCH_RELEASED
, ids
[0]);
129 event
.OnTouch(release2
);
130 event
.CleanupRemovedTouchPoints(release2
);
131 EXPECT_EQ(0, event
.GetActionIndex());
132 EXPECT_EQ(1U, event
.GetPointerCount());
134 TouchEvent release0
= TouchWithType(ET_TOUCH_RELEASED
, ids
[2]);
135 event
.OnTouch(release0
);
136 event
.CleanupRemovedTouchPoints(release0
);
137 EXPECT_EQ(0U, event
.GetPointerCount());
140 TEST(MotionEventAuraTest
, PointerLocations
) {
141 // Test that location information is stored correctly.
142 MotionEventAura event
;
144 const float kRawOffsetX
= 11.1f
;
145 const float kRawOffsetY
= 13.3f
;
147 int ids
[] = {15, 13};
157 raw_x
= x
+ kRawOffsetX
;
158 raw_y
= y
+ kRawOffsetY
;
162 TouchWithPosition(ET_TOUCH_PRESSED
, ids
[0], x
, y
, raw_x
, raw_y
, r
, p
);
163 event
.OnTouch(press0
);
165 EXPECT_EQ(1U, event
.GetPointerCount());
166 EXPECT_FLOAT_EQ(x
, event
.GetX(0));
167 EXPECT_FLOAT_EQ(y
, event
.GetY(0));
168 EXPECT_FLOAT_EQ(raw_x
, event
.GetRawX(0));
169 EXPECT_FLOAT_EQ(raw_y
, event
.GetRawY(0));
170 EXPECT_FLOAT_EQ(r
, event
.GetTouchMajor(0) / 2);
171 EXPECT_FLOAT_EQ(p
, event
.GetPressure(0));
175 raw_x
= x
+ kRawOffsetX
;
176 raw_y
= y
+ kRawOffsetY
;
180 TouchWithPosition(ET_TOUCH_PRESSED
, ids
[1], x
, y
, raw_x
, raw_y
, r
, p
);
181 event
.OnTouch(press1
);
183 EXPECT_EQ(2U, event
.GetPointerCount());
184 EXPECT_FLOAT_EQ(x
, event
.GetX(1));
185 EXPECT_FLOAT_EQ(y
, event
.GetY(1));
186 EXPECT_FLOAT_EQ(raw_x
, event
.GetRawX(1));
187 EXPECT_FLOAT_EQ(raw_y
, event
.GetRawY(1));
188 EXPECT_FLOAT_EQ(r
, event
.GetTouchMajor(1) / 2);
189 EXPECT_FLOAT_EQ(p
, event
.GetPressure(1));
191 // Test cloning of pointer location information.
192 scoped_ptr
<MotionEvent
> clone
= event
.Clone();
193 EXPECT_EQ(2U, clone
->GetPointerCount());
194 EXPECT_FLOAT_EQ(x
, clone
->GetX(1));
195 EXPECT_FLOAT_EQ(y
, clone
->GetY(1));
196 EXPECT_FLOAT_EQ(raw_x
, event
.GetRawX(1));
197 EXPECT_FLOAT_EQ(raw_y
, event
.GetRawY(1));
198 EXPECT_FLOAT_EQ(r
, clone
->GetTouchMajor(1) / 2);
199 EXPECT_FLOAT_EQ(p
, clone
->GetPressure(1));
203 raw_x
= x
+ kRawOffsetX
;
204 raw_y
= y
+ kRawOffsetY
;
208 TouchWithPosition(ET_TOUCH_MOVED
, ids
[1], x
, y
, raw_x
, raw_y
, r
, p
);
209 event
.OnTouch(move1
);
211 EXPECT_FLOAT_EQ(x
, event
.GetX(1));
212 EXPECT_FLOAT_EQ(y
, event
.GetY(1));
213 EXPECT_FLOAT_EQ(raw_x
, event
.GetRawX(1));
214 EXPECT_FLOAT_EQ(raw_y
, event
.GetRawY(1));
215 EXPECT_FLOAT_EQ(r
, event
.GetTouchMajor(1) / 2);
216 EXPECT_FLOAT_EQ(p
, event
.GetPressure(1));
220 raw_x
= x
+ kRawOffsetX
;
221 raw_y
= y
+ kRawOffsetY
;
225 TouchWithPosition(ET_TOUCH_MOVED
, ids
[0], x
, y
, raw_x
, raw_y
, r
, p
);
226 event
.OnTouch(move0
);
228 EXPECT_FLOAT_EQ(x
, event
.GetX(0));
229 EXPECT_FLOAT_EQ(y
, event
.GetY(0));
230 EXPECT_FLOAT_EQ(raw_x
, event
.GetRawX(0));
231 EXPECT_FLOAT_EQ(raw_y
, event
.GetRawY(0));
232 EXPECT_FLOAT_EQ(r
, event
.GetTouchMajor(0) / 2);
233 EXPECT_FLOAT_EQ(p
, event
.GetPressure(0));
236 TEST(MotionEventAuraTest
, Timestamps
) {
237 // Test that timestamp information is stored and converted correctly.
238 MotionEventAura event
;
240 int times_in_ms
[] = {59436, 60263, 82175};
242 TouchEvent press0
= TouchWithTime(
243 ui::ET_TOUCH_PRESSED
, ids
[0], times_in_ms
[0]);
244 event
.OnTouch(press0
);
245 EXPECT_EQ(MsToTicks(times_in_ms
[0]), event
.GetEventTime());
247 TouchEvent press1
= TouchWithTime(
248 ui::ET_TOUCH_PRESSED
, ids
[1], times_in_ms
[1]);
249 event
.OnTouch(press1
);
250 EXPECT_EQ(MsToTicks(times_in_ms
[1]), event
.GetEventTime());
252 TouchEvent move0
= TouchWithTime(
253 ui::ET_TOUCH_MOVED
, ids
[0], times_in_ms
[2]);
254 event
.OnTouch(move0
);
255 EXPECT_EQ(MsToTicks(times_in_ms
[2]), event
.GetEventTime());
257 // Test cloning of timestamp information.
258 scoped_ptr
<MotionEvent
> clone
= event
.Clone();
259 EXPECT_EQ(MsToTicks(times_in_ms
[2]), clone
->GetEventTime());
262 TEST(MotionEventAuraTest
, CachedAction
) {
263 // Test that the cached action and cached action index are correct.
265 MotionEventAura event
;
267 TouchEvent press0
= TouchWithType(ET_TOUCH_PRESSED
, ids
[0]);
268 event
.OnTouch(press0
);
269 EXPECT_EQ(MotionEvent::ACTION_DOWN
, event
.GetAction());
270 EXPECT_EQ(1U, event
.GetPointerCount());
272 TouchEvent press1
= TouchWithType(ET_TOUCH_PRESSED
, ids
[1]);
273 event
.OnTouch(press1
);
274 EXPECT_EQ(MotionEvent::ACTION_POINTER_DOWN
, event
.GetAction());
275 EXPECT_EQ(1, event
.GetActionIndex());
276 EXPECT_EQ(2U, event
.GetPointerCount());
278 // Test cloning of CachedAction information.
279 scoped_ptr
<MotionEvent
> clone
= event
.Clone();
280 EXPECT_EQ(MotionEvent::ACTION_POINTER_DOWN
, clone
->GetAction());
281 EXPECT_EQ(1, clone
->GetActionIndex());
283 TouchEvent move0
= TouchWithType(ET_TOUCH_MOVED
, ids
[0]);
284 event
.OnTouch(move0
);
285 EXPECT_EQ(MotionEvent::ACTION_MOVE
, event
.GetAction());
286 EXPECT_EQ(2U, event
.GetPointerCount());
288 TouchEvent release0
= TouchWithType(ET_TOUCH_RELEASED
, ids
[0]);
289 event
.OnTouch(release0
);
290 EXPECT_EQ(MotionEvent::ACTION_POINTER_UP
, event
.GetAction());
291 EXPECT_EQ(2U, event
.GetPointerCount());
292 event
.CleanupRemovedTouchPoints(release0
);
293 EXPECT_EQ(1U, event
.GetPointerCount());
295 TouchEvent release1
= TouchWithType(ET_TOUCH_RELEASED
, ids
[1]);
296 event
.OnTouch(release1
);
297 EXPECT_EQ(MotionEvent::ACTION_UP
, event
.GetAction());
298 EXPECT_EQ(1U, event
.GetPointerCount());
299 event
.CleanupRemovedTouchPoints(release1
);
300 EXPECT_EQ(0U, event
.GetPointerCount());
303 TEST(MotionEventAuraTest
, Cancel
) {
305 MotionEventAura event
;
307 TouchEvent press0
= TouchWithType(ET_TOUCH_PRESSED
, ids
[0]);
308 event
.OnTouch(press0
);
309 EXPECT_EQ(MotionEvent::ACTION_DOWN
, event
.GetAction());
310 EXPECT_EQ(1U, event
.GetPointerCount());
312 TouchEvent press1
= TouchWithType(ET_TOUCH_PRESSED
, ids
[1]);
313 event
.OnTouch(press1
);
314 EXPECT_EQ(MotionEvent::ACTION_POINTER_DOWN
, event
.GetAction());
315 EXPECT_EQ(1, event
.GetActionIndex());
316 EXPECT_EQ(2U, event
.GetPointerCount());
318 scoped_ptr
<MotionEvent
> cancel
= event
.Cancel();
319 EXPECT_EQ(MotionEvent::ACTION_CANCEL
, cancel
->GetAction());
320 EXPECT_EQ(2U, static_cast<MotionEventAura
*>(cancel
.get())->GetPointerCount());
323 TEST(MotionEventAuraTest
, ToolType
) {
324 MotionEventAura event
;
326 // For now, all pointers have an unknown tool type.
327 // TODO(jdduke): Expand this test when ui::TouchEvent identifies the source
328 // touch type, crbug.com/404128.
329 event
.OnTouch(TouchWithType(ET_TOUCH_PRESSED
, 7));
330 ASSERT_EQ(1U, event
.GetPointerCount());
331 EXPECT_EQ(MotionEvent::TOOL_TYPE_UNKNOWN
, event
.GetToolType(0));