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

Commit 8d7ed908 authored by Hawkwood Glazier's avatar Hawkwood Glazier
Browse files

Additional Tests for DefaultClock

DefaultClock needed additional units as coverage of much of that class
was too low. This additionally adds an animation disable flag into
AnimatableClockView for use by the screenshot test.

Bug: 229771520
Test: atest DefaultClockProviderTest
Change-Id: I7d585cedb9b43ac0fb6405e36bec99ee8a246a61
(cherry picked from commit a823b466)
parent c254146e
Loading
Loading
Loading
Loading
+12 −10
Original line number Diff line number Diff line
@@ -29,6 +29,7 @@ import android.util.AttributeSet
import android.widget.TextView
import com.android.internal.R.attr.contentDescription
import com.android.internal.R.attr.format
import com.android.internal.annotations.VisibleForTesting
import com.android.systemui.animation.GlyphCallback
import com.android.systemui.animation.Interpolators
import com.android.systemui.animation.TextAnimator
@@ -77,7 +78,8 @@ class AnimatableClockView @JvmOverloads constructor(
    private var textAnimator: TextAnimator? = null
    private var onTextAnimatorInitialized: Runnable? = null

    var timeOverrideInMillis: Long? = null
    @VisibleForTesting var isAnimationEnabled: Boolean = true
    @VisibleForTesting var timeOverrideInMillis: Long? = null

    val dozingWeight: Int
        get() = if (useBoldedVersion()) dozingWeightInternal + 100 else dozingWeightInternal
@@ -217,7 +219,7 @@ class AnimatableClockView @JvmOverloads constructor(
    }

    fun animateAppearOnLockscreen() {
        if (textAnimator == null) {
        if (isAnimationEnabled && textAnimator == null) {
            return
        }
        setTextStyle(
@@ -233,7 +235,7 @@ class AnimatableClockView @JvmOverloads constructor(
            weight = lockScreenWeight,
            textSize = -1f,
            color = lockScreenColor,
            animate = true,
            animate = isAnimationEnabled,
            duration = APPEAR_ANIM_DURATION,
            delay = 0,
            onAnimationEnd = null
@@ -241,7 +243,7 @@ class AnimatableClockView @JvmOverloads constructor(
    }

    fun animateFoldAppear(animate: Boolean = true) {
        if (textAnimator == null) {
        if (isAnimationEnabled && textAnimator == null) {
            return
        }
        setTextStyle(
@@ -257,7 +259,7 @@ class AnimatableClockView @JvmOverloads constructor(
            weight = dozingWeightInternal,
            textSize = -1f,
            color = dozingColor,
            animate = animate,
            animate = animate && isAnimationEnabled,
            interpolator = Interpolators.EMPHASIZED_DECELERATE,
            duration = ANIMATION_DURATION_FOLD_TO_AOD.toLong(),
            delay = 0,
@@ -275,7 +277,7 @@ class AnimatableClockView @JvmOverloads constructor(
                weight = if (isDozing()) dozingWeight else lockScreenWeight,
                textSize = -1f,
                color = null,
                animate = true,
                animate = isAnimationEnabled,
                duration = CHARGE_ANIM_DURATION_PHASE_1,
                delay = 0,
                onAnimationEnd = null
@@ -285,7 +287,7 @@ class AnimatableClockView @JvmOverloads constructor(
            weight = if (isDozing()) lockScreenWeight else dozingWeight,
            textSize = -1f,
            color = null,
            animate = true,
            animate = isAnimationEnabled,
            duration = CHARGE_ANIM_DURATION_PHASE_0,
            delay = chargeAnimationDelay.toLong(),
            onAnimationEnd = startAnimPhase2
@@ -297,7 +299,7 @@ class AnimatableClockView @JvmOverloads constructor(
            weight = if (isDozing) dozingWeight else lockScreenWeight,
            textSize = -1f,
            color = if (isDozing) dozingColor else lockScreenColor,
            animate = animate,
            animate = animate && isAnimationEnabled,
            duration = DOZE_ANIM_DURATION,
            delay = 0,
            onAnimationEnd = null
@@ -331,7 +333,7 @@ class AnimatableClockView @JvmOverloads constructor(
                weight = weight,
                textSize = textSize,
                color = color,
                animate = animate,
                animate = animate && isAnimationEnabled,
                duration = duration,
                interpolator = interpolator,
                delay = delay,
@@ -369,7 +371,7 @@ class AnimatableClockView @JvmOverloads constructor(
            weight = weight,
            textSize = textSize,
            color = color,
            animate = animate,
            animate = animate && isAnimationEnabled,
            interpolator = null,
            duration = duration,
            delay = delay,
+5 −1
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@ import android.graphics.drawable.Drawable
import android.icu.text.NumberFormat
import android.util.TypedValue
import android.view.LayoutInflater
import com.android.internal.annotations.VisibleForTesting
import com.android.systemui.dagger.qualifiers.Main
import com.android.systemui.plugins.Clock
import com.android.systemui.plugins.ClockAnimations
@@ -83,6 +84,9 @@ class DefaultClock(
        resources.getFloat(R.dimen.keyguard_clock_line_spacing_scale_burmese)
    private val defaultLineSpacing = resources.getFloat(R.dimen.keyguard_clock_line_spacing_scale)

    private var smallRegionDarkness = false
    private var largeRegionDarkness = false

    private fun updateClockColor(clock: AnimatableClockView, isRegionDark: Boolean) {
        val color = if (isRegionDark) {
            resources.getColor(android.R.color.system_accent1_100)
@@ -227,7 +231,7 @@ class DefaultClock(
    override fun dump(pw: PrintWriter) = clocks.forEach { it.dump(pw) }

    companion object {
        private const val DOZE_COLOR = Color.WHITE
        @VisibleForTesting const val DOZE_COLOR = Color.WHITE
        private const val FORMAT_NUMBER = 1234567890
    }
}
+85 −5
Original line number Diff line number Diff line
@@ -19,17 +19,27 @@ package com.android.systemui.shared.clocks
import android.content.res.Resources
import android.graphics.drawable.Drawable
import android.testing.AndroidTestingRunner
import android.util.TypedValue
import android.view.LayoutInflater
import androidx.test.filters.SmallTest
import com.android.systemui.R
import com.android.systemui.SysuiTestCase
import com.android.systemui.shared.clocks.DefaultClock.Companion.DOZE_COLOR
import com.android.systemui.util.mockito.eq
import com.android.systemui.util.mockito.mock
import java.util.Locale
import java.util.TimeZone
import junit.framework.Assert.assertEquals
import junit.framework.Assert.assertNotNull
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.ArgumentMatchers.anyFloat
import org.mockito.ArgumentMatchers.anyInt
import org.mockito.Mock
import org.mockito.Mockito.times
import org.mockito.Mockito.verify
import org.mockito.Mockito.`when` as whenever
import org.mockito.junit.MockitoJUnit

@@ -39,7 +49,8 @@ class DefaultClockProviderTest : SysuiTestCase() {

    @JvmField @Rule val mockito = MockitoJUnit.rule()

    @Mock private lateinit var mockClockView: AnimatableClockView
    @Mock private lateinit var mockSmallClockView: AnimatableClockView
    @Mock private lateinit var mockLargeClockView: AnimatableClockView
    @Mock private lateinit var layoutInflater: LayoutInflater
    @Mock private lateinit var mockClockThumbnail: Drawable
    @Mock private lateinit var resources: Resources
@@ -48,9 +59,9 @@ class DefaultClockProviderTest : SysuiTestCase() {
    @Before
    fun setUp() {
        whenever(layoutInflater.inflate(R.layout.clock_default_small, null))
            .thenReturn(mockClockView)
            .thenReturn(mockSmallClockView)
        whenever(layoutInflater.inflate(R.layout.clock_default_large, null))
            .thenReturn(mockClockView)
            .thenReturn(mockLargeClockView)
        whenever(resources.getDrawable(R.drawable.clock_default_thumbnail, null))
            .thenReturn(mockClockThumbnail)

@@ -71,7 +82,76 @@ class DefaultClockProviderTest : SysuiTestCase() {
        // Default clock provider must always provide the default clock
        val clock = provider.createClock(DEFAULT_CLOCK_ID)
        assertNotNull(clock)
        assertEquals(clock.smallClock, mockClockView)
        assertEquals(clock.largeClock, mockClockView)
        assertEquals(clock.smallClock, mockSmallClockView)
        assertEquals(clock.largeClock, mockLargeClockView)
    }

    @Test
    fun defaultClock_initialize() {
        val clock = provider.createClock(DEFAULT_CLOCK_ID)
        clock.initialize(resources, 0f, 0f)

        verify(mockSmallClockView, times(2)).setColors(eq(DOZE_COLOR), anyInt())
        verify(mockLargeClockView, times(2)).setColors(eq(DOZE_COLOR), anyInt())
        verify(mockSmallClockView).refreshTime()
        verify(mockLargeClockView).refreshTime()
    }

    @Test
    fun defaultClock_events_onTimeTick() {
        val clock = provider.createClock(DEFAULT_CLOCK_ID)
        clock.events.onTimeTick()

        verify(mockSmallClockView).refreshTime()
        verify(mockLargeClockView).refreshTime()
    }

    @Test
    fun defaultClock_events_onTimeFormatChanged() {
        val clock = provider.createClock(DEFAULT_CLOCK_ID)
        clock.events.onTimeFormatChanged(true)

        verify(mockSmallClockView).refreshFormat(true)
        verify(mockLargeClockView).refreshFormat(true)
    }

    @Test
    fun defaultClock_events_onTimeZoneChanged() {
        val timeZone = mock<TimeZone>()
        val clock = provider.createClock(DEFAULT_CLOCK_ID)
        clock.events.onTimeZoneChanged(timeZone)

        verify(mockSmallClockView).onTimeZoneChanged(timeZone)
        verify(mockLargeClockView).onTimeZoneChanged(timeZone)
    }

    @Test
    fun defaultClock_events_onFontSettingChanged() {
        val clock = provider.createClock(DEFAULT_CLOCK_ID)
        clock.events.onFontSettingChanged()

        verify(mockSmallClockView).setTextSize(eq(TypedValue.COMPLEX_UNIT_PX), anyFloat())
        verify(mockLargeClockView).setTextSize(eq(TypedValue.COMPLEX_UNIT_PX), anyFloat())
        verify(mockLargeClockView).setPadding(eq(0), anyInt(), eq(0), eq(0))
    }

    @Test
    fun defaultClock_events_onColorPaletteChanged() {
        val clock = provider.createClock(DEFAULT_CLOCK_ID)
        clock.events.onColorPaletteChanged(resources, true, true)

        verify(mockSmallClockView, times(2)).setColors(eq(DOZE_COLOR), anyInt())
        verify(mockLargeClockView, times(2)).setColors(eq(DOZE_COLOR), anyInt())
    }

    @Test
    fun defaultClock_events_onLocaleChanged() {
        val clock = provider.createClock(DEFAULT_CLOCK_ID)
        clock.events.onLocaleChanged(Locale.getDefault())

        verify(mockSmallClockView, times(2)).setLineSpacingScale(anyFloat())
        verify(mockLargeClockView, times(2)).setLineSpacingScale(anyFloat())
        verify(mockSmallClockView, times(2)).refreshFormat()
        verify(mockLargeClockView, times(2)).refreshFormat()
    }
}