1 /* 2 * Copyright (C) 2023 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.accessibility.AccessibilityUtil.State.OFF; 20 import static com.android.settings.accessibility.AccessibilityUtil.State.ON; 21 import static com.android.settings.core.BasePreferenceController.AVAILABLE; 22 import static com.android.settings.core.BasePreferenceController.UNSUPPORTED_ON_DEVICE; 23 24 import static com.google.common.truth.Truth.assertThat; 25 26 import static org.mockito.ArgumentMatchers.any; 27 import static org.mockito.ArgumentMatchers.eq; 28 import static org.mockito.Mockito.spy; 29 import static org.mockito.Mockito.verify; 30 import static org.mockito.Mockito.when; 31 32 import android.app.settings.SettingsEnums; 33 import android.content.ContentResolver; 34 import android.content.Context; 35 import android.content.res.Resources; 36 import android.platform.test.flag.junit.SetFlagsRule; 37 import android.provider.Settings; 38 39 import androidx.preference.PreferenceScreen; 40 import androidx.preference.SwitchPreference; 41 import androidx.test.core.app.ApplicationProvider; 42 43 import com.android.settings.testutils.FakeFeatureFactory; 44 45 import org.junit.Before; 46 import org.junit.Rule; 47 import org.junit.Test; 48 import org.junit.runner.RunWith; 49 import org.mockito.Mock; 50 import org.mockito.MockitoAnnotations; 51 import org.robolectric.RobolectricTestRunner; 52 53 /** Tests for {@link KeyboardVibrationTogglePreferenceController}. */ 54 @RunWith(RobolectricTestRunner.class) 55 public class KeyboardVibrationTogglePreferenceControllerTest { 56 @Rule 57 public final SetFlagsRule mSetFlagsRule = new SetFlagsRule(); 58 59 @Mock 60 private PreferenceScreen mPreferenceScreen; 61 62 @Mock 63 private ContentResolver mContentResolver; 64 65 private Context mContext; 66 private Resources mResources; 67 private KeyboardVibrationTogglePreferenceController mController; 68 private SwitchPreference mPreference; 69 private FakeFeatureFactory mFeatureFactory; 70 71 @Before setUp()72 public void setUp() { 73 MockitoAnnotations.initMocks(this); 74 mContext = spy(ApplicationProvider.getApplicationContext()); 75 mResources = spy(mContext.getResources()); 76 when(mContext.getResources()).thenReturn(mResources); 77 when(mContext.getContentResolver()).thenReturn(mContentResolver); 78 mFeatureFactory = FakeFeatureFactory.setupForTest(); 79 mController = new KeyboardVibrationTogglePreferenceController(mContext, "preferenceKey"); 80 mPreference = new SwitchPreference(mContext); 81 when(mPreferenceScreen.findPreference( 82 mController.getPreferenceKey())).thenReturn(mPreference); 83 mController.displayPreference(mPreferenceScreen); 84 } 85 86 @Test getAvailabilityStatus_featureSupported_available()87 public void getAvailabilityStatus_featureSupported_available() { 88 when(mResources.getBoolean( 89 com.android.internal.R.bool.config_keyboardVibrationSettingsSupported)) 90 .thenReturn(true); 91 92 assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE); 93 } 94 95 @Test getAvailabilityStatus_featureNotSupported_unavailable()96 public void getAvailabilityStatus_featureNotSupported_unavailable() { 97 when(mResources.getBoolean( 98 com.android.internal.R.bool.config_keyboardVibrationSettingsSupported)) 99 .thenReturn(false); 100 101 assertThat(mController.getAvailabilityStatus()).isEqualTo(UNSUPPORTED_ON_DEVICE); 102 } 103 104 105 @Test updateState_mainVibrateDisabled_shouldReturnFalseForCheckedAndEnabled()106 public void updateState_mainVibrateDisabled_shouldReturnFalseForCheckedAndEnabled() { 107 updateSystemSetting(VibrationPreferenceConfig.MAIN_SWITCH_SETTING_KEY, OFF); 108 109 mController.updateState(mPreference); 110 111 assertThat(mPreference.isEnabled()).isFalse(); 112 assertThat(mPreference.isChecked()).isFalse(); 113 } 114 115 @Test updateState_mainVibrateEnabled_shouldReturnTrueForEnabled()116 public void updateState_mainVibrateEnabled_shouldReturnTrueForEnabled() { 117 updateSystemSetting(VibrationPreferenceConfig.MAIN_SWITCH_SETTING_KEY, ON); 118 119 mController.updateState(mPreference); 120 121 assertThat(mPreference.isEnabled()).isTrue(); 122 } 123 124 @Test isChecked_keyboardVibrateEnabled_shouldReturnTrue()125 public void isChecked_keyboardVibrateEnabled_shouldReturnTrue() { 126 updateSystemSetting(VibrationPreferenceConfig.MAIN_SWITCH_SETTING_KEY, ON); 127 updateSystemSetting(Settings.System.KEYBOARD_VIBRATION_ENABLED, ON); 128 129 mController.updateState(mPreference); 130 131 assertThat(mPreference.isChecked()).isTrue(); 132 } 133 134 @Test isChecked_keyboardVibrateDisabled_shouldReturnFalse()135 public void isChecked_keyboardVibrateDisabled_shouldReturnFalse() { 136 updateSystemSetting(VibrationPreferenceConfig.MAIN_SWITCH_SETTING_KEY, ON); 137 updateSystemSetting(Settings.System.KEYBOARD_VIBRATION_ENABLED, OFF); 138 139 mController.updateState(mPreference); 140 141 assertThat(mPreference.isChecked()).isFalse(); 142 } 143 144 @Test setChecked_checked_updateSettings()145 public void setChecked_checked_updateSettings() throws Settings.SettingNotFoundException { 146 // set an off state initially 147 updateSystemSetting(Settings.System.KEYBOARD_VIBRATION_ENABLED, OFF); 148 149 assertThat(readSystemSetting(Settings.System.KEYBOARD_VIBRATION_ENABLED)).isEqualTo(OFF); 150 151 mController.setChecked(true); 152 153 assertThat(readSystemSetting(Settings.System.KEYBOARD_VIBRATION_ENABLED)).isEqualTo(ON); 154 verify(mFeatureFactory.metricsFeatureProvider).action(any(), 155 eq(SettingsEnums.ACTION_KEYBOARD_VIBRATION_CHANGED), eq(true)); 156 } 157 158 @Test setChecked_unchecked_updateSettings()159 public void setChecked_unchecked_updateSettings() throws Settings.SettingNotFoundException { 160 // set an on state initially 161 updateSystemSetting(Settings.System.KEYBOARD_VIBRATION_ENABLED, ON); 162 163 assertThat(readSystemSetting(Settings.System.KEYBOARD_VIBRATION_ENABLED)).isEqualTo(ON); 164 165 mController.setChecked(false); 166 167 assertThat(readSystemSetting(Settings.System.KEYBOARD_VIBRATION_ENABLED)).isEqualTo(OFF); 168 verify(mFeatureFactory.metricsFeatureProvider).action(any(), 169 eq(SettingsEnums.ACTION_KEYBOARD_VIBRATION_CHANGED), eq(false)); 170 } 171 updateSystemSetting(String key, int value)172 private void updateSystemSetting(String key, int value) { 173 Settings.System.putInt(mContext.getContentResolver(), key, value); 174 } 175 readSystemSetting(String key)176 private int readSystemSetting(String key) throws Settings.SettingNotFoundException { 177 return Settings.System.getInt(mContext.getContentResolver(), key); 178 } 179 } 180