Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / ui / events / gesture_detection / motion_event_generic_unittest.cc
blob2e0294ffb0e7684f41b26ed7df6d500c87ebfe30
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_constants.h"
12 #include "ui/events/gesture_detection/motion_event_generic.h"
13 #include "ui/events/test/motion_event_test_utils.h"
15 namespace ui {
17 TEST(MotionEventGenericTest, Basic) {
18 base::TimeTicks event_time = base::TimeTicks::Now();
19 MotionEventGeneric event(
20 MotionEvent::ACTION_DOWN, event_time, PointerProperties());
21 EXPECT_EQ(1U, event.GetPointerCount());
22 EXPECT_EQ(0U, event.GetHistorySize());
23 EXPECT_EQ(event_time, event.GetEventTime());
25 DCHECK_NE(0U, event.GetUniqueEventId());
26 event.set_unique_event_id(123456U);
27 EXPECT_EQ(123456U, event.GetUniqueEventId());
29 event.PushPointer(PointerProperties(8.3f, 4.7f, 0.9f));
30 ASSERT_EQ(2U, event.GetPointerCount());
31 EXPECT_EQ(8.3f, event.GetX(1));
32 EXPECT_EQ(4.7f, event.GetY(1));
34 event.PushPointer(PointerProperties(2.3f, -3.7f, 5.8f));
35 ASSERT_EQ(3U, event.GetPointerCount());
36 EXPECT_EQ(2.3f, event.GetX(2));
37 EXPECT_EQ(-3.7f, event.GetY(2));
39 event.pointer(0).id = 3;
40 EXPECT_EQ(3, event.GetPointerId(0));
42 event.set_action(MotionEvent::ACTION_POINTER_DOWN);
43 EXPECT_EQ(MotionEvent::ACTION_POINTER_DOWN, event.GetAction());
45 event_time += base::TimeDelta::FromMilliseconds(5);
46 event.set_event_time(event_time);
47 EXPECT_EQ(event_time, event.GetEventTime());
49 event.set_button_state(MotionEvent::BUTTON_PRIMARY);
50 EXPECT_EQ(MotionEvent::BUTTON_PRIMARY, event.GetButtonState());
52 event.set_flags(EF_ALT_DOWN | EF_SHIFT_DOWN);
53 EXPECT_EQ(EF_ALT_DOWN | EF_SHIFT_DOWN, event.GetFlags());
55 event.set_action_index(1);
56 EXPECT_EQ(1, event.GetActionIndex());
58 event.set_action(MotionEvent::ACTION_MOVE);
59 EXPECT_EQ(MotionEvent::ACTION_MOVE, event.GetAction());
61 PointerProperties historical_pointer0(1.2f, 2.4f, 1.f);
62 PointerProperties historical_pointer1(2.4f, 4.8f, 2.f);
63 PointerProperties historical_pointer2(4.8f, 9.6f, 3.f);
64 MotionEventGeneric historical_event(
65 MotionEvent::ACTION_MOVE,
66 event_time - base::TimeDelta::FromMilliseconds(5),
67 historical_pointer0);
68 historical_event.PushPointer(historical_pointer1);
69 historical_event.PushPointer(historical_pointer2);
71 event.PushHistoricalEvent(historical_event.Clone());
72 EXPECT_EQ(1U, event.GetHistorySize());
73 EXPECT_EQ(event_time - base::TimeDelta::FromMilliseconds(5),
74 event.GetHistoricalEventTime(0));
75 EXPECT_EQ(1.2f, event.GetHistoricalX(0, 0));
76 EXPECT_EQ(2.4f, event.GetHistoricalY(0, 0));
77 EXPECT_EQ(1.f, event.GetHistoricalTouchMajor(0, 0));
78 EXPECT_EQ(2.4f, event.GetHistoricalX(1, 0));
79 EXPECT_EQ(4.8f, event.GetHistoricalY(1, 0));
80 EXPECT_EQ(2.f, event.GetHistoricalTouchMajor(1, 0));
81 EXPECT_EQ(4.8f, event.GetHistoricalX(2, 0));
82 EXPECT_EQ(9.6f, event.GetHistoricalY(2, 0));
83 EXPECT_EQ(3.f, event.GetHistoricalTouchMajor(2, 0));
86 TEST(MotionEventGenericTest, Clone) {
87 MotionEventGeneric event(MotionEvent::ACTION_DOWN,
88 base::TimeTicks::Now(),
89 PointerProperties(8.3f, 4.7f, 2.f));
90 event.set_button_state(MotionEvent::BUTTON_PRIMARY);
92 scoped_ptr<MotionEvent> clone = event.Clone();
93 ASSERT_TRUE(clone);
94 EXPECT_EQ(event.GetUniqueEventId(), clone->GetUniqueEventId());
95 EXPECT_EQ(test::ToString(event), test::ToString(*clone));
98 TEST(MotionEventGenericTest, CloneWithHistory) {
99 base::TimeTicks event_time = base::TimeTicks::Now();
100 base::TimeTicks historical_event_time =
101 event_time - base::TimeDelta::FromMilliseconds(5);
103 PointerProperties pointer(8.3f, 4.7f, 10.1f);
104 MotionEventGeneric event(MotionEvent::ACTION_MOVE, event_time, pointer);
106 PointerProperties historical_pointer(3.4f, -4.3f, 11.5);
107 scoped_ptr<MotionEvent> historical_event(new MotionEventGeneric(
108 MotionEvent::ACTION_MOVE, historical_event_time, historical_pointer));
110 event.PushHistoricalEvent(historical_event.Pass());
111 EXPECT_EQ(1U, event.GetHistorySize());
113 scoped_ptr<MotionEvent> clone = event.Clone();
114 ASSERT_TRUE(clone);
115 EXPECT_EQ(event.GetUniqueEventId(), clone->GetUniqueEventId());
116 EXPECT_EQ(test::ToString(event), test::ToString(*clone));
119 TEST(MotionEventGenericTest, Cancel) {
120 MotionEventGeneric event(MotionEvent::ACTION_UP,
121 base::TimeTicks::Now(),
122 PointerProperties(8.7f, 4.3f, 1.f));
123 event.set_button_state(MotionEvent::BUTTON_SECONDARY);
125 scoped_ptr<MotionEvent> cancel = event.Cancel();
126 event.set_action(MotionEvent::ACTION_CANCEL);
127 ASSERT_TRUE(cancel);
128 EXPECT_NE(event.GetUniqueEventId(), cancel->GetUniqueEventId());
129 EXPECT_EQ(test::ToString(event), test::ToString(*cancel));
132 TEST(MotionEventGenericTest, FindPointerIndexOfId) {
133 base::TimeTicks event_time = base::TimeTicks::Now();
134 PointerProperties pointer;
135 pointer.id = 0;
136 MotionEventGeneric event0(MotionEvent::ACTION_DOWN, event_time, pointer);
137 EXPECT_EQ(0, event0.FindPointerIndexOfId(0));
138 EXPECT_EQ(-1, event0.FindPointerIndexOfId(1));
139 EXPECT_EQ(-1, event0.FindPointerIndexOfId(-1));
141 MotionEventGeneric event1(event0);
142 pointer.id = 7;
143 EXPECT_EQ(1u, event1.PushPointer(pointer));
144 EXPECT_EQ(0, event1.FindPointerIndexOfId(0));
145 EXPECT_EQ(1, event1.FindPointerIndexOfId(7));
146 EXPECT_EQ(-1, event1.FindPointerIndexOfId(6));
147 EXPECT_EQ(-1, event1.FindPointerIndexOfId(1));
149 MotionEventGeneric event2(event1);
150 pointer.id = 3;
151 EXPECT_EQ(2u, event2.PushPointer(pointer));
152 EXPECT_EQ(0, event2.FindPointerIndexOfId(0));
153 EXPECT_EQ(1, event2.FindPointerIndexOfId(7));
154 EXPECT_EQ(2, event2.FindPointerIndexOfId(3));
155 EXPECT_EQ(-1, event2.FindPointerIndexOfId(1));
156 EXPECT_EQ(-1, event2.FindPointerIndexOfId(2));
159 TEST(MotionEventGenericTest, RemovePointerAt) {
160 base::TimeTicks event_time = base::TimeTicks::Now();
161 PointerProperties pointer;
162 pointer.id = 0;
163 MotionEventGeneric event(MotionEvent::ACTION_DOWN, event_time, pointer);
165 pointer.id = 7;
166 EXPECT_EQ(1u, event.PushPointer(pointer));
167 EXPECT_EQ(2u, event.GetPointerCount());
169 // Remove from the end.
170 event.RemovePointerAt(1);
171 EXPECT_EQ(1u, event.GetPointerCount());
172 EXPECT_EQ(-1, event.FindPointerIndexOfId(7));
173 EXPECT_EQ(0, event.FindPointerIndexOfId(0));
175 EXPECT_EQ(1u, event.PushPointer(pointer));
176 EXPECT_EQ(2u, event.GetPointerCount());
178 // Remove from the beginning.
179 event.RemovePointerAt(0);
180 EXPECT_EQ(1u, event.GetPointerCount());
181 EXPECT_EQ(0, event.FindPointerIndexOfId(7));
182 EXPECT_EQ(-1, event.FindPointerIndexOfId(0));
185 TEST(MotionEventGenericTest, AxisAndOrientation) {
187 PointerProperties properties;
188 float radius_x = 10;
189 float radius_y = 5;
190 float rotation_angle_deg = 0;
191 properties.SetAxesAndOrientation(radius_x, radius_y, rotation_angle_deg);
192 EXPECT_EQ(20, properties.touch_major);
193 EXPECT_EQ(10, properties.touch_minor);
194 EXPECT_NEAR(-M_PI_2, properties.orientation, 0.001);
197 PointerProperties properties;
198 float radius_x = 5;
199 float radius_y = 10;
200 float rotation_angle_deg = 0;
201 properties.SetAxesAndOrientation(radius_x, radius_y, rotation_angle_deg);
202 EXPECT_EQ(20, properties.touch_major);
203 EXPECT_EQ(10, properties.touch_minor);
204 EXPECT_NEAR(0, properties.orientation, 0.001);
207 PointerProperties properties;
208 float radius_x = 10;
209 float radius_y = 5;
210 float rotation_angle_deg = 179.99f;
211 properties.SetAxesAndOrientation(radius_x, radius_y, rotation_angle_deg);
212 EXPECT_EQ(20, properties.touch_major);
213 EXPECT_EQ(10, properties.touch_minor);
214 EXPECT_NEAR(M_PI_2, properties.orientation, 0.001);
217 PointerProperties properties;
218 float radius_x = 10;
219 float radius_y = 5;
220 float rotation_angle_deg = 90;
221 properties.SetAxesAndOrientation(radius_x, radius_y, rotation_angle_deg);
222 EXPECT_EQ(20, properties.touch_major);
223 EXPECT_EQ(10, properties.touch_minor);
224 EXPECT_NEAR(0, properties.orientation, 0.001);
228 TEST(MotionEventGenericTest, ToString) {
229 base::TimeTicks event_time = base::TimeTicks::Now();
230 base::TimeTicks historical_event_time0 =
231 event_time - base::TimeDelta::FromMilliseconds(10);
232 base::TimeTicks historical_event_time1 =
233 event_time - base::TimeDelta::FromMilliseconds(5);
235 PointerProperties pointer0(1, 2, 3);
236 pointer0.id = 7;
237 pointer0.pressure = 10;
238 pointer0.touch_minor = 15;
239 pointer0.touch_major = 20;
240 pointer0.orientation = 1;
242 PointerProperties pointer1(4, 5, 6);
243 pointer1.id = 3;
244 pointer0.pressure = 25;
245 pointer0.touch_minor = 30;
246 pointer0.touch_major = 35;
247 pointer0.orientation = -1;
249 MotionEventGeneric event(MotionEvent::ACTION_MOVE, event_time, pointer0);
250 event.PushPointer(pointer1);
252 pointer0.x += 50;
253 pointer1.x -= 50;
254 scoped_ptr<MotionEventGeneric> historical_event0(new MotionEventGeneric(
255 MotionEvent::ACTION_MOVE, historical_event_time0, pointer0));
256 historical_event0->PushPointer(pointer1);
258 pointer0.x += 100;
259 pointer1.x -= 100;
260 scoped_ptr<MotionEventGeneric> historical_event1(new MotionEventGeneric(
261 MotionEvent::ACTION_MOVE, historical_event_time1, pointer0));
262 historical_event1->PushPointer(pointer1);
264 event.PushHistoricalEvent(historical_event0.Pass());
265 event.PushHistoricalEvent(historical_event1.Pass());
266 ASSERT_EQ(2U, event.GetHistorySize());
267 ASSERT_EQ(2U, event.GetPointerCount());
269 // Do a basic smoke exercise of event stringification to ensure things don't
270 // explode in the process.
271 std::string event_string = test::ToString(event);
272 EXPECT_FALSE(event_string.empty());
275 } // namespace ui