1 /*
2  * Copyright (C) 2023 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.pipeline.domain.autoaddable
18 
19 import android.content.ComponentName
20 import android.platform.test.annotations.EnabledOnRavenwood
21 import androidx.test.ext.junit.runners.AndroidJUnit4
22 import androidx.test.filters.SmallTest
23 import com.android.systemui.res.R
24 import com.android.systemui.SysuiTestCase
25 import com.android.systemui.qs.pipeline.shared.TileSpec
26 import com.android.systemui.util.mockito.mock
27 import com.google.common.truth.Truth.assertThat
28 import org.junit.Test
29 import org.junit.runner.RunWith
30 
31 @SmallTest
32 @EnabledOnRavenwood
33 @RunWith(AndroidJUnit4::class)
34 class AutoAddableSettingListTest : SysuiTestCase() {
35 
36     private val factory =
37         object : AutoAddableSetting.Factory {
createnull38             override fun create(setting: String, spec: TileSpec): AutoAddableSetting {
39                 return AutoAddableSetting(
40                     mock(),
41                     mock(),
42                     setting,
43                     spec,
44                 )
45             }
46         }
47 
48     @Test
correctLines_correctAutoAddablesnull49     fun correctLines_correctAutoAddables() {
50         val setting1 = "setting1"
51         val setting2 = "setting2"
52         val spec1 = TileSpec.create("spec1")
53         val spec2 = TileSpec.create(ComponentName("pkg", "cls"))
54 
55         context.orCreateTestableResources.addOverride(
56             R.array.config_quickSettingsAutoAdd,
57             arrayOf(toStringLine(setting1, spec1), toStringLine(setting2, spec2))
58         )
59 
60         val autoAddables = AutoAddableSettingList.parseSettingsResource(context.resources, factory)
61 
62         assertThat(autoAddables)
63             .containsExactly(factory.create(setting1, spec1), factory.create(setting2, spec2))
64     }
65 
66     @Test
malformedLine_ignorednull67     fun malformedLine_ignored() {
68         val setting = "setting"
69         val spec = TileSpec.create("spec")
70 
71         context.orCreateTestableResources.addOverride(
72             R.array.config_quickSettingsAutoAdd,
73             arrayOf(toStringLine(setting, spec), "bad_line")
74         )
75 
76         val autoAddables = AutoAddableSettingList.parseSettingsResource(context.resources, factory)
77 
78         assertThat(autoAddables).containsExactly(factory.create(setting, spec))
79     }
80 
81     @Test
invalidSpec_ignorednull82     fun invalidSpec_ignored() {
83         val setting = "setting"
84         val spec = TileSpec.create("spec")
85 
86         context.orCreateTestableResources.addOverride(
87             R.array.config_quickSettingsAutoAdd,
88             arrayOf(toStringLine(setting, spec), "invalid:")
89         )
90 
91         val autoAddables = AutoAddableSettingList.parseSettingsResource(context.resources, factory)
92 
93         assertThat(autoAddables).containsExactly(factory.create(setting, spec))
94     }
95 
96     companion object {
toStringLinenull97         private fun toStringLine(setting: String, spec: TileSpec) =
98             "$setting$SETTINGS_SEPARATOR${spec.spec}"
99         private const val SETTINGS_SEPARATOR = ":"
100     }
101 }
102