xref: /aosp_15_r20/frameworks/base/tests/Input/src/com/android/test/input/CaptureEventActivity.kt (revision d57664e9bc4670b3ecf6748a746a57c557b6bc9e)
1 /*
2  * Copyright 2024 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 com.android.test.input
18 
19 import android.app.Activity
20 import android.os.Bundle
21 import android.view.InputEvent
22 import android.view.KeyEvent
23 import android.view.MotionEvent
24 import java.util.concurrent.LinkedBlockingQueue
25 import java.util.concurrent.TimeUnit
26 import org.junit.Assert.assertNull
27 
28 class CaptureEventActivity : Activity() {
29     private val events = LinkedBlockingQueue<InputEvent>()
30     var shouldHandleKeyEvents = true
31 
onCreatenull32     override fun onCreate(savedInstanceState: Bundle?) {
33         super.onCreate(savedInstanceState)
34 
35         // Set the fixed orientation if requested
36         if (intent.hasExtra(EXTRA_FIXED_ORIENTATION)) {
37             val orientation = intent.getIntExtra(EXTRA_FIXED_ORIENTATION, 0)
38             setRequestedOrientation(orientation)
39         }
40 
41         // Set the flag if requested
42         if (intent.hasExtra(EXTRA_WINDOW_FLAGS)) {
43             val flags = intent.getIntExtra(EXTRA_WINDOW_FLAGS, 0)
44             window.addFlags(flags)
45         }
46     }
47 
dispatchGenericMotionEventnull48     override fun dispatchGenericMotionEvent(ev: MotionEvent?): Boolean {
49         events.add(MotionEvent.obtain(ev))
50         return true
51     }
52 
dispatchTouchEventnull53     override fun dispatchTouchEvent(ev: MotionEvent?): Boolean {
54         events.add(MotionEvent.obtain(ev))
55         return true
56     }
57 
dispatchKeyEventnull58     override fun dispatchKeyEvent(event: KeyEvent?): Boolean {
59         events.add(KeyEvent(event))
60         return shouldHandleKeyEvents
61     }
62 
dispatchTrackballEventnull63     override fun dispatchTrackballEvent(ev: MotionEvent?): Boolean {
64         events.add(MotionEvent.obtain(ev))
65         return true
66     }
67 
getInputEventnull68     fun getInputEvent(): InputEvent? {
69         return events.poll(5, TimeUnit.SECONDS)
70     }
71 
hasReceivedEventsnull72     fun hasReceivedEvents(): Boolean {
73         return !events.isEmpty()
74     }
75 
assertNoEventsnull76     fun assertNoEvents() {
77         val event = events.poll(100, TimeUnit.MILLISECONDS)
78         assertNull("Expected no events, but received $event", event)
79     }
80 
81     companion object {
82         const val EXTRA_FIXED_ORIENTATION = "fixed_orientation"
83         const val EXTRA_WINDOW_FLAGS = "window_flags"
84     }
85 }
86