Drive: Add BatchableRequest subclass.
[chromium-blink-merge.git] / ui / events / gestures / motion_event_aura_unittest.cc
blob8decac0bc198a35eecc5416fb5f5d6533cc24019
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
8 #include <cmath>
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"
15 namespace {
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,
23 int id,
24 float x,
25 float y,
26 float raw_x,
27 float raw_y) {
28 ui::TouchEvent event(type,
29 gfx::PointF(x, y),
31 id,
32 base::TimeDelta::FromMilliseconds(0),
36 0);
37 event.set_root_location(gfx::PointF(raw_x, raw_y));
38 return event;
41 ui::TouchEvent TouchWithTapParams(ui::EventType type,
42 int id,
43 float radius_x,
44 float radius_y,
45 float rotation_angle,
46 float pressure) {
47 ui::TouchEvent event(type,
48 gfx::PointF(1, 1),
50 id,
51 base::TimeDelta::FromMilliseconds(0),
52 radius_x,
53 radius_y,
54 rotation_angle,
55 pressure);
56 event.set_root_location(gfx::PointF(1, 1));
57 return event;
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);
69 } // namespace
71 namespace ui {
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, crbug.com/450655
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
133 // been removed.
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};
175 float x;
176 float y;
177 float raw_x;
178 float raw_y;
180 x = 14.4f;
181 y = 17.3f;
182 raw_x = x + kRawOffsetX;
183 raw_y = y + kRawOffsetY;
184 TouchEvent press0 =
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));
194 x = 17.8f;
195 y = 12.1f;
196 raw_x = x + kRawOffsetX;
197 raw_y = y + kRawOffsetY;
198 TouchEvent press1 =
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();
210 EXPECT_EQ(test::ToString(event), test::ToString(*clone));
211 EXPECT_EQ(2U, clone->GetPointerCount());
212 EXPECT_FLOAT_EQ(x, clone->GetX(1));
213 EXPECT_FLOAT_EQ(y, clone->GetY(1));
214 EXPECT_FLOAT_EQ(raw_x, clone->GetRawX(1));
215 EXPECT_FLOAT_EQ(raw_y, clone->GetRawY(1));
217 x = 27.9f;
218 y = 22.3f;
219 raw_x = x + kRawOffsetX;
220 raw_y = y + kRawOffsetY;
221 TouchEvent move1 =
222 TouchWithPosition(ET_TOUCH_MOVED, ids[1], x, y, raw_x, raw_y);
223 EXPECT_TRUE(event.OnTouch(move1));
225 EXPECT_FLOAT_EQ(x, event.GetX(1));
226 EXPECT_FLOAT_EQ(y, event.GetY(1));
227 EXPECT_FLOAT_EQ(raw_x, event.GetRawX(1));
228 EXPECT_FLOAT_EQ(raw_y, event.GetRawY(1));
230 x = 34.6f;
231 y = 23.8f;
232 raw_x = x + kRawOffsetX;
233 raw_y = y + kRawOffsetY;
234 TouchEvent move0 =
235 TouchWithPosition(ET_TOUCH_MOVED, ids[0], x, y, raw_x, raw_y);
236 EXPECT_TRUE(event.OnTouch(move0));
238 EXPECT_FLOAT_EQ(x, event.GetX(0));
239 EXPECT_FLOAT_EQ(y, event.GetY(0));
240 EXPECT_FLOAT_EQ(raw_x, event.GetRawX(0));
241 EXPECT_FLOAT_EQ(raw_y, event.GetRawY(0));
244 TEST(MotionEventAuraTest, TapParams) {
245 // Test that touch params are stored correctly.
246 MotionEventAura event;
248 int ids[] = {15, 13, 25, 23};
250 float radius_x;
251 float radius_y;
252 float rotation_angle;
253 float pressure;
255 // Test case: radius_x > radius_y, rotation_angle < 90
256 radius_x = 123.45f;
257 radius_y = 67.89f;
258 rotation_angle = 23.f;
259 pressure = 0.123f;
260 TouchEvent press0 = TouchWithTapParams(
261 ET_TOUCH_PRESSED, ids[0], radius_x, radius_y, rotation_angle, pressure);
262 EXPECT_TRUE(event.OnTouch(press0));
264 EXPECT_EQ(1U, event.GetPointerCount());
265 EXPECT_FLOAT_EQ(radius_x, event.GetTouchMajor(0) / 2);
266 EXPECT_FLOAT_EQ(radius_y, event.GetTouchMinor(0) / 2);
267 EXPECT_FLOAT_EQ(rotation_angle, event.GetOrientation(0) * 180 / M_PI + 90);
268 EXPECT_FLOAT_EQ(pressure, event.GetPressure(0));
270 // Test case: radius_x < radius_y, rotation_angle < 90
271 radius_x = 67.89f;
272 radius_y = 123.45f;
273 rotation_angle = 46.f;
274 pressure = 0.456f;
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 // TODO(mustaq): Make a separate clone test, crbug.com/450655
287 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));
295 // TODO(mustaq): The move test seems out-of-scope here, crbug.com/450655
296 radius_x = 76.98f;
297 radius_y = 321.54f;
298 rotation_angle = 64.f;
299 pressure = 0.654f;
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));
311 // Test case: radius_x > radius_y, rotation_angle > 90
312 radius_x = 123.45f;
313 radius_y = 67.89f;
314 rotation_angle = 92.f;
315 pressure = 0.789f;
316 TouchEvent press2 = TouchWithTapParams(
317 ET_TOUCH_PRESSED, ids[2], radius_x, radius_y, rotation_angle, pressure);
318 EXPECT_TRUE(event.OnTouch(press2));
320 EXPECT_EQ(3U, event.GetPointerCount());
321 EXPECT_FLOAT_EQ(radius_x, event.GetTouchMajor(2) / 2);
322 EXPECT_FLOAT_EQ(radius_y, event.GetTouchMinor(2) / 2);
323 EXPECT_FLOAT_EQ(rotation_angle, event.GetOrientation(2) * 180 / M_PI + 90);
324 EXPECT_FLOAT_EQ(pressure, event.GetPressure(2));
326 // Test case: radius_x < radius_y, rotation_angle > 90
327 radius_x = 67.89f;
328 radius_y = 123.45f;
329 rotation_angle = 135.f;
330 pressure = 0.012f;
331 TouchEvent press3 = TouchWithTapParams(
332 ET_TOUCH_PRESSED, ids[3], radius_x, radius_y, rotation_angle, pressure);
333 EXPECT_TRUE(event.OnTouch(press3));
335 EXPECT_EQ(4U, event.GetPointerCount());
336 EXPECT_FLOAT_EQ(radius_y, event.GetTouchMajor(3) / 2);
337 EXPECT_FLOAT_EQ(radius_x, event.GetTouchMinor(3) / 2);
338 EXPECT_FLOAT_EQ(rotation_angle, event.GetOrientation(3) * 180 / M_PI + 180);
339 EXPECT_FLOAT_EQ(pressure, event.GetPressure(3));
342 TEST(MotionEventAuraTest, Timestamps) {
343 // Test that timestamp information is stored and converted correctly.
344 MotionEventAura event;
345 int ids[] = {7, 13};
346 int times_in_ms[] = {59436, 60263, 82175};
348 TouchEvent press0 = TouchWithTime(
349 ui::ET_TOUCH_PRESSED, ids[0], times_in_ms[0]);
350 EXPECT_TRUE(event.OnTouch(press0));
351 EXPECT_EQ(MsToTicks(times_in_ms[0]), event.GetEventTime());
353 TouchEvent press1 = TouchWithTime(
354 ui::ET_TOUCH_PRESSED, ids[1], times_in_ms[1]);
355 EXPECT_TRUE(event.OnTouch(press1));
356 EXPECT_EQ(MsToTicks(times_in_ms[1]), event.GetEventTime());
358 TouchEvent move0 = TouchWithTime(
359 ui::ET_TOUCH_MOVED, ids[0], times_in_ms[2]);
360 move0.set_location(gfx::PointF(12, 21));
361 EXPECT_TRUE(event.OnTouch(move0));
362 EXPECT_EQ(MsToTicks(times_in_ms[2]), event.GetEventTime());
364 // Test cloning of timestamp information.
365 scoped_ptr<MotionEvent> clone = event.Clone();
366 EXPECT_EQ(MsToTicks(times_in_ms[2]), clone->GetEventTime());
369 TEST(MotionEventAuraTest, CachedAction) {
370 // Test that the cached action and cached action index are correct.
371 int ids[] = {4, 6};
372 MotionEventAura event;
374 TouchEvent press0 = TouchWithType(ET_TOUCH_PRESSED, ids[0]);
375 EXPECT_TRUE(event.OnTouch(press0));
376 EXPECT_EQ(MotionEvent::ACTION_DOWN, event.GetAction());
377 EXPECT_EQ(1U, event.GetPointerCount());
379 TouchEvent press1 = TouchWithType(ET_TOUCH_PRESSED, ids[1]);
380 EXPECT_TRUE(event.OnTouch(press1));
381 EXPECT_EQ(MotionEvent::ACTION_POINTER_DOWN, event.GetAction());
382 EXPECT_EQ(1, event.GetActionIndex());
383 EXPECT_EQ(2U, event.GetPointerCount());
385 // Test cloning of CachedAction information.
386 scoped_ptr<MotionEvent> clone = event.Clone();
387 EXPECT_EQ(MotionEvent::ACTION_POINTER_DOWN, clone->GetAction());
388 EXPECT_EQ(1, clone->GetActionIndex());
390 TouchEvent move0 = TouchWithType(ET_TOUCH_MOVED, ids[0]);
391 move0.set_location(gfx::PointF(10, 12));
392 EXPECT_TRUE(event.OnTouch(move0));
393 EXPECT_EQ(MotionEvent::ACTION_MOVE, event.GetAction());
394 EXPECT_EQ(2U, event.GetPointerCount());
396 TouchEvent release0 = TouchWithType(ET_TOUCH_RELEASED, ids[0]);
397 EXPECT_TRUE(event.OnTouch(release0));
398 EXPECT_EQ(MotionEvent::ACTION_POINTER_UP, event.GetAction());
399 EXPECT_EQ(2U, event.GetPointerCount());
400 event.CleanupRemovedTouchPoints(release0);
401 EXPECT_EQ(1U, event.GetPointerCount());
403 TouchEvent release1 = TouchWithType(ET_TOUCH_RELEASED, ids[1]);
404 EXPECT_TRUE(event.OnTouch(release1));
405 EXPECT_EQ(MotionEvent::ACTION_UP, event.GetAction());
406 EXPECT_EQ(1U, event.GetPointerCount());
407 event.CleanupRemovedTouchPoints(release1);
408 EXPECT_EQ(0U, event.GetPointerCount());
411 TEST(MotionEventAuraTest, Cancel) {
412 int ids[] = {4, 6};
413 MotionEventAura event;
415 TouchEvent press0 = TouchWithType(ET_TOUCH_PRESSED, ids[0]);
416 EXPECT_TRUE(event.OnTouch(press0));
417 EXPECT_EQ(MotionEvent::ACTION_DOWN, event.GetAction());
418 EXPECT_EQ(1U, event.GetPointerCount());
420 TouchEvent press1 = TouchWithType(ET_TOUCH_PRESSED, ids[1]);
421 EXPECT_TRUE(event.OnTouch(press1));
422 EXPECT_EQ(MotionEvent::ACTION_POINTER_DOWN, event.GetAction());
423 EXPECT_EQ(1, event.GetActionIndex());
424 EXPECT_EQ(2U, event.GetPointerCount());
426 scoped_ptr<MotionEvent> cancel = event.Cancel();
427 EXPECT_EQ(MotionEvent::ACTION_CANCEL, cancel->GetAction());
428 EXPECT_EQ(2U, cancel->GetPointerCount());
431 TEST(MotionEventAuraTest, ToolType) {
432 MotionEventAura event;
434 // For now, all pointers have an unknown tool type.
435 // TODO(jdduke): Expand this test when ui::TouchEvent identifies the source
436 // touch type, crbug.com/404128.
437 EXPECT_TRUE(event.OnTouch(TouchWithType(ET_TOUCH_PRESSED, 7)));
438 ASSERT_EQ(1U, event.GetPointerCount());
439 EXPECT_EQ(MotionEvent::TOOL_TYPE_UNKNOWN, event.GetToolType(0));
442 TEST(MotionEventAuraTest, Flags) {
443 int ids[] = {7, 11};
444 MotionEventAura event;
446 TouchEvent press0 = TouchWithType(ET_TOUCH_PRESSED, ids[0]);
447 press0.set_flags(EF_CONTROL_DOWN);
448 EXPECT_TRUE(event.OnTouch(press0));
449 EXPECT_EQ(EF_CONTROL_DOWN, event.GetFlags());
451 TouchEvent press1 = TouchWithType(ET_TOUCH_PRESSED, ids[1]);
452 press1.set_flags(EF_CONTROL_DOWN | EF_CAPS_LOCK_DOWN);
453 EXPECT_TRUE(event.OnTouch(press1));
454 EXPECT_EQ(EF_CONTROL_DOWN | EF_CAPS_LOCK_DOWN, event.GetFlags());
457 // Once crbug.com/446852 is fixed, we should ignore redundant presses.
458 TEST(MotionEventAuraTest, DoesntIgnoreRedundantPresses) {
459 const int id = 7;
460 const int position_1 = 10;
461 const int position_2 = 23;
463 MotionEventAura event;
464 TouchEvent press1 = TouchWithPosition(ET_TOUCH_PRESSED, id, position_1,
465 position_1, position_1, position_1);
466 EXPECT_TRUE(event.OnTouch(press1));
467 TouchEvent press2 = TouchWithPosition(ET_TOUCH_PRESSED, id, position_2,
468 position_2, position_2, position_2);
469 EXPECT_TRUE(event.OnTouch(press2));
471 EXPECT_EQ(1U, event.GetPointerCount());
472 EXPECT_FLOAT_EQ(position_2, event.GetX(0));
475 TEST(MotionEventAuraTest, IgnoresEventsWithoutPress) {
476 int id = 7;
477 MotionEventAura event;
478 EXPECT_FALSE(event.OnTouch(TouchWithType(ET_TOUCH_MOVED, id)));
481 TEST(MotionEventAuraTest, IgnoresStationaryMoves) {
482 int id = 7;
483 MotionEventAura event;
484 EXPECT_TRUE(event.OnTouch(TouchWithType(ET_TOUCH_PRESSED, id)));
485 TouchEvent move0 = TouchWithPosition(ET_TOUCH_PRESSED, id, 10, 20, 10, 20);
486 EXPECT_TRUE(event.OnTouch(move0));
488 TouchEvent move1 = TouchWithPosition(ET_TOUCH_MOVED, id, 11, 21, 11, 21);
489 EXPECT_TRUE(event.OnTouch(move1));
490 EXPECT_FALSE(event.OnTouch(move1));
493 } // namespace ui