1 /* 2 * Copyright (C) 2024 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 package com.android.settings.accessibility 17 18 import android.content.Context 19 import android.provider.Settings.System.VIBRATE_ON 20 import androidx.test.core.app.ApplicationProvider 21 import androidx.test.ext.junit.runners.AndroidJUnit4 22 import com.android.settingslib.preference.createAndBindWidget 23 import com.android.settingslib.widget.MainSwitchPreference 24 import com.google.common.truth.Truth.assertThat 25 import org.junit.Test 26 import org.junit.runner.RunWith 27 28 // LINT.IfChange 29 @RunWith(AndroidJUnit4::class) 30 class VibrationMainSwitchPreferenceTest { 31 private val context: Context = ApplicationProvider.getApplicationContext() 32 private val preference = VibrationMainSwitchPreference() 33 34 @Test checked_valueUnset_returnDefaultTruenull35 fun checked_valueUnset_returnDefaultTrue() { 36 setVibrateOn(null) 37 38 assertThat(getMainSwitchPreference().isChecked).isTrue() 39 } 40 41 @Test checked_valueEnabled_returnTruenull42 fun checked_valueEnabled_returnTrue() { 43 setVibrateOn(true) 44 45 assertThat(getMainSwitchPreference().isChecked).isTrue() 46 } 47 48 @Test checked_valueDisabled_returnFalsenull49 fun checked_valueDisabled_returnFalse() { 50 setVibrateOn(false) 51 52 assertThat(getMainSwitchPreference().isChecked).isFalse() 53 } 54 55 @Test click_updatesCorrectlynull56 fun click_updatesCorrectly() { 57 setVibrateOn(null) 58 val widget = getMainSwitchPreference() 59 60 assertThat(widget.isChecked).isTrue() 61 62 widget.performClick() 63 64 assertThat(widget.isChecked).isFalse() 65 66 widget.performClick() 67 68 assertThat(widget.isChecked).isTrue() 69 } 70 getMainSwitchPreferencenull71 private fun getMainSwitchPreference(): MainSwitchPreference = 72 preference.createAndBindWidget(context) 73 74 private fun setVibrateOn(enabled: Boolean?) = 75 preference.storage(context).setBoolean(VIBRATE_ON, enabled) 76 } 77 // LINT.ThenChange(VibrationMainSwitchPreferenceControllerTest.java) 78