Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(13)

Side by Side Diff: ui/events/gesture_detection/motion_event_ui_unittest.cc

Issue 251543003: Unified Gesture Recognizer (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix windows linking Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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.
4
5 #include "testing/gtest/include/gtest/gtest.h"
6 #include "ui/events/event.h"
7 #include "ui/events/gesture_detection/motion_event_ui.h"
8
9 namespace {
10
11 ui::TouchEvent TouchWithType(ui::EventType type, int id) {
12 return ui::TouchEvent(type, gfx::PointF(0, 0), id,
13 base::TimeDelta::FromMilliseconds(0));
14 }
15
16 ui::TouchEvent TouchWithPosition(ui::EventType type, int id,
jdduke (slow) 2014/05/02 17:56:43 Nit, this probably needs a git cl format.
tdresser 2014/05/05 15:42:35 Done.
17 float x, float y, float r, float p) {
18 return ui::TouchEvent(type, gfx::PointF(x, y), 0, id,
19 base::TimeDelta::FromMilliseconds(0),
20 r, r, 0, p);
21 }
22
23 ui::TouchEvent TouchWithTime(ui::EventType type, int id, int ms) {
24 return ui::TouchEvent(type, gfx::PointF(0, 0), id,
25 base::TimeDelta::FromMilliseconds(ms));
26 }
27
28 base::TimeTicks MsToTicks(int ms) {
29 return base::TimeTicks() + base::TimeDelta::FromMilliseconds(ms);
30 }
31
32 } // namespace
33
34 namespace ui {
35
36 TEST(MotionEventUITest, PointerCountAndIds) {
37 // Test that |PointerCount()| returns the correct number of pointers, and ids
38 // are assigned correctly.
39 int ids[] = {4, 6, 1};
40
41 MotionEventUI event;
42 EXPECT_EQ(0U, event.GetPointerCount());
43
44 TouchEvent press0 = TouchWithType(ET_TOUCH_PRESSED, ids[0]);
45 event.OnTouch(press0);
46 EXPECT_EQ(1U, event.GetPointerCount());
47
48 EXPECT_EQ(ids[0], event.GetPointerId(0));
49
50 TouchEvent press1 = TouchWithType(ET_TOUCH_PRESSED, ids[1]);
51 event.OnTouch(press1);
52 EXPECT_EQ(2U, event.GetPointerCount());
53
54 EXPECT_EQ(ids[0], event.GetPointerId(0));
55 EXPECT_EQ(ids[1], event.GetPointerId(1));
56
57 TouchEvent press2 = TouchWithType(ET_TOUCH_PRESSED, ids[2]);
58 event.OnTouch(press2);
59 EXPECT_EQ(3U, event.GetPointerCount());
60
61 EXPECT_EQ(ids[0], event.GetPointerId(0));
62 EXPECT_EQ(ids[1], event.GetPointerId(1));
63 EXPECT_EQ(ids[2], event.GetPointerId(2));
64
65 TouchEvent release1 = TouchWithType(ET_TOUCH_RELEASED, ids[1]);
66 event.OnTouch(release1);
67 event.CleanupRemovedTouchPoints(release1);
68 EXPECT_EQ(2U, event.GetPointerCount());
69
70 EXPECT_EQ(ids[0], event.GetPointerId(0));
71 EXPECT_EQ(ids[2], event.GetPointerId(1));
72
73 // Test cloning of pointer count and id information.
74 scoped_ptr<MotionEvent> clone = event.Clone();
75 EXPECT_EQ(2U, clone->GetPointerCount());
76 EXPECT_EQ(ids[0], clone->GetPointerId(0));
77 EXPECT_EQ(ids[2], clone->GetPointerId(1));
78
79 TouchEvent release0 = TouchWithType(ET_TOUCH_RELEASED, ids[0]);
80 event.OnTouch(release0);
81 event.CleanupRemovedTouchPoints(release0);
82 EXPECT_EQ(1U, event.GetPointerCount());
83
84 EXPECT_EQ(ids[2], event.GetPointerId(0));
85
86 TouchEvent release2 = TouchWithType(ET_TOUCH_RELEASED, ids[2]);
87 event.OnTouch(release2);
88 event.CleanupRemovedTouchPoints(release2);
89 EXPECT_EQ(0U, event.GetPointerCount());
90 }
91
92 TEST(MotionEventUITest, GetActionIndexAfterRemoval) {
93 // Test that |GetActionIndex()| returns the correct index when points have
94 // been removed.
95 int ids[] = {4, 6, 9};
96
97 MotionEventUI event;
98 EXPECT_EQ(0U, event.GetPointerCount());
99
100 TouchEvent press0 = TouchWithType(ET_TOUCH_PRESSED, ids[0]);
101 event.OnTouch(press0);
102 TouchEvent press1 = TouchWithType(ET_TOUCH_PRESSED, ids[1]);
103 event.OnTouch(press1);
104 TouchEvent press2 = TouchWithType(ET_TOUCH_PRESSED, ids[2]);
105 event.OnTouch(press2);
106 EXPECT_EQ(3U, event.GetPointerCount());
107
108 TouchEvent release1 = TouchWithType(ET_TOUCH_RELEASED, ids[1]);
109 event.OnTouch(release1);
110 event.CleanupRemovedTouchPoints(release1);
111 EXPECT_EQ(2U, event.GetPointerCount());
112
113 TouchEvent release2 = TouchWithType(ET_TOUCH_RELEASED, ids[2]);
114 event.OnTouch(release2);
115 event.CleanupRemovedTouchPoints(release2);
116 EXPECT_EQ(1U, event.GetPointerCount());
117
118 TouchEvent release0 = TouchWithType(ET_TOUCH_RELEASED, ids[0]);
119 event.OnTouch(release0);
120 event.CleanupRemovedTouchPoints(release0);
121 EXPECT_EQ(0U, event.GetPointerCount());
122 }
123
124 TEST(MotionEventUITest, PointerLocations) {
125 // Test that location information is stored correctly.
126 MotionEventUI event;
127
128 int ids[] = {15, 13};
129 float x;
130 float y;
131 float r;
132 float p;
133
134 x = 14.4f;
135 y = 17.3f;
136 r = 25.7f;
137 p = 48.2f;
138 TouchEvent press0 = TouchWithPosition(ET_TOUCH_PRESSED, ids[0], x, y, r, p);
139 event.OnTouch(press0);
140
141 EXPECT_EQ(1U, event.GetPointerCount());
142 EXPECT_FLOAT_EQ(x, event.GetX(0));
143 EXPECT_FLOAT_EQ(y, event.GetY(0));
144 EXPECT_FLOAT_EQ(r, event.GetTouchMajor(0) / 2);
145 EXPECT_FLOAT_EQ(p, event.GetPressure(0));
146
147 x = 17.8f;
148 y = 12.1f;
149 r = 21.2f;
150 p = 18.4f;
151 TouchEvent press1 = TouchWithPosition(ET_TOUCH_PRESSED, ids[1], x, y, r, p);
152 event.OnTouch(press1);
153
154 EXPECT_EQ(2U, event.GetPointerCount());
155 EXPECT_FLOAT_EQ(x, event.GetX(1));
156 EXPECT_FLOAT_EQ(y, event.GetY(1));
157 EXPECT_FLOAT_EQ(r, event.GetTouchMajor(1) / 2);
158 EXPECT_FLOAT_EQ(p, event.GetPressure(1));
159
160 // Test cloning of pointer location information.
161 scoped_ptr<MotionEvent> clone = event.Clone();
162 EXPECT_EQ(2U, clone->GetPointerCount());
163 EXPECT_FLOAT_EQ(x, clone->GetX(1));
164 EXPECT_FLOAT_EQ(y, clone->GetY(1));
165 EXPECT_FLOAT_EQ(r, clone->GetTouchMajor(1) / 2);
166 EXPECT_FLOAT_EQ(p, clone->GetPressure(1));
167
168 x = 27.9f;
169 y = 22.3f;
170 r = 7.6f;
171 p = 82.1f;
172 TouchEvent move1 = TouchWithPosition(ET_TOUCH_MOVED, ids[1], x, y, r, p);
173 event.OnTouch(move1);
174
175 EXPECT_FLOAT_EQ(x, event.GetX(1));
176 EXPECT_FLOAT_EQ(y, event.GetY(1));
177 EXPECT_FLOAT_EQ(r, event.GetTouchMajor(1) / 2);
178 EXPECT_FLOAT_EQ(p, event.GetPressure(1));
179
180
181 x = 34.6f;
182 y = 23.8f;
183 r = 12.9f;
184 p = 14.2f;
185 TouchEvent move0 = TouchWithPosition(ET_TOUCH_MOVED, ids[0], x, y, r, p);
186 event.OnTouch(move0);
187
188 EXPECT_FLOAT_EQ(x, event.GetX(0));
189 EXPECT_FLOAT_EQ(y, event.GetY(0));
190 EXPECT_FLOAT_EQ(r, event.GetTouchMajor(0) / 2);
191 EXPECT_FLOAT_EQ(p, event.GetPressure(0));
192 }
193
194 TEST(MotionEventUITest, Timestamps) {
195 // Test that timestamp information is stored and converted correctly.
196 MotionEventUI event;
197 int ids[] = {7, 13};
198 int times_in_ms[] = {59436, 60263, 82175};
199
200 TouchEvent press0 = TouchWithTime(
201 ui::ET_TOUCH_PRESSED, ids[0], times_in_ms[0]);
202 event.OnTouch(press0);
203 EXPECT_EQ(MsToTicks(times_in_ms[0]), event.GetEventTime());
204
205 TouchEvent press1 = TouchWithTime(
206 ui::ET_TOUCH_PRESSED, ids[1], times_in_ms[1]);
207 event.OnTouch(press1);
208 EXPECT_EQ(MsToTicks(times_in_ms[1]), event.GetEventTime());
209
210 TouchEvent move0 = TouchWithTime(
211 ui::ET_TOUCH_MOVED, ids[0], times_in_ms[2]);
212 event.OnTouch(move0);
213 EXPECT_EQ(MsToTicks(times_in_ms[2]), event.GetEventTime());
214
215 // Test cloning of timestamp information.
216 scoped_ptr<MotionEvent> clone = event.Clone();
217 EXPECT_EQ(MsToTicks(times_in_ms[2]), clone->GetEventTime());
218 }
219
220 TEST(MotionEventUITest, CachedAction) {
221 // Test that the cached action and cached action index are correct.
222 int ids[] = {4, 6};
223 MotionEventUI event;
224
225 TouchEvent press0 = TouchWithType(ET_TOUCH_PRESSED, ids[0]);
226 event.OnTouch(press0);
227 EXPECT_EQ(MotionEvent::ACTION_DOWN, event.GetAction());
228 EXPECT_EQ(1U, event.GetPointerCount());
229
230 TouchEvent press1 = TouchWithType(ET_TOUCH_PRESSED, ids[1]);
231 event.OnTouch(press1);
232 EXPECT_EQ(MotionEvent::ACTION_POINTER_DOWN, event.GetAction());
233 EXPECT_EQ(1, event.GetActionIndex());
234 EXPECT_EQ(2U, event.GetPointerCount());
235
236 // Test cloning of CachedAction information.
237 scoped_ptr<MotionEvent> clone = event.Clone();
238 EXPECT_EQ(MotionEvent::ACTION_POINTER_DOWN, clone->GetAction());
239 EXPECT_EQ(1, clone->GetActionIndex());
240
241 TouchEvent move0 = TouchWithType(ET_TOUCH_MOVED, ids[0]);
242 event.OnTouch(move0);
243 EXPECT_EQ(MotionEvent::ACTION_MOVE, event.GetAction());
244 EXPECT_EQ(2U, event.GetPointerCount());
245
246 TouchEvent release0 = TouchWithType(ET_TOUCH_RELEASED, ids[0]);
247 event.OnTouch(release0);
248 EXPECT_EQ(MotionEvent::ACTION_POINTER_UP, event.GetAction());
249 EXPECT_EQ(2U, event.GetPointerCount());
250 event.CleanupRemovedTouchPoints(release0);
251 EXPECT_EQ(1U, event.GetPointerCount());
252
253 TouchEvent release1 = TouchWithType(ET_TOUCH_RELEASED, ids[1]);
254 event.OnTouch(release1);
255 EXPECT_EQ(MotionEvent::ACTION_UP, event.GetAction());
256 EXPECT_EQ(1U, event.GetPointerCount());
257 event.CleanupRemovedTouchPoints(release1);
258 EXPECT_EQ(0U, event.GetPointerCount());
259 }
260
261 TEST(MotionEventUITest, Cancel) {
262 int ids[] = {4, 6};
263 MotionEventUI event;
264
265 TouchEvent press0 = TouchWithType(ET_TOUCH_PRESSED, ids[0]);
266 event.OnTouch(press0);
267 EXPECT_EQ(MotionEvent::ACTION_DOWN, event.GetAction());
268 EXPECT_EQ(1U, event.GetPointerCount());
269
270 TouchEvent press1 = TouchWithType(ET_TOUCH_PRESSED, ids[1]);
271 event.OnTouch(press1);
272 EXPECT_EQ(MotionEvent::ACTION_POINTER_DOWN, event.GetAction());
273 EXPECT_EQ(1, event.GetActionIndex());
274 EXPECT_EQ(2U, event.GetPointerCount());
275
276 scoped_ptr<MotionEvent> clone = event.Cancel();
277 EXPECT_EQ(MotionEvent::ACTION_CANCEL, clone->GetAction());
278 EXPECT_EQ(2U, static_cast<MotionEventUI*>(clone.get())->GetPointerCount());
279 }
280
281 } // namespace ui
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698