1 /* 2 * Copyright (C) 2016 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.provider.Settings; 21 import android.support.v7.preference.Preference; 22 import android.support.v7.preference.PreferenceScreen; 23 import android.support.v7.preference.TwoStatePreference; 24 import android.telephony.TelephonyManager; 25 26 import com.android.settings.SettingsRobolectricTestRunner; 27 import com.android.settings.TestConfig; 28 29 import org.junit.Before; 30 import org.junit.Test; 31 import org.junit.runner.RunWith; 32 import org.mockito.Mock; 33 import org.mockito.MockitoAnnotations; 34 import org.robolectric.annotation.Config; 35 import org.robolectric.shadows.ShadowApplication; 36 37 import static android.provider.Settings.System.VIBRATE_WHEN_RINGING; 38 import static org.mockito.Matchers.any; 39 import static org.mockito.Mockito.mock; 40 import static org.mockito.Mockito.never; 41 import static org.mockito.Mockito.verify; 42 import static org.mockito.Mockito.when; 43 44 @RunWith(SettingsRobolectricTestRunner.class) 45 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) 46 public class VibrateWhenRingPreferenceControllerTest { 47 48 @Mock 49 private Context mContext; 50 @Mock 51 private PreferenceScreen mScreen; 52 @Mock 53 private TelephonyManager mTelephonyManager; 54 55 private VibrateWhenRingPreferenceController mController; 56 57 @Before 58 public void setUp() { 59 MockitoAnnotations.initMocks(this); 60 when(mContext.getSystemService(Context.TELEPHONY_SERVICE)).thenReturn(mTelephonyManager); 61 mController = new VibrateWhenRingPreferenceController(mContext); 62 } 63 64 @Test 65 public void display_voiceCapable_shouldDisplay() { 66 when(mTelephonyManager.isVoiceCapable()).thenReturn(true); 67 when(mScreen.findPreference(mController.getPreferenceKey())) 68 .thenReturn(mock(Preference.class)); 69 70 mController.displayPreference(mScreen); 71 72 verify(mScreen, never()).removePreference(any(Preference.class)); 73 } 74 75 @Test 76 public void display_notVoiceCapable_shouldNotDisplay() { 77 when(mTelephonyManager.isVoiceCapable()).thenReturn(false); 78 final Preference preference = mock(Preference.class); 79 when(mScreen.getPreferenceCount()).thenReturn(1); 80 when(mScreen.getPreference(0)).thenReturn(preference); 81 when(preference.getKey()).thenReturn(mController.getPreferenceKey()); 82 83 mController.displayPreference(mScreen); 84 85 verify(mScreen).removePreference(any(Preference.class)); 86 } 87 88 @Test 89 public void updateState_settingIsOn_preferenceShouldBeChecked() { 90 final TwoStatePreference preference = mock(TwoStatePreference.class); 91 final Context context = ShadowApplication.getInstance().getApplicationContext(); 92 Settings.System.putInt(context.getContentResolver(), VIBRATE_WHEN_RINGING, 1); 93 94 mController = new VibrateWhenRingPreferenceController(context); 95 mController.updateState(preference); 96 97 verify(preference).setChecked(true); 98 } 99 100 @Test 101 public void updateState_settingIsOff_preferenceShouldNotBeChecked() { 102 final TwoStatePreference preference = mock(TwoStatePreference.class); 103 final Context context = ShadowApplication.getInstance().getApplicationContext(); 104 Settings.System.putInt(context.getContentResolver(), VIBRATE_WHEN_RINGING, 0); 105 106 mController = new VibrateWhenRingPreferenceController(context); 107 mController.updateState(preference); 108 109 verify(preference).setChecked(false); 110 } 111 } 112