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