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.content.ContextWrapper 20 import android.content.res.Resources 21 import android.os.Vibrator 22 import androidx.test.core.app.ApplicationProvider 23 import androidx.test.ext.junit.runners.AndroidJUnit4 24 import com.android.settings.R 25 import com.android.settings.flags.Flags 26 import com.android.settingslib.preference.CatalystScreenTestCase 27 import com.google.common.truth.Truth.assertThat 28 import org.junit.Test 29 import org.junit.runner.RunWith 30 import org.mockito.kotlin.doReturn 31 import org.mockito.kotlin.mock 32 import org.mockito.kotlin.spy 33 import org.mockito.kotlin.stub 34 35 // LINT.IfChange 36 @RunWith(AndroidJUnit4::class) 37 class VibrationIntensityScreenTest : CatalystScreenTestCase() { 38 private lateinit var mockVibrator: Vibrator 39 40 private val resourcesSpy: Resources = 41 spy((ApplicationProvider.getApplicationContext() as Context).resources) 42 43 private val context: Context = 44 object : ContextWrapper(ApplicationProvider.getApplicationContext()) { getSystemServicenull45 override fun getSystemService(name: String): Any? = 46 when { 47 name == VIBRATOR_SERVICE -> mockVibrator 48 else -> super.getSystemService(name) 49 } 50 getResourcesnull51 override fun getResources(): Resources = resourcesSpy 52 } 53 54 override val preferenceScreenCreator = VibrationIntensityScreen() 55 56 override val flagName: String 57 get() = Flags.FLAG_CATALYST_VIBRATION_INTENSITY_SCREEN 58 59 @Test 60 fun key() { 61 assertThat(preferenceScreenCreator.key).isEqualTo(VibrationIntensityScreen.KEY) 62 } 63 64 @Test isAvailable_noVibrator_unavailablenull65 fun isAvailable_noVibrator_unavailable() { 66 mockVibrator = mock { on { hasVibrator() } doReturn false } 67 resourcesSpy.stub { 68 on { getInteger(R.integer.config_vibration_supported_intensity_levels) } doReturn 3 69 } 70 assertThat(preferenceScreenCreator.isAvailable(context)).isFalse() 71 } 72 73 @Test isAvailable_hasVibratorAndSingleIntensityLevel_unavailablenull74 fun isAvailable_hasVibratorAndSingleIntensityLevel_unavailable() { 75 mockVibrator = mock { on { hasVibrator() } doReturn true } 76 resourcesSpy.stub { 77 on { getInteger(R.integer.config_vibration_supported_intensity_levels) } doReturn 1 78 } 79 assertThat(preferenceScreenCreator.isAvailable(context)).isFalse() 80 } 81 82 @Test isAvailable_hasVibratorAndMultipleIntensityLevels_availablenull83 fun isAvailable_hasVibratorAndMultipleIntensityLevels_available() { 84 mockVibrator = mock { on { hasVibrator() } doReturn true } 85 resourcesSpy.stub { 86 on { getInteger(R.integer.config_vibration_supported_intensity_levels) } doReturn 2 87 } 88 assertThat(preferenceScreenCreator.isAvailable(context)).isTrue() 89 } 90 } 91 // LINT.ThenChange(VibrationPreferenceControllerTest.java) 92