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 // MSVC++ requires this to be set before any other includes to get M_PI.
6 #define _USE_MATH_DEFINES
10 #include "testing/gtest/include/gtest/gtest.h"
11 #include "ui/events/event.h"
12 #include "ui/events/gestures/motion_event_aura.h"
13 #include "ui/events/test/motion_event_test_utils.h"
17 ui::TouchEvent
TouchWithType(ui::EventType type
, int id
) {
18 return ui::TouchEvent(
19 type
, gfx::PointF(0, 0), id
, base::TimeDelta::FromMilliseconds(0));
22 ui::TouchEvent
TouchWithPosition(ui::EventType type
,
28 ui::TouchEvent
event(type
,
32 base::TimeDelta::FromMilliseconds(0),
37 event
.set_root_location(gfx::PointF(raw_x
, raw_y
));
41 ui::TouchEvent
TouchWithTapParams(ui::EventType type
,
47 ui::TouchEvent
event(type
,
51 base::TimeDelta::FromMilliseconds(0),
56 event
.set_root_location(gfx::PointF(1, 1));
60 ui::TouchEvent
TouchWithTime(ui::EventType type
, int id
, int ms
) {
61 return ui::TouchEvent(
62 type
, gfx::PointF(0, 0), id
, base::TimeDelta::FromMilliseconds(ms
));
65 base::TimeTicks
MsToTicks(int ms
) {
66 return base::TimeTicks() + base::TimeDelta::FromMilliseconds(ms
);
73 TEST(MotionEventAuraTest
, PointerCountAndIds
) {
74 // Test that |PointerCount()| returns the correct number of pointers, and ids
75 // are assigned correctly.
76 int ids
[] = {4, 6, 1};
78 MotionEventAura event
;
79 EXPECT_EQ(0U, event
.GetPointerCount());
81 TouchEvent press0
= TouchWithType(ET_TOUCH_PRESSED
, ids
[0]);
82 EXPECT_TRUE(event
.OnTouch(press0
));
83 EXPECT_EQ(1U, event
.GetPointerCount());
85 EXPECT_EQ(ids
[0], event
.GetPointerId(0));
87 TouchEvent press1
= TouchWithType(ET_TOUCH_PRESSED
, ids
[1]);
88 EXPECT_TRUE(event
.OnTouch(press1
));
89 EXPECT_EQ(2U, event
.GetPointerCount());
91 EXPECT_EQ(ids
[0], event
.GetPointerId(0));
92 EXPECT_EQ(ids
[1], event
.GetPointerId(1));
94 TouchEvent press2
= TouchWithType(ET_TOUCH_PRESSED
, ids
[2]);
95 EXPECT_TRUE(event
.OnTouch(press2
));
96 EXPECT_EQ(3U, event
.GetPointerCount());
98 EXPECT_EQ(ids
[0], event
.GetPointerId(0));
99 EXPECT_EQ(ids
[1], event
.GetPointerId(1));
100 EXPECT_EQ(ids
[2], event
.GetPointerId(2));
102 TouchEvent release1
= TouchWithType(ET_TOUCH_RELEASED
, ids
[1]);
103 EXPECT_TRUE(event
.OnTouch(release1
));
104 event
.CleanupRemovedTouchPoints(release1
);
105 EXPECT_EQ(2U, event
.GetPointerCount());
107 EXPECT_EQ(ids
[0], event
.GetPointerId(0));
108 EXPECT_EQ(ids
[2], event
.GetPointerId(1));
110 // Test cloning of pointer count and id information.
111 // TODO(mustaq): Make a separate clone test
112 scoped_ptr
<MotionEvent
> clone
= event
.Clone();
113 EXPECT_EQ(2U, clone
->GetPointerCount());
114 EXPECT_EQ(ids
[0], clone
->GetPointerId(0));
115 EXPECT_EQ(ids
[2], clone
->GetPointerId(1));
116 EXPECT_EQ(test::ToString(event
), test::ToString(*clone
));
118 TouchEvent release0
= TouchWithType(ET_TOUCH_RELEASED
, ids
[0]);
119 EXPECT_TRUE(event
.OnTouch(release0
));
120 event
.CleanupRemovedTouchPoints(release0
);
121 EXPECT_EQ(1U, event
.GetPointerCount());
123 EXPECT_EQ(ids
[2], event
.GetPointerId(0));
125 TouchEvent release2
= TouchWithType(ET_TOUCH_RELEASED
, ids
[2]);
126 EXPECT_TRUE(event
.OnTouch(release2
));
127 event
.CleanupRemovedTouchPoints(release2
);
128 EXPECT_EQ(0U, event
.GetPointerCount());
131 TEST(MotionEventAuraTest
, GetActionIndexAfterRemoval
) {
132 // Test that |GetActionIndex()| returns the correct index when points have
134 int ids
[] = {4, 6, 9};
136 MotionEventAura event
;
137 EXPECT_EQ(0U, event
.GetPointerCount());
139 TouchEvent press0
= TouchWithType(ET_TOUCH_PRESSED
, ids
[0]);
140 EXPECT_TRUE(event
.OnTouch(press0
));
141 TouchEvent press1
= TouchWithType(ET_TOUCH_PRESSED
, ids
[1]);
142 EXPECT_TRUE(event
.OnTouch(press1
));
143 EXPECT_EQ(1, event
.GetActionIndex());
144 TouchEvent press2
= TouchWithType(ET_TOUCH_PRESSED
, ids
[2]);
145 EXPECT_TRUE(event
.OnTouch(press2
));
146 EXPECT_EQ(2, event
.GetActionIndex());
147 EXPECT_EQ(3U, event
.GetPointerCount());
149 TouchEvent release1
= TouchWithType(ET_TOUCH_RELEASED
, ids
[1]);
150 EXPECT_TRUE(event
.OnTouch(release1
));
151 EXPECT_EQ(1, event
.GetActionIndex());
152 event
.CleanupRemovedTouchPoints(release1
);
153 EXPECT_EQ(2U, event
.GetPointerCount());
155 TouchEvent release2
= TouchWithType(ET_TOUCH_RELEASED
, ids
[0]);
156 EXPECT_TRUE(event
.OnTouch(release2
));
157 EXPECT_EQ(0, event
.GetActionIndex());
158 event
.CleanupRemovedTouchPoints(release2
);
159 EXPECT_EQ(1U, event
.GetPointerCount());
161 TouchEvent release0
= TouchWithType(ET_TOUCH_RELEASED
, ids
[2]);
162 EXPECT_TRUE(event
.OnTouch(release0
));
163 event
.CleanupRemovedTouchPoints(release0
);
164 EXPECT_EQ(0U, event
.GetPointerCount());
167 TEST(MotionEventAuraTest
, PointerLocations
) {
168 // Test that location information is stored correctly.
169 MotionEventAura event
;
171 const float kRawOffsetX
= 11.1f
;
172 const float kRawOffsetY
= 13.3f
;
174 int ids
[] = {15, 13};
182 raw_x
= x
+ kRawOffsetX
;
183 raw_y
= y
+ kRawOffsetY
;
185 TouchWithPosition(ET_TOUCH_PRESSED
, ids
[0], x
, y
, raw_x
, raw_y
);
186 EXPECT_TRUE(event
.OnTouch(press0
));
188 EXPECT_EQ(1U, event
.GetPointerCount());
189 EXPECT_FLOAT_EQ(x
, event
.GetX(0));
190 EXPECT_FLOAT_EQ(y
, event
.GetY(0));
191 EXPECT_FLOAT_EQ(raw_x
, event
.GetRawX(0));
192 EXPECT_FLOAT_EQ(raw_y
, event
.GetRawY(0));
196 raw_x
= x
+ kRawOffsetX
;
197 raw_y
= y
+ kRawOffsetY
;
199 TouchWithPosition(ET_TOUCH_PRESSED
, ids
[1], x
, y
, raw_x
, raw_y
);
200 EXPECT_TRUE(event
.OnTouch(press1
));
202 EXPECT_EQ(2U, event
.GetPointerCount());
203 EXPECT_FLOAT_EQ(x
, event
.GetX(1));
204 EXPECT_FLOAT_EQ(y
, event
.GetY(1));
205 EXPECT_FLOAT_EQ(raw_x
, event
.GetRawX(1));
206 EXPECT_FLOAT_EQ(raw_y
, event
.GetRawY(1));
208 // Test cloning of pointer location information.
209 scoped_ptr
<MotionEvent
> clone
= event
.Clone();
211 EXPECT_EQ(test::ToString(event
), test::ToString(*clone
));
212 EXPECT_EQ(2U, clone
->GetPointerCount());
213 EXPECT_FLOAT_EQ(x
, clone
->GetX(1));
214 EXPECT_FLOAT_EQ(y
, clone
->GetY(1));
215 EXPECT_FLOAT_EQ(raw_x
, clone
->GetRawX(1));
216 EXPECT_FLOAT_EQ(raw_y
, clone
->GetRawY(1));
221 raw_x
= x
+ kRawOffsetX
;
222 raw_y
= y
+ kRawOffsetY
;
224 TouchWithPosition(ET_TOUCH_MOVED
, ids
[1], x
, y
, raw_x
, raw_y
);
225 EXPECT_TRUE(event
.OnTouch(move1
));
227 EXPECT_FLOAT_EQ(x
, event
.GetX(1));
228 EXPECT_FLOAT_EQ(y
, event
.GetY(1));
229 EXPECT_FLOAT_EQ(raw_x
, event
.GetRawX(1));
230 EXPECT_FLOAT_EQ(raw_y
, event
.GetRawY(1));
234 raw_x
= x
+ kRawOffsetX
;
235 raw_y
= y
+ kRawOffsetY
;
237 TouchWithPosition(ET_TOUCH_MOVED
, ids
[0], x
, y
, raw_x
, raw_y
);
238 EXPECT_TRUE(event
.OnTouch(move0
));
240 EXPECT_FLOAT_EQ(x
, event
.GetX(0));
241 EXPECT_FLOAT_EQ(y
, event
.GetY(0));
242 EXPECT_FLOAT_EQ(raw_x
, event
.GetRawX(0));
243 EXPECT_FLOAT_EQ(raw_y
, event
.GetRawY(0));
246 TEST(MotionEventAuraTest
, TapParams
) {
247 // Test that touch params are stored correctly.
248 MotionEventAura event
;
250 int ids
[] = {15, 13};
254 float rotation_angle
;
259 rotation_angle
= 23.f
;
261 TouchEvent press0
= TouchWithTapParams(
262 ET_TOUCH_PRESSED
, ids
[0], radius_x
, radius_y
, rotation_angle
, pressure
);
263 EXPECT_TRUE(event
.OnTouch(press0
));
265 EXPECT_EQ(1U, event
.GetPointerCount());
266 EXPECT_FLOAT_EQ(radius_x
, event
.GetTouchMajor(0) / 2);
267 EXPECT_FLOAT_EQ(radius_y
, event
.GetTouchMinor(0) / 2);
268 EXPECT_FLOAT_EQ(rotation_angle
, event
.GetOrientation(0) * 180 / M_PI
+ 90);
269 EXPECT_FLOAT_EQ(pressure
, event
.GetPressure(0));
273 rotation_angle
= 46.f
;
275 TouchEvent press1
= TouchWithTapParams(
276 ET_TOUCH_PRESSED
, ids
[1], radius_x
, radius_y
, rotation_angle
, pressure
);
277 EXPECT_TRUE(event
.OnTouch(press1
));
279 EXPECT_EQ(2U, event
.GetPointerCount());
280 EXPECT_FLOAT_EQ(radius_y
, event
.GetTouchMajor(1) / 2);
281 EXPECT_FLOAT_EQ(radius_x
, event
.GetTouchMinor(1) / 2);
282 EXPECT_FLOAT_EQ(rotation_angle
, event
.GetOrientation(1) * 180 / M_PI
);
283 EXPECT_FLOAT_EQ(pressure
, event
.GetPressure(1));
285 // Test cloning of tap params
286 scoped_ptr
<MotionEvent
> clone
= event
.Clone();
288 EXPECT_EQ(test::ToString(event
), test::ToString(*clone
));
289 EXPECT_EQ(2U, clone
->GetPointerCount());
290 EXPECT_FLOAT_EQ(radius_y
, clone
->GetTouchMajor(1) / 2);
291 EXPECT_FLOAT_EQ(radius_x
, clone
->GetTouchMinor(1) / 2);
292 EXPECT_FLOAT_EQ(rotation_angle
, clone
->GetOrientation(1) * 180 / M_PI
);
293 EXPECT_FLOAT_EQ(pressure
, clone
->GetPressure(1));
298 rotation_angle
= 64.f
;
300 TouchEvent move1
= TouchWithTapParams(
301 ET_TOUCH_MOVED
, ids
[1], radius_x
, radius_y
, rotation_angle
, pressure
);
302 move1
.set_location(gfx::Point(20, 21));
303 EXPECT_TRUE(event
.OnTouch(move1
));
305 EXPECT_EQ(2U, event
.GetPointerCount());
306 EXPECT_FLOAT_EQ(radius_y
, event
.GetTouchMajor(1) / 2);
307 EXPECT_FLOAT_EQ(radius_x
, event
.GetTouchMinor(1) / 2);
308 EXPECT_FLOAT_EQ(rotation_angle
, event
.GetOrientation(1) * 180 / M_PI
);
309 EXPECT_FLOAT_EQ(pressure
, event
.GetPressure(1));
312 TEST(MotionEventAuraTest
, Timestamps
) {
313 // Test that timestamp information is stored and converted correctly.
314 MotionEventAura event
;
316 int times_in_ms
[] = {59436, 60263, 82175};
318 TouchEvent press0
= TouchWithTime(
319 ui::ET_TOUCH_PRESSED
, ids
[0], times_in_ms
[0]);
320 EXPECT_TRUE(event
.OnTouch(press0
));
321 EXPECT_EQ(MsToTicks(times_in_ms
[0]), event
.GetEventTime());
323 TouchEvent press1
= TouchWithTime(
324 ui::ET_TOUCH_PRESSED
, ids
[1], times_in_ms
[1]);
325 EXPECT_TRUE(event
.OnTouch(press1
));
326 EXPECT_EQ(MsToTicks(times_in_ms
[1]), event
.GetEventTime());
328 TouchEvent move0
= TouchWithTime(
329 ui::ET_TOUCH_MOVED
, ids
[0], times_in_ms
[2]);
330 move0
.set_location(gfx::PointF(12, 21));
331 EXPECT_TRUE(event
.OnTouch(move0
));
332 EXPECT_EQ(MsToTicks(times_in_ms
[2]), event
.GetEventTime());
334 // Test cloning of timestamp information.
335 scoped_ptr
<MotionEvent
> clone
= event
.Clone();
336 EXPECT_EQ(MsToTicks(times_in_ms
[2]), clone
->GetEventTime());
339 TEST(MotionEventAuraTest
, CachedAction
) {
340 // Test that the cached action and cached action index are correct.
342 MotionEventAura event
;
344 TouchEvent press0
= TouchWithType(ET_TOUCH_PRESSED
, ids
[0]);
345 EXPECT_TRUE(event
.OnTouch(press0
));
346 EXPECT_EQ(MotionEvent::ACTION_DOWN
, event
.GetAction());
347 EXPECT_EQ(1U, event
.GetPointerCount());
349 TouchEvent press1
= TouchWithType(ET_TOUCH_PRESSED
, ids
[1]);
350 EXPECT_TRUE(event
.OnTouch(press1
));
351 EXPECT_EQ(MotionEvent::ACTION_POINTER_DOWN
, event
.GetAction());
352 EXPECT_EQ(1, event
.GetActionIndex());
353 EXPECT_EQ(2U, event
.GetPointerCount());
355 // Test cloning of CachedAction information.
356 scoped_ptr
<MotionEvent
> clone
= event
.Clone();
357 EXPECT_EQ(MotionEvent::ACTION_POINTER_DOWN
, clone
->GetAction());
358 EXPECT_EQ(1, clone
->GetActionIndex());
360 TouchEvent move0
= TouchWithType(ET_TOUCH_MOVED
, ids
[0]);
361 move0
.set_location(gfx::PointF(10, 12));
362 EXPECT_TRUE(event
.OnTouch(move0
));
363 EXPECT_EQ(MotionEvent::ACTION_MOVE
, event
.GetAction());
364 EXPECT_EQ(2U, event
.GetPointerCount());
366 TouchEvent release0
= TouchWithType(ET_TOUCH_RELEASED
, ids
[0]);
367 EXPECT_TRUE(event
.OnTouch(release0
));
368 EXPECT_EQ(MotionEvent::ACTION_POINTER_UP
, event
.GetAction());
369 EXPECT_EQ(2U, event
.GetPointerCount());
370 event
.CleanupRemovedTouchPoints(release0
);
371 EXPECT_EQ(1U, event
.GetPointerCount());
373 TouchEvent release1
= TouchWithType(ET_TOUCH_RELEASED
, ids
[1]);
374 EXPECT_TRUE(event
.OnTouch(release1
));
375 EXPECT_EQ(MotionEvent::ACTION_UP
, event
.GetAction());
376 EXPECT_EQ(1U, event
.GetPointerCount());
377 event
.CleanupRemovedTouchPoints(release1
);
378 EXPECT_EQ(0U, event
.GetPointerCount());
381 TEST(MotionEventAuraTest
, Cancel
) {
383 MotionEventAura event
;
385 TouchEvent press0
= TouchWithType(ET_TOUCH_PRESSED
, ids
[0]);
386 EXPECT_TRUE(event
.OnTouch(press0
));
387 EXPECT_EQ(MotionEvent::ACTION_DOWN
, event
.GetAction());
388 EXPECT_EQ(1U, event
.GetPointerCount());
390 TouchEvent press1
= TouchWithType(ET_TOUCH_PRESSED
, ids
[1]);
391 EXPECT_TRUE(event
.OnTouch(press1
));
392 EXPECT_EQ(MotionEvent::ACTION_POINTER_DOWN
, event
.GetAction());
393 EXPECT_EQ(1, event
.GetActionIndex());
394 EXPECT_EQ(2U, event
.GetPointerCount());
396 scoped_ptr
<MotionEvent
> cancel
= event
.Cancel();
397 EXPECT_EQ(MotionEvent::ACTION_CANCEL
, cancel
->GetAction());
398 EXPECT_EQ(2U, cancel
->GetPointerCount());
401 TEST(MotionEventAuraTest
, ToolType
) {
402 MotionEventAura event
;
404 // For now, all pointers have an unknown tool type.
405 // TODO(jdduke): Expand this test when ui::TouchEvent identifies the source
406 // touch type, crbug.com/404128.
407 EXPECT_TRUE(event
.OnTouch(TouchWithType(ET_TOUCH_PRESSED
, 7)));
408 ASSERT_EQ(1U, event
.GetPointerCount());
409 EXPECT_EQ(MotionEvent::TOOL_TYPE_UNKNOWN
, event
.GetToolType(0));
412 TEST(MotionEventAuraTest
, Flags
) {
414 MotionEventAura event
;
416 TouchEvent press0
= TouchWithType(ET_TOUCH_PRESSED
, ids
[0]);
417 press0
.set_flags(EF_CONTROL_DOWN
);
418 EXPECT_TRUE(event
.OnTouch(press0
));
419 EXPECT_EQ(EF_CONTROL_DOWN
, event
.GetFlags());
421 TouchEvent press1
= TouchWithType(ET_TOUCH_PRESSED
, ids
[1]);
422 press1
.set_flags(EF_CONTROL_DOWN
| EF_CAPS_LOCK_DOWN
);
423 EXPECT_TRUE(event
.OnTouch(press1
));
424 EXPECT_EQ(EF_CONTROL_DOWN
| EF_CAPS_LOCK_DOWN
, event
.GetFlags());
427 // Once crbug.com/446852 is fixed, we should ignore redundant presses.
428 TEST(MotionEventAuraTest
, DoesntIgnoreRedundantPresses
) {
430 MotionEventAura event
;
431 EXPECT_TRUE(event
.OnTouch(TouchWithType(ET_TOUCH_PRESSED
, id
)));
432 EXPECT_TRUE(event
.OnTouch(TouchWithType(ET_TOUCH_PRESSED
, id
)));
435 TEST(MotionEventAuraTest
, IgnoresEventsWithoutPress
) {
437 MotionEventAura event
;
438 EXPECT_FALSE(event
.OnTouch(TouchWithType(ET_TOUCH_MOVED
, id
)));
441 TEST(MotionEventAuraTest
, IgnoresStationaryMoves
) {
443 MotionEventAura event
;
444 EXPECT_TRUE(event
.OnTouch(TouchWithType(ET_TOUCH_PRESSED
, id
)));
445 TouchEvent move0
= TouchWithPosition(ET_TOUCH_PRESSED
, id
, 10, 20, 10, 20);
446 EXPECT_TRUE(event
.OnTouch(move0
));
448 TouchEvent move1
= TouchWithPosition(ET_TOUCH_MOVED
, id
, 11, 21, 11, 21);
449 EXPECT_TRUE(event
.OnTouch(move1
));
450 EXPECT_FALSE(event
.OnTouch(move1
));