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

Commit a4ec543f authored by Justin Ghan's avatar Justin Ghan
Browse files

Fix tests for handwriting initiator changes

ag/26416134 updated the handwriting initiator to track EditTexts even if
handwriting is disabled, which broke some tests. This change updates
HandwritingAreaTrackerTest and HandwritableViewInfoTest to use Views
instead of EditTexts.

Bug: 297962571
Bug: 330568768
Test: atest HandwritingInitiatorTest
Test: atest HandwritingAreaTrackerTest
Test: atest HandwritableViewInfoTest
Change-Id: Iedc68d4f7db9a71bd0ef98b48cf982101c0a2e45
parent 3b20234f
Loading
Loading
Loading
Loading
+5 −5
Original line number Diff line number Diff line
@@ -21,7 +21,7 @@ import static android.view.MotionEvent.ACTION_HOVER_MOVE;
import static android.view.MotionEvent.ACTION_MOVE;
import static android.view.MotionEvent.ACTION_UP;
import static android.view.inputmethod.Flags.initiationWithoutInputConnection;
import static android.view.stylus.HandwritingTestUtil.createView;
import static android.view.stylus.HandwritingTestUtil.createEditText;

import static com.android.text.flags.Flags.handwritingCursorPosition;

@@ -111,13 +111,13 @@ public class HandwritingInitiatorTest {
        mHandwritingInitiator =
                spy(new HandwritingInitiator(viewConfiguration, inputMethodManager));

        mTestView1 = createView(sHwArea1, /* autoHandwritingEnabled= */ true,
        mTestView1 = createEditText(sHwArea1, /* autoHandwritingEnabled= */ true,
                /* isStylusHandwritingAvailable= */ true,
                HW_BOUNDS_OFFSETS_LEFT_PX,
                HW_BOUNDS_OFFSETS_TOP_PX,
                HW_BOUNDS_OFFSETS_RIGHT_PX,
                HW_BOUNDS_OFFSETS_BOTTOM_PX);
        mTestView2 = createView(sHwArea2, /* autoHandwritingEnabled= */ true,
        mTestView2 = createEditText(sHwArea2, /* autoHandwritingEnabled= */ true,
                /* isStylusHandwritingAvailable= */ true,
                HW_BOUNDS_OFFSETS_LEFT_PX,
                HW_BOUNDS_OFFSETS_TOP_PX,
@@ -442,7 +442,7 @@ public class HandwritingInitiatorTest {
    @Test
    public void onTouchEvent_notStartHandwriting_whenHandwritingNotAvailable() {
        final Rect rect = new Rect(600, 600, 900, 900);
        final View testView = createView(rect, true /* autoHandwritingEnabled */,
        final View testView = createEditText(rect, true /* autoHandwritingEnabled */,
                false /* isStylusHandwritingAvailable */);
        mHandwritingInitiator.updateHandwritingAreasForView(testView);

@@ -756,7 +756,7 @@ public class HandwritingInitiatorTest {
            mTestView1.setHandwritingDelegatorCallback(null);
            mHandwritingInitiator.updateFocusedView(mTestView1, /*fromTouchEvent*/ true);
        } else {
            View mockView = createView(sHwArea1, false /* autoHandwritingEnabled */,
            View mockView = createEditText(sHwArea1, false /* autoHandwritingEnabled */,
                    true /* isStylusHandwritingAvailable */);
            mHandwritingInitiator.onInputConnectionCreated(mockView);
        }
+53 −25
Original line number Diff line number Diff line
@@ -33,26 +33,63 @@ import android.widget.EditText;

import androidx.test.platform.app.InstrumentationRegistry;

public class HandwritingTestUtil {
    public static EditText createView(Rect handwritingArea) {
class HandwritingTestUtil {
    static View createView(Rect handwritingArea) {
        return createView(handwritingArea, true /* autoHandwritingEnabled */,
                true /* isStylusHandwritingAvailable */);
    }

    public static EditText createView(Rect handwritingArea, boolean autoHandwritingEnabled,
    static View createView(Rect handwritingArea, boolean autoHandwritingEnabled,
            boolean isStylusHandwritingAvailable) {
        return createView(handwritingArea, autoHandwritingEnabled, isStylusHandwritingAvailable,
                0, 0, 0, 0);
    }

    public static EditText createView(Rect handwritingArea, boolean autoHandwritingEnabled,
    static View createView(Rect handwritingArea, boolean autoHandwritingEnabled,
            boolean isStylusHandwritingAvailable,
            float handwritingBoundsOffsetLeft, float handwritingBoundsOffsetTop,
            float handwritingBoundsOffsetRight, float handwritingBoundsOffsetBottom) {
        final Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation();
        final Context context = instrumentation.getTargetContext();
        // mock a parent so that HandwritingInitiator can get visible rect and hit region.
        final ViewGroup parent = new ViewGroup(context) {
        View view = spy(new View(context));
        mockSpy(view, handwritingArea, autoHandwritingEnabled, isStylusHandwritingAvailable,
                handwritingBoundsOffsetLeft, handwritingBoundsOffsetTop,
                handwritingBoundsOffsetRight, handwritingBoundsOffsetBottom);
        return view;
    }

    static EditText createEditText(Rect handwritingArea, boolean autoHandwritingEnabled,
            boolean isStylusHandwritingAvailable) {
        return createEditText(handwritingArea, autoHandwritingEnabled, isStylusHandwritingAvailable,
                0, 0, 0, 0);
    }

    static EditText createEditText(Rect handwritingArea, boolean autoHandwritingEnabled,
            boolean isStylusHandwritingAvailable,
            float handwritingBoundsOffsetLeft, float handwritingBoundsOffsetTop,
            float handwritingBoundsOffsetRight, float handwritingBoundsOffsetBottom) {
        final Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation();
        final Context context = instrumentation.getTargetContext();
        EditText view = spy(new EditText(context));
        doAnswer(invocation -> {
            int[] outLocation = invocation.getArgument(0);
            outLocation[0] = handwritingArea.left;
            outLocation[1] = handwritingArea.top;
            return null;
        }).when(view).getLocationInWindow(any());
        when(view.getOffsetForPosition(anyFloat(), anyFloat())).thenReturn(0);
        mockSpy(view, handwritingArea, autoHandwritingEnabled, isStylusHandwritingAvailable,
                handwritingBoundsOffsetLeft, handwritingBoundsOffsetTop,
                handwritingBoundsOffsetRight, handwritingBoundsOffsetBottom);
        return view;
    }

    private static void mockSpy(View viewSpy, Rect handwritingArea,
            boolean autoHandwritingEnabled, boolean isStylusHandwritingAvailable,
            float handwritingBoundsOffsetLeft, float handwritingBoundsOffsetTop,
            float handwritingBoundsOffsetRight, float handwritingBoundsOffsetBottom) {
        // Mock a parent so that HandwritingInitiator can get visible rect and hit region.
        final ViewGroup parent = new ViewGroup(viewSpy.getContext()) {
            @Override
            protected void onLayout(boolean changed, int l, int t, int r, int b) {
                // We don't layout this view.
@@ -72,24 +109,15 @@ public class HandwritingTestUtil {
            }
        };

        EditText view = spy(new EditText(context));
        when(view.isAttachedToWindow()).thenReturn(true);
        when(view.isAggregatedVisible()).thenReturn(true);
        when(view.isStylusHandwritingAvailable()).thenReturn(isStylusHandwritingAvailable);
        when(view.getHandwritingArea()).thenReturn(handwritingArea);
        when(view.getHandwritingBoundsOffsetLeft()).thenReturn(handwritingBoundsOffsetLeft);
        when(view.getHandwritingBoundsOffsetTop()).thenReturn(handwritingBoundsOffsetTop);
        when(view.getHandwritingBoundsOffsetRight()).thenReturn(handwritingBoundsOffsetRight);
        when(view.getHandwritingBoundsOffsetBottom()).thenReturn(handwritingBoundsOffsetBottom);
        doAnswer(invocation -> {
            int[] outLocation = invocation.getArgument(0);
            outLocation[0] = handwritingArea.left;
            outLocation[1] = handwritingArea.top;
            return null;
        }).when(view).getLocationInWindow(any());
        when(view.getOffsetForPosition(anyFloat(), anyFloat())).thenReturn(0);
        view.setAutoHandwritingEnabled(autoHandwritingEnabled);
        parent.addView(view);
        return view;
        when(viewSpy.isAttachedToWindow()).thenReturn(true);
        when(viewSpy.isAggregatedVisible()).thenReturn(true);
        when(viewSpy.isStylusHandwritingAvailable()).thenReturn(isStylusHandwritingAvailable);
        when(viewSpy.getHandwritingArea()).thenReturn(handwritingArea);
        when(viewSpy.getHandwritingBoundsOffsetLeft()).thenReturn(handwritingBoundsOffsetLeft);
        when(viewSpy.getHandwritingBoundsOffsetTop()).thenReturn(handwritingBoundsOffsetTop);
        when(viewSpy.getHandwritingBoundsOffsetRight()).thenReturn(handwritingBoundsOffsetRight);
        when(viewSpy.getHandwritingBoundsOffsetBottom()).thenReturn(handwritingBoundsOffsetBottom);
        viewSpy.setAutoHandwritingEnabled(autoHandwritingEnabled);
        parent.addView(viewSpy);
    }
}