Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 8ef362fe authored by Siarhei Vishniakou's avatar Siarhei Vishniakou
Browse files

InputTests: add assertReceivedFinishedSignal API

This will make the assertions about receiving 'finished' signal a bit
simpler.

Bug: 376713684
Test: atest InputEventSenderAndReceiverTest
Flag: EXEMPT refactor
Change-Id: I31b17411e893ab6ac26cd28cc923b38f4a879b10
parent adbf06a0
Loading
Loading
Loading
Loading
+5 −7
Original line number Original line Diff line number Diff line
@@ -92,13 +92,12 @@ class InputEventSenderAndReceiverTest {
        val seq = 10
        val seq = 10
        mSender.sendInputEvent(seq, key)
        mSender.sendInputEvent(seq, key)
        val receivedKey = mReceiver.getInputEvent() as KeyEvent
        val receivedKey = mReceiver.getInputEvent() as KeyEvent
        val finishedSignal = mSender.getFinishedSignal()


        // Check receiver
        // Check receiver
        assertKeyEvent(key, receivedKey)
        assertKeyEvent(key, receivedKey)


        // Check sender
        // Check sender
        assertEquals(SpyInputEventSender.FinishedSignal(seq, handled = true), finishedSignal)
        mSender.assertReceivedFinishedSignal(seq, handled = true)
    }
    }


    // The timeline case is slightly unusual because it goes from InputConsumer to InputPublisher.
    // The timeline case is slightly unusual because it goes from InputConsumer to InputPublisher.
