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

Commit c59d69f3 authored by Treehugger Robot's avatar Treehugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Fix tests for handwriting initiator changes" into main

parents 06acd760 a4ec543f
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;

@@ -112,13 +112,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,
@@ -412,7 +412,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);

@@ -717,7 +717,7 @@ public class HandwritingInitiatorTest {
            mTestView1.setHandwritingDelegatorCallback(null);
            onEditorFocusedOrConnectionCreated(mTestView1);
        } else {
            View mockView = createView(sHwArea1, false /* autoHandwritingEnabled */,
            View mockView = createEditText(sHwArea1, false /* autoHandwritingEnabled */,
                    true /* isStylusHandwritingAvailable */);
            onEditorFocusedOrConnectionCreated(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);
    }
}