• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.panels.domain.interactor
18 
19 import android.content.ComponentName
20 import android.graphics.drawable.TestStubDrawable
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.ContentDescription
25 import com.android.systemui.common.shared.model.Icon
26 import com.android.systemui.common.shared.model.Text
27 import com.android.systemui.kosmos.testScope
28 import com.android.systemui.qs.panels.data.repository.iconAndNameCustomRepository
29 import com.android.systemui.qs.panels.data.repository.stockTilesRepository
30 import com.android.systemui.qs.panels.shared.model.EditTileData
31 import com.android.systemui.qs.pipeline.data.repository.FakeInstalledTilesComponentRepository
32 import com.android.systemui.qs.pipeline.data.repository.fakeInstalledTilesRepository
33 import com.android.systemui.qs.pipeline.shared.TileSpec
34 import com.android.systemui.qs.shared.model.TileCategory
35 import com.android.systemui.qs.tiles.base.shared.model.QSTileConfig
36 import com.android.systemui.qs.tiles.base.shared.model.fakeQSTileConfigProvider
37 import com.android.systemui.qs.tiles.base.shared.model.qSTileConfigProvider
38 import com.android.systemui.qs.tiles.impl.battery.qsBatterySaverTileConfig
39 import com.android.systemui.qs.tiles.impl.flashlight.qsFlashlightTileConfig
40 import com.android.systemui.qs.tiles.impl.internet.qsInternetTileConfig
41 import com.android.systemui.settings.userTracker
42 import com.android.systemui.testKosmos
43 import com.google.common.truth.Truth.assertThat
44 import kotlinx.coroutines.test.runTest
45 import org.junit.Before
46 import org.junit.Test
47 import org.junit.runner.RunWith
48 
49 @RunWith(AndroidJUnit4::class)
50 @SmallTest
51 class EditTilesListInteractorTest : SysuiTestCase() {
52     private val kosmos = testKosmos()
53 
54     // Only have some configurations so we can test the effect of missing configurations.
55     // As the configurations are injected by dagger, we'll have all the existing configurations
56     private val internetTileConfig = kosmos.qsInternetTileConfig
57     private val flashlightTileConfig = kosmos.qsFlashlightTileConfig
58     private val batteryTileConfig = kosmos.qsBatterySaverTileConfig
59 
60     private val serviceInfo =
61         FakeInstalledTilesComponentRepository.ServiceInfo(component, tileName, icon, appName)
62 
63     private val underTest =
<lambda>null64         with(kosmos) {
65             EditTilesListInteractor(
66                 stockTilesRepository,
67                 qSTileConfigProvider,
68                 iconAndNameCustomRepository,
69             )
70         }
71 
72     @Before
setUpnull73     fun setUp() {
74         with(kosmos) {
75             fakeInstalledTilesRepository.setInstalledServicesForUser(
76                 userTracker.userId,
77                 listOf(serviceInfo),
78             )
79 
80             with(fakeQSTileConfigProvider) {
81                 putConfig(internetTileConfig.tileSpec, internetTileConfig)
82                 putConfig(flashlightTileConfig.tileSpec, flashlightTileConfig)
83                 putConfig(batteryTileConfig.tileSpec, batteryTileConfig)
84             }
85         }
86     }
87 
88     @Test
getTilesToEdit_stockTilesHaveNoAppNamenull89     fun getTilesToEdit_stockTilesHaveNoAppName() =
90         with(kosmos) {
91             testScope.runTest {
92                 val editTiles = underTest.getTilesToEdit()
93 
94                 assertThat(editTiles.stockTiles.all { it.appName == null }).isTrue()
95             }
96         }
97 
98     @Test
getTilesToEdit_stockTilesAreAllPlatformSpecsnull99     fun getTilesToEdit_stockTilesAreAllPlatformSpecs() =
100         with(kosmos) {
101             testScope.runTest {
102                 val editTiles = underTest.getTilesToEdit()
103 
104                 assertThat(editTiles.stockTiles.all { it.tileSpec is TileSpec.PlatformTileSpec })
105                     .isTrue()
106             }
107         }
108 
109     @Test
getTilesToEdit_stockTiles_sameOrderAsRepositorynull110     fun getTilesToEdit_stockTiles_sameOrderAsRepository() =
111         with(kosmos) {
112             testScope.runTest {
113                 val editTiles = underTest.getTilesToEdit()
114 
115                 assertThat(editTiles.stockTiles.map { it.tileSpec })
116                     .isEqualTo(stockTilesRepository.stockTiles)
117             }
118         }
119 
120     @Test
getTilesToEdit_customTileData_matchesServicenull121     fun getTilesToEdit_customTileData_matchesService() =
122         with(kosmos) {
123             testScope.runTest {
124                 val editTiles = underTest.getTilesToEdit()
125                 val expected =
126                     EditTileData(
127                         tileSpec = TileSpec.create(component),
128                         icon = Icon.Loaded(icon, ContentDescription.Loaded(tileName)),
129                         label = Text.Loaded(tileName),
130                         appName = Text.Loaded(appName),
131                         category = TileCategory.PROVIDED_BY_APP,
132                     )
133 
134                 assertThat(editTiles.customTiles).hasSize(1)
135                 assertThat(editTiles.customTiles[0]).isEqualTo(expected)
136             }
137         }
138 
139     @Test
getTilesToEdit_tilesInConfigProvider_correctDatanull140     fun getTilesToEdit_tilesInConfigProvider_correctData() =
141         with(kosmos) {
142             testScope.runTest {
143                 val editTiles = underTest.getTilesToEdit()
144 
145                 assertThat(
146                         editTiles.stockTiles.first { it.tileSpec == internetTileConfig.tileSpec }
147                     )
148                     .isEqualTo(internetTileConfig.toEditTileData())
149                 assertThat(
150                         editTiles.stockTiles.first { it.tileSpec == flashlightTileConfig.tileSpec }
151                     )
152                     .isEqualTo(flashlightTileConfig.toEditTileData())
153                 assertThat(editTiles.stockTiles.first { it.tileSpec == batteryTileConfig.tileSpec })
154                     .isEqualTo(batteryTileConfig.toEditTileData())
155             }
156         }
157 
158     @Test
getTilesToEdit_tilesNotInConfigProvider_useDefaultDatanull159     fun getTilesToEdit_tilesNotInConfigProvider_useDefaultData() =
160         with(kosmos) {
161             testScope.runTest {
162                 underTest
163                     .getTilesToEdit()
164                     .stockTiles
165                     .filterNot { qSTileConfigProvider.hasConfig(it.tileSpec.spec) }
166                     .forEach { assertThat(it).isEqualTo(it.tileSpec.missingConfigEditTileData()) }
167             }
168         }
169 
170     private companion object {
171         val component = ComponentName("pkg", "srv")
172         const val tileName = "Tile Service"
173         const val appName = "App"
174         val icon = TestStubDrawable("icon")
175 
TileSpecnull176         fun TileSpec.missingConfigEditTileData(): EditTileData {
177             return EditTileData(
178                 tileSpec = this,
179                 icon = Icon.Resource(android.R.drawable.star_on, ContentDescription.Loaded(spec)),
180                 label = Text.Loaded(spec),
181                 appName = null,
182                 category = TileCategory.UNKNOWN,
183             )
184         }
185 
toEditTileDatanull186         fun QSTileConfig.toEditTileData(): EditTileData {
187             return EditTileData(
188                 tileSpec = tileSpec,
189                 icon =
190                     Icon.Resource(uiConfig.iconRes, ContentDescription.Resource(uiConfig.labelRes)),
191                 label = Text.Resource(uiConfig.labelRes),
192                 appName = null,
193                 category = category,
194             )
195         }
196     }
197 }
198