• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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.accessibility;
18 
19 import static com.android.settings.core.BasePreferenceController.AVAILABLE;
20 import static com.android.settings.core.BasePreferenceController.UNSUPPORTED_ON_DEVICE;
21 
22 import static com.google.common.truth.Truth.assertThat;
23 
24 import static org.mockito.ArgumentMatchers.anyBoolean;
25 import static org.mockito.Mockito.never;
26 import static org.mockito.Mockito.spy;
27 import static org.mockito.Mockito.verify;
28 import static org.mockito.Mockito.when;
29 
30 import android.content.Context;
31 import android.media.AudioManager;
32 import android.os.Vibrator;
33 import android.provider.Settings;
34 import android.telephony.TelephonyManager;
35 
36 import androidx.preference.PreferenceScreen;
37 import androidx.preference.SwitchPreference;
38 import androidx.test.core.app.ApplicationProvider;
39 
40 import com.android.settingslib.core.lifecycle.Lifecycle;
41 
42 import org.junit.Before;
43 import org.junit.Ignore;
44 import org.junit.Test;
45 import org.junit.runner.RunWith;
46 import org.mockito.Mock;
47 import org.mockito.MockitoAnnotations;
48 import org.robolectric.RobolectricTestRunner;
49 
50 @RunWith(RobolectricTestRunner.class)
51 public class VibrationRampingRingerTogglePreferenceControllerTest {
52 
53     private static final String PREFERENCE_KEY = "preference_key";
54 
55     @Mock private PreferenceScreen mScreen;
56     @Mock private TelephonyManager mTelephonyManager;
57     @Mock private AudioManager mAudioManager;
58     @Mock private VibrationRampingRingerTogglePreferenceController.DeviceConfigProvider
59             mDeviceConfigProvider;
60 
61     private Lifecycle mLifecycle;
62     private Context mContext;
63     private VibrationRampingRingerTogglePreferenceController mController;
64     private SwitchPreference mPreference;
65 
66     @Before
setUp()67     public void setUp() {
68         MockitoAnnotations.initMocks(this);
69         mLifecycle = new Lifecycle(() -> mLifecycle);
70         mContext = spy(ApplicationProvider.getApplicationContext());
71         when(mContext.getSystemService(Context.TELEPHONY_SERVICE)).thenReturn(mTelephonyManager);
72         when(mContext.getSystemService(Context.AUDIO_SERVICE)).thenReturn(mAudioManager);
73         when(mAudioManager.getRingerModeInternal()).thenReturn(AudioManager.RINGER_MODE_NORMAL);
74         mController = new VibrationRampingRingerTogglePreferenceController(mContext,
75                 PREFERENCE_KEY, mDeviceConfigProvider);
76         mLifecycle.addObserver(mController);
77         mPreference = new SwitchPreference(mContext);
78         mPreference.setSummary("Test summary");
79         when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
80         mController.displayPreference(mScreen);
81     }
82 
83     @Test
verifyConstants()84     public void verifyConstants() {
85         assertThat(mController.getPreferenceKey()).isEqualTo(PREFERENCE_KEY);
86     }
87 
88     @Test
89     @Ignore
getAvailabilityStatus_notVoiceCapable_returnUnsupportedOnDevice()90     public void getAvailabilityStatus_notVoiceCapable_returnUnsupportedOnDevice() {
91         when(mTelephonyManager.isVoiceCapable()).thenReturn(false);
92         when(mDeviceConfigProvider.isRampingRingerEnabledOnTelephonyConfig()).thenReturn(false);
93 
94         assertThat(mController.getAvailabilityStatus()).isEqualTo(UNSUPPORTED_ON_DEVICE);
95     }
96 
97     @Test
98     @Ignore
getAvailabilityStatus_rampingRingerEnabled_returnUnsupportedOnDevice()99     public void getAvailabilityStatus_rampingRingerEnabled_returnUnsupportedOnDevice() {
100         when(mTelephonyManager.isVoiceCapable()).thenReturn(true);
101         when(mDeviceConfigProvider.isRampingRingerEnabledOnTelephonyConfig()).thenReturn(true);
102 
103         assertThat(mController.getAvailabilityStatus()).isEqualTo(UNSUPPORTED_ON_DEVICE);
104     }
105 
106     @Test
107     @Ignore
getAvailabilityStatus_voiceCapableAndRampingRingerDisabled_returnAvailable()108     public void getAvailabilityStatus_voiceCapableAndRampingRingerDisabled_returnAvailable() {
109         when(mTelephonyManager.isVoiceCapable()).thenReturn(true);
110         when(mDeviceConfigProvider.isRampingRingerEnabledOnTelephonyConfig()).thenReturn(false);
111 
112         assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE);
113     }
114 
115     @Test
updateState_withRingDisabled_shouldReturnFalseForCheckedAndEnabled()116     public void updateState_withRingDisabled_shouldReturnFalseForCheckedAndEnabled() {
117         updateSetting(Settings.System.RING_VIBRATION_INTENSITY, Vibrator.VIBRATION_INTENSITY_OFF);
118         when(mAudioManager.isRampingRingerEnabled()).thenReturn(true);
119         mController.updateState(mPreference);
120 
121         assertThat(mPreference.isEnabled()).isFalse();
122         assertThat(mPreference.isChecked()).isFalse();
123     }
124 
125     @Test
updateState_withRingEnabled_shouldReturnTheSettingStateAndAlwaysEnabled()126     public void updateState_withRingEnabled_shouldReturnTheSettingStateAndAlwaysEnabled() {
127         updateSetting(Settings.System.RING_VIBRATION_INTENSITY, Vibrator.VIBRATION_INTENSITY_HIGH);
128         when(mAudioManager.isRampingRingerEnabled()).thenReturn(true, false);
129 
130         mController.updateState(mPreference);
131         assertThat(mPreference.isEnabled()).isTrue();
132         assertThat(mPreference.isChecked()).isTrue();
133 
134         mController.updateState(mPreference);
135         assertThat(mPreference.isEnabled()).isTrue();
136         assertThat(mPreference.isChecked()).isFalse();
137     }
138 
139     @Test
setChecked_withRingDisabled_ignoresUpdates()140     public void setChecked_withRingDisabled_ignoresUpdates() {
141         updateSetting(Settings.System.RING_VIBRATION_INTENSITY, Vibrator.VIBRATION_INTENSITY_OFF);
142 
143         mController.setChecked(true);
144         mController.setChecked(false);
145         verify(mAudioManager, never()).setRampingRingerEnabled(anyBoolean());
146     }
147 
148     @Test
149     @Ignore
setChecked_withRingEnabled_updatesSetting()150     public void setChecked_withRingEnabled_updatesSetting() {
151         updateSetting(Settings.System.RING_VIBRATION_INTENSITY, Vibrator.VIBRATION_INTENSITY_HIGH);
152 
153         mController.setChecked(true);
154         verify(mAudioManager).setRampingRingerEnabled(true);
155 
156         mController.setChecked(false);
157         verify(mAudioManager).setRampingRingerEnabled(false);
158     }
159 
updateSetting(String key, int value)160     private void updateSetting(String key, int value) {
161         Settings.System.putInt(mContext.getContentResolver(), key, value);
162     }
163 }
164