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

Commit 018cab41 authored by Gavin Williams's avatar Gavin Williams
Browse files

Autoclick: Test scroll and drag click types

Scroll: Test all 4 scroll directions

Drag: Drag the button a specific distance

Bug: 411187653, 411186857
Test: AutoclickClickTypeTests
Flag: com.android.server.accessibility.enable_autoclick_indicator
Change-Id: I09bd2215fbdab7c08971cce62aa34fb20b6d5a78
parent bfcd3c33
Loading
Loading
Loading
Loading
+130 −9
Original line number Diff line number Diff line
@@ -32,10 +32,12 @@ import android.widget.Button
import android.widget.LinearLayout
import androidx.test.ext.junit.rules.ActivityScenarioRule
import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.uiautomator.By
import androidx.test.uiautomator.Configurator
import androidx.test.uiautomator.UiDevice
import com.android.compatibility.common.util.SettingsStateChangerRule
import com.android.server.accessibility.Flags
import kotlin.test.assertEquals
import org.junit.AfterClass
import org.junit.Before
import org.junit.BeforeClass
@@ -85,14 +87,37 @@ class AutoclickClickTypeTests {
        }
    }

    private fun moveMouseToView(view: View) {
    private fun getViewCenter(view: View): Pair<Int, Int> {
        val xOnScreen = view.locationOnScreen[0]
        val yOnScreen = view.locationOnScreen[1]
        val centerX = xOnScreen + (view.width / 2)
        val centerY = yOnScreen + (view.height / 2)
        return Pair(centerX, centerY)
    }

    // Move the mouse to the center of the view
    private fun moveMouseToView(view: View) {
        val (centerX, centerY) = getViewCenter(view)
        desktopMouseTestRule.move(DEFAULT_DISPLAY, centerX, centerY)
    }

    // Move the mouse a given distance away from the center of the view.
    private fun moveMouseAwayFromView(view: View, deltaX: Int, deltaY: Int) {
        val (centerX, centerY) = getViewCenter(view)
        desktopMouseTestRule.move(DEFAULT_DISPLAY, centerX + deltaX, centerY + deltaY)
    }

    private fun moveMouseToScrollButton(resourceId: String) {
        val scrollButton = findObject(
            uiDevice, By.res(resourceId)
        )
        desktopMouseTestRule.move(
            DEFAULT_DISPLAY,
            scrollButton.visibleCenter.x,
            scrollButton.visibleCenter.y
        )
    }

    @Test
    fun performLeftClick_buttonReflectsClickType() {
        changeClickType(uiDevice, desktopMouseTestRule, LEFT_CLICK_BUTTON_LAYOUT_ID)
@@ -133,10 +158,73 @@ class AutoclickClickTypeTests {
        }
    }

    @Test
    fun performDrag_buttonReflectsClickType() {
        val (testClickButtonInitialX, testClickButtonInitialY) = getViewCenter(testClickButton)

        changeClickType(uiDevice, desktopMouseTestRule, DRAG_CLICK_BUTTON_LAYOUT_ID)
        moveMouseToView(testClickButton)

        // Wait until the button detects a long press, this confirms the initial drag click has
        // completed.
        waitAndAssert {
            testClickButton.text == LONG_PRESS_TEXT
        }

        val dragDistanceX = 50
        val dragDistanceY = 100
        moveMouseAwayFromView(testClickButton, dragDistanceX, dragDistanceY)

        // Wait for the click type to switch back to left click which signals the drag is done.
        findObject(uiDevice, By.res(LEFT_CLICK_BUTTON_LAYOUT_ID))

        // Use a small tolerance when verifying the new button location to account for the autoclick
        // default slop.
        val (testClickButtonCurrentX, testClickButtonCurrentY) = getViewCenter(testClickButton)
        assertEquals(
            testClickButtonCurrentX.toDouble(),
            (testClickButtonInitialX + dragDistanceX).toDouble(),
            20.0
        )
        assertEquals(
            testClickButtonCurrentY.toDouble(),
            (testClickButtonInitialY + dragDistanceY).toDouble(),
            20.0
        )
    }

    @Test
    fun performScroll_buttonReflectsClickType() {
        changeClickType(uiDevice, desktopMouseTestRule, SCROLL_BUTTON_LAYOUT_ID)
        moveMouseToView(testClickButton)

        moveMouseToScrollButton(SCROLL_UP_BUTTON_LAYOUT_ID)
        waitAndAssert {
            testClickButton.text == SCROLL_UP_TEXT
        }

        moveMouseToScrollButton(SCROLL_DOWN_BUTTON_LAYOUT_ID)
        waitAndAssert {
            testClickButton.text == SCROLL_DOWN_TEXT
        }

        moveMouseToScrollButton(SCROLL_LEFT_BUTTON_LAYOUT_ID)
        waitAndAssert {
            testClickButton.text == SCROLL_LEFT_TEXT
        }

        moveMouseToScrollButton(SCROLL_RIGHT_BUTTON_LAYOUT_ID)
        waitAndAssert {
            testClickButton.text == SCROLL_RIGHT_TEXT
        }
    }

    // Test activity responsible for receiving clicks and updating its UI depending on the click
    // type.
    class TestClickActivity : Activity() {
        private lateinit var gestureDetector: GestureDetector
        private var initialX = 0f
        private var initialY = 0f

        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
@@ -161,19 +249,48 @@ class AutoclickClickTypeTests {
                        testButton.text = LONG_PRESS_TEXT
                    }
                })
            testButton.setOnTouchListener { _, event ->

            testButton.setOnTouchListener { view, event ->
                // Move the button when drag detected.
                when (event.actionMasked) {
                    MotionEvent.ACTION_DOWN -> {
                        // Store offset from touch point to buttons's top-left corner.
                        initialX = event.rawX - view.x
                        initialY = event.rawY - view.y
                    }

                    MotionEvent.ACTION_MOVE -> {
                        // Set buttons's new position directly based on mouse's raw position and initial offset.
                        view.x = event.rawX - initialX
                        view.y = event.rawY - initialY
                    }
                }

                gestureDetector.onTouchEvent(event)
            }

            // Right click listener.
            // Right click and scroll listener.
            val genericMotionListener = View.OnGenericMotionListener { _, motionEvent ->
                if (motionEvent.isFromSource(InputDevice.SOURCE_MOUSE)
                    && motionEvent.action == MotionEvent.ACTION_BUTTON_PRESS
                    && motionEvent.actionButton == MotionEvent.BUTTON_SECONDARY
                ) {
                if (motionEvent.isFromSource(InputDevice.SOURCE_MOUSE)) {
                    if (motionEvent.action == MotionEvent.ACTION_SCROLL) {
                        val vScroll = motionEvent.getAxisValue(MotionEvent.AXIS_VSCROLL, 0)
                        val hScroll = motionEvent.getAxisValue(MotionEvent.AXIS_HSCROLL, 0)

                        if (vScroll > 0) {
                            testButton.text = SCROLL_UP_TEXT
                        } else if (vScroll < 0) {
                            testButton.text = SCROLL_DOWN_TEXT
                        } else if (hScroll > 0) {
                            testButton.text = SCROLL_LEFT_TEXT
                        } else if (hScroll < 0) {
                            testButton.text = SCROLL_RIGHT_TEXT
                        }
                        true
                    } else if (motionEvent.action == MotionEvent.ACTION_BUTTON_PRESS && motionEvent.actionButton == MotionEvent.BUTTON_SECONDARY) {
                        testButton.text = RIGHT_CLICK_TEXT
                        true
                    }
                }
                false
            }
            testButton.setOnGenericMotionListener(genericMotionListener)
@@ -191,6 +308,10 @@ class AutoclickClickTypeTests {
        private val DOUBLE_CLICK_TEXT = "Double Clicked!"
        private val RIGHT_CLICK_TEXT = "Right Clicked!"
        private val LONG_PRESS_TEXT = "Long Press Clicked!"
        private val SCROLL_UP_TEXT = "Scrolled Up!"
        private val SCROLL_DOWN_TEXT = "Scrolled Down!"
        private val SCROLL_LEFT_TEXT = "Scrolled Left!"
        private val SCROLL_RIGHT_TEXT = "Scrolled Right!"

        @BeforeClass
        @JvmStatic
+6 −0
Original line number Diff line number Diff line
@@ -37,6 +37,12 @@ val RIGHT_CLICK_BUTTON_LAYOUT_ID =
    "android:id/accessibility_autoclick_right_click_layout"
val DOUBLE_CLICK_BUTTON_LAYOUT_ID =
    "android:id/accessibility_autoclick_double_click_layout"
val DRAG_CLICK_BUTTON_LAYOUT_ID = "android:id/accessibility_autoclick_drag_layout"
val SCROLL_BUTTON_LAYOUT_ID = "android:id/accessibility_autoclick_scroll_layout"
val SCROLL_UP_BUTTON_LAYOUT_ID = "android:id/scroll_up"
val SCROLL_DOWN_BUTTON_LAYOUT_ID = "android:id/scroll_down"
val SCROLL_LEFT_BUTTON_LAYOUT_ID = "android:id/scroll_left"
val SCROLL_RIGHT_BUTTON_LAYOUT_ID = "android:id/scroll_right"
val CLICK_TYPE_BUTTON_GROUP_ID =
    "android:id/accessibility_autoclick_click_type_button_group_container"
val PAUSE_BUTTON_LAYOUT_ID = "android:id/accessibility_autoclick_pause_layout"