• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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.cts.input
18 
19 import android.view.InputEvent
20 import android.view.KeyEvent
21 import android.view.MotionEvent
22 import org.hamcrest.Matcher
23 import org.hamcrest.MatcherAssert.assertThat
24 import org.junit.Assert.fail
25 
26 class EventVerifier(val getInputEvent: () -> InputEvent?) {
27     @JvmOverloads
assertReceivedMotionnull28     fun assertReceivedMotion(matcher: Matcher<MotionEvent>, msg: String? = null) {
29         val event = getMotionEvent()
30         assertThat(msg ?: "Additional MotionEvent checks", event, matcher)
31     }
32 
getMotionEventnull33     private fun getMotionEvent(): MotionEvent {
34         val event = getInputEvent() ?: fail("Failed to receive input event")
35         val motionEvent =
36             event as? MotionEvent ?: fail("Instead of MotionEvent, got: $event")
37         return motionEvent as MotionEvent
38     }
39 
assertReceivedKeynull40     fun assertReceivedKey(matcher: Matcher<KeyEvent>) {
41         val event = getKeyEvent()
42         assertThat("Additional KeyEvent checks", event, matcher)
43     }
44 
getKeyEventnull45     private fun getKeyEvent(): KeyEvent {
46         val event = getInputEvent() ?: fail("Failed to receive input event")
47         val keyEvent =
48             event as? KeyEvent ?: fail("Instead of KeyEvent, got: $event")
49         return keyEvent as KeyEvent
50     }
51 }
52