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.Test; 44 import org.junit.runner.RunWith; 45 import org.mockito.Mock; 46 import org.mockito.MockitoAnnotations; 47 import org.robolectric.RobolectricTestRunner; 48 49 @RunWith(RobolectricTestRunner.class) 50 public class VibrationRampingRingerTogglePreferenceControllerTest { 51 52 private static final String PREFERENCE_KEY = "preference_key"; 53 54 @Mock private PreferenceScreen mScreen; 55 @Mock private TelephonyManager mTelephonyManager; 56 @Mock private AudioManager mAudioManager; 57 @Mock private VibrationRampingRingerTogglePreferenceController.DeviceConfigProvider 58 mDeviceConfigProvider; 59 60 private Lifecycle mLifecycle; 61 private Context mContext; 62 private VibrationRampingRingerTogglePreferenceController mController; 63 private SwitchPreference mPreference; 64 65 @Before setUp()66 public void setUp() { 67 MockitoAnnotations.initMocks(this); 68 mLifecycle = new Lifecycle(() -> mLifecycle); 69 mContext = spy(ApplicationProvider.getApplicationContext()); 70 when(mContext.getSystemService(Context.TELEPHONY_SERVICE)).thenReturn(mTelephonyManager); 71 when(mContext.getSystemService(Context.AUDIO_SERVICE)).thenReturn(mAudioManager); 72 when(mAudioManager.getRingerModeInternal()).thenReturn(AudioManager.RINGER_MODE_NORMAL); 73 mController = new VibrationRampingRingerTogglePreferenceController(mContext, 74 PREFERENCE_KEY, mDeviceConfigProvider); 75 mLifecycle.addObserver(mController); 76 mPreference = new SwitchPreference(mContext); 77 mPreference.setSummary("Test summary"); 78 when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference); 79 mController.displayPreference(mScreen); 80 } 81 82 @Test verifyConstants()83 public void verifyConstants() { 84 assertThat(mController.getPreferenceKey()).isEqualTo(PREFERENCE_KEY); 85 } 86 87 @Test getAvailabilityStatus_notVoiceCapable_returnUnsupportedOnDevice()88 public void getAvailabilityStatus_notVoiceCapable_returnUnsupportedOnDevice() { 89 when(mTelephonyManager.isVoiceCapable()).thenReturn(false); 90 when(mDeviceConfigProvider.isRampingRingerEnabledOnTelephonyConfig()).thenReturn(false); 91 92 assertThat(mController.getAvailabilityStatus()).isEqualTo(UNSUPPORTED_ON_DEVICE); 93 } 94 95 @Test getAvailabilityStatus_rampingRingerEnabled_returnUnsupportedOnDevice()96 public void getAvailabilityStatus_rampingRingerEnabled_returnUnsupportedOnDevice() { 97 when(mTelephonyManager.isVoiceCapable()).thenReturn(true); 98 when(mDeviceConfigProvider.isRampingRingerEnabledOnTelephonyConfig()).thenReturn(true); 99 100 assertThat(mController.getAvailabilityStatus()).isEqualTo(UNSUPPORTED_ON_DEVICE); 101 } 102 103 @Test getAvailabilityStatus_voiceCapableAndRampingRingerDisabled_returnAvailable()104 public void getAvailabilityStatus_voiceCapableAndRampingRingerDisabled_returnAvailable() { 105 when(mTelephonyManager.isVoiceCapable()).thenReturn(true); 106 when(mDeviceConfigProvider.isRampingRingerEnabledOnTelephonyConfig()).thenReturn(false); 107 108 assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE); 109 } 110 111 @Test updateState_withRingDisabled_shouldReturnFalseForCheckedAndEnabled()112 public void updateState_withRingDisabled_shouldReturnFalseForCheckedAndEnabled() { 113 updateSetting(Settings.System.RING_VIBRATION_INTENSITY, Vibrator.VIBRATION_INTENSITY_OFF); 114 when(mAudioManager.isRampingRingerEnabled()).thenReturn(true); 115 mController.updateState(mPreference); 116 117 assertThat(mPreference.isEnabled()).isFalse(); 118 assertThat(mPreference.isChecked()).isFalse(); 119 } 120 121 @Test updateState_withRingEnabled_shouldReturnTheSettingStateAndAlwaysEnabled()122 public void updateState_withRingEnabled_shouldReturnTheSettingStateAndAlwaysEnabled() { 123 updateSetting(Settings.System.RING_VIBRATION_INTENSITY, Vibrator.VIBRATION_INTENSITY_HIGH); 124 when(mAudioManager.isRampingRingerEnabled()).thenReturn(true, false); 125 126 mController.updateState(mPreference); 127 assertThat(mPreference.isEnabled()).isTrue(); 128 assertThat(mPreference.isChecked()).isTrue(); 129 130 mController.updateState(mPreference); 131 assertThat(mPreference.isEnabled()).isTrue(); 132 assertThat(mPreference.isChecked()).isFalse(); 133 } 134 135 @Test setChecked_withRingDisabled_ignoresUpdates()136 public void setChecked_withRingDisabled_ignoresUpdates() { 137 updateSetting(Settings.System.RING_VIBRATION_INTENSITY, Vibrator.VIBRATION_INTENSITY_OFF); 138 139 mController.setChecked(true); 140 mController.setChecked(false); 141 verify(mAudioManager, never()).setRampingRingerEnabled(anyBoolean()); 142 } 143 144 @Test setChecked_withRingEnabled_updatesSetting()145 public void setChecked_withRingEnabled_updatesSetting() { 146 updateSetting(Settings.System.RING_VIBRATION_INTENSITY, Vibrator.VIBRATION_INTENSITY_HIGH); 147 148 mController.setChecked(true); 149 verify(mAudioManager).setRampingRingerEnabled(true); 150 151 mController.setChecked(false); 152 verify(mAudioManager).setRampingRingerEnabled(false); 153 } 154 updateSetting(String key, int value)155 private void updateSetting(String key, int value) { 156 Settings.System.putInt(mContext.getContentResolver(), key, value); 157 } 158 } 159