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

Commit a355f47b authored by Hiroki Sato's avatar Hiroki Sato
Browse files

Fix LetterboxScrollProcessor correctly recycle event

When the event is synthesized inside the processor, it's processor's
responsibility to recyle the event.

Bug: none
Test: LetterboxScrollProcessorTest
Flag: EXEMPT bugfix
Change-Id: I434ed23ad683abc79bac1f5ee288633be8104c6f
parent 78ed9b43
Loading
Loading
Loading
Loading
+5 −1
Original line number Diff line number Diff line
@@ -159,7 +159,11 @@ public class LetterboxScrollProcessor {
     */
    @Nullable
    public InputEvent processInputEventBeforeFinish(@NonNull InputEvent inputEvent) {
        return mGeneratedEventIds.remove(inputEvent.getId()) ? null : inputEvent;
        if (mGeneratedEventIds.remove(inputEvent.getId())) {
            inputEvent.recycleIfNeededAfterDispatch();
            return null;
        }
        return inputEvent;
    }

    @NonNull
+13 −0
Original line number Diff line number Diff line
@@ -24,6 +24,8 @@ import static android.view.MotionEvent.ACTION_UP;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import android.content.Context;
import android.graphics.Rect;
@@ -40,6 +42,7 @@ import androidx.test.platform.app.InstrumentationRegistry;

import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;

import java.util.ArrayList;
import java.util.List;
@@ -193,6 +196,16 @@ public class LetterboxScrollProcessorTest {

        // Ensure all processed events (that are not ignored) are offset over the app.
        assertXCoordinatesAdjustedToZero(scrollGestureEvents, processedEvents);

        // The first event is synthesized event. Ensure it is recycled in the processor.
        // Mockito cannot mock final class, the event is faked by returning the same ID.
        final InputEvent mockEvent = Mockito.mock(InputEvent.class);
        when(mockEvent.getId()).thenReturn(processedEvents.getFirst().getId());
        final InputEvent firstEventToBeFinished =
                mLetterboxScrollProcessor.processInputEventBeforeFinish(mockEvent);
        verify(mockEvent).recycleIfNeededAfterDispatch();
        assertNull(firstEventToBeFinished);

        // Except the first generated ACTION_DOWN event, ensure the following events should be
        // finished (these events should not be generated).
        assertMotionEventsShouldBeFinished(processedEvents.subList(1, processedEvents.size()));