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

Commit 7187056a authored by William Xiao's avatar William Xiao Committed by Android (Google) Code Review
Browse files

Merge "Convert ShadeTouchHandlerTest to Kotlin" into main

parents 79eaf40d 67ff3356
Loading
Loading
Loading
Loading
+169 −0
Original line number Diff line number Diff line
@@ -13,175 +13,157 @@
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.systemui.ambient.touch;


import static com.google.common.truth.Truth.assertThat;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import android.app.DreamManager;
import android.platform.test.annotations.DisableFlags;
import android.platform.test.annotations.EnableFlags;
import android.view.GestureDetector;
import android.view.MotionEvent;

import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.filters.SmallTest;

import com.android.systemui.Flags;
import com.android.systemui.SysuiTestCase;
import com.android.systemui.shade.ShadeViewController;
import com.android.systemui.shared.system.InputChannelCompat;
import com.android.systemui.statusbar.phone.CentralSurfaces;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;

import java.util.Optional;
package com.android.systemui.ambient.touch

import android.app.DreamManager
import android.platform.test.annotations.DisableFlags
import android.platform.test.annotations.EnableFlags
import android.view.GestureDetector
import android.view.MotionEvent
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.SmallTest
import com.android.systemui.Flags
import com.android.systemui.SysuiTestCase
import com.android.systemui.ambient.touch.TouchHandler.TouchSession
import com.android.systemui.shade.ShadeViewController
import com.android.systemui.shared.system.InputChannelCompat
import com.android.systemui.statusbar.phone.CentralSurfaces
import com.google.common.truth.Truth
import java.util.Optional
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.kotlin.any
import org.mockito.kotlin.argumentCaptor
import org.mockito.kotlin.clearInvocations
import org.mockito.kotlin.mock
import org.mockito.kotlin.never
import org.mockito.kotlin.times
import org.mockito.kotlin.verify
import org.mockito.kotlin.whenever

@SmallTest
@RunWith(AndroidJUnit4.class)
public class ShadeTouchHandlerTest extends SysuiTestCase {
    @Mock
    CentralSurfaces mCentralSurfaces;

    @Mock
    ShadeViewController mShadeViewController;

    @Mock
    DreamManager mDreamManager;
@RunWith(AndroidJUnit4::class)
class ShadeTouchHandlerTest : SysuiTestCase() {
    private var mCentralSurfaces = mock<CentralSurfaces>()
    private var mShadeViewController = mock<ShadeViewController>()
    private var mDreamManager = mock<DreamManager>()
    private var mTouchSession = mock<TouchSession>()

    @Mock
    TouchHandler.TouchSession mTouchSession;
    private lateinit var mTouchHandler: ShadeTouchHandler

    ShadeTouchHandler mTouchHandler;

    @Captor
    ArgumentCaptor<GestureDetector.OnGestureListener> mGestureListenerCaptor;
    @Captor
    ArgumentCaptor<InputChannelCompat.InputEventListener> mInputListenerCaptor;

    private static final int TOUCH_HEIGHT = 20;
    private var mGestureListenerCaptor = argumentCaptor<GestureDetector.OnGestureListener>()
    private var mInputListenerCaptor = argumentCaptor<InputChannelCompat.InputEventListener>()

