2 * Copyright (C) 2016 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.settings.notification;
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;
26 import com.android.settings.SettingsRobolectricTestRunner;
27 import com.android.settings.TestConfig;
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;
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;
44 @RunWith(SettingsRobolectricTestRunner.class)
45 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
46 public class VibrateWhenRingPreferenceControllerTest {
49 private Context mContext;
51 private PreferenceScreen mScreen;
53 private TelephonyManager mTelephonyManager;
55 private VibrateWhenRingPreferenceController mController;
59 MockitoAnnotations.initMocks(this);
60 when(mContext.getSystemService(Context.TELEPHONY_SERVICE)).thenReturn(mTelephonyManager);
61 mController = new VibrateWhenRingPreferenceController(mContext);
65 public void display_voiceCapable_shouldDisplay() {
66 when(mTelephonyManager.isVoiceCapable()).thenReturn(true);
67 when(mScreen.findPreference(mController.getPreferenceKey()))
68 .thenReturn(mock(Preference.class));
70 mController.displayPreference(mScreen);
72 verify(mScreen, never()).removePreference(any(Preference.class));
76 public void display_notVoiceCapable_shouldNotDisplay() {
77 when(mTelephonyManager.isVoiceCapable()).thenReturn(false);
78 when(mScreen.findPreference(mController.getPreferenceKey()))
79 .thenReturn(mock(Preference.class));
81 mController.displayPreference(mScreen);
83 verify(mScreen).removePreference(any(Preference.class));
87 public void updateState_settingIsOn_preferenceShouldBeChecked() {
88 final TwoStatePreference preference = mock(TwoStatePreference.class);
89 final Context context = ShadowApplication.getInstance().getApplicationContext();
90 Settings.System.putInt(context.getContentResolver(), VIBRATE_WHEN_RINGING, 1);
92 mController = new VibrateWhenRingPreferenceController(context);
93 mController.updateState(preference);
95 verify(preference).setChecked(true);
99 public void updateState_settingIsOff_preferenceShouldNotBeChecked() {
100 final TwoStatePreference preference = mock(TwoStatePreference.class);
101 final Context context = ShadowApplication.getInstance().getApplicationContext();
102 Settings.System.putInt(context.getContentResolver(), VIBRATE_WHEN_RINGING, 0);
104 mController = new VibrateWhenRingPreferenceController(context);
105 mController.updateState(preference);
107 verify(preference).setChecked(false);