Scott Su | 743b7ad | 2009-04-22 01:09:28 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.view.cts; |
| 18 | |
| 19 | import dalvik.annotation.TestLevel; |
| 20 | import dalvik.annotation.TestTargetClass; |
| 21 | import dalvik.annotation.TestTargetNew; |
| 22 | import dalvik.annotation.TestTargets; |
| 23 | import dalvik.annotation.ToBeFixed; |
| 24 | |
Jeff Brown | 5fd1ec4 | 2010-08-23 11:58:23 -0700 | [diff] [blame] | 25 | import android.graphics.Matrix; |
Scott Su | 743b7ad | 2009-04-22 01:09:28 -0700 | [diff] [blame] | 26 | import android.os.Parcel; |
| 27 | import android.os.Parcelable; |
| 28 | import android.os.SystemClock; |
| 29 | import android.test.AndroidTestCase; |
Jeff Brown | 5fd1ec4 | 2010-08-23 11:58:23 -0700 | [diff] [blame] | 30 | import android.view.InputDevice; |
Scott Su | 743b7ad | 2009-04-22 01:09:28 -0700 | [diff] [blame] | 31 | import android.view.KeyEvent; |
| 32 | import android.view.MotionEvent; |
Jeff Brown | 5fd1ec4 | 2010-08-23 11:58:23 -0700 | [diff] [blame] | 33 | import android.view.MotionEvent.PointerCoords; |
Jeff Brown | 91dbbb7 | 2011-05-07 01:28:53 -0700 | [diff] [blame^] | 34 | import android.view.MotionEvent.PointerProperties; |
Scott Su | 743b7ad | 2009-04-22 01:09:28 -0700 | [diff] [blame] | 35 | |
| 36 | /** |
| 37 | * Test {@link MotionEvent}. |
| 38 | */ |
| 39 | @TestTargetClass(MotionEvent.class) |
| 40 | public class MotionEventTest extends AndroidTestCase { |
| 41 | private MotionEvent mMotionEvent1; |
| 42 | private MotionEvent mMotionEvent2; |
| 43 | private long mDownTime; |
| 44 | private long mEventTime; |
| 45 | private static final float X_3F = 3.0f; |
| 46 | private static final float Y_4F = 4.0f; |
| 47 | private static final int META_STATE = KeyEvent.META_SHIFT_ON; |
| 48 | private static final float PRESSURE_1F = 1.0f; |
| 49 | private static final float SIZE_1F = 1.0f; |
| 50 | private static final float X_PRECISION_3F = 3.0f; |
| 51 | private static final float Y_PRECISION_4F = 4.0f; |
| 52 | private static final int DEVICE_ID_1 = 1; |
| 53 | private static final int EDGE_FLAGS = MotionEvent.EDGE_TOP; |
| 54 | private static final float DELTA = 0.01f; |
| 55 | |
| 56 | @Override |
| 57 | protected void setUp() throws Exception { |
| 58 | super.setUp(); |
| 59 | |
| 60 | mDownTime = SystemClock.uptimeMillis(); |
| 61 | mEventTime = SystemClock.uptimeMillis(); |
| 62 | mMotionEvent1 = MotionEvent.obtain(mDownTime, mEventTime, |
| 63 | MotionEvent.ACTION_MOVE, X_3F, Y_4F, META_STATE); |
| 64 | mMotionEvent2 = MotionEvent.obtain(mDownTime, mEventTime, |
| 65 | MotionEvent.ACTION_MOVE, X_3F, Y_4F, PRESSURE_1F, SIZE_1F, META_STATE, |
| 66 | X_PRECISION_3F, Y_PRECISION_4F, DEVICE_ID_1, EDGE_FLAGS); |
| 67 | } |
| 68 | |
| 69 | @Override |
| 70 | protected void tearDown() throws Exception { |
| 71 | if (null != mMotionEvent1) { |
| 72 | mMotionEvent1.recycle(); |
| 73 | } |
| 74 | if (null != mMotionEvent2) { |
| 75 | mMotionEvent2.recycle(); |
| 76 | } |
| 77 | super.tearDown(); |
| 78 | } |
| 79 | |
| 80 | @TestTargets({ |
| 81 | @TestTargetNew( |
| 82 | level = TestLevel.COMPLETE, |
| 83 | method = "obtain", |
| 84 | args = {long.class, long.class, int.class, float.class, float.class, int.class} |
| 85 | ), |
| 86 | @TestTargetNew( |
| 87 | level = TestLevel.COMPLETE, |
| 88 | method = "getX", |
| 89 | args = {} |
| 90 | ), |
| 91 | @TestTargetNew( |
| 92 | level = TestLevel.COMPLETE, |
| 93 | method = "getDownTime", |
| 94 | args = {} |
| 95 | ), |
| 96 | @TestTargetNew( |
| 97 | level = TestLevel.COMPLETE, |
| 98 | method = "getEventTime", |
| 99 | args = {} |
| 100 | ), |
| 101 | @TestTargetNew( |
| 102 | level = TestLevel.COMPLETE, |
| 103 | method = "getX", |
| 104 | args = {} |
| 105 | ), |
| 106 | @TestTargetNew( |
| 107 | level = TestLevel.COMPLETE, |
| 108 | method = "getY", |
| 109 | args = {} |
| 110 | ), |
| 111 | @TestTargetNew( |
| 112 | level = TestLevel.COMPLETE, |
| 113 | method = "getRawX", |
| 114 | args = {} |
| 115 | ), |
| 116 | @TestTargetNew( |
| 117 | level = TestLevel.COMPLETE, |
| 118 | method = "getRawY", |
| 119 | args = {} |
| 120 | ), |
| 121 | @TestTargetNew( |
| 122 | level = TestLevel.COMPLETE, |
| 123 | method = "getMetaState", |
| 124 | args = {} |
| 125 | ), |
| 126 | @TestTargetNew( |
| 127 | level = TestLevel.COMPLETE, |
| 128 | method = "getDeviceId", |
| 129 | args = {} |
| 130 | ), |
| 131 | @TestTargetNew( |
| 132 | level = TestLevel.COMPLETE, |
| 133 | method = "getPressure", |
| 134 | args = {} |
| 135 | ), |
| 136 | @TestTargetNew( |
| 137 | level = TestLevel.COMPLETE, |
| 138 | method = "getSize", |
| 139 | args = {} |
| 140 | ), |
| 141 | @TestTargetNew( |
| 142 | level = TestLevel.COMPLETE, |
| 143 | method = "getXPrecision", |
| 144 | args = {} |
| 145 | ), |
| 146 | @TestTargetNew( |
| 147 | level = TestLevel.COMPLETE, |
| 148 | method = "getYPrecision", |
| 149 | args = {} |
| 150 | ) |
| 151 | }) |
| 152 | public void testObtain1() { |
| 153 | mMotionEvent1 = MotionEvent.obtain(mDownTime, mEventTime, |
| 154 | MotionEvent.ACTION_DOWN, X_3F, Y_4F, META_STATE); |
| 155 | assertNotNull(mMotionEvent1); |
| 156 | assertEquals(mDownTime, mMotionEvent1.getDownTime()); |
| 157 | assertEquals(mEventTime, mMotionEvent1.getEventTime()); |
| 158 | assertEquals(MotionEvent.ACTION_DOWN, mMotionEvent1.getAction()); |
| 159 | assertEquals(X_3F, mMotionEvent1.getX(), DELTA); |
| 160 | assertEquals(Y_4F, mMotionEvent1.getY(), DELTA); |
| 161 | assertEquals(X_3F, mMotionEvent1.getRawX(), DELTA); |
| 162 | assertEquals(Y_4F, mMotionEvent1.getRawY(), DELTA); |
| 163 | assertEquals(META_STATE, mMotionEvent1.getMetaState()); |
| 164 | assertEquals(0, mMotionEvent1.getDeviceId()); |
| 165 | assertEquals(0, mMotionEvent1.getEdgeFlags()); |
| 166 | assertEquals(PRESSURE_1F, mMotionEvent1.getPressure(), DELTA); |
| 167 | assertEquals(SIZE_1F, mMotionEvent1.getSize(), DELTA); |
| 168 | assertEquals(1.0f, mMotionEvent1.getXPrecision(), DELTA); |
| 169 | assertEquals(1.0f, mMotionEvent1.getYPrecision(), DELTA); |
| 170 | } |
| 171 | |
| 172 | @TestTargetNew( |
| 173 | level = TestLevel.COMPLETE, |
| 174 | method = "obtain", |
| 175 | args = {MotionEvent.class} |
| 176 | ) |
| 177 | public void testObtain2() { |
| 178 | MotionEvent motionEvent = MotionEvent.obtain(mDownTime, mEventTime, |
| 179 | MotionEvent.ACTION_DOWN, X_3F, Y_4F, META_STATE); |
| 180 | mMotionEvent1 = MotionEvent.obtain(motionEvent); |
| 181 | assertNotNull(mMotionEvent1); |
| 182 | assertEquals(motionEvent.getDownTime(), mMotionEvent1.getDownTime()); |
| 183 | assertEquals(motionEvent.getEventTime(), mMotionEvent1.getEventTime()); |
| 184 | assertEquals(motionEvent.getAction(), mMotionEvent1.getAction()); |
| 185 | assertEquals(motionEvent.getX(), mMotionEvent1.getX(), DELTA); |
| 186 | assertEquals(motionEvent.getY(), mMotionEvent1.getY(), DELTA); |
| 187 | assertEquals(motionEvent.getX(), mMotionEvent1.getRawX(), DELTA); |
| 188 | assertEquals(motionEvent.getY(), mMotionEvent1.getRawY(), DELTA); |
| 189 | assertEquals(motionEvent.getMetaState(), mMotionEvent1.getMetaState()); |
| 190 | assertEquals(motionEvent.getDeviceId(), mMotionEvent1.getDeviceId()); |
| 191 | assertEquals(motionEvent.getEdgeFlags(), mMotionEvent1.getEdgeFlags()); |
| 192 | assertEquals(motionEvent.getPressure(), mMotionEvent1.getPressure(), DELTA); |
| 193 | assertEquals(motionEvent.getSize(), mMotionEvent1.getSize(), DELTA); |
| 194 | assertEquals(motionEvent.getXPrecision(), mMotionEvent1.getXPrecision(), DELTA); |
| 195 | assertEquals(motionEvent.getYPrecision(), mMotionEvent1.getYPrecision(), DELTA); |
| 196 | } |
| 197 | |
| 198 | @TestTargetNew( |
| 199 | level = TestLevel.COMPLETE, |
| 200 | method = "obtain", |
| 201 | args = {long.class, long.class, int.class, float.class, float.class, float.class, |
| 202 | float.class, int.class, float.class, float.class, int.class, int.class} |
| 203 | ) |
| 204 | public void testObtain3() { |
| 205 | mMotionEvent1 = null; |
| 206 | mMotionEvent1 = MotionEvent.obtain(mDownTime, mEventTime, |
| 207 | MotionEvent.ACTION_DOWN, X_3F, Y_4F, PRESSURE_1F, SIZE_1F, META_STATE, |
| 208 | X_PRECISION_3F, Y_PRECISION_4F, DEVICE_ID_1, EDGE_FLAGS); |
| 209 | assertNotNull(mMotionEvent1); |
| 210 | assertEquals(mDownTime, mMotionEvent1.getDownTime()); |
| 211 | assertEquals(mEventTime, mMotionEvent1.getEventTime()); |
| 212 | assertEquals(MotionEvent.ACTION_DOWN, mMotionEvent1.getAction()); |
| 213 | assertEquals(X_3F, mMotionEvent1.getX(), DELTA); |
| 214 | assertEquals(Y_4F, mMotionEvent1.getY(), DELTA); |
| 215 | assertEquals(X_3F, mMotionEvent1.getRawX(), DELTA); |
| 216 | assertEquals(Y_4F, mMotionEvent1.getRawY(), DELTA); |
| 217 | assertEquals(META_STATE, mMotionEvent1.getMetaState()); |
| 218 | assertEquals(DEVICE_ID_1, mMotionEvent1.getDeviceId()); |
| 219 | assertEquals(EDGE_FLAGS, mMotionEvent1.getEdgeFlags()); |
| 220 | assertEquals(PRESSURE_1F, mMotionEvent1.getPressure(), DELTA); |
| 221 | assertEquals(SIZE_1F, mMotionEvent1.getSize(), DELTA); |
| 222 | assertEquals(X_PRECISION_3F, mMotionEvent1.getXPrecision(), DELTA); |
| 223 | assertEquals(Y_PRECISION_4F, mMotionEvent1.getYPrecision(), DELTA); |
| 224 | } |
| 225 | |
| 226 | @TestTargets({ |
| 227 | @TestTargetNew( |
| 228 | level = TestLevel.COMPLETE, |
| 229 | method = "getAction", |
| 230 | args = {} |
| 231 | ), |
| 232 | @TestTargetNew( |
| 233 | level = TestLevel.COMPLETE, |
| 234 | method = "setAction", |
| 235 | args = {int.class} |
| 236 | ) |
| 237 | }) |
| 238 | public void testAccessAction() { |
| 239 | assertEquals(MotionEvent.ACTION_MOVE, mMotionEvent1.getAction()); |
| 240 | |
| 241 | mMotionEvent1.setAction(MotionEvent.ACTION_UP); |
| 242 | assertEquals(MotionEvent.ACTION_UP, mMotionEvent1.getAction()); |
| 243 | |
| 244 | mMotionEvent1.setAction(MotionEvent.ACTION_MOVE); |
| 245 | assertEquals(MotionEvent.ACTION_MOVE, mMotionEvent1.getAction()); |
| 246 | |
| 247 | mMotionEvent1.setAction(MotionEvent.ACTION_CANCEL); |
| 248 | assertEquals(MotionEvent.ACTION_CANCEL, mMotionEvent1.getAction()); |
| 249 | |
| 250 | mMotionEvent1.setAction(MotionEvent.ACTION_DOWN); |
| 251 | assertEquals(MotionEvent.ACTION_DOWN, mMotionEvent1.getAction()); |
| 252 | } |
| 253 | |
| 254 | @TestTargetNew( |
| 255 | level = TestLevel.COMPLETE, |
| 256 | method = "describeContents", |
| 257 | args = {} |
| 258 | ) |
| 259 | @ToBeFixed(bug = "1695243", explanation = "Android API javadocs are incomplete") |
| 260 | public void testDescribeContents() { |
| 261 | // make sure this method never throw any exception. |
| 262 | mMotionEvent2.describeContents(); |
| 263 | } |
| 264 | |
| 265 | @TestTargets({ |
| 266 | @TestTargetNew( |
| 267 | level = TestLevel.COMPLETE, |
| 268 | method = "getEdgeFlags", |
| 269 | args = {} |
| 270 | ), |
| 271 | @TestTargetNew( |
| 272 | level = TestLevel.COMPLETE, |
| 273 | method = "setEdgeFlags", |
| 274 | args = {int.class} |
| 275 | ) |
| 276 | }) |
| 277 | public void testAccessEdgeFlags() { |
| 278 | assertEquals(EDGE_FLAGS, mMotionEvent2.getEdgeFlags()); |
| 279 | |
| 280 | int edgeFlags = 10; |
| 281 | mMotionEvent2.setEdgeFlags(edgeFlags); |
| 282 | assertEquals(edgeFlags, mMotionEvent2.getEdgeFlags()); |
| 283 | } |
| 284 | |
| 285 | @TestTargetNew( |
| 286 | level = TestLevel.COMPLETE, |
| 287 | method = "writeToParcel", |
| 288 | args = {Parcel.class, int.class} |
| 289 | ) |
| 290 | public void testWriteToParcel() { |
| 291 | Parcel parcel = Parcel.obtain(); |
| 292 | mMotionEvent2.writeToParcel(parcel, Parcelable.PARCELABLE_WRITE_RETURN_VALUE); |
| 293 | parcel.setDataPosition(0); |
| 294 | |
| 295 | MotionEvent motionEvent = MotionEvent.CREATOR.createFromParcel(parcel); |
| 296 | assertEquals(mMotionEvent2.getRawY(), motionEvent.getRawY(), DELTA); |
| 297 | assertEquals(mMotionEvent2.getRawX(), motionEvent.getRawX(), DELTA); |
| 298 | assertEquals(mMotionEvent2.getY(), motionEvent.getY(), DELTA); |
| 299 | assertEquals(mMotionEvent2.getX(), motionEvent.getX(), DELTA); |
| 300 | assertEquals(mMotionEvent2.getAction(), motionEvent.getAction()); |
| 301 | assertEquals(mMotionEvent2.getDownTime(), motionEvent.getDownTime()); |
| 302 | assertEquals(mMotionEvent2.getEventTime(), motionEvent.getEventTime()); |
| 303 | assertEquals(mMotionEvent2.getEdgeFlags(), motionEvent.getEdgeFlags()); |
| 304 | assertEquals(mMotionEvent2.getDeviceId(), motionEvent.getDeviceId()); |
| 305 | } |
| 306 | |
| 307 | @TestTargetNew( |
| 308 | level = TestLevel.COMPLETE, |
| 309 | method = "toString", |
| 310 | args = {} |
| 311 | ) |
| 312 | public void testToString() { |
| 313 | // make sure this method never throw exception. |
| 314 | mMotionEvent2.toString(); |
| 315 | } |
| 316 | |
| 317 | @TestTargetNew( |
| 318 | level = TestLevel.COMPLETE, |
| 319 | method = "offsetLocation", |
| 320 | args = {float.class, float.class} |
| 321 | ) |
| 322 | public void testOffsetLocation() { |
| 323 | assertEquals(X_3F, mMotionEvent2.getX(), DELTA); |
| 324 | assertEquals(Y_4F, mMotionEvent2.getY(), DELTA); |
| 325 | |
| 326 | float offsetX = 1.0f; |
| 327 | float offsetY = 1.0f; |
| 328 | mMotionEvent2.offsetLocation(offsetX, offsetY); |
| 329 | assertEquals(X_3F + offsetX, mMotionEvent2.getX(), DELTA); |
| 330 | assertEquals(Y_4F + offsetY, mMotionEvent2.getY(), DELTA); |
| 331 | } |
| 332 | |
| 333 | @TestTargetNew( |
| 334 | level = TestLevel.COMPLETE, |
| 335 | method = "setLocation", |
| 336 | args = {float.class, float.class} |
| 337 | ) |
| 338 | public void testSetLocation() { |
| 339 | assertEquals(X_3F, mMotionEvent2.getX(), DELTA); |
| 340 | assertEquals(Y_4F, mMotionEvent2.getY(), DELTA); |
| 341 | |
| 342 | float newLocationX = 0.0f; |
| 343 | float newLocationY = 0.0f; |
| 344 | mMotionEvent2.setLocation(newLocationX, newLocationY); |
| 345 | assertEquals(newLocationX, mMotionEvent2.getX(), DELTA); |
| 346 | assertEquals(newLocationY, mMotionEvent2.getY(), DELTA); |
| 347 | |
| 348 | newLocationX = 2.0f; |
| 349 | newLocationY = 2.0f; |
| 350 | mMotionEvent2.setLocation(newLocationX, newLocationY); |
| 351 | assertEquals(newLocationX, mMotionEvent2.getX(), DELTA); |
| 352 | assertEquals(newLocationY, mMotionEvent2.getY(), DELTA); |
| 353 | } |
| 354 | |
| 355 | @TestTargetNew( |
| 356 | level = TestLevel.COMPLETE, |
| 357 | method = "getHistoricalX", |
| 358 | args = {int.class} |
| 359 | ) |
| 360 | public void testGetHistoricalX() { |
| 361 | float x = X_3F + 5.0f; |
| 362 | mMotionEvent2.addBatch(mEventTime, x, 5.0f, 1.0f, 0.0f, 0); |
| 363 | assertEquals(X_3F, mMotionEvent2.getHistoricalX(0), DELTA); |
| 364 | |
| 365 | mMotionEvent2.addBatch(mEventTime, X_3F + 10.0f, 10.0f, 0.0f, 1.0f, 0); |
| 366 | assertEquals(x, mMotionEvent2.getHistoricalX(1), DELTA); |
| 367 | } |
| 368 | |
| 369 | @TestTargetNew( |
| 370 | level = TestLevel.COMPLETE, |
| 371 | method = "getHistoricalY", |
| 372 | args = {int.class} |
| 373 | ) |
| 374 | public void testGetHistoricalY() { |
| 375 | float y = Y_4F + 5.0f; |
| 376 | mMotionEvent2.addBatch(mEventTime, 5.0f, y, 1.0f, 0.0f, 0); |
| 377 | assertEquals(Y_4F, mMotionEvent2.getHistoricalY(0), DELTA); |
| 378 | |
| 379 | mMotionEvent2.addBatch(mEventTime, 15.0f, Y_4F + 15.0f, 0.0f, 1.0f, 0); |
| 380 | assertEquals(y, mMotionEvent2.getHistoricalY(1), DELTA); |
| 381 | } |
| 382 | |
| 383 | @TestTargetNew( |
| 384 | level = TestLevel.COMPLETE, |
| 385 | method = "getHistoricalSize", |
| 386 | args = {int.class} |
| 387 | ) |
| 388 | public void testGetHistoricalSize() { |
| 389 | float size = 0.5f; |
| 390 | mMotionEvent2.addBatch(mEventTime, 5.0f, 5.0f, 1.0f, size, 0); |
| 391 | assertEquals(SIZE_1F, mMotionEvent2.getHistoricalSize(0), DELTA); |
| 392 | |
| 393 | mMotionEvent2.addBatch(mEventTime, 15.0f, 15.0f, 1.0f, 0.0f, 0); |
| 394 | assertEquals(size, mMotionEvent2.getHistoricalSize(1), DELTA); |
| 395 | } |
| 396 | |
| 397 | @TestTargetNew( |
| 398 | level = TestLevel.COMPLETE, |
| 399 | method = "getHistoricalPressure", |
| 400 | args = {int.class} |
| 401 | ) |
| 402 | public void testGetHistoricalPressure() { |
| 403 | float pressure = 0.5f; |
| 404 | mMotionEvent2.addBatch(mEventTime, 5.0f, 5.0f, pressure, 0.0f, 0); |
| 405 | assertEquals(PRESSURE_1F, mMotionEvent2.getHistoricalPressure(0), DELTA); |
| 406 | |
| 407 | mMotionEvent2.addBatch(mEventTime, 15.0f, 15.0f, 0.0f, 0.0f, 0); |
| 408 | assertEquals(pressure, mMotionEvent2.getHistoricalPressure(1), DELTA); |
| 409 | } |
| 410 | |
| 411 | @TestTargetNew( |
| 412 | level = TestLevel.COMPLETE, |
| 413 | method = "getHistoricalEventTime", |
| 414 | args = {int.class} |
| 415 | ) |
| 416 | public void testGetHistoricalEventTime() { |
| 417 | long eventTime = mEventTime + 5l; |
| 418 | mMotionEvent2.addBatch(eventTime, 5.0f, 5.0f, 0.0f, 1.0f, 0); |
| 419 | assertEquals(mEventTime, mMotionEvent2.getHistoricalEventTime(0)); |
| 420 | |
| 421 | mMotionEvent2.addBatch(mEventTime + 10l, 15.0f, 15.0f, 1.0f, 0.0f, 0); |
| 422 | assertEquals(eventTime, mMotionEvent2.getHistoricalEventTime(1)); |
| 423 | } |
| 424 | |
| 425 | @TestTargetNew( |
| 426 | level = TestLevel.COMPLETE, |
| 427 | method = "addBatch", |
| 428 | args = {long.class, float.class, float.class, float.class, float.class, int.class} |
| 429 | ) |
| 430 | @ToBeFixed(bug = "1695243", explanation = "Android API javadocs are incomplete") |
| 431 | public void testAddBatch() { |
| 432 | long eventTime = SystemClock.uptimeMillis(); |
| 433 | float x = 10.0f; |
| 434 | float y = 20.0f; |
| 435 | float pressure = 1.0f; |
| 436 | float size = 1.0f; |
| 437 | |
| 438 | // get original attribute values. |
| 439 | long origEventTime = mMotionEvent2.getEventTime(); |
| 440 | float origX = mMotionEvent2.getX(); |
| 441 | float origY = mMotionEvent2.getY(); |
| 442 | float origPressure = mMotionEvent2.getPressure(); |
| 443 | float origSize = mMotionEvent2.getSize(); |
| 444 | |
| 445 | assertEquals(0, mMotionEvent2.getHistorySize()); |
| 446 | mMotionEvent2.addBatch(eventTime, x, y, pressure, size, 0); |
| 447 | assertEquals(1, mMotionEvent2.getHistorySize()); |
| 448 | assertEquals(origEventTime, mMotionEvent2.getHistoricalEventTime(0)); |
| 449 | assertEquals(origX, mMotionEvent2.getHistoricalX(0), DELTA); |
| 450 | assertEquals(origY, mMotionEvent2.getHistoricalY(0), DELTA); |
| 451 | assertEquals(origPressure, mMotionEvent2.getHistoricalPressure(0), DELTA); |
| 452 | assertEquals(origSize, mMotionEvent2.getHistoricalSize(0), DELTA); |
| 453 | |
| 454 | mMotionEvent2.addBatch(mEventTime, 6, 6, 0.1f, 0, 0); |
| 455 | assertEquals(2, mMotionEvent2.getHistorySize()); |
| 456 | assertEquals(eventTime, mMotionEvent2.getHistoricalEventTime(1)); |
| 457 | assertEquals(x, mMotionEvent2.getHistoricalX(1), DELTA); |
| 458 | assertEquals(y, mMotionEvent2.getHistoricalY(1), DELTA); |
| 459 | assertEquals(pressure, mMotionEvent2.getHistoricalPressure(1), DELTA); |
| 460 | assertEquals(size, mMotionEvent2.getHistoricalSize(1), DELTA); |
| 461 | } |
| 462 | |
| 463 | @TestTargetNew( |
| 464 | level = TestLevel.COMPLETE, |
| 465 | method = "getHistorySize", |
| 466 | args = {} |
| 467 | ) |
| 468 | public void testGetHistorySize() { |
| 469 | long eventTime = SystemClock.uptimeMillis(); |
| 470 | float x = 10.0f; |
| 471 | float y = 20.0f; |
| 472 | float pressure = 1.0f; |
| 473 | float size = 1.0f; |
| 474 | |
| 475 | mMotionEvent2.setAction(MotionEvent.ACTION_DOWN); |
| 476 | assertEquals(0, mMotionEvent2.getHistorySize()); |
| 477 | |
| 478 | mMotionEvent2.setAction(MotionEvent.ACTION_MOVE); |
| 479 | mMotionEvent2.addBatch(eventTime, x, y, pressure, size, 0); |
| 480 | assertEquals(1, mMotionEvent2.getHistorySize()); |
| 481 | } |
| 482 | |
| 483 | @TestTargetNew( |
| 484 | level = TestLevel.COMPLETE, |
| 485 | method = "recycle", |
| 486 | args = {} |
| 487 | ) |
| 488 | public void testRecycle() { |
| 489 | mMotionEvent2.setAction(MotionEvent.ACTION_MOVE); |
| 490 | assertEquals(0, mMotionEvent2.getHistorySize()); |
| 491 | mMotionEvent2.addBatch(mEventTime, 10.0f, 5.0f, 1.0f, 0.0f, 0); |
| 492 | assertEquals(1, mMotionEvent2.getHistorySize()); |
| 493 | |
| 494 | mMotionEvent2.recycle(); |
Jeff Brown | ba7e10c | 2010-06-26 00:38:26 -0700 | [diff] [blame] | 495 | |
| 496 | try { |
| 497 | mMotionEvent2.recycle(); |
| 498 | fail("recycle() should throw an exception when the event has already been recycled."); |
| 499 | } catch (RuntimeException ex) { |
| 500 | } |
| 501 | |
| 502 | mMotionEvent2 = null; // since it was recycled, don't try to recycle again in tear down |
Scott Su | 743b7ad | 2009-04-22 01:09:28 -0700 | [diff] [blame] | 503 | } |
Jeff Brown | 5fd1ec4 | 2010-08-23 11:58:23 -0700 | [diff] [blame] | 504 | |
| 505 | @TestTargetNew( |
| 506 | level = TestLevel.COMPLETE, |
| 507 | method = "transform", |
| 508 | args = {} |
| 509 | ) |
| 510 | public void testTransformShouldThrowWhenMatrixIsNull() { |
| 511 | try { |
| 512 | mMotionEvent1.transform(null); |
| 513 | fail("transform() should throw an exception when matrix is null."); |
| 514 | } catch (IllegalArgumentException ex) { |
| 515 | } |
| 516 | } |
| 517 | |
| 518 | @TestTargetNew( |
| 519 | level = TestLevel.COMPLETE, |
| 520 | method = "transform", |
| 521 | args = {} |
| 522 | ) |
| 523 | public void testTransformShouldApplyMatrixToPointsAndPreserveRawPosition() { |
| 524 | // Generate some points on a circle. |
| 525 | // Each point 'i' is a point on a circle of radius ROTATION centered at (3,2) at an angle |
| 526 | // of ARC * i degrees clockwise relative to the Y axis. |
| 527 | // The geometrical representation is irrelevant to the test, it's just easy to generate |
| 528 | // and check rotation. We set the orientation to the same angle. |
| 529 | // Coordinate system: down is increasing Y, right is increasing X. |
| 530 | final float PI_180 = (float) (Math.PI / 180); |
| 531 | final float RADIUS = 10; |
| 532 | final float ARC = 36; |
| 533 | final float ROTATION = ARC * 2; |
| 534 | |
| 535 | final int pointerCount = 11; |
| 536 | final int[] pointerIds = new int[pointerCount]; |
| 537 | final PointerCoords[] pointerCoords = new PointerCoords[pointerCount]; |
| 538 | for (int i = 0; i < pointerCount; i++) { |
| 539 | final PointerCoords c = new PointerCoords(); |
| 540 | final float angle = (float) (i * ARC * PI_180); |
| 541 | pointerIds[i] = i; |
| 542 | pointerCoords[i] = c; |
| 543 | c.x = (float) (Math.sin(angle) * RADIUS + 3); |
| 544 | c.y = (float) (- Math.cos(angle) * RADIUS + 2); |
| 545 | c.orientation = angle; |
| 546 | } |
| 547 | final MotionEvent event = MotionEvent.obtain(0, 0, MotionEvent.ACTION_MOVE, |
| 548 | pointerCount, pointerIds, pointerCoords, 0, 0, 0, 0, 0, 0, 0); |
| 549 | final float originalRawX = 0 + 3; |
| 550 | final float originalRawY = - RADIUS + 2; |
| 551 | dump("Original points.", event); |
| 552 | |
| 553 | // Check original raw X and Y assumption. |
| 554 | assertEquals(originalRawX, event.getRawX(), 0.001); |
| 555 | assertEquals(originalRawY, event.getRawY(), 0.001); |
| 556 | |
| 557 | // Now translate the motion event so the circle's origin is at (0,0). |
| 558 | event.offsetLocation(-3, -2); |
| 559 | dump("Translated points.", event); |
| 560 | |
| 561 | // Offsetting the location should preserve the raw X and Y of the first point. |
| 562 | assertEquals(originalRawX, event.getRawX(), 0.001); |
| 563 | assertEquals(originalRawY, event.getRawY(), 0.001); |
| 564 | |
| 565 | // Apply a rotation about the origin by ROTATION degrees clockwise. |
| 566 | Matrix matrix = new Matrix(); |
| 567 | matrix.setRotate(ROTATION); |
| 568 | event.transform(matrix); |
| 569 | dump("Rotated points.", event); |
| 570 | |
| 571 | // Check the points. |
| 572 | for (int i = 0; i < pointerCount; i++) { |
| 573 | final PointerCoords c = pointerCoords[i]; |
| 574 | event.getPointerCoords(i, c); |
| 575 | |
| 576 | final float angle = (float) ((i * ARC + ROTATION) * PI_180); |
| 577 | assertEquals(Math.sin(angle) * RADIUS, c.x, 0.001); |
| 578 | assertEquals(- Math.cos(angle) * RADIUS, c.y, 0.001); |
| 579 | assertEquals(Math.tan(angle), Math.tan(c.orientation), 0.1); |
| 580 | } |
| 581 | |
| 582 | // Applying the transformation should preserve the raw X and Y of the first point. |
| 583 | assertEquals(originalRawX, event.getRawX(), 0.001); |
| 584 | assertEquals(originalRawY, event.getRawY(), 0.001); |
| 585 | } |
| 586 | |
| 587 | private void dump(String label, MotionEvent ev) { |
| 588 | if (false) { |
| 589 | StringBuilder msg = new StringBuilder(); |
| 590 | msg.append(label).append("\n"); |
| 591 | |
| 592 | msg.append(" Raw: (").append(ev.getRawX()).append(",").append(ev.getRawY()).append(")\n"); |
| 593 | int pointerCount = ev.getPointerCount(); |
| 594 | for (int i = 0; i < pointerCount; i++) { |
| 595 | msg.append(" Pointer[").append(i).append("]: (") |
| 596 | .append(ev.getX(i)).append(",").append(ev.getY(i)).append("), orientation=") |
| 597 | .append(ev.getOrientation(i) * 180 / Math.PI).append(" deg\n"); |
| 598 | } |
| 599 | |
| 600 | android.util.Log.i("TEST", msg.toString()); |
| 601 | } |
| 602 | } |
Jeff Brown | 9e24ab6 | 2011-03-15 19:07:47 -0700 | [diff] [blame] | 603 | |
| 604 | public void testPointerCoordsDefaultConstructor() { |
| 605 | PointerCoords coords = new PointerCoords(); |
| 606 | |
| 607 | assertEquals(0f, coords.x); |
| 608 | assertEquals(0f, coords.y); |
| 609 | assertEquals(0f, coords.pressure); |
| 610 | assertEquals(0f, coords.size); |
| 611 | assertEquals(0f, coords.touchMajor); |
| 612 | assertEquals(0f, coords.touchMinor); |
| 613 | assertEquals(0f, coords.toolMajor); |
| 614 | assertEquals(0f, coords.toolMinor); |
| 615 | assertEquals(0f, coords.orientation); |
| 616 | } |
| 617 | |
| 618 | public void testPointerCoordsCopyConstructor() { |
| 619 | PointerCoords coords = new PointerCoords(); |
| 620 | coords.x = 1; |
| 621 | coords.y = 2; |
| 622 | coords.pressure = 3; |
| 623 | coords.size = 4; |
| 624 | coords.touchMajor = 5; |
| 625 | coords.touchMinor = 6; |
| 626 | coords.toolMajor = 7; |
| 627 | coords.toolMinor = 8; |
| 628 | coords.orientation = 9; |
| 629 | coords.setAxisValue(MotionEvent.AXIS_GENERIC_1, 10); |
| 630 | |
| 631 | PointerCoords copy = new PointerCoords(coords); |
| 632 | assertEquals(1f, copy.x); |
| 633 | assertEquals(2f, copy.y); |
| 634 | assertEquals(3f, copy.pressure); |
| 635 | assertEquals(4f, copy.size); |
| 636 | assertEquals(5f, copy.touchMajor); |
| 637 | assertEquals(6f, copy.touchMinor); |
| 638 | assertEquals(7f, copy.toolMajor); |
| 639 | assertEquals(8f, copy.toolMinor); |
| 640 | assertEquals(9f, copy.orientation); |
| 641 | assertEquals(10f, coords.getAxisValue(MotionEvent.AXIS_GENERIC_1)); |
| 642 | } |
| 643 | |
| 644 | public void testPointerCoordsCopyFrom() { |
| 645 | PointerCoords coords = new PointerCoords(); |
| 646 | coords.x = 1; |
| 647 | coords.y = 2; |
| 648 | coords.pressure = 3; |
| 649 | coords.size = 4; |
| 650 | coords.touchMajor = 5; |
| 651 | coords.touchMinor = 6; |
| 652 | coords.toolMajor = 7; |
| 653 | coords.toolMinor = 8; |
| 654 | coords.orientation = 9; |
| 655 | coords.setAxisValue(MotionEvent.AXIS_GENERIC_1, 10); |
| 656 | |
| 657 | PointerCoords copy = new PointerCoords(); |
| 658 | copy.copyFrom(coords); |
| 659 | assertEquals(1f, copy.x); |
| 660 | assertEquals(2f, copy.y); |
| 661 | assertEquals(3f, copy.pressure); |
| 662 | assertEquals(4f, copy.size); |
| 663 | assertEquals(5f, copy.touchMajor); |
| 664 | assertEquals(6f, copy.touchMinor); |
| 665 | assertEquals(7f, copy.toolMajor); |
| 666 | assertEquals(8f, copy.toolMinor); |
| 667 | assertEquals(9f, copy.orientation); |
| 668 | assertEquals(10f, coords.getAxisValue(MotionEvent.AXIS_GENERIC_1)); |
| 669 | } |
Jeff Brown | 91dbbb7 | 2011-05-07 01:28:53 -0700 | [diff] [blame^] | 670 | |
| 671 | public void testPointerPropertiesDefaultConstructor() { |
| 672 | PointerProperties properties = new PointerProperties(); |
| 673 | |
| 674 | assertEquals(MotionEvent.INVALID_POINTER_ID, properties.id); |
| 675 | assertEquals(MotionEvent.TOOL_TYPE_UNKNOWN, properties.toolType); |
| 676 | } |
| 677 | |
| 678 | public void testPointerPropertiesCopyConstructor() { |
| 679 | PointerProperties properties = new PointerProperties(); |
| 680 | properties.id = 1; |
| 681 | properties.toolType = MotionEvent.TOOL_TYPE_MOUSE; |
| 682 | |
| 683 | PointerProperties copy = new PointerProperties(properties); |
| 684 | assertEquals(1, copy.id); |
| 685 | assertEquals(MotionEvent.TOOL_TYPE_MOUSE, copy.toolType); |
| 686 | } |
| 687 | |
| 688 | public void testPointerPropertiesCopyFrom() { |
| 689 | PointerProperties properties = new PointerProperties(); |
| 690 | properties.id = 1; |
| 691 | properties.toolType = MotionEvent.TOOL_TYPE_MOUSE; |
| 692 | |
| 693 | PointerProperties copy = new PointerProperties(); |
| 694 | copy.copyFrom(properties); |
| 695 | assertEquals(1, copy.id); |
| 696 | assertEquals(MotionEvent.TOOL_TYPE_MOUSE, copy.toolType); |
| 697 | } |
Scott Su | 743b7ad | 2009-04-22 01:09:28 -0700 | [diff] [blame] | 698 | } |