Tyler Gunn | 5d66e1d | 2018-01-25 20:22:49 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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.server.telecom.tests; |
| 18 | |
| 19 | import static com.android.server.telecom.tests.TelecomSystemTest.TEST_TIMEOUT; |
| 20 | |
| 21 | import static junit.framework.Assert.assertFalse; |
| 22 | import static junit.framework.Assert.assertTrue; |
| 23 | |
| 24 | import static org.mockito.ArgumentMatchers.any; |
| 25 | import static org.mockito.Mockito.verify; |
| 26 | import static org.mockito.Mockito.when; |
| 27 | |
| 28 | import android.content.ComponentName; |
| 29 | import android.media.AudioFormat; |
| 30 | import android.media.AudioManager; |
| 31 | import android.media.AudioRecordingConfiguration; |
| 32 | import android.media.MediaRecorder; |
| 33 | import android.os.Handler; |
| 34 | import android.os.Looper; |
| 35 | import android.telecom.PhoneAccountHandle; |
| 36 | import android.test.suitebuilder.annotation.MediumTest; |
| 37 | |
| 38 | import com.android.server.telecom.Call; |
| 39 | import com.android.server.telecom.CallRecordingTonePlayer; |
| 40 | import com.android.server.telecom.TelecomSystem; |
| 41 | |
| 42 | import org.junit.Before; |
| 43 | import org.junit.Test; |
| 44 | import org.junit.runner.RunWith; |
| 45 | import org.junit.runners.JUnit4; |
| 46 | import org.mockito.ArgumentCaptor; |
| 47 | import org.mockito.Mock; |
| 48 | import org.mockito.Mockito; |
| 49 | import org.mockito.MockitoAnnotations; |
| 50 | |
| 51 | import java.util.ArrayList; |
| 52 | import java.util.List; |
| 53 | |
| 54 | /** |
| 55 | * Unit tests for the {@link com.android.server.telecom.CallRecordingTonePlayer} class. |
| 56 | */ |
| 57 | @RunWith(JUnit4.class) |
| 58 | public class CallRecordingTonePlayerTest extends TelecomTestCase { |
| 59 | |
| 60 | private static final String PHONE_ACCOUNT_PACKAGE = "com.android.telecom.test"; |
| 61 | private static final String PHONE_ACCOUNT_CLASS = "MyFancyConnectionService"; |
| 62 | private static final String PHONE_ACCOUNT_ID = "1"; |
| 63 | private static final String RECORDING_APP_PACKAGE = "com.recording.app"; |
| 64 | |
| 65 | private static final PhoneAccountHandle TEST_PHONE_ACCOUNT = new PhoneAccountHandle( |
| 66 | new ComponentName(PHONE_ACCOUNT_PACKAGE, PHONE_ACCOUNT_CLASS), PHONE_ACCOUNT_ID); |
| 67 | |
| 68 | private CallRecordingTonePlayer mCallRecordingTonePlayer; |
| 69 | private TelecomSystem.SyncRoot mSyncRoot = new TelecomSystem.SyncRoot() { }; |
| 70 | @Mock private AudioManager mAudioManager; |
| 71 | |
| 72 | @Override |
| 73 | @Before |
| 74 | public void setUp() throws Exception { |
| 75 | super.setUp(); |
| 76 | MockitoAnnotations.initMocks(this); |
| 77 | mCallRecordingTonePlayer = new CallRecordingTonePlayer( |
| 78 | mComponentContextFixture.getTestDouble().getApplicationContext(), |
| 79 | mAudioManager, mSyncRoot); |
| 80 | when(mAudioManager.getActiveRecordingConfigurations()).thenReturn(null); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Ensures that child calls are not tracked. |
| 85 | */ |
| 86 | @MediumTest |
| 87 | @Test |
| 88 | public void testChildCall() { |
| 89 | Call childCall = Mockito.mock(Call.class); |
| 90 | Call parentcall = Mockito.mock(Call.class); |
| 91 | when(childCall.getParentCall()).thenReturn(parentcall); |
| 92 | mCallRecordingTonePlayer.onCallAdded(childCall); |
| 93 | |
| 94 | assertFalse(mCallRecordingTonePlayer.hasCalls()); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Ensures that external calls are not tracked. |
| 99 | */ |
| 100 | @MediumTest |
| 101 | @Test |
| 102 | public void testAddExternalCall() { |
| 103 | Call call = Mockito.mock(Call.class); |
| 104 | when(call.getParentCall()).thenReturn(null); |
| 105 | when(call.isExternalCall()).thenReturn(true); |
| 106 | mCallRecordingTonePlayer.onCallAdded(call); |
| 107 | |
| 108 | assertFalse(mCallRecordingTonePlayer.hasCalls()); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Ensures that emergency calls are not tracked. |
| 113 | */ |
| 114 | @MediumTest |
| 115 | @Test |
| 116 | public void testAddEmergencyCall() { |
| 117 | Call call = Mockito.mock(Call.class); |
| 118 | when(call.getParentCall()).thenReturn(null); |
| 119 | when(call.isExternalCall()).thenReturn(false); |
| 120 | when(call.isEmergencyCall()).thenReturn(true); |
| 121 | mCallRecordingTonePlayer.onCallAdded(call); |
| 122 | |
| 123 | assertFalse(mCallRecordingTonePlayer.hasCalls()); |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Ensures that calls which don't use the recording tone are not tracked. |
| 128 | */ |
| 129 | @MediumTest |
| 130 | @Test |
| 131 | public void testAddIneligibleCall() { |
| 132 | Call call = Mockito.mock(Call.class); |
| 133 | when(call.getParentCall()).thenReturn(null); |
| 134 | when(call.isExternalCall()).thenReturn(false); |
| 135 | when(call.isEmergencyCall()).thenReturn(false); |
| 136 | when(call.isUsingCallRecordingTone()).thenReturn(false); |
| 137 | mCallRecordingTonePlayer.onCallAdded(call); |
| 138 | |
| 139 | assertFalse(mCallRecordingTonePlayer.hasCalls()); |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Ensures that an eligible call is tracked. |
| 144 | */ |
| 145 | @MediumTest |
| 146 | @Test |
| 147 | public void testAddEligibleCall() { |
| 148 | Call call = addValidCall(); |
| 149 | |
| 150 | mCallRecordingTonePlayer.onCallRemoved(call); |
| 151 | assertFalse(mCallRecordingTonePlayer.hasCalls()); |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Verifies registration and unregistration of the recording callback. |
| 156 | */ |
| 157 | @MediumTest |
| 158 | @Test |
| 159 | public void testRecordingCallbackRegistered() { |
| 160 | Call call = addValidCall(); |
| 161 | |
| 162 | // Ensure we got a request for the first set of recordings. |
| 163 | verify(mAudioManager).getActiveRecordingConfigurations(); |
| 164 | |
| 165 | // Ensure that we registered an audio recording callback. |
| 166 | verify(mAudioManager).registerAudioRecordingCallback( |
| 167 | any(AudioManager.AudioRecordingCallback.class), any()); |
| 168 | |
| 169 | mCallRecordingTonePlayer.onCallRemoved(call); |
| 170 | |
| 171 | // Ensure we unregistered the audio recording callback after the last call was removed. |
| 172 | verify(mAudioManager).unregisterAudioRecordingCallback( |
| 173 | any(AudioManager.AudioRecordingCallback.class)); |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Verify that we are in a recording state when we add a call and there is a recording taking |
| 178 | * place prior to the call starting. |
| 179 | */ |
| 180 | @MediumTest |
| 181 | @Test |
| 182 | public void testIsRecordingInitial() { |
| 183 | // Return an active recording configuration when we add the first call. |
| 184 | when(mAudioManager.getActiveRecordingConfigurations()).thenReturn( |
| 185 | getAudioRecordingConfig(RECORDING_APP_PACKAGE)); |
| 186 | |
| 187 | addValidCall(); |
| 188 | |
| 189 | // Ensure we got a request for the first set of recordings. |
| 190 | verify(mAudioManager).getActiveRecordingConfigurations(); |
| 191 | |
| 192 | assertTrue(mCallRecordingTonePlayer.isRecording()); |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * Verify that we are in a recording state when we add a call and a recording start after the |
| 197 | * call starts. |
| 198 | */ |
| 199 | @MediumTest |
| 200 | @Test |
| 201 | public void testIsRecordingLater() { |
| 202 | // Return no active recording configuration when we add the first call. |
| 203 | when(mAudioManager.getActiveRecordingConfigurations()).thenReturn( null); |
| 204 | |
| 205 | addValidCall(); |
| 206 | |
| 207 | // Capture the registered callback so we can pass back test data via it. |
| 208 | ArgumentCaptor<AudioManager.AudioRecordingCallback> callbackCaptor = |
| 209 | ArgumentCaptor.forClass(AudioManager.AudioRecordingCallback.class); |
| 210 | verify(mAudioManager).registerAudioRecordingCallback(callbackCaptor.capture(), any()); |
| 211 | |
| 212 | // Pass back some test configuration data. |
| 213 | callbackCaptor.getValue().onRecordingConfigChanged(getAudioRecordingConfig( |
| 214 | RECORDING_APP_PACKAGE)); |
| 215 | waitForHandlerAction(new Handler(Looper.getMainLooper()), TEST_TIMEOUT); |
| 216 | |
| 217 | assertTrue(mCallRecordingTonePlayer.isRecording()); |
| 218 | } |
| 219 | |
| 220 | /** |
| 221 | * Verifies that we are not in a recording state if the PhoneAccount associated with the call is |
| 222 | * the recording app. |
| 223 | */ |
| 224 | @MediumTest |
| 225 | @Test |
| 226 | public void testNotRecordingApp() { |
| 227 | // Return no active recording configuration when we add the first call. |
| 228 | when(mAudioManager.getActiveRecordingConfigurations()).thenReturn( null); |
| 229 | |
| 230 | addValidCall(); |
| 231 | |
| 232 | // Capture the registered callback so we can pass back test data via it. |
| 233 | ArgumentCaptor<AudioManager.AudioRecordingCallback> callbackCaptor = |
| 234 | ArgumentCaptor.forClass(AudioManager.AudioRecordingCallback.class); |
| 235 | verify(mAudioManager).registerAudioRecordingCallback(callbackCaptor.capture(), any()); |
| 236 | |
| 237 | // Report that the recording app is the call's phone account. |
| 238 | callbackCaptor.getValue().onRecordingConfigChanged(getAudioRecordingConfig( |
| 239 | PHONE_ACCOUNT_PACKAGE)); |
| 240 | waitForHandlerAction(new Handler(Looper.getMainLooper()), TEST_TIMEOUT); |
| 241 | |
| 242 | // Since the app which is recording is the phone account of the call, we should not be in |
| 243 | // a recording state. |
| 244 | assertFalse(mCallRecordingTonePlayer.isRecording()); |
| 245 | } |
| 246 | |
| 247 | /** |
| 248 | * @return Test audio recording configuration. |
| 249 | */ |
| 250 | private List<AudioRecordingConfiguration> getAudioRecordingConfig(String packageName) { |
| 251 | List<AudioRecordingConfiguration> configs = new ArrayList<>(); |
| 252 | configs.add(new AudioRecordingConfiguration(0, 0, MediaRecorder.AudioSource.MIC, |
| 253 | new AudioFormat.Builder().build(), new AudioFormat.Builder().build(), |
| 254 | 0, packageName)); |
| 255 | return configs; |
| 256 | } |
| 257 | |
| 258 | private Call addValidCall() { |
| 259 | Call call = Mockito.mock(Call.class); |
| 260 | when(call.getParentCall()).thenReturn(null); |
| 261 | when(call.isExternalCall()).thenReturn(false); |
| 262 | when(call.isEmergencyCall()).thenReturn(false); |
| 263 | when(call.isUsingCallRecordingTone()).thenReturn(true); |
| 264 | when(call.getConnectionManagerPhoneAccount()).thenReturn(null); |
| 265 | when(call.getTargetPhoneAccount()).thenReturn(TEST_PHONE_ACCOUNT); |
| 266 | mCallRecordingTonePlayer.onCallAdded(call); |
| 267 | assertTrue(mCallRecordingTonePlayer.hasCalls()); |
| 268 | return call; |
| 269 | } |
| 270 | |
| 271 | |
| 272 | } |