Fix flaky ActivityThreadTest

Always reset application configuration after test to prevent it from
affecting other test cases.

Fix: 279720755
Test: atest FrameworksCoreTests:ActivityThreadTest
Change-Id: I726a979da802abf4835f9893d4d8247d70f5537b
This commit is contained in:
Chris Li
2023-07-26 19:04:54 +08:00
parent 6927452e34
commit 03b6297630

View File

@@ -106,6 +106,7 @@ public class ActivityThreadTest {
false /* launchActivity */);
private WindowTokenClientController mOriginalWindowTokenClientController;
private Configuration mOriginalAppConfig;
private ArrayList<VirtualDisplay> mCreatedVirtualDisplays;
@@ -114,6 +115,8 @@ public class ActivityThreadTest {
// Keep track of the original controller, so that it can be used to restore in tearDown()
// when there is override in some test cases.
mOriginalWindowTokenClientController = WindowTokenClientController.getInstance();
mOriginalAppConfig = new Configuration(ActivityThread.currentActivityThread()
.getConfiguration());
}
@After
@@ -123,6 +126,8 @@ public class ActivityThreadTest {
mCreatedVirtualDisplays = null;
}
WindowTokenClientController.overrideForTesting(mOriginalWindowTokenClientController);
InstrumentationRegistry.getInstrumentation().runOnMainSync(
() -> restoreConfig(ActivityThread.currentActivityThread(), mOriginalAppConfig));
}
@Test
@@ -564,16 +569,10 @@ public class ActivityThreadTest {
activityThread.updatePendingConfiguration(newAppConfig);
activityThread.handleConfigurationChanged(newAppConfig, DEVICE_ID_INVALID);
try {
assertEquals("Virtual display orientation must not change when process"
+ " configuration orientation changes.",
originalVirtualDisplayOrientation,
virtualDisplayContext.getResources().getConfiguration().orientation);
} finally {
// Make sure to reset the process config to prevent side effects to other
// tests.
restoreConfig(activityThread, originalAppConfig);
}
assertEquals("Virtual display orientation must not change when process"
+ " configuration orientation changes.",
originalVirtualDisplayOrientation,
virtualDisplayContext.getResources().getConfiguration().orientation);
});
}