Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / ui / events / gesture_detection / motion_event_generic_unittest.cc
bloba67646b24f57a5f94a574cada63b75be91afc4d5
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"
10 namespace ui {
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 event.PushPointer(PointerProperties(8.3f, 4.7f, 0.9f));
21 ASSERT_EQ(2U, event.GetPointerCount());
22 EXPECT_EQ(8.3f, event.GetX(1));
23 EXPECT_EQ(4.7f, event.GetY(1));
25 event.PushPointer(PointerProperties(2.3f, -3.7f, 5.8f));
26 ASSERT_EQ(3U, event.GetPointerCount());
27 EXPECT_EQ(2.3f, event.GetX(2));
28 EXPECT_EQ(-3.7f, event.GetY(2));
30 event.set_id(1);
31 EXPECT_EQ(1, event.GetId());
33 event.set_action(MotionEvent::ACTION_POINTER_DOWN);
34 EXPECT_EQ(MotionEvent::ACTION_POINTER_DOWN, event.GetAction());
36 event_time += base::TimeDelta::FromMilliseconds(5);
37 event.set_event_time(event_time);
38 EXPECT_EQ(event_time, event.GetEventTime());
40 event.set_button_state(MotionEvent::BUTTON_PRIMARY);
41 EXPECT_EQ(MotionEvent::BUTTON_PRIMARY, event.GetButtonState());
43 event.set_flags(EF_ALT_DOWN | EF_SHIFT_DOWN);
44 EXPECT_EQ(EF_ALT_DOWN | EF_SHIFT_DOWN, event.GetFlags());
46 event.set_action_index(1);
47 EXPECT_EQ(1, event.GetActionIndex());
49 event.set_action(MotionEvent::ACTION_MOVE);
50 EXPECT_EQ(MotionEvent::ACTION_MOVE, event.GetAction());
52 PointerProperties historical_pointer0(1.2f, 2.4f, 1.f);
53 PointerProperties historical_pointer1(2.4f, 4.8f, 2.f);
54 PointerProperties historical_pointer2(4.8f, 9.6f, 3.f);
55 MotionEventGeneric historical_event(
56 MotionEvent::ACTION_MOVE,
57 event_time - base::TimeDelta::FromMilliseconds(5),
58 historical_pointer0);
59 historical_event.PushPointer(historical_pointer1);
60 historical_event.PushPointer(historical_pointer2);
62 event.PushHistoricalEvent(historical_event.Clone());
63 EXPECT_EQ(1U, event.GetHistorySize());
64 EXPECT_EQ(event_time - base::TimeDelta::FromMilliseconds(5),
65 event.GetHistoricalEventTime(0));
66 EXPECT_EQ(1.2f, event.GetHistoricalX(0, 0));
67 EXPECT_EQ(2.4f, event.GetHistoricalY(0, 0));
68 EXPECT_EQ(1.f, event.GetHistoricalTouchMajor(0, 0));
69 EXPECT_EQ(2.4f, event.GetHistoricalX(1, 0));
70 EXPECT_EQ(4.8f, event.GetHistoricalY(1, 0));
71 EXPECT_EQ(2.f, event.GetHistoricalTouchMajor(1, 0));
72 EXPECT_EQ(4.8f, event.GetHistoricalX(2, 0));
73 EXPECT_EQ(9.6f, event.GetHistoricalY(2, 0));
74 EXPECT_EQ(3.f, event.GetHistoricalTouchMajor(2, 0));
77 TEST(MotionEventGenericTest, Clone) {
78 MotionEventGeneric event(MotionEvent::ACTION_DOWN,
79 base::TimeTicks::Now(),
80 PointerProperties(8.3f, 4.7f, 2.f));
81 event.set_id(1);
82 event.set_button_state(MotionEvent::BUTTON_PRIMARY);
84 scoped_ptr<MotionEvent> clone = event.Clone();
85 ASSERT_TRUE(clone);
86 EXPECT_EQ(test::ToString(event), test::ToString(*clone));
89 TEST(MotionEventGenericTest, CloneWithHistory) {
90 base::TimeTicks event_time = base::TimeTicks::Now();
91 base::TimeTicks historical_event_time =
92 event_time - base::TimeDelta::FromMilliseconds(5);
94 PointerProperties pointer(8.3f, 4.7f, 10.1f);
95 MotionEventGeneric event(MotionEvent::ACTION_MOVE, event_time, pointer);
97 PointerProperties historical_pointer(3.4f, -4.3f, 11.5);
98 scoped_ptr<MotionEvent> historical_event(new MotionEventGeneric(
99 MotionEvent::ACTION_MOVE, historical_event_time, historical_pointer));
101 event.PushHistoricalEvent(historical_event.Pass());
102 EXPECT_EQ(1U, event.GetHistorySize());
104 scoped_ptr<MotionEvent> clone = event.Clone();
105 ASSERT_TRUE(clone);
106 EXPECT_EQ(test::ToString(event), test::ToString(*clone));
109 TEST(MotionEventGenericTest, Cancel) {
110 MotionEventGeneric event(MotionEvent::ACTION_UP,
111 base::TimeTicks::Now(),
112 PointerProperties(8.7f, 4.3f, 1.f));
113 event.set_id(2);
114 event.set_button_state(MotionEvent::BUTTON_SECONDARY);
116 scoped_ptr<MotionEvent> cancel = event.Cancel();
117 event.set_action(MotionEvent::ACTION_CANCEL);
118 ASSERT_TRUE(cancel);
119 EXPECT_EQ(test::ToString(event), test::ToString(*cancel));
122 TEST(MotionEventGenericTest, FindPointerIndexOfId) {
123 base::TimeTicks event_time = base::TimeTicks::Now();
124 PointerProperties pointer;
125 pointer.id = 0;
126 MotionEventGeneric event0(MotionEvent::ACTION_DOWN, event_time, pointer);
127 EXPECT_EQ(0, event0.FindPointerIndexOfId(0));
128 EXPECT_EQ(-1, event0.FindPointerIndexOfId(1));
129 EXPECT_EQ(-1, event0.FindPointerIndexOfId(-1));
131 MotionEventGeneric event1(event0);
132 pointer.id = 7;
133 EXPECT_EQ(1u, event1.PushPointer(pointer));
134 EXPECT_EQ(0, event1.FindPointerIndexOfId(0));
135 EXPECT_EQ(1, event1.FindPointerIndexOfId(7));
136 EXPECT_EQ(-1, event1.FindPointerIndexOfId(6));
137 EXPECT_EQ(-1, event1.FindPointerIndexOfId(1));
139 MotionEventGeneric event2(event1);
140 pointer.id = 3;
141 EXPECT_EQ(2u, event2.PushPointer(pointer));
142 EXPECT_EQ(0, event2.FindPointerIndexOfId(0));
143 EXPECT_EQ(1, event2.FindPointerIndexOfId(7));
144 EXPECT_EQ(2, event2.FindPointerIndexOfId(3));
145 EXPECT_EQ(-1, event2.FindPointerIndexOfId(1));
146 EXPECT_EQ(-1, event2.FindPointerIndexOfId(2));
149 TEST(MotionEventGenericTest, RemovePointerAt) {
150 base::TimeTicks event_time = base::TimeTicks::Now();
151 PointerProperties pointer;
152 pointer.id = 0;
153 MotionEventGeneric event(MotionEvent::ACTION_DOWN, event_time, pointer);
155 pointer.id = 7;
156 EXPECT_EQ(1u, event.PushPointer(pointer));
157 EXPECT_EQ(2u, event.GetPointerCount());
159 // Remove from the end.
160 event.RemovePointerAt(1);
161 EXPECT_EQ(1u, event.GetPointerCount());
162 EXPECT_EQ(-1, event.FindPointerIndexOfId(7));
163 EXPECT_EQ(0, event.FindPointerIndexOfId(0));
165 EXPECT_EQ(1u, event.PushPointer(pointer));
166 EXPECT_EQ(2u, event.GetPointerCount());
168 // Remove from the beginning.
169 event.RemovePointerAt(0);
170 EXPECT_EQ(1u, event.GetPointerCount());
171 EXPECT_EQ(0, event.FindPointerIndexOfId(7));
172 EXPECT_EQ(-1, event.FindPointerIndexOfId(0));
175 TEST(MotionEventGenericTest, ToString) {
176 base::TimeTicks event_time = base::TimeTicks::Now();
177 base::TimeTicks historical_event_time0 =
178 event_time - base::TimeDelta::FromMilliseconds(10);
179 base::TimeTicks historical_event_time1 =
180 event_time - base::TimeDelta::FromMilliseconds(5);
182 PointerProperties pointer0(1, 2, 3);
183 pointer0.id = 7;
184 pointer0.pressure = 10;
185 pointer0.touch_minor = 15;
186 pointer0.touch_major = 20;
187 pointer0.orientation = 1;
189 PointerProperties pointer1(4, 5, 6);
190 pointer1.id = 3;
191 pointer0.pressure = 25;
192 pointer0.touch_minor = 30;
193 pointer0.touch_major = 35;
194 pointer0.orientation = -1;
196 MotionEventGeneric event(MotionEvent::ACTION_MOVE, event_time, pointer0);
197 event.PushPointer(pointer1);
199 pointer0.x += 50;
200 pointer1.x -= 50;
201 scoped_ptr<MotionEventGeneric> historical_event0(new MotionEventGeneric(
202 MotionEvent::ACTION_MOVE, historical_event_time0, pointer0));
203 historical_event0->PushPointer(pointer1);
205 pointer0.x += 100;
206 pointer1.x -= 100;
207 scoped_ptr<MotionEventGeneric> historical_event1(new MotionEventGeneric(
208 MotionEvent::ACTION_MOVE, historical_event_time1, pointer0));
209 historical_event1->PushPointer(pointer1);
211 event.PushHistoricalEvent(historical_event0.Pass());
212 event.PushHistoricalEvent(historical_event1.Pass());
213 ASSERT_EQ(2U, event.GetHistorySize());
214 ASSERT_EQ(2U, event.GetPointerCount());
216 // Do a basic smoke exercise of event stringification to ensure things don't
217 // explode in the process.
218 std::string event_string = test::ToString(event);
219 EXPECT_FALSE(event_string.empty());
222 } // namespace ui