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