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.accessibility.AccessibilityUtil.State.OFF; 20 import static com.android.settings.accessibility.AccessibilityUtil.State.ON; 21 import static com.android.settings.core.BasePreferenceController.AVAILABLE; 22 23 import static com.google.common.truth.Truth.assertThat; 24 25 import static org.mockito.Mockito.when; 26 27 import android.content.Context; 28 import android.provider.Settings; 29 30 import androidx.preference.PreferenceScreen; 31 import androidx.test.core.app.ApplicationProvider; 32 33 import com.android.settingslib.core.lifecycle.Lifecycle; 34 import com.android.settingslib.widget.MainSwitchPreference; 35 36 import org.junit.Before; 37 import org.junit.Test; 38 import org.junit.runner.RunWith; 39 import org.mockito.Mock; 40 import org.mockito.MockitoAnnotations; 41 import org.robolectric.RobolectricTestRunner; 42 43 /** Tests for {@link VibrationMainSwitchPreferenceController}. */ 44 // LINT.IfChange 45 @RunWith(RobolectricTestRunner.class) 46 public class VibrationMainSwitchPreferenceControllerTest { 47 48 private static final String PREFERENCE_KEY = "preference_key"; 49 50 @Mock private PreferenceScreen mScreen; 51 52 private Lifecycle mLifecycle; 53 private Context mContext; 54 private VibrationMainSwitchPreferenceController mController; 55 private MainSwitchPreference mPreference; 56 57 @Before setUp()58 public void setUp() { 59 MockitoAnnotations.initMocks(this); 60 mLifecycle = new Lifecycle(() -> mLifecycle); 61 mContext = ApplicationProvider.getApplicationContext(); 62 mController = new VibrationMainSwitchPreferenceController(mContext, PREFERENCE_KEY); 63 mLifecycle.addObserver(mController); 64 mPreference = new MainSwitchPreference(mContext); 65 mPreference.setTitle("Test title"); 66 when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference); 67 mController.displayPreference(mScreen); 68 } 69 70 @Test verifyConstants()71 public void verifyConstants() { 72 assertThat(mController.getPreferenceKey()).isEqualTo(PREFERENCE_KEY); 73 assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE); 74 } 75 76 @Test updateState_shouldReturnTheSettingState()77 public void updateState_shouldReturnTheSettingState() { 78 updateSetting(Settings.System.VIBRATE_ON, ON); 79 mController.updateState(mPreference); 80 assertThat(mPreference.isChecked()).isTrue(); 81 82 updateSetting(Settings.System.VIBRATE_ON, OFF); 83 mController.updateState(mPreference); 84 assertThat(mPreference.isChecked()).isFalse(); 85 } 86 87 @Test setChecked_updatesSetting()88 public void setChecked_updatesSetting() throws Settings.SettingNotFoundException { 89 updateSetting(Settings.System.VIBRATE_ON, OFF); 90 mController.updateState(mPreference); 91 assertThat(mPreference.isChecked()).isFalse(); 92 93 mController.setChecked(true); 94 assertThat(readSetting(Settings.System.VIBRATE_ON)).isEqualTo(ON); 95 96 mController.setChecked(false); 97 assertThat(readSetting(Settings.System.VIBRATE_ON)).isEqualTo(OFF); 98 } 99 updateSetting(String key, int value)100 private void updateSetting(String key, int value) { 101 Settings.System.putInt(mContext.getContentResolver(), key, value); 102 } 103 readSetting(String settingKey)104 private int readSetting(String settingKey) throws Settings.SettingNotFoundException { 105 return Settings.System.getInt(mContext.getContentResolver(), settingKey); 106 } 107 } 108 // LINT.ThenChange(VibrationMainSwitchPreferenceTest.kt) 109