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

Commit 41f05198 authored by Xiaowen Lei's avatar Xiaowen Lei
Browse files

Fix confusion related to EXPANSION_HIDDEN/VISIBLE and improve related test coverage.

Also fixed arguments for one FlingAnimationUtils.apply(...) call and
added test coverage for it.

Bug: 225379274
Test: atest BouncerSwipeTouchHandlerTest
Test: manually on device
Change-Id: Ie0c7ceb37866fdce7988b6fc6468235374bb7731
parent bc3aaf19
Loading
Loading
Loading
Loading
+22 −17
Original line number Diff line number Diff line
@@ -31,8 +31,8 @@ import android.view.MotionEvent;
import android.view.VelocityTracker;

import com.android.systemui.statusbar.NotificationShadeWindowController;
import com.android.systemui.statusbar.phone.KeyguardBouncer;
import com.android.systemui.statusbar.phone.CentralSurfaces;
import com.android.systemui.statusbar.phone.KeyguardBouncer;
import com.android.systemui.statusbar.phone.StatusBarKeyguardViewManager;
import com.android.wm.shell.animation.FlingAnimationUtils;

@@ -134,9 +134,9 @@ public class BouncerSwipeTouchHandler implements DreamTouchHandler {
            NotificationShadeWindowController notificationShadeWindowController,
            ValueAnimatorCreator valueAnimatorCreator,
            VelocityTrackerFactory velocityTrackerFactory,
            @Named(SWIPE_TO_BOUNCER_FLING_ANIMATION_UTILS_CLOSING)
                    FlingAnimationUtils flingAnimationUtils,
            @Named(SWIPE_TO_BOUNCER_FLING_ANIMATION_UTILS_OPENING)
                    FlingAnimationUtils flingAnimationUtils,
            @Named(SWIPE_TO_BOUNCER_FLING_ANIMATION_UTILS_CLOSING)
                    FlingAnimationUtils flingAnimationUtilsClosing,
            @Named(SWIPE_TO_BOUNCER_START_REGION) float swipeRegionPercentage) {
        mDisplayMetrics = displayMetrics;
@@ -154,11 +154,14 @@ public class BouncerSwipeTouchHandler implements DreamTouchHandler {
    public void getTouchInitiationRegion(Region region) {
        if (mCentralSurfaces.isBouncerShowing()) {
            region.op(new Rect(0, 0, mDisplayMetrics.widthPixels,
                    Math.round(mDisplayMetrics.heightPixels * mBouncerZoneScreenPercentage)),
                            Math.round(
                                    mDisplayMetrics.heightPixels * mBouncerZoneScreenPercentage)),
                    Region.Op.UNION);
        } else {
            region.op(new Rect(0,
                    Math.round(mDisplayMetrics.heightPixels * (1 - mBouncerZoneScreenPercentage)),
                            Math.round(
                                    mDisplayMetrics.heightPixels
                                            * (1 - mBouncerZoneScreenPercentage)),
                            mDisplayMetrics.widthPixels,
                            mDisplayMetrics.heightPixels),
                    Region.Op.UNION);
@@ -210,7 +213,6 @@ public class BouncerSwipeTouchHandler implements DreamTouchHandler {
                final float velocityVector =
                        (float) Math.hypot(horizontalVelocity, verticalVelocity);


                final float expansion = flingRevealsOverlay(verticalVelocity, velocityVector)
                        ? KeyguardBouncer.EXPANSION_HIDDEN : KeyguardBouncer.EXPANSION_VISIBLE;
                flingToExpansion(verticalVelocity, expansion);
@@ -236,8 +238,8 @@ public class BouncerSwipeTouchHandler implements DreamTouchHandler {
    }

    protected boolean flingRevealsOverlay(float velocity, float velocityVector) {
        // Fully expand if the user has expanded the bouncer less than halfway or final velocity was
        // positive, indicating an downward direction.
        // Fully expand the space above the bouncer, if the user has expanded the bouncer less
        // than halfway or final velocity was positive, indicating a downward direction.
        if (Math.abs(velocityVector) < mFlingAnimationUtils.getMinVelocityPxPerSecond()) {
            return mCurrentExpansion > FLING_PERCENTAGE_THRESHOLD;
        } else {
@@ -246,17 +248,20 @@ public class BouncerSwipeTouchHandler implements DreamTouchHandler {
    }

    protected void flingToExpansion(float velocity, float expansion) {
        // The animation utils deal in pixel units, rather than expansion height.
        final float viewHeight = mCentralSurfaces.getDisplayHeight();
        final float currentHeight = viewHeight * mCurrentExpansion;
        final float targetHeight = viewHeight * expansion;

        final ValueAnimator animator = createExpansionAnimator(expansion);
        if (expansion == KeyguardBouncer.EXPANSION_HIDDEN) {
            // The animation utils deal in pixel units, rather than expansion height.
            mFlingAnimationUtils.apply(animator, currentHeight, targetHeight, velocity, viewHeight);
            // Hides the bouncer, i.e., fully expands the space above the bouncer.
            mFlingAnimationUtilsClosing.apply(animator, currentHeight, targetHeight, velocity,
                    viewHeight);
        } else {
            mFlingAnimationUtilsClosing.apply(
                    animator, mCurrentExpansion, currentHeight, targetHeight, viewHeight);
            // Shows the bouncer, i.e., fully collapses the space above the bouncer.
            mFlingAnimationUtils.apply(
                    animator, currentHeight, targetHeight, velocity, viewHeight);
        }

        animator.start();
+102 −19
Original line number Diff line number Diff line
@@ -39,8 +39,8 @@ import androidx.test.filters.SmallTest;
import com.android.systemui.SysuiTestCase;
import com.android.systemui.shared.system.InputChannelCompat;
import com.android.systemui.statusbar.NotificationShadeWindowController;
import com.android.systemui.statusbar.phone.KeyguardBouncer;
import com.android.systemui.statusbar.phone.CentralSurfaces;
import com.android.systemui.statusbar.phone.KeyguardBouncer;
import com.android.systemui.statusbar.phone.StatusBarKeyguardViewManager;
import com.android.wm.shell.animation.FlingAnimationUtils;

@@ -112,8 +112,10 @@ public class BouncerSwipeTouchHandlerTest extends SysuiTestCase {
                TOUCH_REGION);

        when(mCentralSurfaces.getDisplayHeight()).thenReturn((float) SCREEN_HEIGHT_PX);
        when(mCentralSurfaces.isBouncerShowing()).thenReturn(false);
        when(mValueAnimatorCreator.create(anyFloat(), anyFloat())).thenReturn(mValueAnimator);
        when(mVelocityTrackerFactory.obtain()).thenReturn(mVelocityTracker);
        when(mFlingAnimationUtils.getMinVelocityPxPerSecond()).thenReturn(Float.MAX_VALUE);
    }

    /**
@@ -154,7 +156,7 @@ public class BouncerSwipeTouchHandlerTest extends SysuiTestCase {
     * Makes sure expansion amount is proportional to scroll.
     */
    @Test
    public void testExpansionAmount() {
    public void testExpansionAmount_whenBouncerHidden_setsCorrectValue() {
        mTouchHandler.onSessionStart(mTouchSession);
        ArgumentCaptor<GestureDetector.OnGestureListener> gestureListenerCaptor =
                ArgumentCaptor.forClass(GestureDetector.OnGestureListener.class);
@@ -180,6 +182,38 @@ public class BouncerSwipeTouchHandlerTest extends SysuiTestCase {
                .onPanelExpansionChanged(eq(1 - scrollAmount), eq(false), eq(true));
    }

    /**
     * Makes sure expansion amount is proportional to scroll.
     */
    @Test
    public void testExpansionAmount_whenBouncerShown_setsCorrectValue() {
        when(mCentralSurfaces.isBouncerShowing()).thenReturn(true);

        mTouchHandler.onSessionStart(mTouchSession);
        ArgumentCaptor<GestureDetector.OnGestureListener> gestureListenerCaptor =
                ArgumentCaptor.forClass(GestureDetector.OnGestureListener.class);
        verify(mTouchSession).registerGestureListener(gestureListenerCaptor.capture());

        final float scrollAmount = .3f;
        final float distanceY = SCREEN_HEIGHT_PX * scrollAmount;

        final MotionEvent event1 = MotionEvent.obtain(0, 0, MotionEvent.ACTION_MOVE,
                0, SCREEN_HEIGHT_PX, 0);
        final MotionEvent event2 = MotionEvent.obtain(0, 0, MotionEvent.ACTION_MOVE,
                0, SCREEN_HEIGHT_PX - distanceY, 0);

        assertThat(gestureListenerCaptor.getValue().onScroll(event1, event2, 0, distanceY))
                .isTrue();

        // Ensure only called once
        verify(mStatusBarKeyguardViewManager)
                .onPanelExpansionChanged(anyFloat(), anyBoolean(), anyBoolean());

        // Ensure correct expansion passed in.
        verify(mStatusBarKeyguardViewManager)
                .onPanelExpansionChanged(eq(scrollAmount), eq(false), eq(true));
    }

    private void swipeToPosition(float position, float velocityY) {
        mTouchHandler.onSessionStart(mTouchSession);
        ArgumentCaptor<GestureDetector.OnGestureListener> gestureListenerCaptor =
@@ -212,14 +246,18 @@ public class BouncerSwipeTouchHandlerTest extends SysuiTestCase {
     * down.
     */
    @Test
    public void testCollapseOnThreshold() {
    public void testSwipeBelowThreshold_collapsesBouncer() {
        final float swipeUpPercentage = .3f;
        swipeToPosition(swipeUpPercentage, -1);

        verify(mValueAnimatorCreator).create(eq(1 - swipeUpPercentage),
                eq(KeyguardBouncer.EXPANSION_VISIBLE));
        verify(mFlingAnimationUtilsClosing).apply(eq(mValueAnimator), anyFloat(), anyFloat(),
                anyFloat(), anyFloat());
        final float expansion = 1 - swipeUpPercentage;
        // The upward velocity is ignored.
        final float velocityY = -1;
        swipeToPosition(swipeUpPercentage, velocityY);

        verify(mValueAnimatorCreator).create(eq(expansion), eq(KeyguardBouncer.EXPANSION_HIDDEN));
        verify(mFlingAnimationUtilsClosing).apply(eq(mValueAnimator),
                eq(SCREEN_HEIGHT_PX * expansion),
                eq(SCREEN_HEIGHT_PX * KeyguardBouncer.EXPANSION_HIDDEN),
                eq(velocityY), eq((float) SCREEN_HEIGHT_PX));
        verify(mValueAnimator).start();
    }

@@ -227,14 +265,59 @@ public class BouncerSwipeTouchHandlerTest extends SysuiTestCase {
     * Tests that ending a swipe above the set expansion threshold will continue the expansion.
     */
    @Test
    public void testExpandOnThreshold() {
    public void testSwipeAboveThreshold_expandsBouncer() {
        final float swipeUpPercentage = .7f;
        final float expansion = 1 - swipeUpPercentage;
        // The downward velocity is ignored.
        final float velocityY = 1;
        swipeToPosition(swipeUpPercentage, velocityY);

        verify(mValueAnimatorCreator).create(eq(expansion), eq(KeyguardBouncer.EXPANSION_VISIBLE));
        verify(mFlingAnimationUtils).apply(eq(mValueAnimator), eq(SCREEN_HEIGHT_PX * expansion),
                eq(SCREEN_HEIGHT_PX * KeyguardBouncer.EXPANSION_VISIBLE),
                eq(velocityY), eq((float) SCREEN_HEIGHT_PX));
        verify(mValueAnimator).start();
    }

    /**
     * Tests that swiping down with a speed above the set threshold leads to bouncer collapsing
     * down.
     */
    @Test
    public void testSwipeDownVelocityAboveMin_collapsesBouncer() {
        when(mFlingAnimationUtils.getMinVelocityPxPerSecond()).thenReturn((float) 0);

        // The swipe amount above the set expansion threshold is ignored.
        final float swipeUpPercentage = .7f;
        swipeToPosition(swipeUpPercentage, 1);
        final float expansion = 1 - swipeUpPercentage;
        final float velocityY = 1;
        swipeToPosition(swipeUpPercentage, velocityY);

        verify(mValueAnimatorCreator).create(eq(expansion), eq(KeyguardBouncer.EXPANSION_HIDDEN));
        verify(mFlingAnimationUtilsClosing).apply(eq(mValueAnimator),
                eq(SCREEN_HEIGHT_PX * expansion),
                eq(SCREEN_HEIGHT_PX * KeyguardBouncer.EXPANSION_HIDDEN),
                eq(velocityY), eq((float) SCREEN_HEIGHT_PX));
        verify(mValueAnimator).start();
    }

    /**
     * Tests that swiping up with a speed above the set threshold will continue the expansion.
     */
    @Test
    public void testSwipeUpVelocityAboveMin_expandsBouncer() {
        when(mFlingAnimationUtils.getMinVelocityPxPerSecond()).thenReturn((float) 0);

        verify(mValueAnimatorCreator).create(eq(1 - swipeUpPercentage),
                eq(KeyguardBouncer.EXPANSION_HIDDEN));
        verify(mFlingAnimationUtils).apply(eq(mValueAnimator), anyFloat(), anyFloat(),
                anyFloat(), anyFloat());
        // The swipe amount below the set expansion threshold is ignored.
        final float swipeUpPercentage = .3f;
        final float expansion = 1 - swipeUpPercentage;
        final float velocityY = -1;
        swipeToPosition(swipeUpPercentage, velocityY);

        verify(mValueAnimatorCreator).create(eq(expansion), eq(KeyguardBouncer.EXPANSION_VISIBLE));
        verify(mFlingAnimationUtils).apply(eq(mValueAnimator), eq(SCREEN_HEIGHT_PX * expansion),
                eq(SCREEN_HEIGHT_PX * KeyguardBouncer.EXPANSION_VISIBLE),
                eq(velocityY), eq((float) SCREEN_HEIGHT_PX));
        verify(mValueAnimator).start();
    }
}