Binding tests to TouchInteractionService to preventing it from getting destroyed

Fix: 257514573
Bug: 266716173
Test: atest MemoryTests
Test: atest TaplTestsLauncher3#testDragAppIcon
Change-Id: Ib53bf11f45f9e05bf55116d52f3406f45dfaf6c2
diff --git a/tests/Android.bp b/tests/Android.bp
index 7144d65..dfbaf86 100644
--- a/tests/Android.bp
+++ b/tests/Android.bp
@@ -44,6 +44,7 @@
     srcs: [
       "src/com/android/launcher3/ui/AbstractLauncherUiTest.java",
       "src/com/android/launcher3/ui/PortraitLandscapeRunner.java",
+      "src/com/android/launcher3/ui/TaplTestsLauncher3.java",
       "src/com/android/launcher3/util/TestUtil.java",
       "src/com/android/launcher3/util/Wait.java",
       "src/com/android/launcher3/util/WidgetUtils.java",
@@ -54,7 +55,7 @@
       "src/com/android/launcher3/util/rule/ShellCommandRule.java",
       "src/com/android/launcher3/util/rule/SimpleActivityRule.java",
       "src/com/android/launcher3/util/rule/TestStabilityRule.java",
-      "src/com/android/launcher3/ui/TaplTestsLauncher3.java",
+      "src/com/android/launcher3/util/rule/TISBindRule.java",
       "src/com/android/launcher3/testcomponent/BaseTestingActivity.java",
       "src/com/android/launcher3/testcomponent/OtherBaseTestingActivity.java",
       "src/com/android/launcher3/testcomponent/CustomShortcutConfigActivity.java",
diff --git a/tests/src/com/android/launcher3/ui/TaplTestsLauncher3.java b/tests/src/com/android/launcher3/ui/TaplTestsLauncher3.java
index 401c25a4..19b8b0c 100644
--- a/tests/src/com/android/launcher3/ui/TaplTestsLauncher3.java
+++ b/tests/src/com/android/launcher3/ui/TaplTestsLauncher3.java
@@ -51,11 +51,13 @@
 import com.android.launcher3.tapl.Workspace;
 import com.android.launcher3.util.TestUtil;
 import com.android.launcher3.util.rule.ScreenRecordRule.ScreenRecord;
+import com.android.launcher3.util.rule.TISBindRule;
 import com.android.launcher3.widget.picker.WidgetsFullSheet;
 import com.android.launcher3.widget.picker.WidgetsRecyclerView;
 
 import org.junit.Before;
 import org.junit.Ignore;
+import org.junit.Rule;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 
@@ -71,6 +73,9 @@
     private static final String STORE_APP_NAME = "Play Store";
     private static final String GMAIL_APP_NAME = "Gmail";
 
+    @Rule
+    public TISBindRule mTISBindRule = new TISBindRule();
+
     @Before
     public void setUp() throws Exception {
         super.setUp();
@@ -214,7 +219,7 @@
                 false /* tapRight */);
     }
 
-    @IwTest(focusArea="launcher")
+    @IwTest(focusArea = "launcher")
     @Test
     @ScreenRecord // b/202433017
     public void testWorkspace() throws Exception {
@@ -341,7 +346,7 @@
         }
     }
 
-    @IwTest(focusArea="launcher")
+    @IwTest(focusArea = "launcher")
     @Test
     @PortraitLandscape
     @ScreenRecord // b/256898879
@@ -610,16 +615,16 @@
 
     /**
      * @return List of workspace grid coordinates. Those are not pixels. See {@link
-     *     Workspace#getIconGridDimensions()}
+     * Workspace#getIconGridDimensions()}
      */
     private Point[] getCornersAndCenterPositions() {
         final Point dimensions = mLauncher.getWorkspace().getIconGridDimensions();
-        return new Point[] {
-            new Point(0, 1),
-            new Point(0, dimensions.y - 2),
-            new Point(dimensions.x - 1, 1),
-            new Point(dimensions.x - 1, dimensions.y - 2),
-            new Point(dimensions.x / 2, dimensions.y / 2)
+        return new Point[]{
+                new Point(0, 1),
+                new Point(0, dimensions.y - 2),
+                new Point(dimensions.x - 1, 1),
+                new Point(dimensions.x - 1, dimensions.y - 2),
+                new Point(dimensions.x / 2, dimensions.y / 2)
         };
     }
 
diff --git a/tests/src/com/android/launcher3/util/rule/TISBindRule.java b/tests/src/com/android/launcher3/util/rule/TISBindRule.java
new file mode 100644
index 0000000..3ec4a29
--- /dev/null
+++ b/tests/src/com/android/launcher3/util/rule/TISBindRule.java
@@ -0,0 +1,81 @@
+/*
+ * Copyright (C) 2023 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.android.launcher3.util.rule;
+
+import android.app.UiAutomation;
+import android.content.ComponentName;
+import android.content.Context;
+import android.content.Intent;
+import android.content.ServiceConnection;
+import android.os.IBinder;
+import android.util.Log;
+
+import androidx.annotation.NonNull;
+import androidx.test.platform.app.InstrumentationRegistry;
+
+import org.junit.rules.TestRule;
+import org.junit.runner.Description;
+import org.junit.runners.model.Statement;
+
+public class TISBindRule implements TestRule {
+    public static String TAG = "TISBindRule";
+    public static String INTENT_FILTER = "android.intent.action.QUICKSTEP_SERVICE";
+    public static String TIS_PERMISSIONS = "android.permission.STATUS_BAR_SERVICE";
+
+    private String getLauncherPackageName(Context context) {
+        return ComponentName.unflattenFromString(context.getString(
+                com.android.internal.R.string.config_recentsComponentName)).getPackageName();
+    }
+
+    private ServiceConnection createConnection() {
+        return new ServiceConnection() {
+            @Override
+            public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
+                Log.d(TAG, "Connected to TouchInteractionService");
+            }
+
+            @Override
+            public void onServiceDisconnected(ComponentName componentName) {
+                Log.d(TAG, "Disconnected from TouchInteractionService");
+            }
+        };
+    }
+
+    @NonNull
+    @Override
+    public Statement apply(@NonNull Statement base, @NonNull Description description) {
+        return new Statement() {
+
+            @Override
+            public void evaluate() throws Throwable {
+                Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();
+                final ServiceConnection connection = createConnection();
+                UiAutomation uiAutomation =
+                        InstrumentationRegistry.getInstrumentation().getUiAutomation();
+                uiAutomation.adoptShellPermissionIdentity(TIS_PERMISSIONS);
+                Intent launchIntent = new Intent(INTENT_FILTER);
+                launchIntent.setPackage(getLauncherPackageName(context));
+                context.bindService(launchIntent, connection, Context.BIND_AUTO_CREATE);
+                uiAutomation.dropShellPermissionIdentity();
+                try {
+                    base.evaluate();
+                } finally {
+                    context.unbindService(connection);
+                }
+            }
+        };
+    }
+}