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 package com.android.settings.notification 17 18 import android.content.Context 19 import android.content.ContextWrapper 20 import android.provider.Settings.System.DTMF_TONE_WHEN_DIALING 21 import android.telephony.TelephonyManager 22 import androidx.preference.SwitchPreferenceCompat 23 import androidx.test.core.app.ApplicationProvider 24 import androidx.test.ext.junit.runners.AndroidJUnit4 25 import com.android.settingslib.datastore.SettingsSystemStore 26 import com.android.settingslib.preference.createAndBindWidget 27 import com.google.common.truth.Truth.assertThat 28 import org.junit.Test 29 import org.junit.runner.RunWith 30 import org.mockito.kotlin.doReturn 31 import org.mockito.kotlin.mock 32 33 // LINT.IfChange 34 @RunWith(AndroidJUnit4::class) 35 class DialPadTonePreferenceTest { 36 private var telephonyManager: TelephonyManager? = null 37 38 private val context = 39 object : ContextWrapper(ApplicationProvider.getApplicationContext()) { getSystemServicenull40 override fun getSystemService(name: String): Any? = 41 when (name) { 42 Context.TELEPHONY_SERVICE -> telephonyManager 43 else -> super.getSystemService(name) 44 } 45 } 46 47 private val dialPadTonePreference = DialPadTonePreference() 48 49 @Test isAvailable_voiceCapable_shouldReturnTruenull50 fun isAvailable_voiceCapable_shouldReturnTrue() { 51 telephonyManager = mock { on { isVoiceCapable } doReturn true } 52 53 assertThat(dialPadTonePreference.isAvailable(context)).isTrue() 54 } 55 56 @Test isAvailable_noVoicCapable_shouldReturnFalsenull57 fun isAvailable_noVoicCapable_shouldReturnFalse() { 58 telephonyManager = mock { on { isVoiceCapable } doReturn false } 59 60 assertThat(dialPadTonePreference.isAvailable(context)).isFalse() 61 } 62 63 @Test performClick_shouldPreferenceChangeToCheckednull64 fun performClick_shouldPreferenceChangeToChecked() { 65 enableDialPadTone(false) 66 67 val preference = getSwitchPreference().apply { performClick() } 68 69 assertThat(preference.isChecked).isTrue() 70 } 71 72 @Test performClick_shouldPreferenceChangeToUncheckednull73 fun performClick_shouldPreferenceChangeToUnchecked() { 74 enableDialPadTone(true) 75 76 val preference = getSwitchPreference().apply { performClick() } 77 78 assertThat(preference.isChecked).isFalse() 79 } 80 81 @Test dialToneEnabled_shouldCheckedPreferencenull82 fun dialToneEnabled_shouldCheckedPreference() { 83 enableDialPadTone(true) 84 85 assertThat(getSwitchPreference().isChecked).isTrue() 86 } 87 88 @Test dialToneDisabled_shouldUncheckedPreferencenull89 fun dialToneDisabled_shouldUncheckedPreference() { 90 enableDialPadTone(false) 91 92 assertThat(getSwitchPreference().isChecked).isFalse() 93 } 94 getSwitchPreferencenull95 private fun getSwitchPreference(): SwitchPreferenceCompat = 96 dialPadTonePreference.createAndBindWidget(context) 97 98 private fun enableDialPadTone(enabled: Boolean) = 99 SettingsSystemStore.get(context).setBoolean(DTMF_TONE_WHEN_DIALING, enabled) 100 } 101 // LINT.ThenChange(DialPadTonePreferenceControllerTest.java) 102