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

Commit 54fbfb3c authored by Lucas Dupin's avatar Lucas Dupin
Browse files

Use test's TestableLooper

Fixes: 124858892
Test: atest ScrimControllerTest
Change-Id: Ib35e2d39a660472ff238cf5e3eded5c5afa39e8b
parent 71a6e6d2
Loading
Loading
Loading
Loading
+10 −10
Original line number Diff line number Diff line
@@ -114,6 +114,7 @@ public class ScrimController implements ViewTreeObserver.OnPreDrawListener, OnCo
    private final DozeParameters mDozeParameters;
    private final AlarmTimeout mTimeTicker;
    private final KeyguardVisibilityCallback mKeyguardVisibilityCallback;
    private final Handler mHandler;

    private final SysuiColorExtractor mColorExtractor;
    private GradientColors mLockColors;
@@ -174,8 +175,9 @@ public class ScrimController implements ViewTreeObserver.OnPreDrawListener, OnCo
        mKeyguardVisibilityCallback = new KeyguardVisibilityCallback();
        mKeyguardUpdateMonitor.registerCallback(mKeyguardVisibilityCallback);
        mScrimBehindAlphaResValue = mContext.getResources().getFloat(R.dimen.scrim_behind_alpha);
        mHandler = getHandler();
        mTimeTicker = new AlarmTimeout(alarmManager, this::onHideWallpaperTimeout,
                "hide_aod_wallpaper", new Handler());
                "hide_aod_wallpaper", mHandler);
        mWakeLock = createWakeLock();
        // Scrim alpha is initially set to the value on the resource but might be changed
        // to make sure that text on top of it is legible.
@@ -253,8 +255,8 @@ public class ScrimController implements ViewTreeObserver.OnPreDrawListener, OnCo
            mScrimBehind.removeCallbacks(mPendingFrameCallback);
            mPendingFrameCallback = null;
        }
        if (getHandler().hasCallbacks(mBlankingTransitionRunnable)) {
            getHandler().removeCallbacks(mBlankingTransitionRunnable);
        if (mHandler.hasCallbacks(mBlankingTransitionRunnable)) {
            mHandler.removeCallbacks(mBlankingTransitionRunnable);
            mBlankingTransitionRunnable = null;
        }

@@ -768,7 +770,7 @@ public class ScrimController implements ViewTreeObserver.OnPreDrawListener, OnCo
            if (DEBUG) {
                Log.d(TAG, "Fading out scrims with delay: " + delay);
            }
            getHandler().postDelayed(mBlankingTransitionRunnable, delay);
            mHandler.postDelayed(mBlankingTransitionRunnable, delay);
        };
        doOnTheNextFrame(mPendingFrameCallback);
    }
