blob: beece1ea9cef2e12d98679e7ad23b75143bee406 [file] [log] [blame]
Scott Su743b7ad2009-04-22 01:09:28 -07001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.view.cts;
18
19import dalvik.annotation.TestLevel;
20import dalvik.annotation.TestTargetClass;
21import dalvik.annotation.TestTargetNew;
22import dalvik.annotation.TestTargets;
23import dalvik.annotation.ToBeFixed;
24
25import android.os.Parcel;
26import android.os.Parcelable;
27import android.os.SystemClock;
28import android.test.AndroidTestCase;
29import android.view.KeyEvent;
30import android.view.MotionEvent;
31
32/**
33 * Test {@link MotionEvent}.
34 */
35@TestTargetClass(MotionEvent.class)
36public class MotionEventTest extends AndroidTestCase {
37 private MotionEvent mMotionEvent1;
38 private MotionEvent mMotionEvent2;
39 private long mDownTime;
40 private long mEventTime;
41 private static final float X_3F = 3.0f;
42 private static final float Y_4F = 4.0f;
43 private static final int META_STATE = KeyEvent.META_SHIFT_ON;
44 private static final float PRESSURE_1F = 1.0f;
45 private static final float SIZE_1F = 1.0f;
46 private static final float X_PRECISION_3F = 3.0f;
47 private static final float Y_PRECISION_4F = 4.0f;
48 private static final int DEVICE_ID_1 = 1;
49 private static final int EDGE_FLAGS = MotionEvent.EDGE_TOP;
50 private static final float DELTA = 0.01f;
51
52 @Override
53 protected void setUp() throws Exception {
54 super.setUp();
55
56 mDownTime = SystemClock.uptimeMillis();
57 mEventTime = SystemClock.uptimeMillis();
58 mMotionEvent1 = MotionEvent.obtain(mDownTime, mEventTime,
59 MotionEvent.ACTION_MOVE, X_3F, Y_4F, META_STATE);
60 mMotionEvent2 = MotionEvent.obtain(mDownTime, mEventTime,
61 MotionEvent.ACTION_MOVE, X_3F, Y_4F, PRESSURE_1F, SIZE_1F, META_STATE,
62 X_PRECISION_3F, Y_PRECISION_4F, DEVICE_ID_1, EDGE_FLAGS);
63 }
64
65 @Override
66 protected void tearDown() throws Exception {
67 if (null != mMotionEvent1) {
68 mMotionEvent1.recycle();
69 }
70 if (null != mMotionEvent2) {
71 mMotionEvent2.recycle();
72 }
73 super.tearDown();
74 }
75
76 @TestTargets({
77 @TestTargetNew(
78 level = TestLevel.COMPLETE,
79 method = "obtain",
80 args = {long.class, long.class, int.class, float.class, float.class, int.class}
81 ),
82 @TestTargetNew(
83 level = TestLevel.COMPLETE,
84 method = "getX",
85 args = {}
86 ),
87 @TestTargetNew(
88 level = TestLevel.COMPLETE,
89 method = "getDownTime",
90 args = {}
91 ),
92 @TestTargetNew(
93 level = TestLevel.COMPLETE,
94 method = "getEventTime",
95 args = {}
96 ),
97 @TestTargetNew(
98 level = TestLevel.COMPLETE,
99 method = "getX",
100 args = {}
101 ),
102 @TestTargetNew(
103 level = TestLevel.COMPLETE,
104 method = "getY",
105 args = {}
106 ),
107 @TestTargetNew(
108 level = TestLevel.COMPLETE,
109 method = "getRawX",
110 args = {}
111 ),
112 @TestTargetNew(
113 level = TestLevel.COMPLETE,
114 method = "getRawY",
115 args = {}
116 ),
117 @TestTargetNew(
118 level = TestLevel.COMPLETE,
119 method = "getMetaState",
120 args = {}
121 ),
122 @TestTargetNew(
123 level = TestLevel.COMPLETE,
124 method = "getDeviceId",
125 args = {}
126 ),
127 @TestTargetNew(
128 level = TestLevel.COMPLETE,
129 method = "getPressure",
130 args = {}
131 ),
132 @TestTargetNew(
133 level = TestLevel.COMPLETE,
134 method = "getSize",
135 args = {}
136 ),
137 @TestTargetNew(
138 level = TestLevel.COMPLETE,
139 method = "getXPrecision",
140 args = {}
141 ),
142 @TestTargetNew(
143 level = TestLevel.COMPLETE,
144 method = "getYPrecision",
145 args = {}
146 )
147 })
148 public void testObtain1() {
149 mMotionEvent1 = MotionEvent.obtain(mDownTime, mEventTime,
150 MotionEvent.ACTION_DOWN, X_3F, Y_4F, META_STATE);
151 assertNotNull(mMotionEvent1);
152 assertEquals(mDownTime, mMotionEvent1.getDownTime());
153 assertEquals(mEventTime, mMotionEvent1.getEventTime());
154 assertEquals(MotionEvent.ACTION_DOWN, mMotionEvent1.getAction());
155 assertEquals(X_3F, mMotionEvent1.getX(), DELTA);
156 assertEquals(Y_4F, mMotionEvent1.getY(), DELTA);
157 assertEquals(X_3F, mMotionEvent1.getRawX(), DELTA);
158 assertEquals(Y_4F, mMotionEvent1.getRawY(), DELTA);
159 assertEquals(META_STATE, mMotionEvent1.getMetaState());
160 assertEquals(0, mMotionEvent1.getDeviceId());
161 assertEquals(0, mMotionEvent1.getEdgeFlags());
162 assertEquals(PRESSURE_1F, mMotionEvent1.getPressure(), DELTA);
163 assertEquals(SIZE_1F, mMotionEvent1.getSize(), DELTA);
164 assertEquals(1.0f, mMotionEvent1.getXPrecision(), DELTA);
165 assertEquals(1.0f, mMotionEvent1.getYPrecision(), DELTA);
166 }
167
168 @TestTargetNew(
169 level = TestLevel.COMPLETE,
170 method = "obtain",
171 args = {MotionEvent.class}
172 )
173 public void testObtain2() {
174 MotionEvent motionEvent = MotionEvent.obtain(mDownTime, mEventTime,
175 MotionEvent.ACTION_DOWN, X_3F, Y_4F, META_STATE);
176 mMotionEvent1 = MotionEvent.obtain(motionEvent);
177 assertNotNull(mMotionEvent1);
178 assertEquals(motionEvent.getDownTime(), mMotionEvent1.getDownTime());
179 assertEquals(motionEvent.getEventTime(), mMotionEvent1.getEventTime());
180 assertEquals(motionEvent.getAction(), mMotionEvent1.getAction());
181 assertEquals(motionEvent.getX(), mMotionEvent1.getX(), DELTA);
182 assertEquals(motionEvent.getY(), mMotionEvent1.getY(), DELTA);
183 assertEquals(motionEvent.getX(), mMotionEvent1.getRawX(), DELTA);
184 assertEquals(motionEvent.getY(), mMotionEvent1.getRawY(), DELTA);
185 assertEquals(motionEvent.getMetaState(), mMotionEvent1.getMetaState());
186 assertEquals(motionEvent.getDeviceId(), mMotionEvent1.getDeviceId());
187 assertEquals(motionEvent.getEdgeFlags(), mMotionEvent1.getEdgeFlags());
188 assertEquals(motionEvent.getPressure(), mMotionEvent1.getPressure(), DELTA);
189 assertEquals(motionEvent.getSize(), mMotionEvent1.getSize(), DELTA);
190 assertEquals(motionEvent.getXPrecision(), mMotionEvent1.getXPrecision(), DELTA);
191 assertEquals(motionEvent.getYPrecision(), mMotionEvent1.getYPrecision(), DELTA);
192 }
193
194 @TestTargetNew(
195 level = TestLevel.COMPLETE,
196 method = "obtain",
197 args = {long.class, long.class, int.class, float.class, float.class, float.class,
198 float.class, int.class, float.class, float.class, int.class, int.class}
199 )
200 public void testObtain3() {
201 mMotionEvent1 = null;
202 mMotionEvent1 = MotionEvent.obtain(mDownTime, mEventTime,
203 MotionEvent.ACTION_DOWN, X_3F, Y_4F, PRESSURE_1F, SIZE_1F, META_STATE,
204 X_PRECISION_3F, Y_PRECISION_4F, DEVICE_ID_1, EDGE_FLAGS);
205 assertNotNull(mMotionEvent1);
206 assertEquals(mDownTime, mMotionEvent1.getDownTime());
207 assertEquals(mEventTime, mMotionEvent1.getEventTime());
208 assertEquals(MotionEvent.ACTION_DOWN, mMotionEvent1.getAction());
209 assertEquals(X_3F, mMotionEvent1.getX(), DELTA);
210 assertEquals(Y_4F, mMotionEvent1.getY(), DELTA);
211 assertEquals(X_3F, mMotionEvent1.getRawX(), DELTA);
212 assertEquals(Y_4F, mMotionEvent1.getRawY(), DELTA);
213 assertEquals(META_STATE, mMotionEvent1.getMetaState());
214 assertEquals(DEVICE_ID_1, mMotionEvent1.getDeviceId());
215 assertEquals(EDGE_FLAGS, mMotionEvent1.getEdgeFlags());
216 assertEquals(PRESSURE_1F, mMotionEvent1.getPressure(), DELTA);
217 assertEquals(SIZE_1F, mMotionEvent1.getSize(), DELTA);
218 assertEquals(X_PRECISION_3F, mMotionEvent1.getXPrecision(), DELTA);
219 assertEquals(Y_PRECISION_4F, mMotionEvent1.getYPrecision(), DELTA);
220 }
221
222 @TestTargets({
223 @TestTargetNew(
224 level = TestLevel.COMPLETE,
225 method = "getAction",
226 args = {}
227 ),
228 @TestTargetNew(
229 level = TestLevel.COMPLETE,
230 method = "setAction",
231 args = {int.class}
232 )
233 })
234 public void testAccessAction() {
235 assertEquals(MotionEvent.ACTION_MOVE, mMotionEvent1.getAction());
236
237 mMotionEvent1.setAction(MotionEvent.ACTION_UP);
238 assertEquals(MotionEvent.ACTION_UP, mMotionEvent1.getAction());
239
240 mMotionEvent1.setAction(MotionEvent.ACTION_MOVE);
241 assertEquals(MotionEvent.ACTION_MOVE, mMotionEvent1.getAction());
242
243 mMotionEvent1.setAction(MotionEvent.ACTION_CANCEL);
244 assertEquals(MotionEvent.ACTION_CANCEL, mMotionEvent1.getAction());
245
246 mMotionEvent1.setAction(MotionEvent.ACTION_DOWN);
247 assertEquals(MotionEvent.ACTION_DOWN, mMotionEvent1.getAction());
248 }
249
250 @TestTargetNew(
251 level = TestLevel.COMPLETE,
252 method = "describeContents",
253 args = {}
254 )
255 @ToBeFixed(bug = "1695243", explanation = "Android API javadocs are incomplete")
256 public void testDescribeContents() {
257 // make sure this method never throw any exception.
258 mMotionEvent2.describeContents();
259 }
260
261 @TestTargets({
262 @TestTargetNew(
263 level = TestLevel.COMPLETE,
264 method = "getEdgeFlags",
265 args = {}
266 ),
267 @TestTargetNew(
268 level = TestLevel.COMPLETE,
269 method = "setEdgeFlags",
270 args = {int.class}
271 )
272 })
273 public void testAccessEdgeFlags() {
274 assertEquals(EDGE_FLAGS, mMotionEvent2.getEdgeFlags());
275
276 int edgeFlags = 10;
277 mMotionEvent2.setEdgeFlags(edgeFlags);
278 assertEquals(edgeFlags, mMotionEvent2.getEdgeFlags());
279 }
280
281 @TestTargetNew(
282 level = TestLevel.COMPLETE,
283 method = "writeToParcel",
284 args = {Parcel.class, int.class}
285 )
286 public void testWriteToParcel() {
287 Parcel parcel = Parcel.obtain();
288 mMotionEvent2.writeToParcel(parcel, Parcelable.PARCELABLE_WRITE_RETURN_VALUE);
289 parcel.setDataPosition(0);
290
291 MotionEvent motionEvent = MotionEvent.CREATOR.createFromParcel(parcel);
292 assertEquals(mMotionEvent2.getRawY(), motionEvent.getRawY(), DELTA);
293 assertEquals(mMotionEvent2.getRawX(), motionEvent.getRawX(), DELTA);
294 assertEquals(mMotionEvent2.getY(), motionEvent.getY(), DELTA);
295 assertEquals(mMotionEvent2.getX(), motionEvent.getX(), DELTA);
296 assertEquals(mMotionEvent2.getAction(), motionEvent.getAction());
297 assertEquals(mMotionEvent2.getDownTime(), motionEvent.getDownTime());
298 assertEquals(mMotionEvent2.getEventTime(), motionEvent.getEventTime());
299 assertEquals(mMotionEvent2.getEdgeFlags(), motionEvent.getEdgeFlags());
300 assertEquals(mMotionEvent2.getDeviceId(), motionEvent.getDeviceId());
301 }
302
303 @TestTargetNew(
304 level = TestLevel.COMPLETE,
305 method = "toString",
306 args = {}
307 )
308 public void testToString() {
309 // make sure this method never throw exception.
310 mMotionEvent2.toString();
311 }
312
313 @TestTargetNew(
314 level = TestLevel.COMPLETE,
315 method = "offsetLocation",
316 args = {float.class, float.class}
317 )
318 public void testOffsetLocation() {
319 assertEquals(X_3F, mMotionEvent2.getX(), DELTA);
320 assertEquals(Y_4F, mMotionEvent2.getY(), DELTA);
321
322 float offsetX = 1.0f;
323 float offsetY = 1.0f;
324 mMotionEvent2.offsetLocation(offsetX, offsetY);
325 assertEquals(X_3F + offsetX, mMotionEvent2.getX(), DELTA);
326 assertEquals(Y_4F + offsetY, mMotionEvent2.getY(), DELTA);
327 }
328
329 @TestTargetNew(
330 level = TestLevel.COMPLETE,
331 method = "setLocation",
332 args = {float.class, float.class}
333 )
334 public void testSetLocation() {
335 assertEquals(X_3F, mMotionEvent2.getX(), DELTA);
336 assertEquals(Y_4F, mMotionEvent2.getY(), DELTA);
337
338 float newLocationX = 0.0f;
339 float newLocationY = 0.0f;
340 mMotionEvent2.setLocation(newLocationX, newLocationY);
341 assertEquals(newLocationX, mMotionEvent2.getX(), DELTA);
342 assertEquals(newLocationY, mMotionEvent2.getY(), DELTA);
343
344 newLocationX = 2.0f;
345 newLocationY = 2.0f;
346 mMotionEvent2.setLocation(newLocationX, newLocationY);
347 assertEquals(newLocationX, mMotionEvent2.getX(), DELTA);
348 assertEquals(newLocationY, mMotionEvent2.getY(), DELTA);
349 }
350
351 @TestTargetNew(
352 level = TestLevel.COMPLETE,
353 method = "getHistoricalX",
354 args = {int.class}
355 )
356 public void testGetHistoricalX() {
357 float x = X_3F + 5.0f;
358 mMotionEvent2.addBatch(mEventTime, x, 5.0f, 1.0f, 0.0f, 0);
359 assertEquals(X_3F, mMotionEvent2.getHistoricalX(0), DELTA);
360
361 mMotionEvent2.addBatch(mEventTime, X_3F + 10.0f, 10.0f, 0.0f, 1.0f, 0);
362 assertEquals(x, mMotionEvent2.getHistoricalX(1), DELTA);
363 }
364
365 @TestTargetNew(
366 level = TestLevel.COMPLETE,
367 method = "getHistoricalY",
368 args = {int.class}
369 )
370 public void testGetHistoricalY() {
371 float y = Y_4F + 5.0f;
372 mMotionEvent2.addBatch(mEventTime, 5.0f, y, 1.0f, 0.0f, 0);
373 assertEquals(Y_4F, mMotionEvent2.getHistoricalY(0), DELTA);
374
375 mMotionEvent2.addBatch(mEventTime, 15.0f, Y_4F + 15.0f, 0.0f, 1.0f, 0);
376 assertEquals(y, mMotionEvent2.getHistoricalY(1), DELTA);
377 }
378
379 @TestTargetNew(
380 level = TestLevel.COMPLETE,
381 method = "getHistoricalSize",
382 args = {int.class}
383 )
384 public void testGetHistoricalSize() {
385 float size = 0.5f;
386 mMotionEvent2.addBatch(mEventTime, 5.0f, 5.0f, 1.0f, size, 0);
387 assertEquals(SIZE_1F, mMotionEvent2.getHistoricalSize(0), DELTA);
388
389 mMotionEvent2.addBatch(mEventTime, 15.0f, 15.0f, 1.0f, 0.0f, 0);
390 assertEquals(size, mMotionEvent2.getHistoricalSize(1), DELTA);
391 }
392
393 @TestTargetNew(
394 level = TestLevel.COMPLETE,
395 method = "getHistoricalPressure",
396 args = {int.class}
397 )
398 public void testGetHistoricalPressure() {
399 float pressure = 0.5f;
400 mMotionEvent2.addBatch(mEventTime, 5.0f, 5.0f, pressure, 0.0f, 0);
401 assertEquals(PRESSURE_1F, mMotionEvent2.getHistoricalPressure(0), DELTA);
402
403 mMotionEvent2.addBatch(mEventTime, 15.0f, 15.0f, 0.0f, 0.0f, 0);
404 assertEquals(pressure, mMotionEvent2.getHistoricalPressure(1), DELTA);
405 }
406
407 @TestTargetNew(
408 level = TestLevel.COMPLETE,
409 method = "getHistoricalEventTime",
410 args = {int.class}
411 )
412 public void testGetHistoricalEventTime() {
413 long eventTime = mEventTime + 5l;
414 mMotionEvent2.addBatch(eventTime, 5.0f, 5.0f, 0.0f, 1.0f, 0);
415 assertEquals(mEventTime, mMotionEvent2.getHistoricalEventTime(0));
416
417 mMotionEvent2.addBatch(mEventTime + 10l, 15.0f, 15.0f, 1.0f, 0.0f, 0);
418 assertEquals(eventTime, mMotionEvent2.getHistoricalEventTime(1));
419 }
420
421 @TestTargetNew(
422 level = TestLevel.COMPLETE,
423 method = "addBatch",
424 args = {long.class, float.class, float.class, float.class, float.class, int.class}
425 )
426 @ToBeFixed(bug = "1695243", explanation = "Android API javadocs are incomplete")
427 public void testAddBatch() {
428 long eventTime = SystemClock.uptimeMillis();
429 float x = 10.0f;
430 float y = 20.0f;
431 float pressure = 1.0f;
432 float size = 1.0f;
433
434 // get original attribute values.
435 long origEventTime = mMotionEvent2.getEventTime();
436 float origX = mMotionEvent2.getX();
437 float origY = mMotionEvent2.getY();
438 float origPressure = mMotionEvent2.getPressure();
439 float origSize = mMotionEvent2.getSize();
440
441 assertEquals(0, mMotionEvent2.getHistorySize());
442 mMotionEvent2.addBatch(eventTime, x, y, pressure, size, 0);
443 assertEquals(1, mMotionEvent2.getHistorySize());
444 assertEquals(origEventTime, mMotionEvent2.getHistoricalEventTime(0));
445 assertEquals(origX, mMotionEvent2.getHistoricalX(0), DELTA);
446 assertEquals(origY, mMotionEvent2.getHistoricalY(0), DELTA);
447 assertEquals(origPressure, mMotionEvent2.getHistoricalPressure(0), DELTA);
448 assertEquals(origSize, mMotionEvent2.getHistoricalSize(0), DELTA);
449
450 mMotionEvent2.addBatch(mEventTime, 6, 6, 0.1f, 0, 0);
451 assertEquals(2, mMotionEvent2.getHistorySize());
452 assertEquals(eventTime, mMotionEvent2.getHistoricalEventTime(1));
453 assertEquals(x, mMotionEvent2.getHistoricalX(1), DELTA);
454 assertEquals(y, mMotionEvent2.getHistoricalY(1), DELTA);
455 assertEquals(pressure, mMotionEvent2.getHistoricalPressure(1), DELTA);
456 assertEquals(size, mMotionEvent2.getHistoricalSize(1), DELTA);
457 }
458
459 @TestTargetNew(
460 level = TestLevel.COMPLETE,
461 method = "getHistorySize",
462 args = {}
463 )
464 public void testGetHistorySize() {
465 long eventTime = SystemClock.uptimeMillis();
466 float x = 10.0f;
467 float y = 20.0f;
468 float pressure = 1.0f;
469 float size = 1.0f;
470
471 mMotionEvent2.setAction(MotionEvent.ACTION_DOWN);
472 assertEquals(0, mMotionEvent2.getHistorySize());
473
474 mMotionEvent2.setAction(MotionEvent.ACTION_MOVE);
475 mMotionEvent2.addBatch(eventTime, x, y, pressure, size, 0);
476 assertEquals(1, mMotionEvent2.getHistorySize());
477 }
478
479 @TestTargetNew(
480 level = TestLevel.COMPLETE,
481 method = "recycle",
482 args = {}
483 )
484 public void testRecycle() {
485 mMotionEvent2.setAction(MotionEvent.ACTION_MOVE);
486 assertEquals(0, mMotionEvent2.getHistorySize());
487 mMotionEvent2.addBatch(mEventTime, 10.0f, 5.0f, 1.0f, 0.0f, 0);
488 assertEquals(1, mMotionEvent2.getHistorySize());
489
490 mMotionEvent2.recycle();
491 }
492}