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