1 /*
2  * Copyright 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 package com.android.server.bluetooth.test
17 
18 import android.content.ContentResolver
19 import android.content.Context
20 import android.os.Looper
21 import android.provider.Settings
22 import androidx.test.core.app.ApplicationProvider
23 import com.android.server.bluetooth.Log
24 import com.android.server.bluetooth.initializeRadioModeListener
25 import com.google.common.truth.Truth.assertThat
26 import org.junit.Before
27 import org.junit.Rule
28 import org.junit.Test
29 import org.junit.rules.TestName
30 import org.junit.runner.RunWith
31 import org.robolectric.RobolectricTestRunner
32 import org.robolectric.Shadows.shadowOf
33 
34 private const val RADIO = "my_awesome_radio"
35 private const val MODE_KEY = "is_awesome_radio_enabled"
36 private const val RADIO_BLUETOOTH = Settings.Global.RADIO_BLUETOOTH
37 
enableSensitivenull38 internal fun enableSensitive(resolver: ContentResolver, looper: Looper, radio: String) {
39     Settings.Global.putString(resolver, radio, "foo," + RADIO_BLUETOOTH + ",bar")
40     shadowOf(looper).idle()
41 }
42 
disableSensitivenull43 internal fun disableSensitive(resolver: ContentResolver, looper: Looper, radio: String) {
44     Settings.Global.putString(resolver, radio, "foo,bar")
45     shadowOf(looper).idle()
46 }
47 
disableModenull48 internal fun disableMode(resolver: ContentResolver, looper: Looper, modeKey: String) {
49     Settings.Global.putInt(resolver, modeKey, 0)
50     shadowOf(looper).idle()
51 }
52 
enableModenull53 internal fun enableMode(resolver: ContentResolver, looper: Looper, modeKey: String) {
54     Settings.Global.putInt(resolver, modeKey, 1)
55     shadowOf(looper).idle()
56 }
57 
58 @RunWith(RobolectricTestRunner::class)
59 class RadioModeListenerTest {
60     private val resolver: ContentResolver =
61         ApplicationProvider.getApplicationContext<Context>().getContentResolver()
62     @JvmField @Rule val testName = TestName()
63 
64     private val looper: Looper = Looper.getMainLooper()
65 
66     private lateinit var mode: ArrayList<Boolean>
67 
68     @Before
setupnull69     public fun setup() {
70         Log.i("RadioModeListenerTest", "\t--> setup of " + testName.getMethodName())
71         mode = ArrayList()
72     }
73 
startListenernull74     private fun startListener(): Boolean {
75         return initializeRadioModeListener(looper, resolver, RADIO, MODE_KEY, this::callback)
76     }
77 
enableSensitivenull78     private fun enableSensitive() {
79         enableSensitive(resolver, looper, RADIO)
80     }
81 
disableSensitivenull82     private fun disableSensitive() {
83         disableSensitive(resolver, looper, RADIO)
84     }
85 
disableModenull86     private fun disableMode() {
87         disableMode(resolver, looper, MODE_KEY)
88     }
89 
enableModenull90     private fun enableMode() {
91         enableMode(resolver, looper, MODE_KEY)
92     }
93 
callbacknull94     private fun callback(newMode: Boolean) = mode.add(newMode)
95 
96     @Test
97     fun initialize_whenNullSensitive_isOff() {
98         Settings.Global.putString(resolver, RADIO, null)
99         enableMode()
100 
101         val initialValue = startListener()
102 
103         assertThat(initialValue).isFalse()
104         assertThat(mode).isEmpty()
105     }
106 
107     @Test
initialize_whenNotSensitive_isOffnull108     fun initialize_whenNotSensitive_isOff() {
109         disableSensitive()
110         enableMode()
111 
112         val initialValue = startListener()
113 
114         assertThat(initialValue).isFalse()
115         assertThat(mode).isEmpty()
116     }
117 
118     @Test
enable_whenNotSensitive_isOffnull119     fun enable_whenNotSensitive_isOff() {
120         disableSensitive()
121         disableMode()
122 
123         val initialValue = startListener()
124 
125         enableMode()
126 
127         assertThat(initialValue).isFalse()
128         assertThat(mode).containsExactly(false)
129     }
130 
131     @Test
initialize_whenSensitive_isOffnull132     fun initialize_whenSensitive_isOff() {
133         enableSensitive()
134         disableMode()
135 
136         val initialValue = startListener()
137 
138         assertThat(initialValue).isFalse()
139         assertThat(mode).isEmpty()
140     }
141 
142     @Test
initialize_whenSensitive_isOnnull143     fun initialize_whenSensitive_isOn() {
144         enableSensitive()
145         enableMode()
146 
147         val initialValue = startListener()
148 
149         assertThat(initialValue).isTrue()
150         assertThat(mode).isEmpty()
151     }
152 
153     @Test
toggleSensitive_whenEnabled_isOnOffOnnull154     fun toggleSensitive_whenEnabled_isOnOffOn() {
155         enableSensitive()
156         enableMode()
157 
158         val initialValue = startListener()
159 
160         disableSensitive()
161         enableSensitive()
162 
163         assertThat(initialValue).isTrue()
164         assertThat(mode).containsExactly(false, true)
165     }
166 
167     @Test
toggleEnable_whenSensitive_isOffOnOffnull168     fun toggleEnable_whenSensitive_isOffOnOff() {
169         enableSensitive()
170         disableMode()
171 
172         val initialValue = startListener()
173 
174         enableMode()
175         disableMode()
176 
177         assertThat(initialValue).isFalse()
178         assertThat(mode).containsExactly(true, false)
179     }
180 
181     @Test
disable_whenDisabled_isDiscardednull182     fun disable_whenDisabled_isDiscarded() {
183         enableSensitive()
184         disableMode()
185 
186         val initialValue = startListener()
187 
188         disableMode()
189         disableMode()
190 
191         assertThat(initialValue).isFalse()
192         assertThat(mode).isEmpty()
193     }
194 
195     @Test
enabled_whenEnabled_isDiscardednull196     fun enabled_whenEnabled_isDiscarded() {
197         enableSensitive()
198         enableMode()
199 
200         val initialValue = startListener()
201 
202         enableMode()
203         enableMode()
204 
205         assertThat(initialValue).isTrue()
206         assertThat(mode).isEmpty()
207     }
208 
209     @Test
changeContent_whenDisabled_noDiscardnull210     fun changeContent_whenDisabled_noDiscard() {
211         enableSensitive()
212         disableMode()
213 
214         val initialValue = startListener()
215 
216         disableSensitive() // The value is changed but the result is still false
217         enableMode() // The value is changed but the result is still false
218 
219         assertThat(initialValue).isFalse()
220         assertThat(mode).containsExactly(false, false)
221     }
222 }
223