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

Commit 834025aa authored by Siarhei Vishniakou's avatar Siarhei Vishniakou
Browse files

Use uinput-based injection in AnrTest

Prior to this CL, we were injecting events into the unresponsive
activity using "injectInputEvent". The up event was missing, which would
cause the pointer to linger in the gesture monitors. Later in
the test, we were clicking the "close" button. This click would start to
interact with the previous unreleased pointer, causing the notification
shade to get pulled down.

In this CL, we switch to uinput-based injection to make it hermetic.

Bug: 343912075
Test: atest com.android.test.input.AnrTest
Change-Id: I295963080203ee64f50ed9890086b945665c204e
parent da37b50d
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -31,6 +31,7 @@ android_test {
        "androidx.test.runner",
        "androidx.test.uiautomator_uiautomator",
        "compatibility-device-util-axt",
        "cts-input-lib",
        "flag-junit",
        "frameworks-base-testutils",
        "hamcrest-library",
+17 −9
Original line number Diff line number Diff line
@@ -21,21 +21,23 @@ import androidx.test.filters.MediumTest

import android.app.ActivityManager
import android.app.ApplicationExitInfo
import android.content.Context
import android.graphics.Rect
import android.hardware.display.DisplayManager
import android.os.Build
import android.os.IInputConstants.UNMULTIPLIED_DEFAULT_DISPATCHING_TIMEOUT_MILLIS
import android.os.SystemClock
import android.provider.Settings
import android.provider.Settings.Global.HIDE_ERROR_DIALOGS
import android.testing.PollingCheck
import android.view.InputDevice
import android.view.MotionEvent

import androidx.test.uiautomator.By
import androidx.test.uiautomator.UiDevice
import androidx.test.uiautomator.UiObject2
import androidx.test.uiautomator.Until

import com.android.cts.input.UinputTouchScreen

import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
@@ -150,6 +152,18 @@ class AnrTest {
        assertEquals(ApplicationExitInfo.REASON_ANR, reasons[0].reason)
    }

    private fun clickOnObject(obj: UiObject2) {
        val displayManager =
            instrumentation.context.getSystemService(Context.DISPLAY_SERVICE) as DisplayManager
        val display = displayManager.getDisplay(obj.getDisplayId())
        val touchScreen = UinputTouchScreen(instrumentation, display)

        val rect: Rect = obj.visibleBounds
        val pointer = touchScreen.touchDown(rect.centerX(), rect.centerY())
        pointer.lift()
        touchScreen.close()
    }

    private fun triggerAnr() {
        startUnresponsiveActivity()
        val uiDevice: UiDevice = UiDevice.getInstance(instrumentation)
@@ -160,13 +174,7 @@ class AnrTest {
            return
        }

        val rect: Rect = obj.visibleBounds
        val downTime = SystemClock.uptimeMillis()
        val downEvent = MotionEvent.obtain(downTime, downTime,
                MotionEvent.ACTION_DOWN, rect.left.toFloat(), rect.top.toFloat(), 0 /* metaState */)
        downEvent.source = InputDevice.SOURCE_TOUCHSCREEN

        instrumentation.uiAutomation.injectInputEvent(downEvent, false /* sync*/)
        clickOnObject(obj)

        SystemClock.sleep(DISPATCHING_TIMEOUT.toLong()) // default ANR timeout for gesture monitors
    }