From 4fbc3437b77c246ee9e61b349b256969e9829951 Mon Sep 17 00:00:00 2001 From: Tyler Freeman Date: Fri, 12 May 2023 01:35:09 +0000 Subject: [PATCH] fix(non linear font scaling): restore font scale to default instead of original The original is unreliable and often undefined, which means we don't restore the proper font scale when it returns MIN_VALUE. Fix: 281854884 Test: atest FrameworksCoreTests:android.content.res.FontScaleConverterActivityTest Change-Id: Ie5ae64e36f62d962dd36131fc7c1d70a512f7b6f --- .../res/FontScaleConverterActivityTest.java | 38 +++++++++++-------- 1 file changed, 23 insertions(+), 15 deletions(-) diff --git a/core/tests/coretests/src/android/content/res/FontScaleConverterActivityTest.java b/core/tests/coretests/src/android/content/res/FontScaleConverterActivityTest.java index c6bb07b17fd4a..6d99e94a3656b 100644 --- a/core/tests/coretests/src/android/content/res/FontScaleConverterActivityTest.java +++ b/core/tests/coretests/src/android/content/res/FontScaleConverterActivityTest.java @@ -45,7 +45,6 @@ import com.android.frameworks.coretests.R; import org.hamcrest.Description; import org.hamcrest.Matcher; import org.junit.After; -import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.TestRule; @@ -67,22 +66,9 @@ public class FontScaleConverterActivityTest { @Rule public TestRule compatChangeRule = new PlatformCompatChangeRule(); - private float mOriginalFontScale = Float.MIN_VALUE; - - @Before - public void setup() { - mOriginalFontScale = Settings.System.getFloat( - InstrumentationRegistry.getInstrumentation().getContext().getContentResolver(), - Settings.System.FONT_SCALE, - Float.MIN_VALUE - ); - } - @After public void teardown() { - if (mOriginalFontScale != Float.MIN_VALUE) { - setSystemFontScale(mOriginalFontScale); - } + restoreSystemFontScaleToDefault(); } @IwTest(focusArea = "accessibility") @@ -160,6 +146,28 @@ public class FontScaleConverterActivityTest { ); } + private static void restoreSystemFontScaleToDefault() { + ShellIdentityUtils.invokeWithShellPermissions(() -> { + // TODO(b/279083734): would use Settings.System.resetToDefaults() if it existed + Settings.System.putString( + InstrumentationRegistry.getInstrumentation() + .getContext() + .getContentResolver(), + Settings.System.FONT_SCALE, + null, + /* overrideableByRestore= */ true); + }); + + PollingCheck.waitFor( + /* timeout= */ 5000, + () -> InstrumentationRegistry.getInstrumentation() + .getContext() + .getResources() + .getConfiguration() + .fontScale == 1 + ); + } + private Matcher withTextSizeInRange(float sizeStartPx, float sizeEndPx) { return new BoundedMatcher<>(TextView.class) { private static final float TOLERANCE = 0.05f;