Wait for UI changes.

The car mode change broadcast and subsequent UI changes don't always
happen quickly, so wait for the UI change to know when the broadcast has
been processed.

Bug: 151199750
Test: make cts -j 64 && cts-tradefed run cts-on-gsi -m CtsBatterySavingTestCases -t android.os.cts.batterysaving.BatterySaverTest#testCarModeExceptions
Change-Id: I760a44c7cfaf2d2ebaaf8dbaebbdb040b1dc0e74
diff --git a/tests/tests/batterysaving/src/android/os/cts/batterysaving/BatterySaverTest.java b/tests/tests/batterysaving/src/android/os/cts/batterysaving/BatterySaverTest.java
index afae17e..56fce94 100644
--- a/tests/tests/batterysaving/src/android/os/cts/batterysaving/BatterySaverTest.java
+++ b/tests/tests/batterysaving/src/android/os/cts/batterysaving/BatterySaverTest.java
@@ -17,6 +17,7 @@
 
 import static com.android.compatibility.common.util.BatteryUtils.enableBatterySaver;
 import static com.android.compatibility.common.util.BatteryUtils.runDumpsysBatteryUnplug;
+import static com.android.compatibility.common.util.TestUtils.waitUntil;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
@@ -112,36 +113,38 @@
 
             enableBatterySaver(true);
 
-            // Allow time for UI change.
-            Thread.sleep(1000);
             assertTrue(powerManager.isPowerSaveMode());
             assertEquals(PowerManager.LOCATION_MODE_ALL_DISABLED_WHEN_SCREEN_OFF,
                     powerManager.getLocationPowerSaveMode());
-            assertEquals(Configuration.UI_MODE_NIGHT_YES,
-                    getContext().getResources().getConfiguration().uiMode
-                        & Configuration.UI_MODE_NIGHT_MASK);
+            // UI change can take a while to propagate, so need to wait for this check.
+            waitUntil("UI mode didn't change to " + Configuration.UI_MODE_NIGHT_YES,
+                    () -> Configuration.UI_MODE_NIGHT_YES ==
+                            (getContext().getResources().getConfiguration().uiMode
+                                    & Configuration.UI_MODE_NIGHT_MASK));
 
             uiModeManager.enableCarMode(0);
-            // Allow time for UI change.
-            Thread.sleep(1000);
 
+            // Wait for UI change first before checking location mode since we can then be
+            // confident that the broadcast has been processed.
+            waitUntil("UI mode didn't change to " + Configuration.UI_MODE_NIGHT_NO,
+                    () -> Configuration.UI_MODE_NIGHT_NO ==
+                            (getContext().getResources().getConfiguration().uiMode
+                                    & Configuration.UI_MODE_NIGHT_MASK));
             final int locationPowerSaveMode = powerManager.getLocationPowerSaveMode();
             assertTrue("Location power save mode didn't change from " + locationPowerSaveMode,
                     locationPowerSaveMode == PowerManager.LOCATION_MODE_FOREGROUND_ONLY
                             || locationPowerSaveMode == PowerManager.LOCATION_MODE_NO_CHANGE);
-            assertEquals(Configuration.UI_MODE_NIGHT_NO,
-                getContext().getResources().getConfiguration().uiMode
-                    & Configuration.UI_MODE_NIGHT_MASK);
 
             uiModeManager.disableCarMode(0);
-            // Allow time for UI change.
-            Thread.sleep(1000);
 
+            // Wait for UI change first before checking location mode since we can then be
+            // confident that the broadcast has been processed.
+            waitUntil("UI mode didn't change to " + Configuration.UI_MODE_NIGHT_YES,
+                    () -> Configuration.UI_MODE_NIGHT_YES ==
+                            (getContext().getResources().getConfiguration().uiMode
+                                    & Configuration.UI_MODE_NIGHT_MASK));
             assertEquals(PowerManager.LOCATION_MODE_ALL_DISABLED_WHEN_SCREEN_OFF,
                 powerManager.getLocationPowerSaveMode());
-            assertEquals(Configuration.UI_MODE_NIGHT_YES,
-                getContext().getResources().getConfiguration().uiMode
-                    & Configuration.UI_MODE_NIGHT_MASK);
         } finally {
             uiModeManager.disableCarMode(0);
             SettingsUtils.delete(SettingsUtils.NAMESPACE_GLOBAL, "battery_saver_constants");