1 /* 2 * Copyright (C) 2020 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 18 19 import android.view.InputDevice.SOURCE_TOUCHSCREEN 20 import android.view.MotionEvent.ACTION_MOVE 21 import android.view.MotionEvent.FLAG_IS_ACCESSIBILITY_EVENT 22 import android.view.MotionEvent.FLAG_WINDOW_IS_OBSCURED 23 import android.view.MotionEvent.FLAG_WINDOW_IS_PARTIALLY_OBSCURED 24 import android.view.MotionEvent.FLAG_TAINTED 25 import android.os.Parcel 26 import android.platform.test.annotations.Presubmit 27 28 import androidx.test.ext.junit.runners.AndroidJUnit4 29 import androidx.test.filters.SmallTest 30 31 import org.junit.Assert.assertEquals 32 import org.junit.Assert.assertNull 33 import org.junit.Assert.assertTrue 34 import org.junit.runner.RunWith 35 import org.junit.Test 36 37 @RunWith(AndroidJUnit4::class) 38 @SmallTest 39 @Presubmit 40 class VerifiedMotionEventTest { 41 42 @Test testConstructornull43 fun testConstructor() { 44 val event = createVerifiedMotionEvent() 45 46 assertEquals(DEVICE_ID, event.deviceId) 47 assertEquals(EVENT_TIME_NANOS, event.eventTimeNanos) 48 assertEquals(SOURCE, event.source) 49 assertEquals(DISPLAY_ID, event.displayId) 50 51 assertEquals(RAW_X, event.rawX, 0f) 52 assertEquals(RAW_Y, event.rawY, 0f) 53 assertEquals(ACTION_MASKED, event.actionMasked) 54 assertEquals(DOWN_TIME_NANOS, event.downTimeNanos) 55 assertEquals(FLAGS, event.flags) 56 assertEquals(META_STATE, event.metaState) 57 assertEquals(BUTTON_STATE, event.buttonState) 58 } 59 60 /** 61 * Write to parcel as a MotionEvent, read back as a MotionEvent 62 */ 63 @Test testParcelUnparcelnull64 fun testParcelUnparcel() { 65 val motionEvent = createVerifiedMotionEvent() 66 val parcel = Parcel.obtain() 67 motionEvent.writeToParcel(parcel, 0 /*flags*/) 68 parcel.setDataPosition(0) 69 70 val unparceledMotionEvent = VerifiedMotionEvent.CREATOR.createFromParcel(parcel) 71 parcel.recycle() 72 73 compareVerifiedMotionEvents(motionEvent, unparceledMotionEvent) 74 } 75 76 /** 77 * Write to parcel as an InputEvent, read back as an InputEvent 78 */ 79 @Test testParcelInputEventnull80 fun testParcelInputEvent() { 81 val motionEvent = createVerifiedMotionEvent() 82 val inputEvent: VerifiedInputEvent = motionEvent 83 val parcel = Parcel.obtain() 84 inputEvent.writeToParcel(parcel, 0 /*flags*/) 85 parcel.setDataPosition(0) 86 87 val unparceledEvent = VerifiedInputEvent.CREATOR.createFromParcel(parcel) 88 parcel.recycle() 89 90 assertTrue(unparceledEvent is VerifiedMotionEvent) 91 compareVerifiedMotionEvents(motionEvent, unparceledEvent as VerifiedMotionEvent) 92 } 93 94 /** 95 * Write to parcel as a MotionEvent, read back as an InputEvent 96 */ 97 @Test testParcelMotionEventnull98 fun testParcelMotionEvent() { 99 val motionEvent = createVerifiedMotionEvent() 100 val parcel = Parcel.obtain() 101 motionEvent.writeToParcel(parcel, 0 /*flags*/) 102 parcel.setDataPosition(0) 103 104 val unparceledEvent = VerifiedInputEvent.CREATOR.createFromParcel(parcel) 105 parcel.recycle() 106 107 assertTrue(unparceledEvent is VerifiedMotionEvent) 108 compareVerifiedMotionEvents(motionEvent, unparceledEvent as VerifiedMotionEvent) 109 } 110 111 /** 112 * Write to parcel as an InputEvent, read back as a MotionEvent 113 */ 114 @Test testParcelInputToMotionEventnull115 fun testParcelInputToMotionEvent() { 116 val motionEvent = createVerifiedMotionEvent() 117 val inputEvent: VerifiedInputEvent = motionEvent 118 val parcel = Parcel.obtain() 119 inputEvent.writeToParcel(parcel, 0 /*flags*/) 120 parcel.setDataPosition(0) 121 122 val unparceledEvent = VerifiedMotionEvent.CREATOR.createFromParcel(parcel) 123 parcel.recycle() 124 125 compareVerifiedMotionEvents(motionEvent, unparceledEvent) 126 } 127 128 @Test testGetFlagnull129 fun testGetFlag() { 130 val motionEvent = createVerifiedMotionEvent() 131 // Invalid value of a flag 132 assertNull(motionEvent.getFlag(0)) 133 // Flag that was not set 134 assertEquals(false, motionEvent.getFlag(FLAG_WINDOW_IS_PARTIALLY_OBSCURED)) 135 // Flags that were set 136 assertEquals(true, motionEvent.getFlag(FLAG_WINDOW_IS_OBSCURED)) 137 assertEquals(true, motionEvent.getFlag(FLAG_IS_ACCESSIBILITY_EVENT)) 138 // Only 1 flag at a time is accepted 139 assertNull(motionEvent.getFlag( 140 FLAG_WINDOW_IS_PARTIALLY_OBSCURED or FLAG_WINDOW_IS_OBSCURED)) 141 // Flag that is not verified returns null 142 assertNull(motionEvent.getFlag(FLAG_TAINTED)) 143 } 144 145 @Test testEqualsHashcodenull146 fun testEqualsHashcode() { 147 val motionEvent1 = createVerifiedMotionEvent() 148 val motionEvent2 = createVerifiedMotionEvent() 149 compareVerifiedMotionEvents(motionEvent1, motionEvent2) 150 } 151 152 companion object { 153 private const val DEVICE_ID = 0 154 private const val EVENT_TIME_NANOS: Long = 2000 155 private const val SOURCE = SOURCE_TOUCHSCREEN 156 private const val DISPLAY_ID = 2 157 private const val RAW_X = 100f 158 private const val RAW_Y = 200f 159 private const val ACTION_MASKED = ACTION_MOVE 160 private const val DOWN_TIME_NANOS: Long = 1000 161 private const val FLAGS = FLAG_WINDOW_IS_OBSCURED or FLAG_IS_ACCESSIBILITY_EVENT 162 private const val META_STATE = 11 163 private const val BUTTON_STATE = 22 164 createVerifiedMotionEventnull165 private fun createVerifiedMotionEvent(): VerifiedMotionEvent { 166 return VerifiedMotionEvent(DEVICE_ID, EVENT_TIME_NANOS, SOURCE, DISPLAY_ID, 167 RAW_X, RAW_Y, ACTION_MASKED, DOWN_TIME_NANOS, FLAGS, META_STATE, BUTTON_STATE) 168 } 169 compareVerifiedMotionEventsnull170 private fun compareVerifiedMotionEvents( 171 event1: VerifiedMotionEvent, 172 event2: VerifiedMotionEvent 173 ) { 174 assertEquals(event1, event2) 175 assertEquals(event1.hashCode(), event2.hashCode()) 176 177 assertEquals(event1.deviceId, event2.deviceId) 178 assertEquals(event1.eventTimeNanos, event2.eventTimeNanos) 179 assertEquals(event1.source, event2.source) 180 assertEquals(event1.displayId, event2.displayId) 181 182 assertEquals(event1.rawX, event2.rawX, 0f) 183 assertEquals(event1.rawY, event2.rawY, 0f) 184 assertEquals(event1.actionMasked, event2.actionMasked) 185 assertEquals(event1.downTimeNanos, event2.downTimeNanos) 186 assertEquals(event1.flags, event2.flags) 187 assertEquals(event1.metaState, event2.metaState) 188 assertEquals(event1.buttonState, event2.buttonState) 189 } 190 } 191 } 192