• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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.qs.tileimpl
18 
19 import android.content.Context
20 import android.graphics.drawable.Drawable
21 import android.service.quicksettings.Tile
22 import android.testing.AndroidTestingRunner
23 import android.testing.TestableLooper
24 import android.text.TextUtils
25 import android.view.View
26 import android.widget.TextView
27 import androidx.test.filters.SmallTest
28 import com.android.systemui.R
29 import com.android.systemui.SysuiTestCase
30 import com.android.systemui.plugins.qs.QSIconView
31 import com.android.systemui.plugins.qs.QSTile
32 import com.google.common.truth.Truth.assertThat
33 import org.junit.Before
34 import org.junit.Test
35 import org.junit.runner.RunWith
36 import org.mockito.Mock
37 import org.mockito.MockitoAnnotations
38 
39 @RunWith(AndroidTestingRunner::class)
40 @SmallTest
41 @TestableLooper.RunWithLooper(setAsMainLooper = true)
42 class QSTileViewImplTest : SysuiTestCase() {
43 
44     @Mock
45     private lateinit var iconView: QSIconView
46     @Mock
47     private lateinit var customDrawable: Drawable
48 
49     private lateinit var tileView: FakeTileView
50     private lateinit var customDrawableView: View
51     private lateinit var chevronView: View
52 
53     @Before
setUpnull54     fun setUp() {
55         MockitoAnnotations.initMocks(this)
56         context.ensureTestableResources()
57 
58         tileView = FakeTileView(context, iconView, false)
59         customDrawableView = tileView.requireViewById(R.id.customDrawable)
60         chevronView = tileView.requireViewById(R.id.chevron)
61     }
62 
63     @Test
testSecondaryLabelNotModified_unavailablenull64     fun testSecondaryLabelNotModified_unavailable() {
65         val state = QSTile.State()
66         val testString = "TEST STRING"
67         state.state = Tile.STATE_UNAVAILABLE
68         state.secondaryLabel = testString
69 
70         tileView.changeState(state)
71 
72         assertThat(state.secondaryLabel as CharSequence).isEqualTo(testString)
73     }
74 
75     @Test
testSecondaryLabelNotModified_booleanInactivenull76     fun testSecondaryLabelNotModified_booleanInactive() {
77         val state = QSTile.BooleanState()
78         val testString = "TEST STRING"
79         state.state = Tile.STATE_INACTIVE
80         state.secondaryLabel = testString
81 
82         tileView.changeState(state)
83 
84         assertThat(state.secondaryLabel as CharSequence).isEqualTo(testString)
85     }
86 
87     @Test
testSecondaryLabelNotModified_booleanActivenull88     fun testSecondaryLabelNotModified_booleanActive() {
89         val state = QSTile.BooleanState()
90         val testString = "TEST STRING"
91         state.state = Tile.STATE_ACTIVE
92         state.secondaryLabel = testString
93 
94         tileView.changeState(state)
95 
96         assertThat(state.secondaryLabel as CharSequence).isEqualTo(testString)
97     }
98 
99     @Test
testSecondaryLabelNotModified_availableNotBoolean_inactivenull100     fun testSecondaryLabelNotModified_availableNotBoolean_inactive() {
101         val state = QSTile.State()
102         state.state = Tile.STATE_INACTIVE
103         state.secondaryLabel = ""
104 
105         tileView.changeState(state)
106 
107         assertThat(TextUtils.isEmpty(state.secondaryLabel)).isTrue()
108     }
109 
110     @Test
testSecondaryLabelNotModified_availableNotBoolean_activenull111     fun testSecondaryLabelNotModified_availableNotBoolean_active() {
112         val state = QSTile.State()
113         state.state = Tile.STATE_ACTIVE
114         state.secondaryLabel = ""
115 
116         tileView.changeState(state)
117 
118         assertThat(TextUtils.isEmpty(state.secondaryLabel)).isTrue()
119     }
120 
121     @Test
testSecondaryLabelDescription_unavailable_defaultnull122     fun testSecondaryLabelDescription_unavailable_default() {
123         val state = QSTile.State()
124         state.state = Tile.STATE_UNAVAILABLE
125         state.secondaryLabel = ""
126 
127         tileView.changeState(state)
128 
129         assertThat(state.secondaryLabel as CharSequence).isEqualTo(
130             context.getString(R.string.tile_unavailable)
131         )
132     }
133 
134     @Test
testSecondaryLabelDescription_booleanInactive_defaultnull135     fun testSecondaryLabelDescription_booleanInactive_default() {
136         val state = QSTile.BooleanState()
137         state.state = Tile.STATE_INACTIVE
138         state.secondaryLabel = ""
139 
140         tileView.changeState(state)
141 
142         assertThat(state.secondaryLabel as CharSequence).isEqualTo(
143             context.getString(R.string.switch_bar_off)
144         )
145     }
146 
147     @Test
testSecondaryLabelDescription_booleanActive_defaultnull148     fun testSecondaryLabelDescription_booleanActive_default() {
149         val state = QSTile.BooleanState()
150         state.state = Tile.STATE_ACTIVE
151         state.secondaryLabel = ""
152 
153         tileView.changeState(state)
154 
155         assertThat(state.secondaryLabel as CharSequence).isEqualTo(
156             context.getString(R.string.switch_bar_on)
157         )
158     }
159 
160     @Test
testShowCustomDrawableViewBooleanStatenull161     fun testShowCustomDrawableViewBooleanState() {
162         val state = QSTile.BooleanState()
163         state.sideViewCustomDrawable = customDrawable
164 
165         tileView.changeState(state)
166 
167         assertThat(customDrawableView.visibility).isEqualTo(View.VISIBLE)
168         assertThat(chevronView.visibility).isEqualTo(View.GONE)
169     }
170 
171     @Test
testShowCustomDrawableViewNonBooleanStatenull172     fun testShowCustomDrawableViewNonBooleanState() {
173         val state = QSTile.State()
174         state.sideViewCustomDrawable = customDrawable
175 
176         tileView.changeState(state)
177 
178         assertThat(customDrawableView.visibility).isEqualTo(View.VISIBLE)
179         assertThat(chevronView.visibility).isEqualTo(View.GONE)
180     }
181 
182     @Test
testShowCustomDrawableViewBooleanStateForceChevronnull183     fun testShowCustomDrawableViewBooleanStateForceChevron() {
184         val state = QSTile.BooleanState()
185         state.sideViewCustomDrawable = customDrawable
186         state.forceExpandIcon = true
187 
188         tileView.changeState(state)
189 
190         assertThat(customDrawableView.visibility).isEqualTo(View.VISIBLE)
191         assertThat(chevronView.visibility).isEqualTo(View.GONE)
192     }
193 
194     @Test
testShowChevronNonBooleanStatenull195     fun testShowChevronNonBooleanState() {
196         val state = QSTile.State()
197 
198         tileView.changeState(state)
199 
200         assertThat(customDrawableView.visibility).isEqualTo(View.GONE)
201         assertThat(chevronView.visibility).isEqualTo(View.VISIBLE)
202     }
203 
204     @Test
testShowChevronBooleanStateForcheShownull205     fun testShowChevronBooleanStateForcheShow() {
206         val state = QSTile.BooleanState()
207         state.forceExpandIcon = true
208 
209         tileView.changeState(state)
210 
211         assertThat(customDrawableView.visibility).isEqualTo(View.GONE)
212         assertThat(chevronView.visibility).isEqualTo(View.VISIBLE)
213     }
214 
215     @Test
testNoImageShownnull216     fun testNoImageShown() {
217         val state = QSTile.BooleanState()
218 
219         tileView.changeState(state)
220 
221         assertThat(customDrawableView.visibility).isEqualTo(View.GONE)
222         assertThat(chevronView.visibility).isEqualTo(View.GONE)
223     }
224 
225     @Test
testUseStateStringsForKnownSpec_Booleannull226     fun testUseStateStringsForKnownSpec_Boolean() {
227         val state = QSTile.BooleanState()
228         val spec = "internet"
229         state.spec = spec
230 
231         val unavailableString = "${spec}_unavailable"
232         val offString = "${spec}_off"
233         val onString = "${spec}_on"
234 
235         context.orCreateTestableResources.addOverride(R.array.tile_states_internet, arrayOf(
236             unavailableString,
237             offString,
238             onString
239         ))
240 
241         // State UNAVAILABLE
242         state.secondaryLabel = ""
243         state.state = Tile.STATE_UNAVAILABLE
244         tileView.changeState(state)
245         assertThat((tileView.secondaryLabel as TextView).text).isEqualTo(unavailableString)
246 
247         // State INACTIVE
248         state.secondaryLabel = ""
249         state.state = Tile.STATE_INACTIVE
250         tileView.changeState(state)
251         assertThat((tileView.secondaryLabel as TextView).text).isEqualTo(offString)
252 
253         // State ACTIVE
254         state.secondaryLabel = ""
255         state.state = Tile.STATE_ACTIVE
256         tileView.changeState(state)
257         assertThat((tileView.secondaryLabel as TextView).text).isEqualTo(onString)
258     }
259 
260     class FakeTileView(
261         context: Context,
262         icon: QSIconView,
263         collapsed: Boolean
264     ) : QSTileViewImpl(context, icon, collapsed) {
changeStatenull265         fun changeState(state: QSTile.State) {
266             handleStateChanged(state)
267         }
268     }
269 }