• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 android.testing.AndroidTestingRunner
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.leak.GarbageMonitor
54 import com.google.common.truth.Truth.assertThat
55 import javax.inject.Provider
56 import org.junit.Before
57 import org.junit.Test
58 import org.junit.runner.RunWith
59 import org.mockito.Answers
60 import org.mockito.Mock
61 import org.mockito.Mockito.inOrder
62 import org.mockito.Mockito.`when` as whenever
63 import org.mockito.MockitoAnnotations
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(AndroidTestingRunner::class)
97 @SmallTest
98 class QSFactoryImplTest : SysuiTestCase() {
99 
100     @Mock private lateinit var qsHost: QSHost
101     @Mock(answer = Answers.RETURNS_SELF) private lateinit var customTileBuilder: CustomTile.Builder
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 memoryTile: GarbageMonitor.MemoryTile
120     @Mock private lateinit var darkModeTile: UiModeNightTile
121     @Mock private lateinit var screenRecordTile: ScreenRecordTile
122     @Mock private lateinit var reduceBrightColorsTile: ReduceBrightColorsTile
123     @Mock private lateinit var cameraToggleTile: CameraToggleTile
124     @Mock private lateinit var microphoneToggleTile: MicrophoneToggleTile
125     @Mock private lateinit var deviceControlsTile: DeviceControlsTile
126     @Mock private lateinit var alarmTile: AlarmTile
127     @Mock private lateinit var quickAccessWalletTile: QuickAccessWalletTile
128     @Mock private lateinit var qrCodeScannerTile: QRCodeScannerTile
129     @Mock private lateinit var oneHandedModeTile: OneHandedModeTile
130     @Mock private lateinit var colorCorrectionTile: ColorCorrectionTile
131     @Mock private lateinit var dreamTile: DreamTile
132     @Mock private lateinit var fontScalingTile: FontScalingTile
133 
134     private lateinit var factory: QSFactoryImpl
135 
136     @Before
137     fun setUp() {
138         MockitoAnnotations.initMocks(this)
139 
140         whenever(qsHost.context).thenReturn(mContext)
141         whenever(qsHost.userContext).thenReturn(mContext)
142         whenever(customTileBuilder.build()).thenReturn(customTile)
143 
144         val tileMap = mutableMapOf<String, Provider<QSTileImpl<*>>>(
145             "internet" to Provider { internetTile },
146             "bt" to Provider { bluetoothTile },
147             "dnd" to Provider { dndTile },
148             "inversion" to Provider { colorInversionTile },
149             "airplane" to Provider { airplaneTile },
150             "work" to Provider { workTile },
151             "rotation" to Provider { rotationTile },
152             "flashlight" to Provider { flashlightTile },
153             "location" to Provider { locationTile },
154             "cast" to Provider { castTile },
155             "hotspot" to Provider { hotspotTile },
156             "battery" to Provider { batterySaverTile },
157             "saver" to Provider { dataSaverTile },
158             "night" to Provider { nightDisplayTile },
159             "nfc" to Provider { nfcTile },
160             "dark" to Provider { darkModeTile },
161             "screenrecord" to Provider { screenRecordTile },
162             "reduce_brightness" to Provider { reduceBrightColorsTile },
163             "cameratoggle" to Provider { cameraToggleTile },
164             "mictoggle" to Provider { microphoneToggleTile },
165             "controls" to Provider { deviceControlsTile },
166             "alarm" to Provider { alarmTile },
167             "wallet" to Provider { quickAccessWalletTile },
168             "qr_code_scanner" to Provider { qrCodeScannerTile },
169             "onehanded" to Provider { oneHandedModeTile },
170             "color_correction" to Provider { colorCorrectionTile },
171             "dream" to Provider { dreamTile },
172             "font_scaling" to Provider { fontScalingTile }
173         )
174 
175         factory = QSFactoryImpl(
176                 { qsHost },
177                 { customTileBuilder },
178                 tileMap,
179         )
180         // When adding/removing tiles, fix also [specMap] and [tileMap]
181     }
182 
183     @Test
184     fun testCorrectTileClassStock() {
185         specMap.forEach { spec, klazz ->
186             assertThat(factory.createTile(spec)).isInstanceOf(klazz)
187         }
188     }
189 
190     @Test
191     fun testCustomTileClass() {
192         val customSpec = CustomTile.toSpec(ComponentName("test", "test"))
193         assertThat(factory.createTile(customSpec)).isInstanceOf(CustomTile::class.java)
194     }
195 
196     @Test
197     fun testBadTileNull() {
198         assertThat(factory.createTile("-432~")).isNull()
199     }
200 
201     @Test
202     fun testTileInitializedAndStale() {
203         specMap.forEach { spec, _ ->
204             val tile = factory.createTile(spec) as QSTileImpl<*>
205             val inOrder = inOrder(tile)
206             inOrder.verify(tile).initialize()
207             inOrder.verify(tile).postStale()
208         }
209 
210         val customSpec = CustomTile.toSpec(ComponentName("test", "test"))
211         val tile = factory.createTile(customSpec) as QSTileImpl<*>
212         val inOrder = inOrder(tile)
213         inOrder.verify(tile).initialize()
214         inOrder.verify(tile).postStale()
215     }
216 }
217