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.input.cts 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 onCreatenull31 override fun onCreate(savedInstanceState: Bundle?) { 32 super.onCreate(savedInstanceState) 33 34 // Set the fixed orientation if requested 35 if (intent.hasExtra(EXTRA_FIXED_ORIENTATION)) { 36 val orientation = intent.getIntExtra(EXTRA_FIXED_ORIENTATION, 0) 37 setRequestedOrientation(orientation) 38 } 39 40 // Set the flag if requested 41 if (intent.hasExtra(EXTRA_WINDOW_FLAGS)) { 42 val flags = intent.getIntExtra(EXTRA_WINDOW_FLAGS, 0) 43 window.addFlags(flags) 44 } 45 } 46 dispatchGenericMotionEventnull47 override fun dispatchGenericMotionEvent(ev: MotionEvent?): Boolean { 48 events.add(MotionEvent.obtain(ev)) 49 return true 50 } 51 dispatchTouchEventnull52 override fun dispatchTouchEvent(ev: MotionEvent?): Boolean { 53 events.add(MotionEvent.obtain(ev)) 54 return true 55 } 56 dispatchKeyEventnull57 override fun dispatchKeyEvent(event: KeyEvent?): Boolean { 58 events.add(KeyEvent(event)) 59 return true 60 } 61 dispatchTrackballEventnull62 override fun dispatchTrackballEvent(ev: MotionEvent?): Boolean { 63 events.add(MotionEvent.obtain(ev)) 64 return true 65 } 66 getInputEventnull67 fun getInputEvent(): InputEvent? { 68 return events.poll(5, TimeUnit.SECONDS) 69 } 70 assertNoEventsnull71 fun assertNoEvents() { 72 val event = events.poll(100, TimeUnit.MILLISECONDS) 73 assertNull("Expected no events, but received $event", event) 74 } 75 76 companion object { 77 const val EXTRA_FIXED_ORIENTATION = "fixed_orientation" 78 const val EXTRA_WINDOW_FLAGS = "window_flags" 79 } 80 } 81