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.core.BasePreferenceController; 35 import com.android.settings.testutils.shadow.SettingsShadowResources; 36 import com.android.settings.R; 37 import com.android.settingslib.core.lifecycle.Lifecycle; 38 39 import org.junit.After; 40 import org.junit.Before; 41 import org.junit.Test; 42 import org.junit.runner.RunWith; 43 import org.mockito.Mock; 44 import org.mockito.MockitoAnnotations; 45 import org.robolectric.annotation.Config; 46 import org.robolectric.RobolectricTestRunner; 47 48 /** Test for {@link MediaVibrationIntensityPreferenceController}. */ 49 @RunWith(RobolectricTestRunner.class) 50 @Config(shadows = {SettingsShadowResources.class}) 51 public class MediaVibrationTogglePreferenceControllerTest { 52 53 private static final String PREFERENCE_KEY = "preference_key"; 54 55 @Mock private PreferenceScreen mScreen; 56 @Mock AudioManager mAudioManager; 57 58 private Lifecycle mLifecycle; 59 private Context mContext; 60 private Vibrator mVibrator; 61 private MediaVibrationTogglePreferenceController mController; 62 private SwitchPreference mPreference; 63 64 @Before setUp()65 public void setUp() { 66 MockitoAnnotations.initMocks(this); 67 mLifecycle = new Lifecycle(() -> mLifecycle); 68 mContext = spy(ApplicationProvider.getApplicationContext()); 69 when(mContext.getSystemService(Context.AUDIO_SERVICE)).thenReturn(mAudioManager); 70 when(mAudioManager.getRingerModeInternal()).thenReturn(AudioManager.RINGER_MODE_NORMAL); 71 mVibrator = mContext.getSystemService(Vibrator.class); 72 mController = new MediaVibrationTogglePreferenceController(mContext, PREFERENCE_KEY); 73 mLifecycle.addObserver(mController); 74 mPreference = new SwitchPreference(mContext); 75 mPreference.setSummary("Test summary"); 76 when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference); 77 mController.displayPreference(mScreen); 78 } 79 80 @After tearDown()81 public void tearDown() { 82 SettingsShadowResources.reset(); 83 } 84 85 @Test verifyConstants()86 public void verifyConstants() { 87 assertThat(mController.getPreferenceKey()).isEqualTo(PREFERENCE_KEY); 88 assertThat(mController.getAvailabilityStatus()) 89 .isEqualTo(BasePreferenceController.AVAILABLE); 90 } 91 92 @Test missingSetting_shouldReturnDefault()93 public void missingSetting_shouldReturnDefault() { 94 Settings.System.putString(mContext.getContentResolver(), 95 Settings.System.MEDIA_VIBRATION_INTENSITY, /* value= */ null); 96 97 mController.updateState(mPreference); 98 99 assertThat(mPreference.isChecked()).isTrue(); 100 } 101 102 @Test updateState_ringerModeUpdates_shouldNotAffectSettings()103 public void updateState_ringerModeUpdates_shouldNotAffectSettings() { 104 updateSetting(Settings.System.MEDIA_VIBRATION_INTENSITY, Vibrator.VIBRATION_INTENSITY_LOW); 105 106 when(mAudioManager.getRingerModeInternal()).thenReturn(AudioManager.RINGER_MODE_NORMAL); 107 mController.updateState(mPreference); 108 assertThat(mPreference.isChecked()).isTrue(); 109 assertThat(mPreference.isEnabled()).isTrue(); 110 111 when(mAudioManager.getRingerModeInternal()).thenReturn(AudioManager.RINGER_MODE_SILENT); 112 mController.updateState(mPreference); 113 assertThat(mPreference.isChecked()).isTrue(); 114 assertThat(mPreference.isEnabled()).isTrue(); 115 116 when(mAudioManager.getRingerModeInternal()).thenReturn(AudioManager.RINGER_MODE_VIBRATE); 117 mController.updateState(mPreference); 118 assertThat(mPreference.isChecked()).isTrue(); 119 assertThat(mPreference.isEnabled()).isTrue(); 120 } 121 122 @Test updateState_shouldDisplayOnOffState()123 public void updateState_shouldDisplayOnOffState() { 124 updateSetting(Settings.System.MEDIA_VIBRATION_INTENSITY, Vibrator.VIBRATION_INTENSITY_HIGH); 125 mController.updateState(mPreference); 126 assertThat(mPreference.isChecked()).isTrue(); 127 128 updateSetting(Settings.System.MEDIA_VIBRATION_INTENSITY, 129 Vibrator.VIBRATION_INTENSITY_MEDIUM); 130 mController.updateState(mPreference); 131 assertThat(mPreference.isChecked()).isTrue(); 132 133 updateSetting(Settings.System.MEDIA_VIBRATION_INTENSITY, Vibrator.VIBRATION_INTENSITY_LOW); 134 mController.updateState(mPreference); 135 assertThat(mPreference.isChecked()).isTrue(); 136 137 updateSetting(Settings.System.MEDIA_VIBRATION_INTENSITY, Vibrator.VIBRATION_INTENSITY_OFF); 138 mController.updateState(mPreference); 139 assertThat(mPreference.isChecked()).isFalse(); 140 } 141 142 @Test setChecked_updatesIntensityAndDependentSettings()143 public void setChecked_updatesIntensityAndDependentSettings() throws Exception { 144 updateSetting(Settings.System.MEDIA_VIBRATION_INTENSITY, Vibrator.VIBRATION_INTENSITY_OFF); 145 mController.updateState(mPreference); 146 assertThat(mPreference.isChecked()).isFalse(); 147 148 mController.setChecked(true); 149 assertThat(readSetting(Settings.System.MEDIA_VIBRATION_INTENSITY)).isEqualTo( 150 mVibrator.getDefaultVibrationIntensity(VibrationAttributes.USAGE_MEDIA)); 151 152 mController.setChecked(false); 153 assertThat(readSetting(Settings.System.MEDIA_VIBRATION_INTENSITY)) 154 .isEqualTo(Vibrator.VIBRATION_INTENSITY_OFF); 155 } 156 157 @Test configForMediaVibration_enabled_shouldShowToogle()158 public void configForMediaVibration_enabled_shouldShowToogle() { 159 SettingsShadowResources.overrideResource(R.bool.config_media_vibration_supported, true); 160 mController.updateState(mPreference); 161 162 final boolean mediaVibrationConfig = mContext.getResources() 163 .getBoolean(R.bool.config_media_vibration_supported); 164 165 assertThat(mediaVibrationConfig).isTrue(); 166 assertThat(mController.isAvailable()).isTrue(); 167 assertThat(mController.isSupported()).isTrue(); 168 } 169 170 @Test configForMediaVibration_disabled_shouldHideToggle()171 public void configForMediaVibration_disabled_shouldHideToggle() { 172 SettingsShadowResources.overrideResource(R.bool.config_media_vibration_supported, false); 173 mController.updateState(mPreference); 174 175 final boolean mediaVibrationConfig = mContext.getResources() 176 .getBoolean(R.bool.config_media_vibration_supported); 177 178 assertThat(mediaVibrationConfig).isFalse(); 179 assertThat(mController.isAvailable()).isFalse(); 180 assertThat(mController.isSupported()).isFalse(); 181 } 182 updateSetting(String key, int value)183 private void updateSetting(String key, int value) { 184 Settings.System.putInt(mContext.getContentResolver(), key, value); 185 } 186 readSetting(String settingKey)187 private int readSetting(String settingKey) throws Settings.SettingNotFoundException { 188 return Settings.System.getInt(mContext.getContentResolver(), settingKey); 189 } 190 } 191