• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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.os.InputConstants.DEFAULT_DISPATCHING_TIMEOUT_MILLIS
20 import android.os.Looper
21 import android.view.InputChannel
22 import android.view.InputEvent
23 import android.view.InputEventReceiver
24 import android.view.InputEventSender
25 import android.view.KeyEvent
26 import android.view.MotionEvent
27 import java.util.concurrent.LinkedBlockingQueue
28 import java.util.concurrent.TimeUnit
29 
30 import org.junit.Assert.assertNull
31 
getEventnull32 private fun <T> getEvent(queue: LinkedBlockingQueue<T>): T? {
33     return queue.poll(DEFAULT_DISPATCHING_TIMEOUT_MILLIS.toLong(), TimeUnit.MILLISECONDS)
34 }
35 
assertNoEventsnull36 private fun <T> assertNoEvents(queue: LinkedBlockingQueue<T>) {
37     // Poll the queue with a shorter timeout, to make the check faster.
38     assertNull(queue.poll(100L, TimeUnit.MILLISECONDS))
39 }
40 
41 class SpyInputEventReceiver(channel: InputChannel, looper: Looper) :
42         InputEventReceiver(channel, looper) {
43     private val mInputEvents = LinkedBlockingQueue<InputEvent>()
44 
onInputEventnull45     override fun onInputEvent(event: InputEvent) {
46         when (event) {
47             is KeyEvent -> mInputEvents.put(KeyEvent.obtain(event))
48             is MotionEvent -> mInputEvents.put(MotionEvent.obtain(event))
49             else -> throw Exception("Received $event is neither a key nor a motion")
50         }
51         finishInputEvent(event, true /*handled*/)
52     }
53 
getInputEventnull54     fun getInputEvent(): InputEvent? {
55         return getEvent(mInputEvents)
56     }
57 }
58 
59 class SpyInputEventSender(channel: InputChannel, looper: Looper) :
60         InputEventSender(channel, looper) {
61     data class FinishedSignal(val seq: Int, val handled: Boolean)
62     data class Timeline(val inputEventId: Int, val gpuCompletedTime: Long, val presentTime: Long)
63 
64     private val mFinishedSignals = LinkedBlockingQueue<FinishedSignal>()
65     private val mTimelines = LinkedBlockingQueue<Timeline>()
66 
onInputEventFinishednull67     override fun onInputEventFinished(seq: Int, handled: Boolean) {
68         mFinishedSignals.put(FinishedSignal(seq, handled))
69     }
70 
onTimelineReportednull71     override fun onTimelineReported(inputEventId: Int, gpuCompletedTime: Long, presentTime: Long) {
72         mTimelines.put(Timeline(inputEventId, gpuCompletedTime, presentTime))
73     }
74 
getFinishedSignalnull75     fun getFinishedSignal(): FinishedSignal? {
76         return getEvent(mFinishedSignals)
77     }
78 
getTimelinenull79     fun getTimeline(): Timeline? {
80         return getEvent(mTimelines)
81     }
82 
assertNoEventsnull83     fun assertNoEvents() {
84         assertNoEvents(mFinishedSignals)
85         assertNoEvents(mTimelines)
86     }
87 }
88