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