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 
17 package com.android.systemui.qs.tiles.impl.battery.ui
18 
19 import android.graphics.drawable.TestStubDrawable
20 import android.widget.Switch
21 import androidx.test.ext.junit.runners.AndroidJUnit4
22 import androidx.test.filters.SmallTest
23 import com.android.systemui.SysuiTestCase
24 import com.android.systemui.common.shared.model.Icon
25 import com.android.systemui.kosmos.Kosmos
26 import com.android.systemui.qs.tiles.impl.battery.domain.model.BatterySaverTileModel
27 import com.android.systemui.qs.tiles.impl.battery.qsBatterySaverTileConfig
28 import com.android.systemui.qs.tiles.impl.custom.QSTileStateSubject
29 import com.android.systemui.qs.tiles.viewmodel.QSTileState
30 import com.android.systemui.res.R
31 import org.junit.Before
32 import org.junit.Test
33 import org.junit.runner.RunWith
34 
35 @SmallTest
36 @RunWith(AndroidJUnit4::class)
37 class BatterySaverTileMapperTest : SysuiTestCase() {
38     private val kosmos = Kosmos()
39     private val batterySaverTileConfig = kosmos.qsBatterySaverTileConfig
40     private lateinit var mapper: BatterySaverTileMapper
41 
42     @Before
setupnull43     fun setup() {
44         mapper =
45             BatterySaverTileMapper(
46                 context.orCreateTestableResources
47                     .apply {
48                         addOverride(R.drawable.qs_battery_saver_icon_off, TestStubDrawable())
49                         addOverride(R.drawable.qs_battery_saver_icon_on, TestStubDrawable())
50                     }
51                     .resources,
52                 context.theme,
53             )
54     }
55 
56     @Test
map_standard_notPluggedInNotPowerSavingnull57     fun map_standard_notPluggedInNotPowerSaving() {
58         val inputModel = BatterySaverTileModel.Standard(false, false)
59 
60         val outputState = mapper.map(batterySaverTileConfig, inputModel)
61 
62         val expectedState =
63             createBatterySaverTileState(
64                 QSTileState.ActivationState.INACTIVE,
65                 "",
66                 R.drawable.qs_battery_saver_icon_off,
67                 null,
68             )
69         QSTileStateSubject.assertThat(outputState).isEqualTo(expectedState)
70     }
71 
72     @Test
map_standard_notPluggedInPowerSavingnull73     fun map_standard_notPluggedInPowerSaving() {
74         val inputModel = BatterySaverTileModel.Standard(false, true)
75 
76         val outputState = mapper.map(batterySaverTileConfig, inputModel)
77 
78         val expectedState =
79             createBatterySaverTileState(
80                 QSTileState.ActivationState.ACTIVE,
81                 "",
82                 R.drawable.qs_battery_saver_icon_on,
83                 null,
84             )
85         QSTileStateSubject.assertThat(outputState).isEqualTo(expectedState)
86     }
87 
88     @Test
map_standard_pluggedInPowerSavingnull89     fun map_standard_pluggedInPowerSaving() {
90         val inputModel = BatterySaverTileModel.Standard(true, true)
91 
92         val outputState = mapper.map(batterySaverTileConfig, inputModel)
93 
94         val expectedState =
95             createBatterySaverTileState(
96                 QSTileState.ActivationState.UNAVAILABLE,
97                 "",
98                 R.drawable.qs_battery_saver_icon_on,
99                 null,
100             )
101         QSTileStateSubject.assertThat(outputState).isEqualTo(expectedState)
102     }
103 
104     @Test
map_standard_pluggedInNotPowerSavingnull105     fun map_standard_pluggedInNotPowerSaving() {
106         val inputModel = BatterySaverTileModel.Standard(true, false)
107 
108         val outputState = mapper.map(batterySaverTileConfig, inputModel)
109 
110         val expectedState =
111             createBatterySaverTileState(
112                 QSTileState.ActivationState.UNAVAILABLE,
113                 "",
114                 R.drawable.qs_battery_saver_icon_off,
115                 null,
116             )
117         QSTileStateSubject.assertThat(outputState).isEqualTo(expectedState)
118     }
119 
120     @Test
map_extremeSaverDisabledNotPluggedInNotPowerSavingnull121     fun map_extremeSaverDisabledNotPluggedInNotPowerSaving() {
122         val inputModel = BatterySaverTileModel.Extreme(false, false, false)
123 
124         val outputState = mapper.map(batterySaverTileConfig, inputModel)
125 
126         val expectedState =
127             createBatterySaverTileState(
128                 QSTileState.ActivationState.INACTIVE,
129                 "",
130                 R.drawable.qs_battery_saver_icon_off,
131                 null,
132             )
133         QSTileStateSubject.assertThat(outputState).isEqualTo(expectedState)
134     }
135 
136     @Test
map_extremeSaverDisabledNotPluggedInPowerSavingnull137     fun map_extremeSaverDisabledNotPluggedInPowerSaving() {
138         val inputModel = BatterySaverTileModel.Extreme(false, true, false)
139 
140         val outputState = mapper.map(batterySaverTileConfig, inputModel)
141 
142         val expectedState =
143             createBatterySaverTileState(
144                 QSTileState.ActivationState.ACTIVE,
145                 context.getString(R.string.standard_battery_saver_text),
146                 R.drawable.qs_battery_saver_icon_on,
147                 context.getString(R.string.standard_battery_saver_text),
148             )
149         QSTileStateSubject.assertThat(outputState).isEqualTo(expectedState)
150     }
151 
152     @Test
map_extremeSaverDisabledPluggedInPowerSavingnull153     fun map_extremeSaverDisabledPluggedInPowerSaving() {
154         val inputModel = BatterySaverTileModel.Extreme(true, true, false)
155 
156         val outputState = mapper.map(batterySaverTileConfig, inputModel)
157 
158         val expectedState =
159             createBatterySaverTileState(
160                 QSTileState.ActivationState.UNAVAILABLE,
161                 "",
162                 R.drawable.qs_battery_saver_icon_on,
163                 null,
164             )
165         QSTileStateSubject.assertThat(outputState).isEqualTo(expectedState)
166     }
167 
168     @Test
map_extremeSaverDisabledPluggedInNotPowerSavingnull169     fun map_extremeSaverDisabledPluggedInNotPowerSaving() {
170         val inputModel = BatterySaverTileModel.Extreme(true, false, false)
171 
172         val outputState = mapper.map(batterySaverTileConfig, inputModel)
173 
174         val expectedState =
175             createBatterySaverTileState(
176                 QSTileState.ActivationState.UNAVAILABLE,
177                 "",
178                 R.drawable.qs_battery_saver_icon_off,
179                 null,
180             )
181         QSTileStateSubject.assertThat(outputState).isEqualTo(expectedState)
182     }
183 
184     @Test
map_extremeSaverEnabledNotPluggedInNotPowerSavingnull185     fun map_extremeSaverEnabledNotPluggedInNotPowerSaving() {
186         val inputModel = BatterySaverTileModel.Extreme(false, false, true)
187 
188         val outputState = mapper.map(batterySaverTileConfig, inputModel)
189 
190         val expectedState =
191             createBatterySaverTileState(
192                 QSTileState.ActivationState.INACTIVE,
193                 "",
194                 R.drawable.qs_battery_saver_icon_off,
195                 null,
196             )
197         QSTileStateSubject.assertThat(outputState).isEqualTo(expectedState)
198     }
199 
200     @Test
map_extremeSaverEnabledNotPluggedInPowerSavingnull201     fun map_extremeSaverEnabledNotPluggedInPowerSaving() {
202         val inputModel = BatterySaverTileModel.Extreme(false, true, true)
203 
204         val outputState = mapper.map(batterySaverTileConfig, inputModel)
205 
206         val expectedState =
207             createBatterySaverTileState(
208                 QSTileState.ActivationState.ACTIVE,
209                 context.getString(R.string.extreme_battery_saver_text),
210                 R.drawable.qs_battery_saver_icon_on,
211                 context.getString(R.string.extreme_battery_saver_text),
212             )
213         QSTileStateSubject.assertThat(outputState).isEqualTo(expectedState)
214     }
215 
216     @Test
map_extremeSaverEnabledPluggedInPowerSavingnull217     fun map_extremeSaverEnabledPluggedInPowerSaving() {
218         val inputModel = BatterySaverTileModel.Extreme(true, true, true)
219 
220         val outputState = mapper.map(batterySaverTileConfig, inputModel)
221 
222         val expectedState =
223             createBatterySaverTileState(
224                 QSTileState.ActivationState.UNAVAILABLE,
225                 "",
226                 R.drawable.qs_battery_saver_icon_on,
227                 null,
228             )
229         QSTileStateSubject.assertThat(outputState).isEqualTo(expectedState)
230     }
231 
232     @Test
map_extremeSaverEnabledPluggedInNotPowerSavingnull233     fun map_extremeSaverEnabledPluggedInNotPowerSaving() {
234         val inputModel = BatterySaverTileModel.Extreme(true, false, true)
235 
236         val outputState = mapper.map(batterySaverTileConfig, inputModel)
237 
238         val expectedState =
239             createBatterySaverTileState(
240                 QSTileState.ActivationState.UNAVAILABLE,
241                 "",
242                 R.drawable.qs_battery_saver_icon_off,
243                 null,
244             )
245         QSTileStateSubject.assertThat(outputState).isEqualTo(expectedState)
246     }
247 
createBatterySaverTileStatenull248     private fun createBatterySaverTileState(
249         activationState: QSTileState.ActivationState,
250         secondaryLabel: String,
251         iconRes: Int,
252         stateDescription: CharSequence?,
253     ): QSTileState {
254         val label = context.getString(R.string.battery_detail_switch_title)
255         return QSTileState(
256             Icon.Loaded(context.getDrawable(iconRes)!!, null),
257             iconRes,
258             label,
259             activationState,
260             secondaryLabel,
261             if (activationState == QSTileState.ActivationState.UNAVAILABLE)
262                 setOf(QSTileState.UserAction.LONG_CLICK)
263             else setOf(QSTileState.UserAction.CLICK, QSTileState.UserAction.LONG_CLICK),
264             label,
265             stateDescription,
266             QSTileState.SideViewIcon.None,
267             QSTileState.EnabledState.ENABLED,
268             Switch::class.qualifiedName,
269         )
270     }
271 }
272