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