Wait more time for activity start.

- Looks like 5 seconds is too short on some emulator devices.
- Makes sure that the app is installed and enabled.

Bug: 185483743
Bug: 185576411
Test: atest UpdatableSystemFontTest#launchApp --rerun-until-failure
Change-Id: Ibc1dba9f4b2b3c694e186798f65a456e712cba9e
diff --git a/tests/UpdatableSystemFontTest/src/com/android/updatablesystemfont/UpdatableSystemFontTest.java b/tests/UpdatableSystemFontTest/src/com/android/updatablesystemfont/UpdatableSystemFontTest.java
index 032da3f..19dfdab 100644
--- a/tests/UpdatableSystemFontTest/src/com/android/updatablesystemfont/UpdatableSystemFontTest.java
+++ b/tests/UpdatableSystemFontTest/src/com/android/updatablesystemfont/UpdatableSystemFontTest.java
@@ -19,6 +19,8 @@
 import static com.google.common.truth.Truth.assertThat;
 import static com.google.common.truth.Truth.assertWithMessage;
 
+import static java.util.concurrent.TimeUnit.SECONDS;
+
 import android.platform.test.annotations.RootPermissionTest;
 
 import com.android.fsverity.AddFsVerityCertRule;
@@ -35,7 +37,6 @@
 import org.junit.Test;
 import org.junit.runner.RunWith;
 
-import java.util.concurrent.TimeUnit;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
@@ -77,6 +78,7 @@
     private static final String EMOJI_RENDERING_TEST_APP_ID = "com.android.emojirenderingtestapp";
     private static final String EMOJI_RENDERING_TEST_ACTIVITY =
             EMOJI_RENDERING_TEST_APP_ID + "/.EmojiRenderingTestActivity";
+    private static final long ACTIVITY_TIMEOUT_MILLIS = SECONDS.toMillis(10);
 
     private interface ThrowingSupplier<T> {
         T get() throws Exception;
@@ -167,9 +169,8 @@
     public void launchApp() throws Exception {
         String fontPath = getFontPath(NOTO_COLOR_EMOJI_TTF);
         assertThat(fontPath).startsWith(SYSTEM_FONTS_DIR);
-        expectRemoteCommandToSucceed("am force-stop " + EMOJI_RENDERING_TEST_APP_ID);
-        expectRemoteCommandToSucceed("am start-activity -n " + EMOJI_RENDERING_TEST_ACTIVITY);
-        waitUntil(TimeUnit.SECONDS.toMillis(5), () ->
+        startActivity(EMOJI_RENDERING_TEST_APP_ID, EMOJI_RENDERING_TEST_ACTIVITY);
+        waitUntil(ACTIVITY_TIMEOUT_MILLIS, () ->
                 isFileOpenedBy(fontPath, EMOJI_RENDERING_TEST_APP_ID));
     }
 
@@ -181,10 +182,9 @@
                 TEST_NOTO_COLOR_EMOJI_VPLUS1_TTF, TEST_NOTO_COLOR_EMOJI_VPLUS1_TTF_FSV_SIG));
         String updatedFontPath = getFontPath(NOTO_COLOR_EMOJI_TTF);
         assertThat(updatedFontPath).startsWith(DATA_FONTS_DIR);
-        expectRemoteCommandToSucceed("am force-stop " + EMOJI_RENDERING_TEST_APP_ID);
-        expectRemoteCommandToSucceed("am start-activity -n " + EMOJI_RENDERING_TEST_ACTIVITY);
+        startActivity(EMOJI_RENDERING_TEST_APP_ID, EMOJI_RENDERING_TEST_ACTIVITY);
         // The original font should NOT be opened by the app.
-        waitUntil(TimeUnit.SECONDS.toMillis(5), () ->
+        waitUntil(ACTIVITY_TIMEOUT_MILLIS, () ->
                 isFileOpenedBy(updatedFontPath, EMOJI_RENDERING_TEST_APP_ID)
                         && !isFileOpenedBy(originalFontPath, EMOJI_RENDERING_TEST_APP_ID));
     }
@@ -216,6 +216,17 @@
         return null;
     }
 
+    private void startActivity(String appId, String activityId) throws Exception {
+        // Make sure that the app is installed and enabled.
+        waitUntil(ACTIVITY_TIMEOUT_MILLIS, () -> {
+            String packageInfo = expectRemoteCommandToSucceed(
+                    "pm list packages -e " + EMOJI_RENDERING_TEST_APP_ID);
+            return !packageInfo.isEmpty();
+        });
+        expectRemoteCommandToSucceed("am force-stop " + EMOJI_RENDERING_TEST_APP_ID);
+        expectRemoteCommandToSucceed("am start-activity -n " + EMOJI_RENDERING_TEST_ACTIVITY);
+    }
+
     private String expectRemoteCommandToSucceed(String cmd) throws Exception {
         CommandResult result = getDevice().executeShellV2Command(cmd);
         assertWithMessage("`" + cmd + "` failed: " + result.getStderr())
@@ -232,7 +243,7 @@
     }
 
     private void waitUntilFontCommandIsReady() {
-        waitUntil(TimeUnit.SECONDS.toMillis(30), () -> {
+        waitUntil(SECONDS.toMillis(30), () -> {
             try {
                 return getDevice().executeShellV2Command("cmd font status").getStatus()
                         == CommandStatus.SUCCESS;