[Catalyst] Update RemoveAnimationsPreference

Bug: 373451690
Flag: com.android.settings.flags.catalyst_accessibility_color_and_motion
Test: atest & devtool
Change-Id: Id648febf32bebb391f1277e28f58ddb0d5130d59
This commit is contained in:
Jacky Wang
2024-11-23 06:24:13 +08:00
parent 851bf18783
commit 8e73d0f51e
4 changed files with 61 additions and 49 deletions

View File

@@ -20,10 +20,11 @@ import android.content.Context
import androidx.preference.SwitchPreferenceCompat
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.android.settings.R
import com.android.settings.accessibility.RemoveAnimationsPreference.Companion.ANIMATION_ON_VALUE
import com.android.settings.accessibility.RemoveAnimationsPreference.Companion.TOGGLE_ANIMATION_KEYS
import com.android.settingslib.datastore.SettingsGlobalStore
import com.android.settingslib.preference.createAndBindWidget
import com.android.settingslib.preference.PreferenceScreenBindingHelper
import com.android.settingslib.preference.PreferenceScreenFactory
import com.google.common.truth.Truth.assertThat
import org.junit.Test
import org.junit.runner.RunWith
@@ -33,53 +34,59 @@ class RemoveAnimationsPreferenceTest {
private val appContext: Context = ApplicationProvider.getApplicationContext()
private val removeAnimationsPreference =
RemoveAnimationsPreference()
private fun getSwitchPreferenceCompat(): SwitchPreferenceCompat =
removeAnimationsPreference.createAndBindWidget(appContext)
private fun getRemoveAnimationsSwitchPreference(): SwitchPreferenceCompat =
PreferenceScreenFactory(appContext).let {
val preferenceScreen = it.inflate(R.xml.accessibility_color_and_motion)!!
it.preferenceManager.setPreferences(preferenceScreen)
PreferenceScreenBindingHelper.bind(preferenceScreen)
preferenceScreen.findPreference(RemoveAnimationsPreference.KEY)!!
}
@Test
fun animationOff_switchPreferenceIsChecked() {
RemoveAnimationsPreference.setAnimationEnabled(appContext, false)
assertThat(getSwitchPreferenceCompat().isChecked).isTrue()
assertThat(getRemoveAnimationsSwitchPreference().isChecked).isTrue()
}
@Test
fun animationOn_switchPreferenceIsNotChecked() {
RemoveAnimationsPreference.setAnimationEnabled(appContext, true)
assertThat(getSwitchPreferenceCompat().isChecked).isFalse()
assertThat(getRemoveAnimationsSwitchPreference().isChecked).isFalse()
}
@Test
fun oneAnimationValueOn_switchPreferenceIsNotChecked() {
// Animation is disabled, except for one value.
RemoveAnimationsPreference.setAnimationEnabled(appContext, false)
SettingsGlobalStore.get(appContext).setFloat(TOGGLE_ANIMATION_KEYS[0], ANIMATION_ON_VALUE)
SettingsGlobalStore.get(appContext)
.setFloat(RemoveAnimationsPreference.getAnimationKeys()[0], ANIMATION_ON_VALUE)
assertThat(getSwitchPreferenceCompat().isChecked).isFalse()
assertThat(getRemoveAnimationsSwitchPreference().isChecked).isFalse()
}
@Test
fun storageSetTrue_turnsOffAnimation() {
fun toggleOnSwitch_turnsOffAnimation() {
RemoveAnimationsPreference.setAnimationEnabled(appContext, true)
storageSetValue(true)
val switchPreference = getRemoveAnimationsSwitchPreference()
assertThat(switchPreference.isChecked).isFalse()
switchPreference.performClick()
assertThat(switchPreference.isChecked).isTrue()
assertThat(RemoveAnimationsPreference.isAnimationEnabled(appContext)).isFalse()
}
@Test
fun storageSetFalse_turnsOnAnimation() {
fun toggleOffSwitch_turnsOnAnimation() {
RemoveAnimationsPreference.setAnimationEnabled(appContext, false)
storageSetValue(false)
val switchPreference = getRemoveAnimationsSwitchPreference()
assertThat(switchPreference.isChecked).isTrue()
switchPreference.performClick()
assertThat(switchPreference.isChecked).isFalse()
assertThat(RemoveAnimationsPreference.isAnimationEnabled(appContext)).isTrue()
}
private fun storageSetValue(enabled: Boolean) = removeAnimationsPreference.storage(appContext)
.setValue(RemoveAnimationsPreference.KEY, Boolean::class.javaObjectType, enabled)
}
}