From ae3c6231e5dc5b7ee68f96ee769c141d6e63f8e2 Mon Sep 17 00:00:00 2001 From: Riddle Hsu Date: Thu, 30 Dec 2021 18:19:31 +0800 Subject: [PATCH] Fix test flakiness of SplashScreenExceptionListTest The listener map of DeviceConfig uses OnPropertiesChangedListener as key, so when iterating the map for dispatching change event, the order may not be the same as the order of adding the listener. Then the test may fail because the CountDownLatch counts before the listener of SplashScreenExceptionList is called. This change removes the additional test listener by overriding the method which needs to be waited until it is called, which also eliminates the dependency of listener callback order. Fixes: 212660691 Test: atest SplashScreenExceptionListTest Change-Id: I405932729d26f43428c70f5e282aeb8ae82d81c8 --- .../server/wm/SplashScreenExceptionList.java | 3 ++- .../wm/SplashScreenExceptionListTest.java | 20 ++++++++++++------- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/services/core/java/com/android/server/wm/SplashScreenExceptionList.java b/services/core/java/com/android/server/wm/SplashScreenExceptionList.java index e815a0e2682a9..9ca49fe9557e5 100644 --- a/services/core/java/com/android/server/wm/SplashScreenExceptionList.java +++ b/services/core/java/com/android/server/wm/SplashScreenExceptionList.java @@ -64,7 +64,8 @@ class SplashScreenExceptionList { mOnPropertiesChangedListener); } - private void updateDeviceConfig(String values) { + @VisibleForTesting + void updateDeviceConfig(String values) { parseDeviceConfigPackageList(values); } diff --git a/services/tests/wmtests/src/com/android/server/wm/SplashScreenExceptionListTest.java b/services/tests/wmtests/src/com/android/server/wm/SplashScreenExceptionListTest.java index 3714d9984a0c4..f5d915dee2574 100644 --- a/services/tests/wmtests/src/com/android/server/wm/SplashScreenExceptionListTest.java +++ b/services/tests/wmtests/src/com/android/server/wm/SplashScreenExceptionListTest.java @@ -39,6 +39,7 @@ import org.junit.Test; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; +import java.util.function.Consumer; /** * Test for the splash screen exception list @@ -55,7 +56,16 @@ public class SplashScreenExceptionListTest { private DeviceConfig.Properties mInitialWindowManagerProperties; private final HandlerExecutor mExecutor = new HandlerExecutor( new Handler(Looper.getMainLooper())); - private final SplashScreenExceptionList mList = new SplashScreenExceptionList(mExecutor); + private final SplashScreenExceptionList mList = new SplashScreenExceptionList(mExecutor) { + @Override + void updateDeviceConfig(String rawList) { + super.updateDeviceConfig(rawList); + if (mOnUpdateDeviceConfig != null) { + mOnUpdateDeviceConfig.accept(rawList); + } + } + }; + private Consumer mOnUpdateDeviceConfig; @Before public void setUp() throws Exception { @@ -91,13 +101,11 @@ public class SplashScreenExceptionListTest { private void setExceptionListAndWaitForCallback(String commaSeparatedList) { CountDownLatch latch = new CountDownLatch(1); - DeviceConfig.OnPropertiesChangedListener onPropertiesChangedListener = (p) -> { - if (commaSeparatedList.equals(p.getString(KEY_SPLASH_SCREEN_EXCEPTION_LIST, null))) { + mOnUpdateDeviceConfig = rawList -> { + if (commaSeparatedList.equals(rawList)) { latch.countDown(); } }; - DeviceConfig.addOnPropertiesChangedListener(DeviceConfig.NAMESPACE_WINDOW_MANAGER, - mExecutor, onPropertiesChangedListener); DeviceConfig.setProperty(DeviceConfig.NAMESPACE_WINDOW_MANAGER, KEY_SPLASH_SCREEN_EXCEPTION_LIST, commaSeparatedList, false); try { @@ -105,8 +113,6 @@ public class SplashScreenExceptionListTest { latch.await(1, TimeUnit.SECONDS)); } catch (InterruptedException e) { Assert.fail(e.getMessage()); - } finally { - DeviceConfig.removeOnPropertiesChangedListener(onPropertiesChangedListener); } }