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

Commit a823b466 authored by Hawkwood Glazier's avatar Hawkwood Glazier
Browse files

[DO NOT MERGE] 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
parent a724e0b7
Loading
Loading
Loading
Loading
+12 −10
Original line number Original line Diff line number Diff line
@@ -29,6 +29,7 @@ import android.util.AttributeSet
import android.widget.TextView
import android.widget.TextView
import com.android.internal.R.attr.contentDescription
import com.android.internal.R.attr.contentDescription
import com.android.internal.R.attr.format
import com.android.internal.R.attr.format
import com.android.internal.annotations.VisibleForTesting
import com.android.systemui.animation.GlyphCallback
import com.android.systemui.animation.GlyphCallback
import com.android.systemui.animation.Interpolators
import com.android.systemui.animation.Interpolators
import com.android.systemui.animation.TextAnimator
import com.android.systemui.animation.TextAnimator
@@ -77,7 +78,8 @@ class AnimatableClockView @JvmOverloads constructor(
    private var textAnimator: TextAnimator? = null
    private var textAnimator: TextAnimator? = null
    private var onTextAnimatorInitialized: Runnable? = null
    private var onTextAnimatorInitialized: Runnable? = null


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


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


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


    fun animateFoldAppear(animate: Boolean = true) {
    fun animateFoldAppear(animate: Boolean = true) {
        if (textAnimator == null) {
        if (isAnimationEnabled && textAnimator == null) {
            return
            return
        }
        }
        setTextStyle(
        setTextStyle(
@@ -258,7 +260,7 @@ class AnimatableClockView @JvmOverloads constructor(
            weight = dozingWeightInternal,
            weight = dozingWeightInternal,
            textSize = -1f,
            textSize = -1f,
            color = dozingColor,
            color = dozingColor,
            animate = animate,
            animate = animate && isAnimationEnabled,
            interpolator = Interpolators.EMPHASIZED_DECELERATE,
            interpolator = Interpolators.EMPHASIZED_DECELERATE,
            duration = ANIMATION_DURATION_FOLD_TO_AOD.toLong(),
            duration = ANIMATION_DURATION_FOLD_TO_AOD.toLong(),
            delay = 0,
            delay = 0,
@@ -276,7 +278,7 @@ class AnimatableClockView @JvmOverloads constructor(
                weight = if (isDozing()) dozingWeight else lockScreenWeight,
                weight = if (isDozing()) dozingWeight else lockScreenWeight,
                textSize = -1f,
                textSize = -1f,
                color = null,
                color = null,
                animate = true,
                animate = isAnimationEnabled,
                duration = CHARGE_ANIM_DURATION_PHASE_1,
                duration = CHARGE_ANIM_DURATION_PHASE_1,
                delay = 0,
                delay = 0,
                onAnimationEnd = null
                onAnimationEnd = null
@@ -286,7 +288,7 @@ class AnimatableClockView @JvmOverloads constructor(
            weight = if (isDozing()) lockScreenWeight else dozingWeight,
            weight = if (isDozing()) lockScreenWeight else dozingWeight,
            textSize = -1f,
            textSize = -1f,
            color = null,
            color = null,
            animate = true,
            animate = isAnimationEnabled,
            duration = CHARGE_ANIM_DURATION_PHASE_0,
            duration = CHARGE_ANIM_DURATION_PHASE_0,
            delay = chargeAnimationDelay.toLong(),
            delay = chargeAnimationDelay.toLong(),
            onAnimationEnd = startAnimPhase2
            onAnimationEnd = startAnimPhase2
@@ -298,7 +300,7 @@ class AnimatableClockView @JvmOverloads constructor(
            weight = if (isDozing) dozingWeight else lockScreenWeight,
            weight = if (isDozing) dozingWeight else lockScreenWeight,
            textSize = -1f,
            textSize = -1f,
            color = if (isDozing) dozingColor else lockScreenColor,
            color = if (isDozing) dozingColor else lockScreenColor,
            animate = animate,
            animate = animate && isAnimationEnabled,
            duration = DOZE_ANIM_DURATION,
            duration = DOZE_ANIM_DURATION,
            delay = 0,
            delay = 0,
            onAnimationEnd = null
            onAnimationEnd = null
@@ -332,7 +334,7 @@ class AnimatableClockView @JvmOverloads constructor(
                weight = weight,
                weight = weight,
                textSize = textSize,
                textSize = textSize,
                color = color,
                color = color,
                animate = animate,
                animate = animate && isAnimationEnabled,
                duration = duration,
                duration = duration,
                interpolator = interpolator,
                interpolator = interpolator,
                delay = delay,
                delay = delay,
@@ -370,7 +372,7 @@ class AnimatableClockView @JvmOverloads constructor(
            weight = weight,
            weight = weight,
            textSize = textSize,
            textSize = textSize,
            color = color,
            color = color,
            animate = animate,
            animate = animate && isAnimationEnabled,
            interpolator = null,
            interpolator = null,
            duration = duration,
            duration = duration,
            delay = delay,
            delay = delay,
+2 −1
Original line number Original line Diff line number Diff line
@@ -19,6 +19,7 @@ import android.graphics.drawable.Drawable
import android.icu.text.NumberFormat
import android.icu.text.NumberFormat
import android.util.TypedValue
import android.util.TypedValue
import android.view.LayoutInflater
import android.view.LayoutInflater
import com.android.internal.annotations.VisibleForTesting
import com.android.systemui.dagger.qualifiers.Main
import com.android.systemui.dagger.qualifiers.Main
import com.android.systemui.plugins.Clock
import com.android.systemui.plugins.Clock
import com.android.systemui.plugins.ClockAnimations
import com.android.systemui.plugins.ClockAnimations
@@ -221,7 +222,7 @@ class DefaultClock(
    override fun dump(pw: PrintWriter) = clocks.forEach { it.dump(pw) }
    override fun dump(pw: PrintWriter) = clocks.forEach { it.dump(pw) }


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


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


    @JvmField @Rule val mockito = MockitoJUnit.rule()
    @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 layoutInflater: LayoutInflater
    @Mock private lateinit var mockClockThumbnail: Drawable
    @Mock private lateinit var mockClockThumbnail: Drawable
    @Mock private lateinit var resources: Resources
    @Mock private lateinit var resources: Resources
@@ -48,9 +59,9 @@ class DefaultClockProviderTest : SysuiTestCase() {
    @Before
    @Before
    fun setUp() {
    fun setUp() {
        whenever(layoutInflater.inflate(R.layout.clock_default_small, null))
        whenever(layoutInflater.inflate(R.layout.clock_default_small, null))
            .thenReturn(mockClockView)
            .thenReturn(mockSmallClockView)
        whenever(layoutInflater.inflate(R.layout.clock_default_large, null))
        whenever(layoutInflater.inflate(R.layout.clock_default_large, null))
            .thenReturn(mockClockView)
            .thenReturn(mockLargeClockView)
        whenever(resources.getDrawable(R.drawable.clock_default_thumbnail, null))
        whenever(resources.getDrawable(R.drawable.clock_default_thumbnail, null))
            .thenReturn(mockClockThumbnail)
            .thenReturn(mockClockThumbnail)


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