From 3b4d13eafe835579af943a6d2a9e3947316ea7ca Mon Sep 17 00:00:00 2001 From: Tyler Freeman Date: Tue, 16 May 2023 17:54:19 +0000 Subject: [PATCH] fix(non linear font scaling): fix activity test flakiness Always wait for the test activity to pick up the configuration change before starting the test. Fix: 281854884 Test: atest FrameworksCoreTests:android.content.res.FontScaleConverterActivityTest Change-Id: I94e06374833222e1540a26e7e855a207199eea87 --- .../res/FontScaleConverterActivityTest.java | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/core/tests/coretests/src/android/content/res/FontScaleConverterActivityTest.java b/core/tests/coretests/src/android/content/res/FontScaleConverterActivityTest.java index 6d99e94a3656b..6e8e93a8c86b7 100644 --- a/core/tests/coretests/src/android/content/res/FontScaleConverterActivityTest.java +++ b/core/tests/coretests/src/android/content/res/FontScaleConverterActivityTest.java @@ -50,6 +50,7 @@ import org.junit.Test; import org.junit.rules.TestRule; import org.junit.runner.RunWith; +import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicReference; /** @@ -127,7 +128,7 @@ public class FontScaleConverterActivityTest { }); } - private static void setSystemFontScale(float fontScale) { + private void setSystemFontScale(float fontScale) { ShellIdentityUtils.invokeWithShellPermissions(() -> { Settings.System.putFloat( InstrumentationRegistry.getInstrumentation().getContext().getContentResolver(), @@ -136,14 +137,22 @@ public class FontScaleConverterActivityTest { ); }); - PollingCheck.waitFor(/* timeout= */ 5000, () -> - InstrumentationRegistry + PollingCheck.waitFor(/* timeout= */ 5000, () -> { + AtomicBoolean isActivityAtCorrectScale = new AtomicBoolean(false); + rule.getScenario().onActivity(activity -> + isActivityAtCorrectScale.set( + activity.getResources() + .getConfiguration() + .fontScale == fontScale + ) + ); + return isActivityAtCorrectScale.get() && InstrumentationRegistry .getInstrumentation() .getContext() .getResources() .getConfiguration() - .fontScale == fontScale - ); + .fontScale == fontScale; + }); } private static void restoreSystemFontScaleToDefault() {