Fix incorrect remove animation switch status after turned off Talkback

Root cause: The ag/3373822 introduce the comparison by string since 2017, but it would conflict with AccessibilityService#setAnimationScale(float) which is  added by API level 33.
Solution: Use float comparison instead and observe the key changed to update status if the setting page is on foreground.

Bug: 244687190
Test: atest DisableAnimationsPreferenceControllerTest
Change-Id: I59b7edb70a27af4276eccd6f19d2ef46ba30b604
This commit is contained in:
menghanli
2022-09-05 12:04:53 +08:00
parent d7d1d9451b
commit 8541423080
2 changed files with 122 additions and 28 deletions

View File

@@ -23,8 +23,11 @@ import static com.android.settings.accessibility.DisableAnimationsPreferenceCont
import static com.google.common.truth.Truth.assertThat;
import android.content.Context;
import android.os.Looper;
import android.provider.Settings;
import androidx.preference.PreferenceManager;
import androidx.preference.PreferenceScreen;
import androidx.preference.SwitchPreference;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
@@ -38,15 +41,27 @@ import org.junit.runner.RunWith;
@RunWith(AndroidJUnit4.class)
public class DisableAnimationsPreferenceControllerTest {
private Context mContext;
private static final String TEST_PREFERENCE_KEY = "disable_animation";
private final Context mContext = ApplicationProvider.getApplicationContext();
private PreferenceScreen mScreen;
private SwitchPreference mPreference;
private DisableAnimationsPreferenceController mController;
@Before
public void setUp() {
mContext = ApplicationProvider.getApplicationContext();
mPreference = new SwitchPreference(mContext);
mController = new DisableAnimationsPreferenceController(mContext, "disable_animation");
if (Looper.myLooper() == null) {
Looper.prepare();
}
PreferenceManager preferenceManager = new PreferenceManager(mContext);
mScreen = preferenceManager.createPreferenceScreen(mContext);
final SwitchPreference preference = new SwitchPreference(mContext);
preference.setKey(TEST_PREFERENCE_KEY);
mScreen.addPreference(preference);
mController = new DisableAnimationsPreferenceController(mContext, TEST_PREFERENCE_KEY);
mController.displayPreference(mScreen);
mPreference = mScreen.findPreference(TEST_PREFERENCE_KEY);
}
@Test
@@ -57,10 +72,7 @@ public class DisableAnimationsPreferenceControllerTest {
@Test
public void isChecked_enabledAnimation_shouldReturnFalse() {
for (String animationPreference : TOGGLE_ANIMATION_TARGETS) {
Settings.Global.putString(mContext.getContentResolver(), animationPreference,
ANIMATION_ON_VALUE);
}
setAnimationScale(ANIMATION_ON_VALUE);
mController.updateState(mPreference);
@@ -70,10 +82,7 @@ public class DisableAnimationsPreferenceControllerTest {
@Test
public void isChecked_disabledAnimation_shouldReturnTrue() {
for (String animationPreference : TOGGLE_ANIMATION_TARGETS) {
Settings.Global.putString(mContext.getContentResolver(), animationPreference,
ANIMATION_OFF_VALUE);
}
setAnimationScale(ANIMATION_OFF_VALUE);
mController.updateState(mPreference);
@@ -86,8 +95,9 @@ public class DisableAnimationsPreferenceControllerTest {
mController.setChecked(true);
for (String animationSetting : TOGGLE_ANIMATION_TARGETS) {
assertThat(Settings.Global.getString(mContext.getContentResolver(), animationSetting))
.isEqualTo(ANIMATION_OFF_VALUE);
final float value = Settings.Global.getFloat(mContext.getContentResolver(),
animationSetting, /* def= */ -1.0f);
assertThat(Float.compare(value, ANIMATION_OFF_VALUE)).isEqualTo(0);
}
}
@@ -96,8 +106,47 @@ public class DisableAnimationsPreferenceControllerTest {
mController.setChecked(false);
for (String animationSetting : TOGGLE_ANIMATION_TARGETS) {
assertThat(Settings.Global.getString(mContext.getContentResolver(), animationSetting))
.isEqualTo(ANIMATION_ON_VALUE);
final float value = Settings.Global.getFloat(mContext.getContentResolver(),
animationSetting, /* def= */ -1.0f);
assertThat(Float.compare(value, ANIMATION_ON_VALUE)).isEqualTo(0);
}
}
}
@Test
public void onStart_enabledAnimation_shouldReturnFalse() {
mController.onStart();
setAnimationScale(ANIMATION_ON_VALUE);
assertThat(mController.isChecked()).isFalse();
assertThat(mPreference.isChecked()).isFalse();
}
@Test
public void onStart_disabledAnimation_shouldReturnTrue() {
mController.onStart();
setAnimationScale(ANIMATION_OFF_VALUE);
assertThat(mController.isChecked()).isTrue();
assertThat(mPreference.isChecked()).isTrue();
}
@Test
public void onStop_shouldNotUpdateTargets() {
mPreference.setChecked(true);
mController.onStart();
mController.onStop();
setAnimationScale(ANIMATION_ON_VALUE);
assertThat(mPreference.isChecked()).isTrue();
}
private void setAnimationScale(float newValue) {
for (String animationPreference : TOGGLE_ANIMATION_TARGETS) {
Settings.Global.putFloat(mContext.getContentResolver(), animationPreference,
newValue);
}
}
}