Correctly restarting Launcher from OOP tests

Waiting for touch interaction service; waiting for package-restart
events.

Bug: 136215685
Change-Id: I0c31c09fe3a58b898168dfdb4e4d23757b94a47c
diff --git a/go/quickstep/src/com/android/quickstep/TouchInteractionService.java b/go/quickstep/src/com/android/quickstep/TouchInteractionService.java
index c902826..917800f 100644
--- a/go/quickstep/src/com/android/quickstep/TouchInteractionService.java
+++ b/go/quickstep/src/com/android/quickstep/TouchInteractionService.java
@@ -185,4 +185,8 @@
         }
         return mMyBinder;
     }
+
+    public static boolean isInputMonitorInitialized() {
+        return true;
+    }
 }
diff --git a/quickstep/recents_ui_overrides/src/com/android/quickstep/TouchInteractionService.java b/quickstep/recents_ui_overrides/src/com/android/quickstep/TouchInteractionService.java
index 0ef2f5c..dd04024 100644
--- a/quickstep/recents_ui_overrides/src/com/android/quickstep/TouchInteractionService.java
+++ b/quickstep/recents_ui_overrides/src/com/android/quickstep/TouchInteractionService.java
@@ -226,12 +226,17 @@
     };
 
     private static boolean sConnected = false;
+    private static boolean sInputMonitorInitialized = false;
     private static final SwipeSharedState sSwipeSharedState = new SwipeSharedState();
 
     public static boolean isConnected() {
         return sConnected;
     }
 
+    public static boolean isInputMonitorInitialized() {
+        return sInputMonitorInitialized;
+    }
+
     public static SwipeSharedState getSwipeSharedState() {
         return sSwipeSharedState;
     }
@@ -325,6 +330,7 @@
             mInputMonitorCompat.dispose();
             mInputMonitorCompat = null;
         }
+        sInputMonitorInitialized = false;
     }
 
     private void initInputMonitor() {
@@ -342,6 +348,7 @@
             Log.e(TAG, "Unable to create input monitor", e);
         }
         initTouchBounds();
+        sInputMonitorInitialized = true;
     }
 
     private int getNavbarSize(String resName) {
diff --git a/quickstep/src/com/android/quickstep/QuickstepTestInformationHandler.java b/quickstep/src/com/android/quickstep/QuickstepTestInformationHandler.java
index 8951363..b59e133 100644
--- a/quickstep/src/com/android/quickstep/QuickstepTestInformationHandler.java
+++ b/quickstep/src/com/android/quickstep/QuickstepTestInformationHandler.java
@@ -10,7 +10,8 @@
 
 public class QuickstepTestInformationHandler extends TestInformationHandler {
 
-    public QuickstepTestInformationHandler(Context context) { }
+    public QuickstepTestInformationHandler(Context context) {
+    }
 
     @Override
     public Bundle call(String method) {
@@ -29,6 +30,12 @@
                 response.putInt(TestProtocol.TEST_INFO_RESPONSE_FIELD, (int) swipeHeight);
                 return response;
             }
+
+            case TestProtocol.REQUEST_IS_LAUNCHER_INITIALIZED: {
+                response.putBoolean(TestProtocol.TEST_INFO_RESPONSE_FIELD,
+                        TouchInteractionService.isInputMonitorInitialized());
+                break;
+            }
         }
 
         return super.call(method);
diff --git a/src/com/android/launcher3/testing/TestInformationHandler.java b/src/com/android/launcher3/testing/TestInformationHandler.java
index d2e1961..bab454f 100644
--- a/src/com/android/launcher3/testing/TestInformationHandler.java
+++ b/src/com/android/launcher3/testing/TestInformationHandler.java
@@ -74,6 +74,11 @@
                 break;
             }
 
+            case TestProtocol.REQUEST_IS_LAUNCHER_INITIALIZED: {
+                response.putBoolean(TestProtocol.TEST_INFO_RESPONSE_FIELD, true);
+                break;
+            }
+
             case TestProtocol.REQUEST_ENABLE_DEBUG_TRACING:
                 TestProtocol.sDebugTracing = true;
                 break;
diff --git a/src/com/android/launcher3/testing/TestProtocol.java b/src/com/android/launcher3/testing/TestProtocol.java
index 3774042..3a07be0 100644
--- a/src/com/android/launcher3/testing/TestProtocol.java
+++ b/src/com/android/launcher3/testing/TestProtocol.java
@@ -66,6 +66,7 @@
             "all-apps-to-overview-swipe-height";
     public static final String REQUEST_HOME_TO_ALL_APPS_SWIPE_HEIGHT =
             "home-to-all-apps-swipe-height";
+    public static final String REQUEST_IS_LAUNCHER_INITIALIZED = "is-launcher-initialized";
     public static final String REQUEST_FREEZE_APP_LIST = "freeze-app-list";
     public static final String REQUEST_UNFREEZE_APP_LIST = "unfreeze-app-list";
     public static final String REQUEST_APP_LIST_FREEZE_FLAGS = "app-list-freeze-flags";
diff --git a/tests/tapl/com/android/launcher3/tapl/LauncherInstrumentation.java b/tests/tapl/com/android/launcher3/tapl/LauncherInstrumentation.java
index 11b0665..2b0a794 100644
--- a/tests/tapl/com/android/launcher3/tapl/LauncherInstrumentation.java
+++ b/tests/tapl/com/android/launcher3/tapl/LauncherInstrumentation.java
@@ -444,6 +444,8 @@
     }
 
     private UiObject2 verifyContainerType(ContainerType containerType) {
+        waitForTouchInteractionService();
+
         assertEquals("Unexpected display rotation",
                 mExpectedRotation, mDevice.getDisplayRotation());
 
@@ -514,6 +516,18 @@
         }
     }
 
+    private void waitForTouchInteractionService() {
+        for (int i = 0; i < 100; ++i) {
+            if (getTestInfo(
+                    TestProtocol.REQUEST_IS_LAUNCHER_INITIALIZED).
+                    getBoolean(TestProtocol.TEST_INFO_RESPONSE_FIELD)) {
+                return;
+            }
+            SystemClock.sleep(100);
+        }
+        fail("TouchInteractionService didn't connect");
+    }
+
     Parcelable executeAndWaitForEvent(Runnable command,
             UiAutomation.AccessibilityEventFilter eventFilter, String message) {
         try {