[Catalyst] Extract datastore for VibrationMainSwitchPreference

NO_IFTTT=Catalyst only

Bug: 368360218
Flag: EXEMPT refactor
Test: atest
Change-Id: I3c308b0ed1462a10ddd0daf55f30996a23e747d7
This commit is contained in:
Jacky Wang
2025-03-07 00:25:32 +08:00
parent 8a333df6fa
commit 206d27d778
3 changed files with 42 additions and 36 deletions

View File

@@ -21,8 +21,8 @@ import android.content.res.Resources
import android.os.Vibrator
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.android.settings.flags.Flags
import com.android.settings.R
import com.android.settings.flags.Flags
import com.android.settingslib.preference.CatalystScreenTestCase
import com.google.common.truth.Truth.assertThat
import org.junit.Test
@@ -35,7 +35,7 @@ import org.mockito.kotlin.stub
// LINT.IfChange
@RunWith(AndroidJUnit4::class)
class VibrationIntensityScreenTest : CatalystScreenTestCase() {
private lateinit var vibrator: Vibrator
private lateinit var mockVibrator: Vibrator
private val resourcesSpy: Resources =
spy((ApplicationProvider.getApplicationContext() as Context).resources)
@@ -44,9 +44,10 @@ class VibrationIntensityScreenTest : CatalystScreenTestCase() {
object : ContextWrapper(ApplicationProvider.getApplicationContext()) {
override fun getSystemService(name: String): Any? =
when {
name == getSystemServiceName(Vibrator::class.java) -> vibrator
name == VIBRATOR_SERVICE -> mockVibrator
else -> super.getSystemService(name)
}
override fun getResources(): Resources = resourcesSpy
}
@@ -62,7 +63,7 @@ class VibrationIntensityScreenTest : CatalystScreenTestCase() {
@Test
fun isAvailable_noVibrator_unavailable() {
vibrator = mock { on { hasVibrator() } doReturn false }
mockVibrator = mock { on { hasVibrator() } doReturn false }
resourcesSpy.stub {
on { getInteger(R.integer.config_vibration_supported_intensity_levels) } doReturn 3
}
@@ -71,7 +72,7 @@ class VibrationIntensityScreenTest : CatalystScreenTestCase() {
@Test
fun isAvailable_hasVibratorAndSingleIntensityLevel_unavailable() {
vibrator = mock { on { hasVibrator() } doReturn true }
mockVibrator = mock { on { hasVibrator() } doReturn true }
resourcesSpy.stub {
on { getInteger(R.integer.config_vibration_supported_intensity_levels) } doReturn 1
}
@@ -80,7 +81,7 @@ class VibrationIntensityScreenTest : CatalystScreenTestCase() {
@Test
fun isAvailable_hasVibratorAndMultipleIntensityLevels_available() {
vibrator = mock { on { hasVibrator() } doReturn true }
mockVibrator = mock { on { hasVibrator() } doReturn true }
resourcesSpy.stub {
on { getInteger(R.integer.config_vibration_supported_intensity_levels) } doReturn 2
}
@@ -88,4 +89,3 @@ class VibrationIntensityScreenTest : CatalystScreenTestCase() {
}
}
// LINT.ThenChange(VibrationPreferenceControllerTest.java)

View File

@@ -21,8 +21,8 @@ import android.content.res.Resources
import android.os.Vibrator
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.android.settings.flags.Flags
import com.android.settings.R
import com.android.settings.flags.Flags
import com.android.settingslib.preference.CatalystScreenTestCase
import com.google.common.truth.Truth.assertThat
import org.junit.Test
@@ -35,7 +35,7 @@ import org.mockito.kotlin.stub
// LINT.IfChange
@RunWith(AndroidJUnit4::class)
class VibrationScreenTest : CatalystScreenTestCase() {
private lateinit var vibrator: Vibrator
private lateinit var mockVibrator: Vibrator
private val resourcesSpy: Resources =
spy((ApplicationProvider.getApplicationContext() as Context).resources)
@@ -44,9 +44,10 @@ class VibrationScreenTest : CatalystScreenTestCase() {
object : ContextWrapper(ApplicationProvider.getApplicationContext()) {
override fun getSystemService(name: String): Any? =
when {
name == getSystemServiceName(Vibrator::class.java) -> vibrator
name == VIBRATOR_SERVICE -> mockVibrator
else -> super.getSystemService(name)
}
override fun getResources(): Resources = resourcesSpy
}
@@ -62,7 +63,7 @@ class VibrationScreenTest : CatalystScreenTestCase() {
@Test
fun isAvailable_noVibrator_unavailable() {
vibrator = mock { on { hasVibrator() } doReturn false }
mockVibrator = mock { on { hasVibrator() } doReturn false }
resourcesSpy.stub {
on { getInteger(R.integer.config_vibration_supported_intensity_levels) } doReturn 1
}
@@ -71,7 +72,7 @@ class VibrationScreenTest : CatalystScreenTestCase() {
@Test
fun isAvailable_hasVibratorAndMultipleIntensityLevels_unavailable() {
vibrator = mock { on { hasVibrator() } doReturn true }
mockVibrator = mock { on { hasVibrator() } doReturn true }
resourcesSpy.stub {
on { getInteger(R.integer.config_vibration_supported_intensity_levels) } doReturn 3
}
@@ -80,7 +81,7 @@ class VibrationScreenTest : CatalystScreenTestCase() {
@Test
fun isAvailable_hasVibratorAndSingleIntensityLevel_available() {
vibrator = mock { on { hasVibrator() } doReturn true }
mockVibrator = mock { on { hasVibrator() } doReturn true }
resourcesSpy.stub {
on { getInteger(R.integer.config_vibration_supported_intensity_levels) } doReturn 1
}