@@ -786,7 +788,7 @@ public class ScrimController implements ViewTreeObserver.OnPreDrawListener, OnCo

    @VisibleForTesting
    protected Handler getHandler() {
        return Handler.getMain();
        return new Handler();
    }

    public int getBackgroundColor() {
@@ -821,8 +823,7 @@ public class ScrimController implements ViewTreeObserver.OnPreDrawListener, OnCo

    @VisibleForTesting
    protected WakeLock createWakeLock() {
         return new DelayedWakeLock(getHandler(),
                WakeLock.createPartial(mContext, "Scrims"));
        return new DelayedWakeLock(mHandler, WakeLock.createPartial(mContext, "Scrims"));
    }

    @Override
@@ -853,12 +854,11 @@ public class ScrimController implements ViewTreeObserver.OnPreDrawListener, OnCo
     */
    public void onScreenTurnedOn() {
        mScreenOn = true;
        final Handler handler = getHandler();
        if (handler.hasCallbacks(mBlankingTransitionRunnable)) {
        if (mHandler.hasCallbacks(mBlankingTransitionRunnable)) {
            if (DEBUG) {
                Log.d(TAG, "Shorter blanking because screen turned on. All good.");
            }
            handler.removeCallbacks(mBlankingTransitionRunnable);
            mHandler.removeCallbacks(mBlankingTransitionRunnable);
            mBlankingTransitionRunnable.run();
        }
    }
+4 −12
Original line number Diff line number Diff line
@@ -37,12 +37,10 @@ import android.animation.ValueAnimator;
import android.app.AlarmManager;
import android.graphics.Color;
import android.os.Handler;
import android.os.Looper;
import android.testing.AndroidTestingRunner;
import android.testing.TestableLooper;
import android.view.View;

import androidx.test.filters.FlakyTest;
import androidx.test.filters.SmallTest;

import com.android.internal.colorextraction.ColorExtractor.GradientColors;
@@ -78,6 +76,7 @@ public class ScrimControllerTest extends SysuiTestCase {
    private WakeLock mWakeLock;
    private boolean mAlwaysOnEnabled;
    private AlarmManager mAlarmManager;
    private TestableLooper mLooper;


    @Before
@@ -88,6 +87,7 @@ public class ScrimControllerTest extends SysuiTestCase {
        mAlarmManager = mock(AlarmManager.class);
        mAlwaysOnEnabled = true;
        mDozeParamenters = mock(DozeParameters.class);
        mLooper = TestableLooper.get(this);
        when(mDozeParamenters.getAlwaysOn()).thenAnswer(invocation -> mAlwaysOnEnabled);
        when(mDozeParamenters.getDisplayNeedsBlanking()).thenReturn(true);
        mScrimController = new SynchronousScrimController(mScrimBehind, mScrimInFront,
@@ -253,7 +253,6 @@ public class ScrimControllerTest extends SysuiTestCase {
        assertScrimTint(mScrimBehind, false /* tinted */);
    }

    @FlakyTest(bugId = 124858892)
    @Test
    public void transitionToUnlocked() {
        mScrimController.setPanelExpansion(0f);
@@ -298,7 +297,6 @@ public class ScrimControllerTest extends SysuiTestCase {
        Assert.assertEquals(mScrimState, ScrimState.BOUNCER_SCRIMMED);
    }

    @FlakyTest(bugId = 124858892)
    @Test
    public void panelExpansion() {
        mScrimController.setPanelExpansion(0f);
@@ -321,7 +319,6 @@ public class ScrimControllerTest extends SysuiTestCase {
                mScrimBehindAlpha, mScrimBehind.getViewAlpha(), 0.01f);
    }

    @FlakyTest(bugId = 124858892)
    @Test
    public void panelExpansionAffectsAlpha() {
        mScrimController.setPanelExpansion(0f);
@@ -666,7 +663,6 @@ public class ScrimControllerTest extends SysuiTestCase {
     */
    private class SynchronousScrimController extends ScrimController {

        private FakeHandler mHandler;
        private boolean mAnimationCancelled;
        boolean mOnPreDrawCalled;

@@ -676,7 +672,6 @@ public class ScrimControllerTest extends SysuiTestCase {
                AlarmManager alarmManager) {
            super(scrimBehind, scrimInFront, scrimStateListener, scrimVisibleListener,
                    dozeParameters, alarmManager);
            mHandler = new FakeHandler(Looper.myLooper());
        }

        @Override
@@ -688,13 +683,10 @@ public class ScrimControllerTest extends SysuiTestCase {
        void finishAnimationsImmediately() {
            boolean[] animationFinished = {false};
            setOnAnimationFinished(()-> animationFinished[0] = true);

            // Execute code that will trigger animations.
            onPreDraw();

            // Force finish screen blanking.
            mHandler.dispatchQueuedMessages();
            // Force finish all animations.
            mLooper.processAllMessages();
            endAnimation(mScrimBehind, TAG_KEY_ANIM);
            endAnimation(mScrimInFront, TAG_KEY_ANIM);

@@ -724,7 +716,7 @@ public class ScrimControllerTest extends SysuiTestCase {

        @Override
        protected Handler getHandler() {
            return mHandler;
            return new FakeHandler(mLooper.getLooper());
        }

        @Override