diff options
| author | 2024-12-04 21:11:04 +0000 | |
|---|---|---|
| committer | 2024-12-05 13:14:02 +0000 | |
| commit | c353915df01880a7c5ea5a8049b849488b04ba4c (patch) | |
| tree | 0647f125f3859e992979ccc502a5e058fb3c2472 | |
| parent | 57f1b334c89943e3a918b13514c51e371be00b76 (diff) | |
Fix Flaky UiModeManagerServiceTest
Ensure execution order by replacing Map for LinkedHashMap.
Change-Id: Ice300de201509f968b416cf9f79933ecad235957
Test: UiModeManagerServiceTest
Bug: 382042877
Flag: EXEMPT test fix
| -rw-r--r-- | services/tests/uiservicestests/src/com/android/server/UiModeManagerServiceTest.java | 29 |
1 files changed, 16 insertions, 13 deletions
diff --git a/services/tests/uiservicestests/src/com/android/server/UiModeManagerServiceTest.java b/services/tests/uiservicestests/src/com/android/server/UiModeManagerServiceTest.java index 0404b82d306d..c4b8599a483c 100644 --- a/services/tests/uiservicestests/src/com/android/server/UiModeManagerServiceTest.java +++ b/services/tests/uiservicestests/src/com/android/server/UiModeManagerServiceTest.java @@ -118,6 +118,7 @@ import org.mockito.Spy; import java.time.LocalDateTime; import java.time.LocalTime; import java.time.ZoneId; +import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.function.Consumer; @@ -232,7 +233,8 @@ public class UiModeManagerServiceTest extends UiServiceTestCase { any(AlarmManager.OnAlarmListener.class), any(Handler.class)); doAnswer(inv -> { - mCustomListener = () -> {}; + mCustomListener = () -> { + }; return null; }).when(mAlarmManager).cancel(eq(mCustomListener)); when(mContext.getSystemService(eq(Context.POWER_SERVICE))) @@ -1321,7 +1323,7 @@ public class UiModeManagerServiceTest extends UiServiceTestCase { @Test public void enableCarMode_failsForBogusPackageName() throws Exception { when(mPackageManager.getPackageUidAsUser(eq(PACKAGE_NAME), anyInt())) - .thenReturn(TestInjector.DEFAULT_CALLING_UID + 1); + .thenReturn(TestInjector.DEFAULT_CALLING_UID + 1); assertThrows(SecurityException.class, () -> mService.enableCarMode(0, 0, PACKAGE_NAME)); assertThat(mService.getCurrentModeType()).isNotEqualTo(Configuration.UI_MODE_TYPE_CAR); @@ -1343,19 +1345,19 @@ public class UiModeManagerServiceTest extends UiServiceTestCase { @Test public void disableCarMode_failsForBogusPackageName() throws Exception { when(mPackageManager.getPackageUidAsUser(eq(PACKAGE_NAME), anyInt())) - .thenReturn(TestInjector.DEFAULT_CALLING_UID); + .thenReturn(TestInjector.DEFAULT_CALLING_UID); mService.enableCarMode(0, 0, PACKAGE_NAME); assertThat(mService.getCurrentModeType()).isEqualTo(Configuration.UI_MODE_TYPE_CAR); when(mPackageManager.getPackageUidAsUser(eq(PACKAGE_NAME), anyInt())) - .thenReturn(TestInjector.DEFAULT_CALLING_UID + 1); + .thenReturn(TestInjector.DEFAULT_CALLING_UID + 1); assertThrows(SecurityException.class, - () -> mService.disableCarModeByCallingPackage(0, PACKAGE_NAME)); + () -> mService.disableCarModeByCallingPackage(0, PACKAGE_NAME)); assertThat(mService.getCurrentModeType()).isEqualTo(Configuration.UI_MODE_TYPE_CAR); // Clean up when(mPackageManager.getPackageUidAsUser(eq(PACKAGE_NAME), anyInt())) - .thenReturn(TestInjector.DEFAULT_CALLING_UID); + .thenReturn(TestInjector.DEFAULT_CALLING_UID); mService.disableCarModeByCallingPackage(0, PACKAGE_NAME); assertThat(mService.getCurrentModeType()).isNotEqualTo(Configuration.UI_MODE_TYPE_CAR); } @@ -1473,12 +1475,13 @@ public class UiModeManagerServiceTest extends UiServiceTestCase { assertFalse(mUiManagerService.getConfiguration().isNightModeActive()); } - // attention modes with expected night modes - Map<Integer, Boolean> modes = Map.of( - MODE_ATTENTION_THEME_OVERLAY_OFF, initialNightMode, - MODE_ATTENTION_THEME_OVERLAY_DAY, false, - MODE_ATTENTION_THEME_OVERLAY_NIGHT, true - ); + // Attention modes with expected night modes. + // Important to keep modes.put(MODE_ATTENTION_THEME_OVERLAY_OFF, initialNightMode) in the + // first position, hence LinkedHashMap. + Map<Integer, Boolean> modes = new LinkedHashMap<>(); + modes.put(MODE_ATTENTION_THEME_OVERLAY_OFF, initialNightMode); + modes.put(MODE_ATTENTION_THEME_OVERLAY_DAY, false); + modes.put(MODE_ATTENTION_THEME_OVERLAY_NIGHT, true); // test for (int attentionMode : modes.keySet()) { @@ -1562,7 +1565,7 @@ public class UiModeManagerServiceTest extends UiServiceTestCase { private final int callingUid; public TestInjector() { - this(DEFAULT_CALLING_UID); + this(DEFAULT_CALLING_UID); } public TestInjector(int callingUid) { |