• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * 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.settings.notification
18 
19 import android.content.Context
20 import android.content.ContextWrapper
21 import android.content.res.Resources
22 import android.media.AudioManager
23 import android.media.AudioManager.STREAM_BLUETOOTH_SCO
24 import android.media.AudioManager.STREAM_VOICE_CALL
25 import androidx.test.core.app.ApplicationProvider
26 import androidx.test.ext.junit.runners.AndroidJUnit4
27 import com.google.common.truth.Truth.assertThat
28 import org.junit.Test
29 import org.junit.runner.RunWith
30 import org.mockito.ArgumentMatchers.anyInt
31 import org.mockito.kotlin.doReturn
32 import org.mockito.kotlin.mock
33 import org.mockito.kotlin.spy
34 import org.mockito.kotlin.stub
35 
36 // LINT.IfChange
37 @RunWith(AndroidJUnit4::class)
38 class CallVolumePreferenceTest {
39     private var audioHelper = mock<AudioHelper>()
40     private var mockResources = mock<Resources>()
41 
42     private var audioManager: AudioManager? = null
43 
44     private var callVolumePreference = CallVolumePreference()
45     private val context = object : ContextWrapper(ApplicationProvider.getApplicationContext()) {
getSystemServicenull46         override fun getSystemService(name: String): Any? =
47             when (name) {
48                 Context.AUDIO_SERVICE -> audioManager
49                 else -> super.getSystemService(name)
50             }
51 
getResourcesnull52         override fun getResources(): Resources = mockResources
53     }
54 
55     @Test
56     fun isAvailable_configTrueAndNoSingleVolume_shouldReturnTrue() {
57         mockResources.stub { on { getBoolean(anyInt()) } doReturn true }
58         audioHelper = mock { on { isSingleVolume } doReturn false }
59         callVolumePreference = spy(callVolumePreference).stub {
60             onGeneric { createAudioHelper(context) } doReturn audioHelper
61         }
62 
63         assertThat(callVolumePreference.isAvailable(context)).isTrue()
64     }
65 
66     @Test
isAvailable_configTrueAndSingleVolume_shouldReturnFalsenull67     fun isAvailable_configTrueAndSingleVolume_shouldReturnFalse() {
68         mockResources.stub { on { getBoolean(anyInt()) } doReturn true }
69         audioHelper = mock { on { isSingleVolume } doReturn true }
70         callVolumePreference = spy(callVolumePreference).stub {
71             onGeneric { createAudioHelper(context) } doReturn audioHelper
72         }
73 
74         assertThat(callVolumePreference.isAvailable(context)).isFalse()
75     }
76 
77     @Test
isAvailable_configFalse_shouldReturnFalsenull78     fun isAvailable_configFalse_shouldReturnFalse() {
79         mockResources.stub { on { getBoolean(anyInt()) } doReturn false }
80 
81         assertThat(callVolumePreference.isAvailable(context)).isFalse()
82     }
83 
84     @Test
85     @Suppress("DEPRECATION")
getAudioStream_onBluetoothScoOn_shouldEqualToStreamBluetoothSconull86     fun getAudioStream_onBluetoothScoOn_shouldEqualToStreamBluetoothSco() {
87         audioManager = mock { on { isBluetoothScoOn } doReturn true }
88 
89         assertThat(callVolumePreference.getAudioStream(context)).isEqualTo(STREAM_BLUETOOTH_SCO)
90     }
91 
92     @Test
93     @Suppress("DEPRECATION")
getAudioStream_onBluetoothScoOff_shouldEqualToStreamVoiceCallnull94     fun getAudioStream_onBluetoothScoOff_shouldEqualToStreamVoiceCall() {
95         audioManager = mock { on { isBluetoothScoOn } doReturn false }
96 
97         assertThat(callVolumePreference.getAudioStream(context)).isEqualTo(STREAM_VOICE_CALL)
98     }
99 }
100 // LINT.ThenChange(CallVolumePreferenceControllerTest.java)
101