diff --git a/packages/SystemUI/src/com/android/keyguard/AnimatableClockController.java b/packages/SystemUI/src/com/android/keyguard/AnimatableClockController.java index efcf40a662582..a383cab94c366 100644 --- a/packages/SystemUI/src/com/android/keyguard/AnimatableClockController.java +++ b/packages/SystemUI/src/com/android/keyguard/AnimatableClockController.java @@ -20,12 +20,16 @@ import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; +import android.content.res.Resources; import android.graphics.Color; import android.icu.text.NumberFormat; +import androidx.annotation.VisibleForTesting; + import com.android.settingslib.Utils; import com.android.systemui.R; import com.android.systemui.broadcast.BroadcastDispatcher; +import com.android.systemui.dagger.qualifiers.Main; import com.android.systemui.plugins.statusbar.StatusBarStateController; import com.android.systemui.statusbar.phone.KeyguardBypassController; import com.android.systemui.statusbar.policy.BatteryController; @@ -67,20 +71,20 @@ public class AnimatableClockController extends ViewController mAttachCaptor = + ArgumentCaptor.forClass(View.OnAttachStateChangeListener.class); + private View.OnAttachStateChangeListener mAttachListener; + + @Captor private ArgumentCaptor mStatusBarStateCaptor; + private StatusBarStateController.StateListener mStatusBarStateCallback; + + @Before + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + mStaticMockSession = mockitoSession() + .mockStatic(Utils.class) + .strictness(Strictness.LENIENT) // it's ok if mocked classes aren't used + .startMocking(); + when(Utils.getColorAttrDefaultColor(anyObject(), anyInt())).thenReturn(0); + + mAnimatableClockController = new AnimatableClockController( + mClockView, + mStatusBarStateController, + mBroadcastDispatcher, + mBatteryController, + mKeyguardUpdateMonitor, + mBypassController, + mResources + ); + mAnimatableClockController.init(); + captureAttachListener(); + } + + @After + public void tearDown() { + mStaticMockSession.finishMocking(); + } + + @Test + public void testOnAttachedUpdatesDozeStateToTrue() { + // GIVEN dozing + when(mStatusBarStateController.isDozing()).thenReturn(true); + when(mStatusBarStateController.getDozeAmount()).thenReturn(1f); + + // WHEN the clock view gets attached + mAttachListener.onViewAttachedToWindow(mClockView); + + // THEN the clock controller updated its dozing state to true + assertTrue(mAnimatableClockController.isDozing()); + } + + @Test + public void testOnAttachedUpdatesDozeStateToFalse() { + // GIVEN not dozing + when(mStatusBarStateController.isDozing()).thenReturn(false); + when(mStatusBarStateController.getDozeAmount()).thenReturn(0f); + + // WHEN the clock view gets attached + mAttachListener.onViewAttachedToWindow(mClockView); + + // THEN the clock controller updated its dozing state to false + assertFalse(mAnimatableClockController.isDozing()); + } + + private void captureAttachListener() { + verify(mClockView).addOnAttachStateChangeListener(mAttachCaptor.capture()); + mAttachListener = mAttachCaptor.getValue(); + } +}