1 /*
2  * Copyright (C) 2022 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 
17 package com.android.systemui.statusbar.policy
18 
19 import android.testing.AndroidTestingRunner
20 import android.testing.TestableLooper
21 import android.view.View.MeasureSpec.UNSPECIFIED
22 import android.view.View.MeasureSpec.makeMeasureSpec
23 import android.view.ViewGroup.LayoutParams.WRAP_CONTENT
24 import android.widget.LinearLayout
25 import androidx.test.filters.SmallTest
26 import com.android.systemui.SysuiTestCase
27 import org.junit.Before
28 import org.junit.runner.RunWith
29 
30 import com.google.common.truth.Truth.assertThat
31 import org.junit.Test
32 
33 @SmallTest
34 @RunWith(AndroidTestingRunner::class)
35 @TestableLooper.RunWithLooper
36 class ClockTest : SysuiTestCase() {
37     private lateinit var clockView: Clock
38 
39     @Before
40     fun setUp() {
41         allowTestableLooperAsMainThread()
42         TestableLooper.get(this).runWithLooper {
43             val container = LinearLayout(context)
44             val lp = LinearLayout.LayoutParams(1000, WRAP_CONTENT)
45             container.layoutParams = lp
46             clockView = Clock(context, null)
47             container.addView(clockView)
48             measureClock()
49         }
50     }
51 
52     @Test
53     fun testWidthDoesNotDecrease_sameCharLength() {
54         // GIVEN time is narrow
55         clockView.text = ONE_3
56         measureClock()
57         val width1 = clockView.measuredWidth
58 
59         // WHEN the text changes to be wider characters
60         clockView.text = ZERO_3
61         measureClock()
62         val width2 = clockView.measuredWidth
63 
64         // THEN the width should be wider (or equals when using monospace font)
65         assertThat(width2).isAtLeast(width1)
66     }
67 
68     @Test
69     fun testWidthDoesNotDecrease_narrowerFont_sameNumberOfChars() {
70         // GIVEN time is wide
71         clockView.text = ZERO_3
72         measureClock()
73         val width1 = clockView.measuredWidth
74 
75         // WHEN the text changes to a narrower font
76         clockView.text = ONE_3
77         measureClock()
78         val width2 = clockView.measuredWidth
79 
80         // THEN the width should not have decreased, and they should in fact be the same
81         assertThat(width2).isEqualTo(width1)
82     }
83 
84     @Test
85     fun testWidthIncreases_whenCharsChanges() {
86         // GIVEN wide 3-char text
87         clockView.text = ZERO_3
88         measureClock()
89         val width1 = clockView.measuredWidth
90 
91         // WHEN text changes to 4-char wide text
92         clockView.text = ZERO_4
93         measureClock()
94         val width2 = clockView.measuredWidth
95 
96         // THEN the text field is wider
97         assertThat(width2).isGreaterThan(width1)
98     }
99 
100     @Test
101     fun testWidthDecreases_whenCharsChange_longToShort() {
102         // GIVEN wide 4-char text
103         clockView.text = ZERO_4
104         measureClock()
105         val width1 = clockView.measuredWidth
106 
107         // WHEN number of characters changes to a narrow 3-char text
108         clockView.text = ONE_3
109         measureClock()
110         val width2 = clockView.measuredWidth
111 
112         // THEN the width can shrink, because number of chars changed
113         assertThat(width2).isLessThan(width1)
114     }
115 
116     private fun measureClock() {
117         clockView.measure(
118                 makeMeasureSpec(0, UNSPECIFIED),
119                 makeMeasureSpec(0, UNSPECIFIED)
120         )
121     }
122 }
123 
124 /**
125  * In a non-monospace font, it is expected that "0:00" is wider than "1:11"
126  */
127 private const val ZERO_3 = "0:00"
128 private const val ZERO_4 = "00:00"
129 private const val ONE_3 = "1:11"
130 private const val ONE_4 = "11:11"
131