    @Before
    public void setup() {
        MockitoAnnotations.initMocks(this);

        mTouchHandler = new ShadeTouchHandler(Optional.of(mCentralSurfaces), mShadeViewController,
                mDreamManager, TOUCH_HEIGHT);
    fun setup() {
        mTouchHandler =
            ShadeTouchHandler(
                Optional.of(mCentralSurfaces),
                mShadeViewController,
                mDreamManager,
                TOUCH_HEIGHT
            )
    }

    // Verifies that a swipe down in the gesture region is captured by the shade touch handler.
    @Test
    public void testSwipeDown_captured() {
        final boolean captured = swipe(Direction.DOWN);

        assertThat(captured).isTrue();
    fun testSwipeDown_captured() {
        val captured = swipe(Direction.DOWN)
        Truth.assertThat(captured).isTrue()
    }

    // Verifies that a swipe in the upward direction is not captured.
    @Test
    public void testSwipeUp_notCaptured() {
        final boolean captured = swipe(Direction.UP);
    fun testSwipeUp_notCaptured() {
        val captured = swipe(Direction.UP)

        // Motion events not captured as the swipe is going in the wrong direction.
        assertThat(captured).isFalse();
        Truth.assertThat(captured).isFalse()
    }

    // Verifies that a swipe down forwards captured touches to central surfaces for handling.
    @Test
    @EnableFlags(Flags.FLAG_COMMUNAL_HUB)
    public void testSwipeDown_communalEnabled_sentToCentralSurfaces() {
        swipe(Direction.DOWN);
    fun testSwipeDown_communalEnabled_sentToCentralSurfaces() {
        swipe(Direction.DOWN)

        // Both motion events are sent for central surfaces to process.
        verify(mCentralSurfaces, times(2)).handleExternalShadeWindowTouch(any());
        verify(mCentralSurfaces, times(2)).handleExternalShadeWindowTouch(any())
    }

    // Verifies that a swipe down forwards captured touches to the shade view for handling.
    @Test
    @DisableFlags(Flags.FLAG_COMMUNAL_HUB)
    public void testSwipeDown_communalDisabled_sentToShadeView() {
        swipe(Direction.DOWN);
    fun testSwipeDown_communalDisabled_sentToShadeView() {
        swipe(Direction.DOWN)

        // Both motion events are sent for the shade view to process.
        verify(mShadeViewController, times(2)).handleExternalTouch(any());
        verify(mShadeViewController, times(2)).handleExternalTouch(any())
    }

    // Verifies that a swipe down while dreaming forwards captured touches to the shade view for
    // handling.
    @Test
    public void testSwipeDown_dreaming_sentToShadeView() {
        when(mDreamManager.isDreaming()).thenReturn(true);

        swipe(Direction.DOWN);
    fun testSwipeDown_dreaming_sentToShadeView() {
        whenever(mDreamManager.isDreaming).thenReturn(true)
        swipe(Direction.DOWN)

        // Both motion events are sent for the shade view to process.
        verify(mShadeViewController, times(2)).handleExternalTouch(any());
        verify(mShadeViewController, times(2)).handleExternalTouch(any())
    }

    // Verifies that a swipe up is not forwarded to central surfaces.
    @Test
    @EnableFlags(Flags.FLAG_COMMUNAL_HUB)
    public void testSwipeUp_communalEnabled_touchesNotSent() {
        swipe(Direction.UP);
    fun testSwipeUp_communalEnabled_touchesNotSent() {
        swipe(Direction.UP)

        // Motion events are not sent for central surfaces to process as the swipe is going in the
        // wrong direction.
        verify(mCentralSurfaces, never()).handleExternalShadeWindowTouch(any());
        verify(mCentralSurfaces, never()).handleExternalShadeWindowTouch(any())
    }

    // Verifies that a swipe up is not forwarded to the shade view.
    @Test
    @DisableFlags(Flags.FLAG_COMMUNAL_HUB)
    public void testSwipeUp_communalDisabled_touchesNotSent() {
        swipe(Direction.UP);
    fun testSwipeUp_communalDisabled_touchesNotSent() {
        swipe(Direction.UP)

        // Motion events are not sent for the shade view to process as the swipe is going in the
        // wrong direction.
        verify(mShadeViewController, never()).handleExternalTouch(any());
        verify(mShadeViewController, never()).handleExternalTouch(any())
    }

    /**
     * Simulates a swipe in the given direction and returns true if the touch was intercepted by the
     * touch handler's gesture listener.
     * <p>
     *
     * Swipe down starts from a Y coordinate of 0 and goes downward. Swipe up starts from the edge
     * of the gesture region, {@link #TOUCH_HEIGHT}, and goes upward to 0.
     * of the gesture region, [.TOUCH_HEIGHT], and goes upward to 0.
     */
    private boolean swipe(Direction direction) {
        Mockito.clearInvocations(mTouchSession);
        mTouchHandler.onSessionStart(mTouchSession);

        verify(mTouchSession).registerGestureListener(mGestureListenerCaptor.capture());
        verify(mTouchSession).registerInputListener(mInputListenerCaptor.capture());

        final float startY = direction == Direction.UP ? TOUCH_HEIGHT : 0;
        final float endY = direction == Direction.UP ? 0 : TOUCH_HEIGHT;
    private fun swipe(direction: Direction): Boolean {
        clearInvocations(mTouchSession)
        mTouchHandler.onSessionStart(mTouchSession)
        verify(mTouchSession).registerGestureListener(mGestureListenerCaptor.capture())
        verify(mTouchSession).registerInputListener(mInputListenerCaptor.capture())
        val startY = (if (direction == Direction.UP) TOUCH_HEIGHT else 0).toFloat()
        val endY = (if (direction == Direction.UP) 0 else TOUCH_HEIGHT).toFloat()

        // Send touches to the input and gesture listener.
        final MotionEvent event1 = MotionEvent.obtain(0, 0, MotionEvent.ACTION_MOVE, 0, startY, 0);
        final MotionEvent event2 = MotionEvent.obtain(0, 0, MotionEvent.ACTION_MOVE, 0, endY, 0);
        mInputListenerCaptor.getValue().onInputEvent(event1);
        mInputListenerCaptor.getValue().onInputEvent(event2);
        final boolean captured = mGestureListenerCaptor.getValue().onScroll(event1, event2, 0,
                startY - endY);

        return captured;
        val event1 = MotionEvent.obtain(0, 0, MotionEvent.ACTION_MOVE, 0f, startY, 0)
        val event2 = MotionEvent.obtain(0, 0, MotionEvent.ACTION_MOVE, 0f, endY, 0)
        mInputListenerCaptor.lastValue.onInputEvent(event1)
        mInputListenerCaptor.lastValue.onInputEvent(event2)
        return mGestureListenerCaptor.lastValue.onScroll(event1, event2, 0f, startY - endY)
    }

    private enum class Direction {
        DOWN,
        UP
    }

    private enum Direction {
        DOWN, UP,
    companion object {
        private const val TOUCH_HEIGHT = 20
    }
}