1 /*
<lambda>null2  * 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.statusbar.pipeline.mobile.data.model
18 
19 import android.os.PersistableBundle
20 import android.telephony.CarrierConfigManager
21 import android.telephony.CarrierConfigManager.KEY_INFLATE_SIGNAL_STRENGTH_BOOL
22 import android.telephony.CarrierConfigManager.KEY_SHOW_5G_SLICE_ICON_BOOL
23 import android.telephony.CarrierConfigManager.KEY_SHOW_OPERATOR_NAME_IN_STATUSBAR_BOOL
24 import androidx.test.filters.SmallTest
25 import com.android.systemui.SysuiTestCase
26 import com.google.common.truth.Truth.assertThat
27 import kotlinx.coroutines.ExperimentalCoroutinesApi
28 import org.junit.Before
29 import org.junit.Test
30 
31 @OptIn(ExperimentalCoroutinesApi::class)
32 @SmallTest
33 class SystemUiCarrierConfigTest : SysuiTestCase() {
34 
35     lateinit var underTest: SystemUiCarrierConfig
36 
37     @Before
38     fun setUp() {
39         underTest = SystemUiCarrierConfig(SUB_1_ID, createTestConfig())
40     }
41 
42     @Test
43     fun processNewConfig_reflectedByIsUsingDefault() {
44         // Starts out using the defaults
45         assertThat(underTest.isUsingDefault).isTrue()
46 
47         // ANY new config means we're no longer tracking defaults
48         underTest.processNewCarrierConfig(createTestConfig())
49 
50         assertThat(underTest.isUsingDefault).isFalse()
51     }
52 
53     @Test
54     fun processNewConfig_updatesAllFlows() {
55         assertThat(underTest.shouldInflateSignalStrength.value).isFalse()
56         assertThat(underTest.showOperatorNameInStatusBar.value).isFalse()
57         assertThat(underTest.allowNetworkSliceIndicator.value).isTrue()
58 
59         underTest.processNewCarrierConfig(
60             configWithOverrides(
61                 KEY_INFLATE_SIGNAL_STRENGTH_BOOL to true,
62                 KEY_SHOW_OPERATOR_NAME_IN_STATUSBAR_BOOL to true,
63                 KEY_SHOW_5G_SLICE_ICON_BOOL to false,
64             )
65         )
66 
67         assertThat(underTest.shouldInflateSignalStrength.value).isTrue()
68         assertThat(underTest.showOperatorNameInStatusBar.value).isTrue()
69         assertThat(underTest.allowNetworkSliceIndicator.value).isFalse()
70     }
71 
72     @Test
73     fun processNewConfig_defaultsToFalseForConfigOverrides() {
74         // This case is only apparent when:
75         //   1. The default is true
76         //   2. The override config has no value for a given key
77         // In this case (per the old code) we would use the default value of false, despite there
78         // being no override key present in the override config
79 
80         underTest =
81             SystemUiCarrierConfig(
82                 SUB_1_ID,
83                 configWithOverrides(
84                     KEY_INFLATE_SIGNAL_STRENGTH_BOOL to true,
85                     KEY_SHOW_OPERATOR_NAME_IN_STATUSBAR_BOOL to true,
86                     KEY_SHOW_5G_SLICE_ICON_BOOL to true,
87                 )
88             )
89 
90         assertThat(underTest.isUsingDefault).isTrue()
91         assertThat(underTest.shouldInflateSignalStrength.value).isTrue()
92         assertThat(underTest.showOperatorNameInStatusBar.value).isTrue()
93         assertThat(underTest.allowNetworkSliceIndicator.value).isTrue()
94 
95         // Process a new config with no keys
96         underTest.processNewCarrierConfig(PersistableBundle())
97 
98         assertThat(underTest.isUsingDefault).isFalse()
99         assertThat(underTest.shouldInflateSignalStrength.value).isFalse()
100         assertThat(underTest.showOperatorNameInStatusBar.value).isFalse()
101         assertThat(underTest.allowNetworkSliceIndicator.value).isFalse()
102     }
103 
104     companion object {
105         private const val SUB_1_ID = 1
106 
107         /**
108          * In order to keep us from having to update every place that might want to create a config,
109          * make sure to add new keys here
110          */
111         fun createTestConfig() =
112             PersistableBundle().also {
113                 it.putBoolean(CarrierConfigManager.KEY_INFLATE_SIGNAL_STRENGTH_BOOL, false)
114                 it.putBoolean(CarrierConfigManager.KEY_SHOW_OPERATOR_NAME_IN_STATUSBAR_BOOL, false)
115                 it.putBoolean(CarrierConfigManager.KEY_SHOW_5G_SLICE_ICON_BOOL, true)
116             }
117 
118         /** Override the default config with the given (key, value) pair */
119         fun configWithOverride(key: String, override: Boolean): PersistableBundle =
120             createTestConfig().also { it.putBoolean(key, override) }
121 
122         /** Override any number of configs from the default */
123         fun configWithOverrides(vararg overrides: Pair<String, Boolean>) =
124             createTestConfig().also { config ->
125                 overrides.forEach { (key, value) -> config.putBoolean(key, value) }
126             }
127     }
128 }
129