OSDN Git Service

3c3f212087d5c473726515fec5cf216a90f3a93b
[android-x86/packages-apps-Settings.git] / tests / robotests / src / com / android / settings / notification / VibrateWhenRingPreferenceControllerTest.java
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         when(mScreen.findPreference(mController.getPreferenceKey()))
79                 .thenReturn(mock(Preference.class));
80
81         mController.displayPreference(mScreen);
82
83         verify(mScreen).removePreference(any(Preference.class));
84     }
85
86     @Test
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);
91
92         mController = new VibrateWhenRingPreferenceController(context);
93         mController.updateState(preference);
94
95         verify(preference).setChecked(true);
96     }
97
98     @Test
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);
103
104         mController = new VibrateWhenRingPreferenceController(context);
105         mController.updateState(preference);
106
107         verify(preference).setChecked(false);
108     }
109 }