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