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.google.common.truth.Truth.assertThat;
20 
21 import static org.mockito.Mockito.spy;
22 import static org.mockito.Mockito.when;
23 
24 import android.content.Context;
25 import android.media.AudioManager;
26 import android.os.VibrationAttributes;
27 import android.os.Vibrator;
28 import android.provider.Settings;
29 
30 import androidx.preference.PreferenceScreen;
31 import androidx.preference.SwitchPreference;
32 import androidx.test.core.app.ApplicationProvider;
33 
34 import com.android.settings.R;
35 import com.android.settings.core.BasePreferenceController;
36 import com.android.settingslib.core.lifecycle.Lifecycle;
37 
38 import org.junit.Before;
39 import org.junit.Test;
40 import org.junit.runner.RunWith;
41 import org.mockito.Mock;
42 import org.mockito.MockitoAnnotations;
43 import org.robolectric.RobolectricTestRunner;
44 
45 @RunWith(RobolectricTestRunner.class)
46 public class RingVibrationTogglePreferenceControllerTest {
47 
48     private static final String PREFERENCE_KEY = "preference_key";
49     private static final int OFF = 0;
50     private static final int ON = 1;
51 
52     @Mock private PreferenceScreen mScreen;
53     @Mock private AudioManager mAudioManager;
54 
55     private Lifecycle mLifecycle;
56     private Context mContext;
57     private Vibrator mVibrator;
58     private RingVibrationTogglePreferenceController mController;
59     private SwitchPreference mPreference;
60 
61     @Before
setUp()62     public void setUp() {
63         MockitoAnnotations.initMocks(this);
64         mLifecycle = new Lifecycle(() -> mLifecycle);
65         mContext = spy(ApplicationProvider.getApplicationContext());
66         when(mContext.getSystemService(Context.AUDIO_SERVICE)).thenReturn(mAudioManager);
67         when(mAudioManager.getRingerModeInternal()).thenReturn(AudioManager.RINGER_MODE_NORMAL);
68         mVibrator = mContext.getSystemService(Vibrator.class);
69         mController = new RingVibrationTogglePreferenceController(mContext, PREFERENCE_KEY);
70         mLifecycle.addObserver(mController);
71         mPreference = new SwitchPreference(mContext);
72         mPreference.setSummary("Test summary");
73         when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
74         mController.displayPreference(mScreen);
75     }
76 
77     @Test
verifyConstants()78     public void verifyConstants() {
79         assertThat(mController.getPreferenceKey()).isEqualTo(PREFERENCE_KEY);
80         assertThat(mController.getAvailabilityStatus())
81                 .isEqualTo(BasePreferenceController.AVAILABLE);
82     }
83 
84     @Test
missingSetting_shouldReturnDefault()85     public void missingSetting_shouldReturnDefault() {
86         Settings.System.putString(mContext.getContentResolver(),
87                 Settings.System.RING_VIBRATION_INTENSITY, /* value= */ null);
88         mController.updateState(mPreference);
89         assertThat(mPreference.isChecked()).isTrue();
90     }
91 
92     @Test
updateState_ringerModeUpdates_shouldPreserveSettingAndDisplaySummary()93     public void updateState_ringerModeUpdates_shouldPreserveSettingAndDisplaySummary() {
94         updateSetting(Settings.System.RING_VIBRATION_INTENSITY, Vibrator.VIBRATION_INTENSITY_LOW);
95 
96         when(mAudioManager.getRingerModeInternal()).thenReturn(AudioManager.RINGER_MODE_NORMAL);
97         mController.updateState(mPreference);
98         assertThat(mPreference.isChecked()).isTrue();
99         assertThat(mPreference.getSummary()).isNull();
100         assertThat(mPreference.isEnabled()).isTrue();
101 
102         when(mAudioManager.getRingerModeInternal()).thenReturn(AudioManager.RINGER_MODE_SILENT);
103         mController.updateState(mPreference);
104         assertThat(mPreference.isChecked()).isFalse();
105         assertThat(mPreference.getSummary()).isNotNull();
106         assertThat(mPreference.getSummary().toString()).isEqualTo(mContext.getString(
107                 R.string.accessibility_vibration_setting_disabled_for_silent_mode_summary));
108         assertThat(mPreference.isEnabled()).isFalse();
109 
110         when(mAudioManager.getRingerModeInternal()).thenReturn(AudioManager.RINGER_MODE_VIBRATE);
111         mController.updateState(mPreference);
112         assertThat(mPreference.isChecked()).isTrue();
113         assertThat(mPreference.getSummary()).isNull();
114         assertThat(mPreference.isEnabled()).isTrue();
115     }
116 
117     @Test
updateState_vibrateWhenRingingAndRampingRingerOff_shouldIgnoreAndUseIntensity()118     public void updateState_vibrateWhenRingingAndRampingRingerOff_shouldIgnoreAndUseIntensity() {
119         // VIBRATE_WHEN_RINGING is deprecated and should have no effect on the ring vibration
120         // setting. The ramping ringer is also independent now, instead of a 3-state setting.
121         when(mAudioManager.isRampingRingerEnabled()).thenReturn(false);
122         updateSetting(Settings.System.VIBRATE_WHEN_RINGING, OFF);
123 
124         updateSetting(Settings.System.RING_VIBRATION_INTENSITY, Vibrator.VIBRATION_INTENSITY_HIGH);
125         mController.updateState(mPreference);
126         assertThat(mPreference.isChecked()).isTrue();
127 
128         updateSetting(Settings.System.RING_VIBRATION_INTENSITY,
129                 Vibrator.VIBRATION_INTENSITY_MEDIUM);
130         mController.updateState(mPreference);
131         assertThat(mPreference.isChecked()).isTrue();
132 
133         updateSetting(Settings.System.RING_VIBRATION_INTENSITY, Vibrator.VIBRATION_INTENSITY_LOW);
134         mController.updateState(mPreference);
135         assertThat(mPreference.isChecked()).isTrue();
136 
137         updateSetting(Settings.System.RING_VIBRATION_INTENSITY, Vibrator.VIBRATION_INTENSITY_OFF);
138         mController.updateState(mPreference);
139         assertThat(mPreference.isChecked()).isFalse();
140     }
141 
142     @Test
updateState_shouldDisplayOnOffState()143     public void updateState_shouldDisplayOnOffState() {
144         updateSetting(Settings.System.RING_VIBRATION_INTENSITY, Vibrator.VIBRATION_INTENSITY_HIGH);
145         mController.updateState(mPreference);
146         assertThat(mPreference.isChecked()).isTrue();
147 
148         updateSetting(Settings.System.RING_VIBRATION_INTENSITY,
149                 Vibrator.VIBRATION_INTENSITY_MEDIUM);
150         mController.updateState(mPreference);
151         assertThat(mPreference.isChecked()).isTrue();
152 
153         updateSetting(Settings.System.RING_VIBRATION_INTENSITY, Vibrator.VIBRATION_INTENSITY_LOW);
154         mController.updateState(mPreference);
155         assertThat(mPreference.isChecked()).isTrue();
156 
157         updateSetting(Settings.System.RING_VIBRATION_INTENSITY, Vibrator.VIBRATION_INTENSITY_OFF);
158         mController.updateState(mPreference);
159         assertThat(mPreference.isChecked()).isFalse();
160     }
161 
162     @Test
setChecked_updatesIntensityAndDependentSettings()163     public void setChecked_updatesIntensityAndDependentSettings() throws Exception {
164         updateSetting(Settings.System.RING_VIBRATION_INTENSITY, Vibrator.VIBRATION_INTENSITY_OFF);
165         mController.updateState(mPreference);
166         assertThat(mPreference.isChecked()).isFalse();
167 
168         mController.setChecked(true);
169         assertThat(readSetting(Settings.System.RING_VIBRATION_INTENSITY)).isEqualTo(
170                 mVibrator.getDefaultVibrationIntensity(VibrationAttributes.USAGE_RINGTONE));
171         assertThat(readSetting(Settings.System.VIBRATE_WHEN_RINGING)).isEqualTo(ON);
172 
173         mController.setChecked(false);
174         assertThat(readSetting(Settings.System.RING_VIBRATION_INTENSITY))
175                 .isEqualTo(Vibrator.VIBRATION_INTENSITY_OFF);
176         assertThat(readSetting(Settings.System.VIBRATE_WHEN_RINGING)).isEqualTo(OFF);
177     }
178 
updateSetting(String key, int value)179     private void updateSetting(String key, int value) {
180         Settings.System.putInt(mContext.getContentResolver(), key, value);
181     }
182 
readSetting(String settingKey)183     private int readSetting(String settingKey) throws Settings.SettingNotFoundException {
184         return Settings.System.getInt(mContext.getContentResolver(), settingKey);
185     }
186 }
187