Add a CTS test for the new PROC_START_TIMEOUT ANR category

See Iaad8d39221dc9b3877d784d8a79f8de1678eea88.

Test: atest ActivityManagerTest
Bug: 253908737
Change-Id: I129bf788d3045ee9d6200a3f1665773b051bc1a0
diff --git a/tests/app/Android.bp b/tests/app/Android.bp
index 0652fab..d319df1 100644
--- a/tests/app/Android.bp
+++ b/tests/app/Android.bp
@@ -65,6 +65,7 @@
         ":CtsAppTestStubsApp3",
         ":CtsAppTestStubsApp2",
         ":CtsAppTestStubsApi30",
+        ":CtsAppTestWedgedStart",
         ":CtsBadProviderStubs",
         ":CtsCantSaveState1",
         ":CtsCantSaveState2",
diff --git a/tests/app/AndroidTest.xml b/tests/app/AndroidTest.xml
index 6a14b17..84b7f57 100644
--- a/tests/app/AndroidTest.xml
+++ b/tests/app/AndroidTest.xml
@@ -31,6 +31,7 @@
         <option name="test-file-name" value="CtsAppTestStubsApp3.apk" />
         <option name="test-file-name" value="CtsAppTestStubsApp2.apk" />
         <option name="test-file-name" value="CtsAppTestStubsApi30.apk" />
+        <option name="test-file-name" value="CtsAppTestWedgedStart.apk" />
         <option name="test-file-name" value="CtsAppTestCases.apk" />
         <option name="test-file-name" value="CtsBadProviderStubs.apk" />
         <option name="test-file-name" value="CtsCantSaveState1.apk" />
diff --git a/tests/app/app/Android.bp b/tests/app/app/Android.bp
index 4044073..9558171 100644
--- a/tests/app/app/Android.bp
+++ b/tests/app/app/Android.bp
@@ -212,3 +212,26 @@
         "--debug-mode",
     ],
 }
+
+android_test_helper_app {
+    name: "CtsAppTestWedgedStart",
+    defaults: ["cts_support_defaults"],
+    manifest: "WedgedAndroidManifest.xml",
+    libs: [
+        "android.test.runner",
+        "android.test.base",
+    ],
+    srcs: [
+        "src/android/app/stubs/WedgedApplication.java",
+        "src/android/app/stubs/MockApplicationActivity.java",
+    ],
+    // Tag this module as a cts test artifact
+    test_suites: [
+        "general-tests",
+        "sts",
+    ],
+    platform_apis: true,
+    aaptflags: [
+        "--rename-manifest-package com.android.wedged_start",
+    ],
+}
diff --git a/tests/app/app/WedgedAndroidManifest.xml b/tests/app/app/WedgedAndroidManifest.xml
new file mode 100644
index 0000000..a1e98e2
--- /dev/null
+++ b/tests/app/app/WedgedAndroidManifest.xml
@@ -0,0 +1,34 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+ * 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.
+ -->
+
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+          xmlns:tools="http://schemas.android.com/tools"
+     package="android.app.stubs">
+    <application android:label="Android TestCase"
+         android:name="android.app.stubs.WedgedApplication">
+
+        <activity android:name="android.app.stubs.MockApplicationActivity"
+             android:label="MockApplicationActivity"
+             android:exported="true">
+            <intent-filter>
+                <action android:name="android.intent.action.MAIN"/>
+                <category android:name="android.intent.category.LAUNCHER"/>
+            </intent-filter>
+        </activity>
+    </application>
+
+</manifest>
diff --git a/tests/app/app/src/android/app/stubs/WedgedApplication.java b/tests/app/app/src/android/app/stubs/WedgedApplication.java
new file mode 100644
index 0000000..40d4c4a
--- /dev/null
+++ b/tests/app/app/src/android/app/stubs/WedgedApplication.java
@@ -0,0 +1,29 @@
+/*
+ * 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 android.app.stubs;
+
+import android.app.Application;
+import android.os.SystemClock;
+
+public class WedgedApplication extends Application {
+    @Override
+    public void onCreate() {
+        super.onCreate();
+        // Sleep for a while
+        SystemClock.sleep(100000);
+    }
+}
diff --git a/tests/app/src/android/app/cts/ActivityManagerTest.java b/tests/app/src/android/app/cts/ActivityManagerTest.java
index c761c44..8c60c5d 100644
--- a/tests/app/src/android/app/cts/ActivityManagerTest.java
+++ b/tests/app/src/android/app/cts/ActivityManagerTest.java
@@ -152,6 +152,9 @@
     private static final String TAG = ActivityManagerTest.class.getSimpleName();
     private static final String STUB_PACKAGE_NAME = "android.app.stubs";
     private static final long WAITFOR_MSEC = 5000;
+    // Long enough to cover devices with doubled hw multipliers. On most devices
+    // this should be 10s as defined in ActivityManagerService#PROC_START_TIMEOUT
+    private static final long WAITFOR_PROCSTAT_TIMEOUT_MSEC = 30000;
     private static final String SERVICE_NAME = "android.app.stubs.MockService";
     private static final long WAIT_TIME = 2000;
     private static final long WAITFOR_ORDERED_BROADCAST_DRAINED = 60000;
@@ -177,6 +180,7 @@
     private static final String PACKAGE_NAME_APP1 = "com.android.app1";
     private static final String PACKAGE_NAME_APP2 = "com.android.app2";
     private static final String PACKAGE_NAME_APP3 = "com.android.app3";
+    private static final String PACKAGE_NAME_WEDGED_STARTUP = "com.android.wedged_start";
 
     private static final String CANT_SAVE_STATE_1_PACKAGE_NAME = "com.android.test.cantsavestate1";
     private static final String ACTION_FINISH = "com.android.test.action.FINISH";
@@ -1170,6 +1174,37 @@
         }
     }
 
+    /**
+     * This test verifies that the PROC_START_TIMEOUT triggers an ANR.
+     */
+    @Test
+    public void testAppNotRespondingOnStartup() throws Exception {
+        // Setup the ANR monitor
+        AmMonitor monitor = new AmMonitor(mInstrumentation,
+                new String[]{AmMonitor.WAIT_FOR_CRASHED});
+
+        Intent intent = new Intent(Intent.ACTION_MAIN);
+        intent.setClassName(PACKAGE_NAME_WEDGED_STARTUP, MockApplicationActivity.class.getName());
+        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
+
+        mTargetContext.startActivity(intent);
+
+        try {
+            // Verify we got the ANR
+            assertTrue(monitor.waitFor(AmMonitor.WAIT_FOR_EARLY_ANR,
+                            WAITFOR_PROCSTAT_TIMEOUT_MSEC));
+
+            // Just kill the test app
+            monitor.sendCommand(AmMonitor.CMD_KILL);
+        } finally {
+            // clean up
+            monitor.finish();
+            runWithShellPermissionIdentity(() -> {
+                mActivityManager.forceStopPackage(PACKAGE_NAME_WEDGED_STARTUP);
+            });
+        }
+    }
+
     /*
      * Verifies the {@link android.app.ActivityManager#killProcessesWhenImperceptible}.
      */