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