1 /* 2 * Copyright (C) 2017 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 static com.google.common.truth.Truth.assertThat; 20 21 import static org.mockito.Mockito.doReturn; 22 import static org.mockito.Mockito.when; 23 24 import android.content.ContentResolver; 25 import android.content.Context; 26 import android.provider.Settings.System; 27 import android.telephony.TelephonyManager; 28 29 import androidx.fragment.app.FragmentActivity; 30 import androidx.preference.PreferenceScreen; 31 import androidx.preference.SwitchPreference; 32 33 import org.junit.Before; 34 import org.junit.Test; 35 import org.junit.runner.RunWith; 36 import org.mockito.Mock; 37 import org.mockito.MockitoAnnotations; 38 import org.robolectric.RobolectricTestRunner; 39 import org.robolectric.RuntimeEnvironment; 40 import org.robolectric.annotation.Config; 41 42 // LINT.IfChange 43 @RunWith(RobolectricTestRunner.class) 44 @Config(shadows = { 45 com.android.settings.testutils.shadow.ShadowFragment.class, 46 }) 47 public class DialPadTonePreferenceControllerTest { 48 49 @Mock 50 private TelephonyManager mTelephonyManager; 51 @Mock 52 private PreferenceScreen mScreen; 53 @Mock 54 private FragmentActivity mActivity; 55 @Mock 56 private ContentResolver mContentResolver; 57 @Mock 58 private SoundSettings mSetting; 59 @Mock 60 private Context mContext; 61 62 private DialPadTonePreferenceController mController; 63 private SwitchPreference mPreference; 64 65 @Before setUp()66 public void setUp() { 67 MockitoAnnotations.initMocks(this); 68 when(mContext.getSystemService(Context.TELEPHONY_SERVICE)).thenReturn(mTelephonyManager); 69 when(mTelephonyManager.isVoiceCapable()).thenReturn(true); 70 when(mSetting.getActivity()).thenReturn(mActivity); 71 when(mActivity.getSystemService(Context.TELEPHONY_SERVICE)).thenReturn(mTelephonyManager); 72 when(mActivity.getContentResolver()).thenReturn(mContentResolver); 73 mPreference = new SwitchPreference(RuntimeEnvironment.application); 74 mController = new DialPadTonePreferenceController(mContext, mSetting, null); 75 when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference); 76 doReturn(mScreen).when(mSetting).getPreferenceScreen(); 77 } 78 79 @Test isAvailable_voiceCapable_shouldReturnTrue()80 public void isAvailable_voiceCapable_shouldReturnTrue() { 81 assertThat(mController.isAvailable()).isTrue(); 82 } 83 84 @Test isAvailable_notVoiceCapable_shouldReturnFalse()85 public void isAvailable_notVoiceCapable_shouldReturnFalse() { 86 when(mTelephonyManager.isVoiceCapable()).thenReturn(false); 87 88 assertThat(mController.isAvailable()).isFalse(); 89 } 90 91 @Test displayPreference_dialToneEnabled_shouldCheckedPreference()92 public void displayPreference_dialToneEnabled_shouldCheckedPreference() { 93 System.putInt(mContentResolver, System.DTMF_TONE_WHEN_DIALING, 1); 94 95 mController.displayPreference(mScreen); 96 97 assertThat(mPreference.isChecked()).isTrue(); 98 } 99 100 @Test displayPreference_dialToneDisabled_shouldUncheckedPreference()101 public void displayPreference_dialToneDisabled_shouldUncheckedPreference() { 102 System.putInt(mContentResolver, System.DTMF_TONE_WHEN_DIALING, 0); 103 104 mController.displayPreference(mScreen); 105 106 assertThat(mPreference.isChecked()).isFalse(); 107 } 108 109 @Test onPreferenceChanged_preferenceChecked_shouldEnabledDialTone()110 public void onPreferenceChanged_preferenceChecked_shouldEnabledDialTone() { 111 mController.displayPreference(mScreen); 112 113 mPreference.getOnPreferenceChangeListener().onPreferenceChange(mPreference, true); 114 115 assertThat(System.getInt(mContentResolver, System.DTMF_TONE_WHEN_DIALING, 1)).isEqualTo(1); 116 } 117 118 @Test onPreferenceChanged_preferenceUnchecked_shouldDisabledDialTone()119 public void onPreferenceChanged_preferenceUnchecked_shouldDisabledDialTone() { 120 mController.displayPreference(mScreen); 121 122 mPreference.getOnPreferenceChangeListener().onPreferenceChange(mPreference, false); 123 124 assertThat(System.getInt(mContentResolver, System.DTMF_TONE_WHEN_DIALING, 1)).isEqualTo(0); 125 } 126 } 127 // LINT.ThenChange(DialPadTonePreferenceTest.kt) 128