@@ -135,13 +134,13 @@ class InputEventSenderAndReceiverTest {
    @Test
    @Test
    fun testCrashingReceiverDoesNotCrash() {
    fun testCrashingReceiverDoesNotCrash() {
        val channels = InputChannel.openInputChannelPair("TestChannel2")
        val channels = InputChannel.openInputChannelPair("TestChannel2")
        val sender = SpyInputEventSender(channels[0], mHandlerThread.getLooper())
        val sender = SpyInputEventSender(channels[0], mHandlerThread.looper)


        // Need a separate thread for the receiver so that the sender can still get the response
        // Need a separate thread for the receiver so that the sender can still get the response
        // after the receiver crashes
        // after the receiver crashes
        val receiverThread = HandlerThread("Receive input events")
        val receiverThread = HandlerThread("Crash when input event comes in")
        receiverThread.start()
        receiverThread.start()
        val crashingReceiver = CrashingInputEventReceiver(channels[1], receiverThread.getLooper())
        val crashingReceiver = CrashingInputEventReceiver(channels[1], receiverThread.looper)
        receiverThread.setUncaughtExceptionHandler { thread, exception ->
        receiverThread.setUncaughtExceptionHandler { thread, exception ->
            if (thread == receiverThread && exception is IllegalArgumentException) {
            if (thread == receiverThread && exception is IllegalArgumentException) {
                // do nothing - this is the exception that we need to ignore
                // do nothing - this is the exception that we need to ignore
@@ -153,8 +152,7 @@ class InputEventSenderAndReceiverTest {
        val key = getTestKeyEvent()
        val key = getTestKeyEvent()
        val seq = 11
        val seq = 11
        sender.sendInputEvent(seq, key)
        sender.sendInputEvent(seq, key)
        val finishedSignal = sender.getFinishedSignal()
        sender.assertReceivedFinishedSignal(seq, handled = true)
        assertEquals(SpyInputEventSender.FinishedSignal(seq, handled = true), finishedSignal)


        // Clean up
        // Clean up
        crashingReceiver.dispose()
        crashingReceiver.dispose()
+2 −3
Original line number Original line Diff line number Diff line
@@ -24,7 +24,6 @@ import android.view.WindowManagerPolicyConstants.PointerEventListener
import com.android.server.UiThread
import com.android.server.UiThread
import com.android.server.wm.PointerEventDispatcher
import com.android.server.wm.PointerEventDispatcher
import org.junit.After
import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Before
import org.junit.Before
import org.junit.Test
import org.junit.Test


@@ -87,11 +86,11 @@ class PointerEventDispatcherTest {
        motionEvent.source = InputDevice.SOURCE_TOUCHSCREEN
        motionEvent.source = InputDevice.SOURCE_TOUCHSCREEN
        val seq = 10
        val seq = 10
        mSender.sendInputEvent(seq, motionEvent)
        mSender.sendInputEvent(seq, motionEvent)
        val finishedSignal = mSender.getFinishedSignal()


        // Since the listener raises an exception during the event handling, the event should be
        // Since the listener raises an exception during the event handling, the event should be
        // marked as 'not handled'.
        // marked as 'not handled'.
        assertEquals(SpyInputEventSender.FinishedSignal(seq, handled = false), finishedSignal)
        mSender.assertReceivedFinishedSignal(seq, handled = false)

        // Ensure that there aren't double finish calls.
        // Ensure that there aren't double finish calls.
        mSender.assertNoEvents()
        mSender.assertNoEvents()
    }
    }
+21 −7
Original line number Original line Diff line number Diff line
@@ -26,6 +26,8 @@ import android.view.KeyEvent
import android.view.MotionEvent
import android.view.MotionEvent
import java.util.concurrent.LinkedBlockingQueue
import java.util.concurrent.LinkedBlockingQueue
import java.util.concurrent.TimeUnit
import java.util.concurrent.TimeUnit
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotNull
import org.junit.Assert.assertNull
import org.junit.Assert.assertNull


private fun <T> getEvent(queue: LinkedBlockingQueue<T>): T? {
private fun <T> getEvent(queue: LinkedBlockingQueue<T>): T? {
@@ -39,19 +41,28 @@ private fun <T> assertNoEvents(queue: LinkedBlockingQueue<T>) {


class SpyInputEventReceiver(channel: InputChannel, looper: Looper) :
class SpyInputEventReceiver(channel: InputChannel, looper: Looper) :
    InputEventReceiver(channel, looper) {
    InputEventReceiver(channel, looper) {
    private val mInputEvents = LinkedBlockingQueue<InputEvent>()

    private val inputEvents = LinkedBlockingQueue<InputEvent>()


    override fun onInputEvent(event: InputEvent) {
    override fun onInputEvent(event: InputEvent) {
        addInputEvent(event)
        finishInputEvent(event, true /*handled*/)
    }

    private fun addInputEvent(event: InputEvent) {
        when (event) {
        when (event) {
            is KeyEvent -> mInputEvents.put(KeyEvent.obtain(event))
            is KeyEvent -> inputEvents.put(KeyEvent.obtain(event))
            is MotionEvent -> mInputEvents.put(MotionEvent.obtain(event))
            is MotionEvent -> inputEvents.put(MotionEvent.obtain(event))
            else -> throw Exception("Received $event is neither a key nor a motion")
            else -> throw Exception("Received $event is neither a key nor a motion")
        }
        }
        finishInputEvent(event, true /*handled*/)
    }
    }


    fun getInputEvent(): InputEvent? {
    fun getInputEvent(): InputEvent? {
        return getEvent(mInputEvents)
        return getEvent(inputEvents)
    }

    fun assertNoEvents() {
        assertNoEvents(inputEvents)
    }
    }
}
}


@@ -72,8 +83,11 @@ class SpyInputEventSender(channel: InputChannel, looper: Looper) :
        mTimelines.put(Timeline(inputEventId, gpuCompletedTime, presentTime))
        mTimelines.put(Timeline(inputEventId, gpuCompletedTime, presentTime))
    }
    }


    fun getFinishedSignal(): FinishedSignal? {
    fun assertReceivedFinishedSignal(seq: Int, handled: Boolean) {
        return getEvent(mFinishedSignals)
        val finished = getEvent(mFinishedSignals)
        assertNotNull("Did not receive 'finished' event", finished)
        assertEquals("Unexpected sequence number", seq, finished!!.seq)
        assertEquals("Unexpected 'handled' value", handled, finished!!.handled)
    }
    }


    fun getTimeline(): Timeline? {
    fun getTimeline(): Timeline? {