1 /*
<lambda>null2  * Copyright (C) 2024 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.settingslib.satellite
18 
19 import android.content.Context
20 import android.platform.test.annotations.RequiresFlagsEnabled
21 import android.telephony.satellite.SatelliteManager
22 import android.telephony.satellite.SatelliteManager.SATELLITE_MODEM_STATE_ENABLING_SATELLITE
23 import android.telephony.satellite.SatelliteManager.SATELLITE_MODEM_STATE_OFF
24 import android.telephony.satellite.SatelliteManager.SATELLITE_RESULT_MODEM_ERROR
25 import android.telephony.satellite.SatelliteModemStateCallback
26 import android.util.AndroidRuntimeException
27 import androidx.test.core.app.ApplicationProvider
28 import com.android.internal.telephony.flags.Flags
29 import com.android.settingslib.satellite.SatelliteDialogUtils.TYPE_IS_WIFI
30 import kotlinx.coroutines.CoroutineScope
31 import kotlinx.coroutines.Dispatchers
32 import kotlinx.coroutines.runBlocking
33 import org.junit.Assert.assertFalse
34 import org.junit.Assert.assertTrue
35 import org.junit.Before
36 import org.junit.Rule
37 import org.junit.Test
38 import org.junit.runner.RunWith
39 import org.mockito.Mock
40 import org.mockito.Mockito.any
41 import org.mockito.Mockito.`when`
42 import org.mockito.Spy
43 import org.mockito.junit.MockitoJUnit
44 import org.mockito.junit.MockitoRule
45 import org.mockito.Mockito.verify
46 import org.mockito.internal.verification.Times
47 import org.robolectric.RobolectricTestRunner
48 
49 @RunWith(RobolectricTestRunner::class)
50 class SatelliteDialogUtilsTest {
51     @JvmField
52     @Rule
53     val mockitoRule: MockitoRule = MockitoJUnit.rule()
54 
55     @Spy
56     var context: Context = ApplicationProvider.getApplicationContext()
57     @Mock
58     private lateinit var satelliteManager: SatelliteManager
59 
60     private val coroutineScope = CoroutineScope(Dispatchers.Main)
61 
62     @Before
63     fun setUp() {
64         `when`(context.getSystemService(SatelliteManager::class.java))
65                 .thenReturn(satelliteManager)
66     }
67 
68     @Test
69     @RequiresFlagsEnabled(Flags.FLAG_OEM_ENABLED_SATELLITE_FLAG)
70     fun mayStartSatelliteWarningDialog_satelliteIsOn_showWarningDialog() = runBlocking {
71         `when`(satelliteManager.registerForModemStateChanged(any(), any()))
72                 .thenAnswer { invocation ->
73                     val callback = invocation
74                             .getArgument<SatelliteModemStateCallback>(1)
75                     callback.onSatelliteModemStateChanged(SATELLITE_MODEM_STATE_ENABLING_SATELLITE)
76                     null
77                 }
78 
79         try {
80             SatelliteDialogUtils.mayStartSatelliteWarningDialog(
81                     context, coroutineScope, TYPE_IS_WIFI, allowClick = {
82                 assertTrue(it)
83             })
84         } catch (e: AndroidRuntimeException) {
85             // Catch exception of starting activity .
86         }
87     }
88 
89     @Test
90     @RequiresFlagsEnabled(Flags.FLAG_OEM_ENABLED_SATELLITE_FLAG)
91     fun mayStartSatelliteWarningDialog_satelliteIsOff_notShowWarningDialog() = runBlocking {
92         `when`(satelliteManager.registerForModemStateChanged(any(), any()))
93                 .thenAnswer { invocation ->
94                     val callback = invocation
95                             .getArgument<SatelliteModemStateCallback>(1)
96                     callback.onSatelliteModemStateChanged(SATELLITE_MODEM_STATE_OFF)
97                     null
98                 }
99 
100 
101         SatelliteDialogUtils.mayStartSatelliteWarningDialog(
102                 context, coroutineScope, TYPE_IS_WIFI, allowClick = {
103             assertFalse(it)
104         })
105 
106         verify(context, Times(0)).startActivity(any())
107     }
108 
109     @Test
110     @RequiresFlagsEnabled(Flags.FLAG_OEM_ENABLED_SATELLITE_FLAG)
111     fun mayStartSatelliteWarningDialog_noSatelliteManager_notShowWarningDialog() = runBlocking {
112         `when`(context.getSystemService(SatelliteManager::class.java)).thenReturn(null)
113 
114         SatelliteDialogUtils.mayStartSatelliteWarningDialog(
115                 context, coroutineScope, TYPE_IS_WIFI, allowClick = {
116             assertFalse(it)
117         })
118 
119         verify(context, Times(0)).startActivity(any())
120     }
121 
122     @Test
123     @RequiresFlagsEnabled(Flags.FLAG_OEM_ENABLED_SATELLITE_FLAG)
124     fun mayStartSatelliteWarningDialog_satelliteErrorResult_notShowWarningDialog() = runBlocking {
125         `when`(satelliteManager.registerForModemStateChanged(any(), any()))
126                 .thenReturn(SATELLITE_RESULT_MODEM_ERROR)
127 
128         SatelliteDialogUtils.mayStartSatelliteWarningDialog(
129                 context, coroutineScope, TYPE_IS_WIFI, allowClick = {
130             assertFalse(it)
131         })
132 
133         verify(context, Times(0)).startActivity(any())
134     }
135 
136     @Test
137     @RequiresFlagsEnabled(Flags.FLAG_OEM_ENABLED_SATELLITE_FLAG)
138     fun mayStartSatelliteWarningDialog_phoneCrash_notShowWarningDialog() = runBlocking {
139         `when`(satelliteManager.registerForModemStateChanged(any(), any()))
140                 .thenThrow(IllegalStateException("Telephony is null!!!"))
141 
142         SatelliteDialogUtils.mayStartSatelliteWarningDialog(
143                 context, coroutineScope, TYPE_IS_WIFI, allowClick = {
144             assertFalse(it)
145         })
146 
147         verify(context, Times(0)).startActivity(any())
148     }
149 }
150