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