+ * For example, runPhase("testApkOnlyEnableRollback");
+ */
+ private void runPhase(String phase) throws Exception {
+ assertTrue(runDeviceTests("com.android.tests.stagedinstallinternal",
+ "com.android.tests.stagedinstallinternal.StagedInstallInternalTest",
+ phase));
+ }
+
+ // We do not assert the success of cleanup phase since it might fail due to flaky reasons.
+ private void cleanUp() throws Exception {
+ try {
+ runDeviceTests("com.android.tests.stagedinstallinternal",
+ "com.android.tests.stagedinstallinternal.StagedInstallInternalTest",
+ "cleanUp");
+ } catch (AssertionError e) {
+ Log.e(TAG, e);
+ }
+ }
+
+ @Before
+ public void setUp() throws Exception {
+ mWasRoot = getDevice().isAdbRoot();
+ if (!mWasRoot) {
+ getDevice().enableAdbRoot();
+ }
+ cleanUp();
+ // Abandon all staged sessions
+ getDevice().executeShellCommand("pm install-abandon $(pm get-stagedsessions --only-ready "
+ + "--only-parent --only-sessionid)");
+ }
+
+ @After
+ public void tearDown() throws Exception {
+ if (!mWasRoot) {
+ getDevice().disableAdbRoot();
+ }
+ cleanUp();
+ }
+
+ @Test
+ public void testSystemServerRestartDoesNotAffectStagedSessions() throws Exception {
+ runPhase("testSystemServerRestartDoesNotAffectStagedSessions_Commit");
+ restartSystemServer();
+ runPhase("testSystemServerRestartDoesNotAffectStagedSessions_Verify");
+ }
+
+ private void restartSystemServer() throws Exception {
+ // Restart the system server
+ long oldStartTime = getDevice().getProcessByName("system_server").getStartTime();
+ assertThat(getDevice().executeShellCommand("am restart")).contains("Restart the system");
+
+ // Wait for new system server process to start
+ long start = System.currentTimeMillis();
+ long newStartTime = oldStartTime;
+ while (System.currentTimeMillis() < start + SYSTEM_SERVER_TIMEOUT_MS) {
+ ProcessInfo newPs = getDevice().getProcessByName("system_server");
+ if (newPs != null) {
+ newStartTime = newPs.getStartTime();
+ if (newStartTime != oldStartTime) {
+ break;
+ }
+ }
+ Thread.sleep(500);
+ }
+ assertThat(newStartTime).isNotEqualTo(oldStartTime);
+ getDevice().waitForDeviceAvailable();
+ }
+}