Refactor game service CTS AIDLs into their own package.

This avoids a direct dependency from the test to the
test app, which was causing issues when adding layout
resources to the test app.

Also update defaults for the test helper apps and
add some logic to help cleanup game sessions between
tests.

Test: atest CtsGameServiceTestCases
Bug: 206128693
Change-Id: If96dba8d94c919482b063bfe79995a5e53f3c938
diff --git a/tests/tests/gameservice/Android.bp b/tests/tests/gameservice/Android.bp
index ba82daa..76b7941 100644
--- a/tests/tests/gameservice/Android.bp
+++ b/tests/tests/gameservice/Android.bp
@@ -31,8 +31,8 @@
     ],
     static_libs: [
         "androidx.test.core",
-        "CtsGameServiceTestApp",
         "compatibility-device-util-axt",
+        "CtsGameServiceAidls",
         "ctstestrunner-axt",
         "junit",
         "truth-prebuilt",
diff --git a/tests/tests/gameservice/CtsGameServiceAidls/Android.bp b/tests/tests/gameservice/CtsGameServiceAidls/Android.bp
new file mode 100644
index 0000000..f585313
--- /dev/null
+++ b/tests/tests/gameservice/CtsGameServiceAidls/Android.bp
@@ -0,0 +1,30 @@
+// Copyright (C) 2022 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 {
+    default_applicable_licenses: ["Android-Apache-2.0"],
+}
+
+android_library {
+    name: "CtsGameServiceAidls",
+    srcs: [
+        "src/**/*.aidl",
+        "src/**/*.java",
+    ],
+    static_libs: [
+        "androidx.appcompat_appcompat",
+    ],
+    defaults: ["cts_support_defaults"],
+    min_sdk_version: "current",
+}
diff --git a/tests/tests/gameservice/CtsGameServiceAidls/AndroidManifest.xml b/tests/tests/gameservice/CtsGameServiceAidls/AndroidManifest.xml
new file mode 100644
index 0000000..525b3b9
--- /dev/null
+++ b/tests/tests/gameservice/CtsGameServiceAidls/AndroidManifest.xml
@@ -0,0 +1,21 @@
+<?xml version="1.0" encoding="utf-8"?>
+
+<!--
+  ~ Copyright (C) 2022 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.
+  -->
+
+<manifest
+    xmlns:android="http://schemas.android.com/apk/res/android"
+    package="android.service.games.cts.app"/>
\ No newline at end of file
diff --git a/tests/tests/gameservice/CtsGameServiceTestApp/src/android/service/games/cts/app/IGameServiceTestService.aidl b/tests/tests/gameservice/CtsGameServiceAidls/src/android/service/games/cts/app/IGameServiceTestService.aidl
similarity index 90%
rename from tests/tests/gameservice/CtsGameServiceTestApp/src/android/service/games/cts/app/IGameServiceTestService.aidl
rename to tests/tests/gameservice/CtsGameServiceAidls/src/android/service/games/cts/app/IGameServiceTestService.aidl
index 2228551..59731a4 100644
--- a/tests/tests/gameservice/CtsGameServiceTestApp/src/android/service/games/cts/app/IGameServiceTestService.aidl
+++ b/tests/tests/gameservice/CtsGameServiceAidls/src/android/service/games/cts/app/IGameServiceTestService.aidl
@@ -13,13 +13,12 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
+package android.service.games.cts.app;
 
- package android.service.games.cts.app;
-
- interface IGameServiceTestService {
+interface IGameServiceTestService {
     boolean isGameServiceConnected();
 
     void setGamePackageNames(in List<String> packageNames);
 
     List<String> getActiveSessions();
- }
\ No newline at end of file
+}
\ No newline at end of file
diff --git a/tests/tests/gameservice/CtsGameServiceFalsePositiveGame/Android.bp b/tests/tests/gameservice/CtsGameServiceFalsePositiveGame/Android.bp
index 750b89d..58eae22 100644
--- a/tests/tests/gameservice/CtsGameServiceFalsePositiveGame/Android.bp
+++ b/tests/tests/gameservice/CtsGameServiceFalsePositiveGame/Android.bp
@@ -24,6 +24,6 @@
     static_libs: [
         "androidx.appcompat_appcompat",
     ],
-    defaults: ["mts-target-sdk-version-current"],
+    defaults: ["cts_support_defaults"],
     min_sdk_version: "current",
 }
diff --git a/tests/tests/gameservice/CtsGameServiceGame/Android.bp b/tests/tests/gameservice/CtsGameServiceGame/Android.bp
index 6cd56b9..f4a321a 100644
--- a/tests/tests/gameservice/CtsGameServiceGame/Android.bp
+++ b/tests/tests/gameservice/CtsGameServiceGame/Android.bp
@@ -24,6 +24,6 @@
     static_libs: [
         "androidx.appcompat_appcompat",
     ],
-    defaults: ["mts-target-sdk-version-current"],
+    defaults: ["cts_support_defaults"],
     min_sdk_version: "current",
 }
