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 
18 package com.android.systemui.telephony.data.repository
19 
20 import android.telecom.TelecomManager
21 import android.telephony.TelephonyCallback
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.coroutines.collectLastValue
27 import com.android.systemui.kosmos.testDispatcher
28 import com.android.systemui.kosmos.testScope
29 import com.android.systemui.telephony.TelephonyListenerManager
30 import com.android.systemui.testKosmos
31 import com.android.systemui.util.mockito.kotlinArgumentCaptor
32 import com.android.systemui.util.mockito.whenever
33 import com.google.common.truth.Truth.assertThat
34 import kotlinx.coroutines.test.runCurrent
35 import kotlinx.coroutines.test.runTest
36 import org.junit.Before
37 import org.junit.Test
38 import org.junit.runner.RunWith
39 import org.mockito.Mock
40 import org.mockito.Mockito.verify
41 import org.mockito.MockitoAnnotations
42 
43 @SmallTest
44 @RunWith(AndroidJUnit4::class)
45 class TelephonyRepositoryImplTest : SysuiTestCase() {
46 
47     @Mock private lateinit var manager: TelephonyListenerManager
48     @Mock private lateinit var telecomManager: TelecomManager
49 
50     private val kosmos = testKosmos()
51     private val testScope = kosmos.testScope
52 
53     private lateinit var underTest: TelephonyRepositoryImpl
54 
55     @Before
setUpnull56     fun setUp() {
57         MockitoAnnotations.initMocks(this)
58         whenever(telecomManager.isInCall).thenReturn(false)
59 
60         underTest =
61             TelephonyRepositoryImpl(
62                 applicationScope = testScope.backgroundScope,
63                 applicationContext = context,
64                 backgroundDispatcher = kosmos.testDispatcher,
65                 manager = manager,
66                 telecomManager = telecomManager,
67             )
68     }
69 
70     @Test
callStatenull71     fun callState() =
72         testScope.runTest {
73             val callState by collectLastValue(underTest.callState)
74             runCurrent()
75 
76             val listenerCaptor = kotlinArgumentCaptor<TelephonyCallback.CallStateListener>()
77             verify(manager).addCallStateListener(listenerCaptor.capture())
78             val listener = listenerCaptor.value
79 
80             listener.onCallStateChanged(0)
81             assertThat(callState).isEqualTo(0)
82 
83             listener.onCallStateChanged(1)
84             assertThat(callState).isEqualTo(1)
85 
86             listener.onCallStateChanged(2)
87             assertThat(callState).isEqualTo(2)
88         }
89 
90     @Test
isInCallnull91     fun isInCall() =
92         testScope.runTest {
93             val isInCall by collectLastValue(underTest.isInCall)
94             runCurrent()
95 
96             val listenerCaptor = kotlinArgumentCaptor<TelephonyCallback.CallStateListener>()
97             verify(manager).addCallStateListener(listenerCaptor.capture())
98             val listener = listenerCaptor.value
99             whenever(telecomManager.isInCall).thenReturn(true)
100             listener.onCallStateChanged(TelephonyManager.CALL_STATE_OFFHOOK)
101 
102             assertThat(isInCall).isTrue()
103 
104             whenever(telecomManager.isInCall).thenReturn(false)
105             listener.onCallStateChanged(TelephonyManager.CALL_STATE_IDLE)
106 
107             assertThat(isInCall).isFalse()
108         }
109 }
110