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 @RunWith(RobolectricTestRunner.class) 45 public class VibrationMainSwitchPreferenceControllerTest { 46 47 private static final String PREFERENCE_KEY = "preference_key"; 48 49 @Mock private PreferenceScreen mScreen; 50 51 private Lifecycle mLifecycle; 52 private Context mContext; 53 private VibrationMainSwitchPreferenceController mController; 54 private MainSwitchPreference mPreference; 55 56 @Before setUp()57 public void setUp() { 58 MockitoAnnotations.initMocks(this); 59 mLifecycle = new Lifecycle(() -> mLifecycle); 60 mContext = ApplicationProvider.getApplicationContext(); 61 mController = new VibrationMainSwitchPreferenceController(mContext, PREFERENCE_KEY); 62 mLifecycle.addObserver(mController); 63 mPreference = new MainSwitchPreference(mContext); 64 mPreference.setTitle("Test title"); 65 when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference); 66 mController.displayPreference(mScreen); 67 } 68 69 @Test verifyConstants()70 public void verifyConstants() { 71 assertThat(mController.getPreferenceKey()).isEqualTo(PREFERENCE_KEY); 72 assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE); 73 } 74 75 @Test updateState_shouldReturnTheSettingState()76 public void updateState_shouldReturnTheSettingState() { 77 updateSetting(Settings.System.VIBRATE_ON, ON); 78 mController.updateState(mPreference); 79 assertThat(mPreference.isChecked()).isTrue(); 80 81 updateSetting(Settings.System.VIBRATE_ON, OFF); 82 mController.updateState(mPreference); 83 assertThat(mPreference.isChecked()).isFalse(); 84 } 85 86 @Test setChecked_updatesSetting()87 public void setChecked_updatesSetting() throws Settings.SettingNotFoundException { 88 updateSetting(Settings.System.VIBRATE_ON, OFF); 89 mController.updateState(mPreference); 90 assertThat(mPreference.isChecked()).isFalse(); 91 92 mController.setChecked(true); 93 assertThat(readSetting(Settings.System.VIBRATE_ON)).isEqualTo(ON); 94 95 mController.setChecked(false); 96 assertThat(readSetting(Settings.System.VIBRATE_ON)).isEqualTo(OFF); 97 } 98 updateSetting(String key, int value)99 private void updateSetting(String key, int value) { 100 Settings.System.putInt(mContext.getContentResolver(), key, value); 101 } 102 readSetting(String settingKey)103 private int readSetting(String settingKey) throws Settings.SettingNotFoundException { 104 return Settings.System.getInt(mContext.getContentResolver(), settingKey); 105 } 106 } 107