diff --git a/tests/tests/gameservice/CtsGameServiceNotGame/Android.bp b/tests/tests/gameservice/CtsGameServiceNotGame/Android.bp
index 6924d0a..849097c 100644
--- a/tests/tests/gameservice/CtsGameServiceNotGame/Android.bp
+++ b/tests/tests/gameservice/CtsGameServiceNotGame/Android.bp
@@ -24,6 +24,6 @@
     static_libs: [
         "androidx.appcompat_appcompat",
     ],
-    defaults: ["mts-target-sdk-version-current"],
+    defaults: ["cts_support_defaults"],
     min_sdk_version: "current",
 }
diff --git a/tests/tests/gameservice/CtsGameServiceTestApp/Android.bp b/tests/tests/gameservice/CtsGameServiceTestApp/Android.bp
index 7b31b90..83cc646 100644
--- a/tests/tests/gameservice/CtsGameServiceTestApp/Android.bp
+++ b/tests/tests/gameservice/CtsGameServiceTestApp/Android.bp
@@ -19,14 +19,14 @@
 android_test_helper_app {
     name: "CtsGameServiceTestApp",
     srcs: [
-        "src/**/*.aidl",
         "src/**/*.java",
     ],
     static_libs: [
         "androidx.annotation_annotation",
+        "CtsGameServiceAidls",
         "guava",
     ],
-    defaults: ["mts-target-sdk-version-current"],
+    defaults: ["cts_support_defaults"],
     min_sdk_version: "current",
     resource_dirs: ["res"],
 }
diff --git a/tests/tests/gameservice/CtsGameServiceTestApp/AndroidManifest.xml b/tests/tests/gameservice/CtsGameServiceTestApp/AndroidManifest.xml
index 671c17c..6e00040 100644
--- a/tests/tests/gameservice/CtsGameServiceTestApp/AndroidManifest.xml
+++ b/tests/tests/gameservice/CtsGameServiceTestApp/AndroidManifest.xml
@@ -19,6 +19,7 @@
 <manifest
     xmlns:android="http://schemas.android.com/apk/res/android"
     package="android.service.games.cts.app">
+
     <application android:label="CtsGameServiceTestApp">
 
         <service
diff --git a/tests/tests/gameservice/CtsGameServiceTestApp/src/android/service/games/cts/app/GameServiceTestService.java b/tests/tests/gameservice/CtsGameServiceTestApp/src/android/service/games/cts/app/GameServiceTestService.java
index 679057e..feb018c 100644
--- a/tests/tests/gameservice/CtsGameServiceTestApp/src/android/service/games/cts/app/GameServiceTestService.java
+++ b/tests/tests/gameservice/CtsGameServiceTestApp/src/android/service/games/cts/app/GameServiceTestService.java
@@ -31,8 +31,6 @@
  * TestGameSessionService}.
  */
 public final class GameServiceTestService extends Service {
-    public static final String TEST_SERVICE = "android.service.games.action.TEST_SERVICE";
-
     private final IGameServiceTestService.Stub mStub = new IGameServiceTestService.Stub() {
         @Override
         public boolean isGameServiceConnected() {
diff --git a/tests/tests/gameservice/src/android/service/games/GameServiceTest.java b/tests/tests/gameservice/src/android/service/games/GameServiceTest.java
index bc88bea..f992f68 100644
--- a/tests/tests/gameservice/src/android/service/games/GameServiceTest.java
+++ b/tests/tests/gameservice/src/android/service/games/GameServiceTest.java
@@ -28,13 +28,13 @@
 import android.content.Intent;
 import android.content.pm.PackageManager;
 import android.os.IBinder;
-import android.service.games.cts.app.GameServiceTestService;
 import android.service.games.cts.app.IGameServiceTestService;
 import android.support.test.uiautomator.By;
 
 import androidx.test.runner.AndroidJUnit4;
 
 import com.android.compatibility.common.util.ShellIdentityUtils;
+import com.android.compatibility.common.util.ShellUtils;
 import com.android.compatibility.common.util.UiAutomatorUtils;
 
 import com.google.common.collect.ImmutableList;
@@ -70,7 +70,7 @@
         mServiceConnection = new ServiceConnection();
         assertThat(
                 getInstrumentation().getContext().bindService(
-                        new Intent(GameServiceTestService.TEST_SERVICE).setPackage(
+                        new Intent("android.service.games.action.TEST_SERVICE").setPackage(
                                 TEST_APP_PACKAGE_NAME),
                         mServiceConnection,
                         Context.BIND_AUTO_CREATE)).isTrue();
@@ -81,6 +81,10 @@
 
     @After
     public void tearDown() {
+        forceStop(GAME_PACKAGE_NAME);
+        forceStop(NOT_GAME_PACKAGE_NAME);
+        forceStop(FALSE_POSITIVE_GAME_PACKAGE_NAME);
+
         GameManager gameManager =
                 getInstrumentation().getContext().getSystemService(GameManager.class);
         ShellIdentityUtils.invokeMethodWithShellPermissionsNoReturn(gameManager,
@@ -122,6 +126,10 @@
         UiAutomatorUtils.waitFindObject(By.pkg(packageName).depth(0));
     }
 
+    private static void forceStop(String packageName) {
+        ShellUtils.runShellCommand("am force-stop %s", packageName);
+    }
+
     private static final class ServiceConnection implements android.content.ServiceConnection {
         private final Semaphore mSemaphore = new Semaphore(0);
         private IGameServiceTestService mService;