1 /* 2 * Copyright (C) 2024 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package systemui.shared.clocks.view 17 18 import android.graphics.Typeface 19 import android.testing.AndroidTestingRunner 20 import androidx.test.filters.SmallTest 21 import com.android.systemui.SysuiTestCase 22 import com.android.systemui.plugins.clocks.ClockMessageBuffers 23 import com.android.systemui.plugins.clocks.ClockSettings 24 import com.android.systemui.shared.clocks.ClockContext 25 import com.android.systemui.shared.clocks.FontTextStyle 26 import com.android.systemui.shared.clocks.LogUtil 27 import com.android.systemui.shared.clocks.TypefaceCache 28 import com.android.systemui.shared.clocks.view.SimpleDigitalClockTextView 29 import org.junit.Assert.assertEquals 30 import org.junit.Before 31 import org.junit.Test 32 import org.junit.runner.RunWith 33 34 @SmallTest 35 @RunWith(AndroidTestingRunner::class) 36 class SimpleDigitalClockTextViewTest : SysuiTestCase() { 37 private val messageBuffer = LogUtil.DEBUG_MESSAGE_BUFFER 38 private lateinit var underTest: SimpleDigitalClockTextView 39 private val defaultLargeClockTextSize = 500F 40 private val smallerTextSize = 300F 41 private val largerTextSize = 800F 42 private val firstMeasureTextSize = 100F 43 44 @Before setupnull45 fun setup() { 46 underTest = 47 SimpleDigitalClockTextView( 48 ClockContext( 49 context, 50 context.resources, 51 ClockSettings(), 52 TypefaceCache(messageBuffer, 20) { 53 // TODO(b/364680873): Move constant to config_clockFontFamily when shipping 54 return@TypefaceCache Typeface.create( 55 "google-sans-flex-clock", 56 Typeface.NORMAL, 57 ) 58 }, 59 ClockMessageBuffers(messageBuffer), 60 messageBuffer, 61 ) 62 ) 63 underTest.textStyle = FontTextStyle() 64 underTest.aodStyle = FontTextStyle() 65 underTest.text = "0" 66 underTest.applyTextSize(defaultLargeClockTextSize) 67 } 68 69 @Test applySmallerConstrainedTextSize_applyConstrainedTextSizenull70 fun applySmallerConstrainedTextSize_applyConstrainedTextSize() { 71 underTest.applyTextSize(smallerTextSize, constrainedByHeight = true) 72 assertEquals(smallerTextSize, underTest.textSize * underTest.fontSizeAdjustFactor) 73 } 74 75 @Test applyLargerConstrainedTextSize_applyUnconstrainedTextSizenull76 fun applyLargerConstrainedTextSize_applyUnconstrainedTextSize() { 77 underTest.applyTextSize(largerTextSize, constrainedByHeight = true) 78 assertEquals(defaultLargeClockTextSize, underTest.textSize) 79 } 80 81 @Test applyFirstMeasureConstrainedTextSize_getConstrainedTextSizenull82 fun applyFirstMeasureConstrainedTextSize_getConstrainedTextSize() { 83 underTest.applyTextSize(firstMeasureTextSize, constrainedByHeight = true) 84 underTest.applyTextSize(smallerTextSize, constrainedByHeight = true) 85 assertEquals(smallerTextSize, underTest.textSize * underTest.fontSizeAdjustFactor) 86 } 87 88 @Test applySmallFirstMeasureConstrainedSizeAndLargerConstrainedTextSize_applyDefaultSizenull89 fun applySmallFirstMeasureConstrainedSizeAndLargerConstrainedTextSize_applyDefaultSize() { 90 underTest.applyTextSize(firstMeasureTextSize, constrainedByHeight = true) 91 underTest.applyTextSize(largerTextSize, constrainedByHeight = true) 92 assertEquals(defaultLargeClockTextSize, underTest.textSize) 93 } 94 95 @Test applyFirstMeasureConstrainedTextSize_applyUnconstrainedTextSizenull96 fun applyFirstMeasureConstrainedTextSize_applyUnconstrainedTextSize() { 97 underTest.applyTextSize(firstMeasureTextSize, constrainedByHeight = true) 98 underTest.applyTextSize(defaultLargeClockTextSize) 99 assertEquals(defaultLargeClockTextSize, underTest.textSize) 100 } 101 } 102