• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 static android.provider.Settings.System.VIBRATE_WHEN_RINGING;
20 
21 import static com.android.settings.core.BasePreferenceController.AVAILABLE;
22 import static com.android.settings.core.BasePreferenceController.UNSUPPORTED_ON_DEVICE;
23 
24 import static com.google.common.truth.Truth.assertThat;
25 
26 import static org.mockito.Mockito.mock;
27 import static org.mockito.Mockito.spy;
28 import static org.mockito.Mockito.when;
29 
30 import android.content.ContentResolver;
31 import android.content.Context;
32 import android.provider.Settings;
33 import android.support.v7.preference.Preference;
34 import android.support.v7.preference.PreferenceScreen;
35 import android.support.v7.preference.TwoStatePreference;
36 import android.telephony.TelephonyManager;
37 
38 import com.android.settings.testutils.SettingsRobolectricTestRunner;
39 
40 import org.junit.Before;
41 import org.junit.Test;
42 import org.junit.runner.RunWith;
43 import org.mockito.Mock;
44 import org.mockito.MockitoAnnotations;
45 import org.robolectric.RuntimeEnvironment;
46 import org.robolectric.shadow.api.Shadow;
47 import org.robolectric.shadows.ShadowContentResolver;
48 
49 @RunWith(SettingsRobolectricTestRunner.class)
50 public class VibrateWhenRingPreferenceControllerTest {
51 
52     private static final String KEY_VIBRATE_WHEN_RINGING = "vibrate_when_ringing";
53     private final int DEFAULT_VALUE = 0;
54     private final int NOTIFICATION_VIBRATE_WHEN_RINGING = 1;
55     private Context mContext;
56     private ContentResolver mContentResolver;
57     @Mock
58     private PreferenceScreen mScreen;
59     @Mock
60     private TelephonyManager mTelephonyManager;
61     private VibrateWhenRingPreferenceController mController;
62     private Preference mPreference;
63 
64     @Before
setUp()65     public void setUp() {
66         MockitoAnnotations.initMocks(this);
67         mContext = spy(RuntimeEnvironment.application);
68         mContentResolver = mContext.getContentResolver();
69         when(mContext.getSystemService(Context.TELEPHONY_SERVICE)).thenReturn(mTelephonyManager);
70         mController = new VibrateWhenRingPreferenceController(mContext, KEY_VIBRATE_WHEN_RINGING);
71         mPreference = new Preference(mContext);
72         mPreference.setKey(mController.getPreferenceKey());
73         when(mScreen.findPreference(mPreference.getKey())).thenReturn(mPreference);
74     }
75 
76     @Test
display_voiceCapable_shouldDisplay()77     public void display_voiceCapable_shouldDisplay() {
78         when(mTelephonyManager.isVoiceCapable()).thenReturn(true);
79 
80         mController.displayPreference(mScreen);
81 
82         assertThat(mPreference.isVisible()).isTrue();
83     }
84 
85     @Test
display_notVoiceCapable_shouldNotDisplay()86     public void display_notVoiceCapable_shouldNotDisplay() {
87         when(mTelephonyManager.isVoiceCapable()).thenReturn(false);
88 
89         mController.displayPreference(mScreen);
90 
91         assertThat(mPreference.isVisible()).isFalse();
92     }
93 
94     @Test
testOnPreferenceChange_turnOn_returnOn()95     public void testOnPreferenceChange_turnOn_returnOn() {
96         mController.onPreferenceChange(null, true);
97         final int mode = Settings.System.getInt(mContext.getContentResolver(),
98                 VIBRATE_WHEN_RINGING, DEFAULT_VALUE);
99 
100         assertThat(mode).isEqualTo(NOTIFICATION_VIBRATE_WHEN_RINGING);
101     }
102 
103     @Test
testOnPreferenceChange_turnOff_returnOff()104     public void testOnPreferenceChange_turnOff_returnOff() {
105         mController.onPreferenceChange(null, false);
106         final int mode = Settings.System.getInt(mContext.getContentResolver(),
107                 VIBRATE_WHEN_RINGING, DEFAULT_VALUE);
108 
109         assertThat(mode).isEqualTo(DEFAULT_VALUE);
110     }
111 
112     @Test
voiceCapable_availabled()113     public void voiceCapable_availabled() {
114         when(mTelephonyManager.isVoiceCapable()).thenReturn(true);
115 
116         assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE);
117     }
118 
119     @Test
voiceCapable_notAvailabled()120     public void voiceCapable_notAvailabled() {
121         when(mTelephonyManager.isVoiceCapable()).thenReturn(false);
122 
123         assertThat(mController.getAvailabilityStatus()).isEqualTo(UNSUPPORTED_ON_DEVICE);
124     }
125 
126     @Test
updateState_settingIsOn_preferenceShouldBeChecked()127     public void updateState_settingIsOn_preferenceShouldBeChecked() {
128         final TwoStatePreference preference = mock(TwoStatePreference.class);
129         Settings.System.putInt(mContext.getContentResolver(), VIBRATE_WHEN_RINGING, 1);
130 
131         mController.updateState(preference);
132 
133         assertThat(mController.isChecked()).isTrue();
134     }
135 
136     @Test
updateState_settingIsOff_preferenceShouldNotBeChecked()137     public void updateState_settingIsOff_preferenceShouldNotBeChecked() {
138         final TwoStatePreference preference = mock(TwoStatePreference.class);
139         Settings.System.putInt(mContext.getContentResolver(), VIBRATE_WHEN_RINGING, 0);
140 
141         mController.updateState(preference);
142 
143         assertThat(mController.isChecked()).isFalse();
144     }
145 
146     @Test
setChecked_settingsIsOn()147     public void setChecked_settingsIsOn() {
148         mController.setChecked(true);
149         final int mode = Settings.System.getInt(mContext.getContentResolver(), VIBRATE_WHEN_RINGING,
150                 -1);
151 
152         assertThat(mode).isEqualTo(NOTIFICATION_VIBRATE_WHEN_RINGING);
153     }
154 
155     @Test
setChecked_settingsIsOff()156     public void setChecked_settingsIsOff() {
157         mController.setChecked(false);
158         final int mode = Settings.System.getInt(mContext.getContentResolver(), VIBRATE_WHEN_RINGING,
159                 -1);
160 
161         assertThat(mode).isEqualTo(DEFAULT_VALUE);
162     }
163 
164     @Test
testObserver_onResume_shouldRegisterObserver()165     public void testObserver_onResume_shouldRegisterObserver() {
166         final ShadowContentResolver shadowContentResolver = Shadow.extract(mContentResolver);
167         mController.displayPreference(mScreen);
168 
169         mController.onResume();
170 
171         assertThat(shadowContentResolver.getContentObservers(
172                 Settings.System.getUriFor(VIBRATE_WHEN_RINGING))).isNotEmpty();
173     }
174 
175     @Test
testObserver_onPause_shouldUnregisterObserver()176     public void testObserver_onPause_shouldUnregisterObserver() {
177         final ShadowContentResolver shadowContentResolver = Shadow.extract(mContentResolver);
178         mController.displayPreference(mScreen);
179 
180         mController.onResume();
181         mController.onPause();
182 
183         assertThat(shadowContentResolver.getContentObservers(
184                 Settings.System.getUriFor(VIBRATE_WHEN_RINGING))).isEmpty();
185     }
186 
187     @Test
isSliceableCorrectKey_returnsTrue()188     public void isSliceableCorrectKey_returnsTrue() {
189         final VibrateWhenRingPreferenceController controller =
190                 new VibrateWhenRingPreferenceController(mContext, "vibrate_when_ringing");
191         assertThat(controller.isSliceable()).isTrue();
192     }
193 
194     @Test
isSliceableIncorrectKey_returnsFalse()195     public void isSliceableIncorrectKey_returnsFalse() {
196         final VibrateWhenRingPreferenceController controller =
197                 new VibrateWhenRingPreferenceController(mContext, "bad_key");
198         assertThat(controller.isSliceable()).isFalse();
199     }
200 }
201