From 5a14c39bbba60de25f72c7d89aa7b088a32d1f1d Mon Sep 17 00:00:00 2001 From: Gavin Williams Date: Wed, 19 Feb 2025 07:14:09 +0000 Subject: a11y: Add onChange() tests for AutoClickController Bug: 395947815 Test: atest AutoclickControllerTest Flag: com.android.server.accessibility.enable_autoclick_indicator Change-Id: I54ed737b7c0e9baa60ecb8a57f951810dbd28531 --- .../autoclick/AutoclickController.java | 5 + .../autoclick/AutoclickControllerTest.java | 140 +++++++++++++++++++++ 2 files changed, 145 insertions(+) diff --git a/services/accessibility/java/com/android/server/accessibility/autoclick/AutoclickController.java b/services/accessibility/java/com/android/server/accessibility/autoclick/AutoclickController.java index aa82df493f84..9ee7ec952ec1 100644 --- a/services/accessibility/java/com/android/server/accessibility/autoclick/AutoclickController.java +++ b/services/accessibility/java/com/android/server/accessibility/autoclick/AutoclickController.java @@ -230,6 +230,11 @@ public class AutoclickController extends BaseEventStreamTransformation { return Flags.enableAutoclickIndicator() && mAutoclickTypePanel.isPaused(); } + @VisibleForTesting + void onChangeForTesting(boolean selfChange, Uri uri) { + mAutoclickSettingsObserver.onChange(selfChange, uri); + } + /** * Observes autoclick setting values, and updates ClickScheduler delay and indicator size * whenever the setting value changes. diff --git a/services/tests/servicestests/src/com/android/server/accessibility/autoclick/AutoclickControllerTest.java b/services/tests/servicestests/src/com/android/server/accessibility/autoclick/AutoclickControllerTest.java index 0745c68fd337..17d8882b487c 100644 --- a/services/tests/servicestests/src/com/android/server/accessibility/autoclick/AutoclickControllerTest.java +++ b/services/tests/servicestests/src/com/android/server/accessibility/autoclick/AutoclickControllerTest.java @@ -41,6 +41,7 @@ import android.view.MotionEvent; import android.view.WindowManager; import android.view.accessibility.AccessibilityManager; +import com.android.internal.accessibility.util.AccessibilityUtils; import com.android.server.accessibility.AccessibilityTraceManager; import org.junit.After; @@ -79,7 +80,9 @@ public class AutoclickControllerTest { @After public void tearDown() { + mController.onDestroy(); mTestableLooper.processAllMessages(); + TestableLooper.remove(this); } @Test @@ -401,6 +404,133 @@ public class AutoclickControllerTest { .isNotEqualTo(initialScheduledTime); } + @Test + @EnableFlags(com.android.server.accessibility.Flags.FLAG_ENABLE_AUTOCLICK_INDICATOR) + public void onCursorAreaSizeSettingsChange_moveWithinCustomRadius_clickNotTriggered() { + // Move mouse to initialize autoclick panel before enabling ignore minor cursor movement. + injectFakeMouseActionHoverMoveEvent(); + enableIgnoreMinorCursorMovement(); + + // Set a custom cursor area size. + int customSize = 250; + Settings.Secure.putIntForUser(mTestableContext.getContentResolver(), + Settings.Secure.ACCESSIBILITY_AUTOCLICK_CURSOR_AREA_SIZE, + customSize, + mTestableContext.getUserId()); + mController.onChangeForTesting(/* selfChange= */ true, + Settings.Secure.getUriFor( + Settings.Secure.ACCESSIBILITY_AUTOCLICK_CURSOR_AREA_SIZE)); + assertThat(mController.mAutoclickIndicatorView.getRadiusForTesting()).isEqualTo(customSize); + + // Move the mouse down, less than customSize radius so a click is not triggered. + float moveDownY = customSize - 25; + MotionEvent hoverMove = MotionEvent.obtain( + /* downTime= */ 0, + /* eventTime= */ 150, + /* action= */ MotionEvent.ACTION_HOVER_MOVE, + /* x= */ 0f, + /* y= */ moveDownY, + /* metaState= */ 0); + hoverMove.setSource(InputDevice.SOURCE_MOUSE); + mController.onMotionEvent(hoverMove, hoverMove, /* policyFlags= */ 0); + assertThat(mController.mClickScheduler.getIsActiveForTesting()).isFalse(); + } + + @Test + @EnableFlags(com.android.server.accessibility.Flags.FLAG_ENABLE_AUTOCLICK_INDICATOR) + public void onCursorAreaSizeSettingsChange_moveOutsideCustomRadius_clickTriggered() { + // Move mouse to initialize autoclick panel before enabling ignore minor cursor movement. + injectFakeMouseActionHoverMoveEvent(); + enableIgnoreMinorCursorMovement(); + + // Set a custom cursor area size. + int customSize = 250; + Settings.Secure.putIntForUser(mTestableContext.getContentResolver(), + Settings.Secure.ACCESSIBILITY_AUTOCLICK_CURSOR_AREA_SIZE, + customSize, + mTestableContext.getUserId()); + mController.onChangeForTesting(/* selfChange= */ true, + Settings.Secure.getUriFor( + Settings.Secure.ACCESSIBILITY_AUTOCLICK_CURSOR_AREA_SIZE)); + assertThat(mController.mAutoclickIndicatorView.getRadiusForTesting()).isEqualTo(customSize); + + // Move the mouse right, greater than customSize radius so a click is triggered. + float moveRightX = customSize + 100; + MotionEvent hoverMove = MotionEvent.obtain( + /* downTime= */ 0, + /* eventTime= */ 200, + /* action= */ MotionEvent.ACTION_HOVER_MOVE, + /* x= */ moveRightX, + /* y= */ 0, + /* metaState= */ 0); + hoverMove.setSource(InputDevice.SOURCE_MOUSE); + mController.onMotionEvent(hoverMove, hoverMove, /* policyFlags= */ 0); + assertThat(mController.mClickScheduler.getIsActiveForTesting()).isTrue(); + } + + @Test + @EnableFlags(com.android.server.accessibility.Flags.FLAG_ENABLE_AUTOCLICK_INDICATOR) + public void onIgnoreCursorMovementFromSettingsChange_clickTriggered() { + // Send initial mouse movement. + injectFakeMouseActionHoverMoveEvent(); + + // Set a custom cursor area size. + int customSize = 250; + Settings.Secure.putIntForUser(mTestableContext.getContentResolver(), + Settings.Secure.ACCESSIBILITY_AUTOCLICK_CURSOR_AREA_SIZE, + customSize, + mTestableContext.getUserId()); + mController.onChangeForTesting(/* selfChange= */ true, + Settings.Secure.getUriFor( + Settings.Secure.ACCESSIBILITY_AUTOCLICK_CURSOR_AREA_SIZE)); + + // Move the mouse down less than customSize radius but ignore custom movement is not enabled + // so a click is triggered. + float moveDownY = customSize - 100; + MotionEvent hoverMove = MotionEvent.obtain( + /* downTime= */ 0, + /* eventTime= */ 150, + /* action= */ MotionEvent.ACTION_HOVER_MOVE, + /* x= */ 0f, + /* y= */ moveDownY, + /* metaState= */ 0); + hoverMove.setSource(InputDevice.SOURCE_MOUSE); + mController.onMotionEvent(hoverMove, hoverMove, /* policyFlags= */ 0); + assertThat(mController.mClickScheduler.getIsActiveForTesting()).isTrue(); + } + + @Test + @EnableFlags(com.android.server.accessibility.Flags.FLAG_ENABLE_AUTOCLICK_INDICATOR) + public void onIgnoreCursorMovementFromSettingsChange_clickNotTriggered() { + // Move mouse to initialize autoclick panel before enabling ignore minor cursor movement. + injectFakeMouseActionHoverMoveEvent(); + enableIgnoreMinorCursorMovement(); + + // Set a custom cursor area size. + int customSize = 250; + Settings.Secure.putIntForUser(mTestableContext.getContentResolver(), + Settings.Secure.ACCESSIBILITY_AUTOCLICK_CURSOR_AREA_SIZE, + customSize, + mTestableContext.getUserId()); + mController.onChangeForTesting(/* selfChange= */ true, + Settings.Secure.getUriFor( + Settings.Secure.ACCESSIBILITY_AUTOCLICK_CURSOR_AREA_SIZE)); + + // After enabling ignore custom movement, move the mouse right, less than customSize radius + // so a click won't be triggered. + float moveRightX = customSize - 100; + MotionEvent hoverMove = MotionEvent.obtain( + /* downTime= */ 0, + /* eventTime= */ 200, + /* action= */ MotionEvent.ACTION_HOVER_MOVE, + /* x= */ moveRightX, + /* y= */ 0, + /* metaState= */ 0); + hoverMove.setSource(InputDevice.SOURCE_MOUSE); + mController.onMotionEvent(hoverMove, hoverMove, /* policyFlags= */ 0); + assertThat(mController.mClickScheduler.getIsActiveForTesting()).isFalse(); + } + @Test @EnableFlags(com.android.server.accessibility.Flags.FLAG_ENABLE_AUTOCLICK_INDICATOR) public void pauseButton_flagOn_clickNotTriggeredWhenPaused() { @@ -473,4 +603,14 @@ public class AutoclickControllerTest { /* y= */ 0, /* metaState= */ 0); } + + private void enableIgnoreMinorCursorMovement() { + Settings.Secure.putIntForUser(mTestableContext.getContentResolver(), + Settings.Secure.ACCESSIBILITY_AUTOCLICK_IGNORE_MINOR_CURSOR_MOVEMENT, + AccessibilityUtils.State.ON, + mTestableContext.getUserId()); + mController.onChangeForTesting(/* selfChange= */ true, + Settings.Secure.getUriFor( + Settings.Secure.ACCESSIBILITY_AUTOCLICK_IGNORE_MINOR_CURSOR_MOVEMENT)); + } } -- cgit v1.2.3-59-g8ed1b