1 /*
2  * Copyright (C) 2022 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.airplane.data.repository
18 
19 import android.net.ConnectivityManager
20 import android.provider.Settings.Global
21 import android.telephony.SubscriptionManager
22 import android.telephony.TelephonyManager
23 import androidx.test.ext.junit.runners.AndroidJUnit4
24 import androidx.test.filters.SmallTest
25 import com.android.systemui.SysuiTestCase
26 import com.android.systemui.log.table.TableLogBuffer
27 import com.android.systemui.util.mockito.eq
28 import com.android.systemui.util.mockito.whenever
29 import com.android.systemui.util.settings.FakeGlobalSettings
30 import com.google.common.truth.Truth.assertThat
31 import kotlinx.coroutines.ExperimentalCoroutinesApi
32 import kotlinx.coroutines.flow.launchIn
33 import kotlinx.coroutines.test.StandardTestDispatcher
34 import kotlinx.coroutines.test.TestScope
35 import kotlinx.coroutines.test.runCurrent
36 import kotlinx.coroutines.test.runTest
37 import org.junit.Before
38 import org.junit.Test
39 import org.junit.runner.RunWith
40 import org.mockito.Mock
41 import org.mockito.Mockito.verify
42 import org.mockito.MockitoAnnotations
43 
44 @OptIn(ExperimentalCoroutinesApi::class)
45 @SmallTest
46 @RunWith(AndroidJUnit4::class)
47 class AirplaneModeRepositoryImplTest : SysuiTestCase() {
48 
49     private lateinit var underTest: AirplaneModeRepository
50 
51     @Mock private lateinit var logger: TableLogBuffer
52     @Mock private lateinit var telephonyManager: TelephonyManager
53     @Mock private lateinit var subscriptionManager: SubscriptionManager
54     @Mock private lateinit var connectivityManager: ConnectivityManager
55 
56     private val testContext = StandardTestDispatcher()
57     private val scope = TestScope(testContext)
58 
59     private lateinit var settings: FakeGlobalSettings
60 
61     @Before
setUpnull62     fun setUp() {
63         MockitoAnnotations.initMocks(this)
64 
65         settings = FakeGlobalSettings(testContext)
66 
67         whenever(telephonyManager.emergencyCallbackMode).thenReturn(false)
68         whenever(subscriptionManager.activeSubscriptionIdList).thenReturn(intArrayOf())
69 
70         underTest =
71             AirplaneModeRepositoryImpl(
72                 connectivityManager,
73                 null,
74                 scope.backgroundScope.coroutineContext,
75                 settings,
76                 logger,
77                 scope.backgroundScope,
78             )
79     }
80 
81     @Test
isAirplaneMode_initiallyGetsSettingsValuenull82     fun isAirplaneMode_initiallyGetsSettingsValue() =
83         scope.runTest {
84             settings.putInt(Global.AIRPLANE_MODE_ON, 1)
85 
86             underTest =
87                 AirplaneModeRepositoryImpl(
88                     connectivityManager,
89                     null,
90                     scope.backgroundScope.coroutineContext,
91                     settings,
92                     logger,
93                     scope.backgroundScope,
94                 )
95 
96             underTest.isAirplaneMode.launchIn(backgroundScope)
97             runCurrent()
98             assertThat(underTest.isAirplaneMode.value).isTrue()
99         }
100 
101     @Test
isAirplaneMode_settingUpdated_valueUpdatednull102     fun isAirplaneMode_settingUpdated_valueUpdated() =
103         scope.runTest {
104             underTest.isAirplaneMode.launchIn(backgroundScope)
105 
106             settings.putInt(Global.AIRPLANE_MODE_ON, 0)
107             runCurrent()
108             assertThat(underTest.isAirplaneMode.value).isFalse()
109 
110             settings.putInt(Global.AIRPLANE_MODE_ON, 1)
111             runCurrent()
112             assertThat(underTest.isAirplaneMode.value).isTrue()
113 
114             settings.putInt(Global.AIRPLANE_MODE_ON, 0)
115             runCurrent()
116             assertThat(underTest.isAirplaneMode.value).isFalse()
117         }
118 
119     @Test
setIsAirplaneModenull120     fun setIsAirplaneMode() =
121         scope.runTest {
122             underTest.setIsAirplaneMode(true)
123             runCurrent()
124 
125             verify(connectivityManager).setAirplaneMode(eq(true))
126         }
127 }
128