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.domain.interactor
18 
19 import androidx.test.ext.junit.runners.AndroidJUnit4
20 import androidx.test.filters.SmallTest
21 import com.android.systemui.SysuiTestCase
22 import com.android.systemui.statusbar.pipeline.airplane.data.repository.FakeAirplaneModeRepository
23 import com.android.systemui.statusbar.pipeline.mobile.data.repository.fakeMobileConnectionsRepository
24 import com.android.systemui.statusbar.pipeline.shared.data.model.ConnectivitySlot
25 import com.android.systemui.statusbar.pipeline.shared.data.repository.FakeConnectivityRepository
26 import com.android.systemui.testKosmos
27 import com.google.common.truth.Truth.assertThat
28 import kotlinx.coroutines.ExperimentalCoroutinesApi
29 import kotlinx.coroutines.flow.launchIn
30 import kotlinx.coroutines.flow.onEach
31 import kotlinx.coroutines.test.runCurrent
32 import kotlinx.coroutines.test.runTest
33 import org.junit.Test
34 import org.junit.runner.RunWith
35 
36 @OptIn(ExperimentalCoroutinesApi::class)
37 @SmallTest
38 @RunWith(AndroidJUnit4::class)
39 class AirplaneModeInteractorTest : SysuiTestCase() {
40     private val kosmos = testKosmos()
41 
42     private val mobileConnectionsRepository = kosmos.fakeMobileConnectionsRepository
43     private val airplaneModeRepository = FakeAirplaneModeRepository()
44     private val connectivityRepository = FakeConnectivityRepository()
45 
46     private val underTest =
47         AirplaneModeInteractor(
48             airplaneModeRepository,
49             connectivityRepository,
50             mobileConnectionsRepository
51         )
52 
53     @Test
<lambda>null54     fun isAirplaneMode_matchesRepo() = runTest {
55         var latest: Boolean? = null
56         underTest.isAirplaneMode.onEach { latest = it }.launchIn(backgroundScope)
57 
58         airplaneModeRepository.setIsAirplaneMode(true)
59         runCurrent()
60         assertThat(latest).isTrue()
61 
62         airplaneModeRepository.setIsAirplaneMode(false)
63         runCurrent()
64         assertThat(latest).isFalse()
65 
66         airplaneModeRepository.setIsAirplaneMode(true)
67         runCurrent()
68         assertThat(latest).isTrue()
69     }
70 
71     @Test
<lambda>null72     fun isForceHidden_repoHasWifiHidden_outputsTrue() = runTest {
73         connectivityRepository.setForceHiddenIcons(setOf(ConnectivitySlot.AIRPLANE))
74 
75         var latest: Boolean? = null
76         underTest.isForceHidden.onEach { latest = it }.launchIn(backgroundScope)
77         runCurrent()
78 
79         assertThat(latest).isTrue()
80     }
81 
82     @Test
<lambda>null83     fun isForceHidden_repoDoesNotHaveWifiHidden_outputsFalse() = runTest {
84         connectivityRepository.setForceHiddenIcons(setOf())
85 
86         var latest: Boolean? = null
87         underTest.isForceHidden.onEach { latest = it }.launchIn(backgroundScope)
88         runCurrent()
89 
90         assertThat(latest).isFalse()
91     }
92 
93     @Test
<lambda>null94     fun testSetAirplaneMode_inEcmMode_Blocked() = runTest {
95         mobileConnectionsRepository.setIsInEcmState(true)
96 
97         assertThat(underTest.setIsAirplaneMode(true))
98             .isEqualTo(AirplaneModeInteractor.SetResult.BLOCKED_BY_ECM)
99         assertThat(airplaneModeRepository.isAirplaneMode.value).isFalse()
100     }
101 
102     @Test
<lambda>null103     fun testSetAirplaneMode_notInEcmMode_Success() = runTest {
104         mobileConnectionsRepository.setIsInEcmState(false)
105 
106         underTest.setIsAirplaneMode(true)
107 
108         assertThat(underTest.setIsAirplaneMode(true))
109             .isEqualTo(AirplaneModeInteractor.SetResult.SUCCESS)
110         assertThat(airplaneModeRepository.isAirplaneMode.value).isTrue()
111     }
112 }
113