1 /*
<lambda>null2  * 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.ComponentName
20 import androidx.test.ext.junit.runners.AndroidJUnit4
21 import androidx.test.filters.SmallTest
22 import com.android.systemui.SysuiTestCase
23 import com.android.systemui.qs.QSHost
24 import com.android.systemui.qs.external.CustomTile
25 import com.android.systemui.qs.tiles.AirplaneModeTile
26 import com.android.systemui.qs.tiles.AlarmTile
27 import com.android.systemui.qs.tiles.BatterySaverTile
28 import com.android.systemui.qs.tiles.BluetoothTile
29 import com.android.systemui.qs.tiles.CameraToggleTile
30 import com.android.systemui.qs.tiles.CastTile
31 import com.android.systemui.qs.tiles.ColorCorrectionTile
32 import com.android.systemui.qs.tiles.ColorInversionTile
33 import com.android.systemui.qs.tiles.DataSaverTile
34 import com.android.systemui.qs.tiles.DeviceControlsTile
35 import com.android.systemui.qs.tiles.DndTile
36 import com.android.systemui.qs.tiles.DreamTile
37 import com.android.systemui.qs.tiles.FlashlightTile
38 import com.android.systemui.qs.tiles.FontScalingTile
39 import com.android.systemui.qs.tiles.HotspotTile
40 import com.android.systemui.qs.tiles.InternetTile
41 import com.android.systemui.qs.tiles.LocationTile
42 import com.android.systemui.qs.tiles.MicrophoneToggleTile
43 import com.android.systemui.qs.tiles.NfcTile
44 import com.android.systemui.qs.tiles.NightDisplayTile
45 import com.android.systemui.qs.tiles.OneHandedModeTile
46 import com.android.systemui.qs.tiles.QRCodeScannerTile
47 import com.android.systemui.qs.tiles.QuickAccessWalletTile
48 import com.android.systemui.qs.tiles.ReduceBrightColorsTile
49 import com.android.systemui.qs.tiles.RotationLockTile
50 import com.android.systemui.qs.tiles.ScreenRecordTile
51 import com.android.systemui.qs.tiles.UiModeNightTile
52 import com.android.systemui.qs.tiles.WorkModeTile
53 import com.android.systemui.util.mockito.any
54 import com.google.common.truth.Truth.assertThat
55 import org.junit.Before
56 import org.junit.Test
57 import org.junit.runner.RunWith
58 import org.mockito.ArgumentMatchers.anyString
59 import org.mockito.Mock
60 import org.mockito.Mockito.inOrder
61 import org.mockito.MockitoAnnotations
62 import javax.inject.Provider
63 import org.mockito.Mockito.`when` as whenever
64 
65 private val specMap = mapOf(
66         "internet" to InternetTile::class.java,
67         "bt" to BluetoothTile::class.java,
68         "dnd" to DndTile::class.java,
69         "inversion" to ColorInversionTile::class.java,
70         "airplane" to AirplaneModeTile::class.java,
71         "work" to WorkModeTile::class.java,
72         "rotation" to RotationLockTile::class.java,
73         "flashlight" to FlashlightTile::class.java,
74         "location" to LocationTile::class.java,
75         "cast" to CastTile::class.java,
76         "hotspot" to HotspotTile::class.java,
77         "battery" to BatterySaverTile::class.java,
78         "saver" to DataSaverTile::class.java,
79         "night" to NightDisplayTile::class.java,
80         "nfc" to NfcTile::class.java,
81         "dark" to UiModeNightTile::class.java,
82         "screenrecord" to ScreenRecordTile::class.java,
83         "reduce_brightness" to ReduceBrightColorsTile::class.java,
84         "cameratoggle" to CameraToggleTile::class.java,
85         "mictoggle" to MicrophoneToggleTile::class.java,
86         "controls" to DeviceControlsTile::class.java,
87         "alarm" to AlarmTile::class.java,
88         "wallet" to QuickAccessWalletTile::class.java,
89         "qr_code_scanner" to QRCodeScannerTile::class.java,
90         "onehanded" to OneHandedModeTile::class.java,
91         "color_correction" to ColorCorrectionTile::class.java,
92         "dream" to DreamTile::class.java,
93         "font_scaling" to FontScalingTile::class.java
94 )
95 
96 @RunWith(AndroidJUnit4::class)
97 @SmallTest
98 class QSFactoryImplTest : SysuiTestCase() {
99 
100     @Mock private lateinit var qsHost: QSHost
101     @Mock private lateinit var customTileFactory: CustomTile.Factory
102     @Mock private lateinit var customTile: CustomTile
103 
104     @Mock private lateinit var internetTile: InternetTile
105     @Mock private lateinit var bluetoothTile: BluetoothTile
106     @Mock private lateinit var dndTile: DndTile
107     @Mock private lateinit var colorInversionTile: ColorInversionTile
108     @Mock private lateinit var airplaneTile: AirplaneModeTile
109     @Mock private lateinit var workTile: WorkModeTile
110     @Mock private lateinit var rotationTile: RotationLockTile
111     @Mock private lateinit var flashlightTile: FlashlightTile
112     @Mock private lateinit var locationTile: LocationTile
113     @Mock private lateinit var castTile: CastTile
114     @Mock private lateinit var hotspotTile: HotspotTile
115     @Mock private lateinit var batterySaverTile: BatterySaverTile
116     @Mock private lateinit var dataSaverTile: DataSaverTile
117     @Mock private lateinit var nightDisplayTile: NightDisplayTile
118     @Mock private lateinit var nfcTile: NfcTile
119     @Mock private lateinit var darkModeTile: UiModeNightTile
120     @Mock private lateinit var screenRecordTile: ScreenRecordTile
121     @Mock private lateinit var reduceBrightColorsTile: ReduceBrightColorsTile
122     @Mock private lateinit var cameraToggleTile: CameraToggleTile
123     @Mock private lateinit var microphoneToggleTile: MicrophoneToggleTile
124     @Mock private lateinit var deviceControlsTile: DeviceControlsTile
125     @Mock private lateinit var alarmTile: AlarmTile
126     @Mock private lateinit var quickAccessWalletTile: QuickAccessWalletTile
127     @Mock private lateinit var qrCodeScannerTile: QRCodeScannerTile
128     @Mock private lateinit var oneHandedModeTile: OneHandedModeTile
129     @Mock private lateinit var colorCorrectionTile: ColorCorrectionTile
130     @Mock private lateinit var dreamTile: DreamTile
131     @Mock private lateinit var fontScalingTile: FontScalingTile
132 
133     private lateinit var factory: QSFactoryImpl
134 
135     @Before
136     fun setUp() {
137         MockitoAnnotations.initMocks(this)
138 
139         whenever(qsHost.context).thenReturn(mContext)
140         whenever(qsHost.userContext).thenReturn(mContext)
141         whenever(customTileFactory.create(anyString(), any())).thenReturn(customTile)
142 
143         val tileMap = mutableMapOf<String, Provider<QSTileImpl<*>>>(
144             "internet" to Provider { internetTile },
145             "bt" to Provider { bluetoothTile },
146             "dnd" to Provider { dndTile },
147             "inversion" to Provider { colorInversionTile },
148             "airplane" to Provider { airplaneTile },
149             "work" to Provider { workTile },
150             "rotation" to Provider { rotationTile },
151             "flashlight" to Provider { flashlightTile },
152             "location" to Provider { locationTile },
153             "cast" to Provider { castTile },
154             "hotspot" to Provider { hotspotTile },
155             "battery" to Provider { batterySaverTile },
156             "saver" to Provider { dataSaverTile },
157             "night" to Provider { nightDisplayTile },
158             "nfc" to Provider { nfcTile },
159             "dark" to Provider { darkModeTile },
160             "screenrecord" to Provider { screenRecordTile },
161             "reduce_brightness" to Provider { reduceBrightColorsTile },
162             "cameratoggle" to Provider { cameraToggleTile },
163             "mictoggle" to Provider { microphoneToggleTile },
164             "controls" to Provider { deviceControlsTile },
165             "alarm" to Provider { alarmTile },
166             "wallet" to Provider { quickAccessWalletTile },
167             "qr_code_scanner" to Provider { qrCodeScannerTile },
168             "onehanded" to Provider { oneHandedModeTile },
169             "color_correction" to Provider { colorCorrectionTile },
170             "dream" to Provider { dreamTile },
171             "font_scaling" to Provider { fontScalingTile }
172         )
173 
174         factory = QSFactoryImpl(
175                 { qsHost },
176                 { customTileFactory },
177                 tileMap,
178         )
179         // When adding/removing tiles, fix also [specMap] and [tileMap]
180     }
181 
182     @Test
183     fun testCorrectTileClassStock() {
184         specMap.forEach { spec, klazz ->
185             assertThat(factory.createTile(spec)).isInstanceOf(klazz)
186         }
187     }
188 
189     @Test
190     fun testCustomTileClass() {
191         val customSpec = CustomTile.toSpec(ComponentName("test", "test"))
192         assertThat(factory.createTile(customSpec)).isInstanceOf(CustomTile::class.java)
193     }
194 
195     @Test
196     fun testBadTileNull() {
197         assertThat(factory.createTile("-432~")).isNull()
198     }
199 
200     @Test
201     fun testTileInitializedAndStale() {
202         specMap.forEach { spec, _ ->
203             val tile = factory.createTile(spec) as QSTileImpl<*>
204             val inOrder = inOrder(tile)
205             inOrder.verify(tile).initialize()
206             inOrder.verify(tile).postStale()
207         }
208 
209         val customSpec = CustomTile.toSpec(ComponentName("test", "test"))
210         val tile = factory.createTile(customSpec) as QSTileImpl<*>
211         val inOrder = inOrder(tile)
212         inOrder.verify(tile).initialize()
213         inOrder.verify(tile).postStale()
214     }
215 }
216