Address destroying activities multiple times.

There are various paths which an activity can be destroyed. One
possibility is if we have already initiated destroying an activity
and then finish all activities in response to a display being
removed. In this case, a request will be made to destroy an activity
that could already be destroying. Additionally, the state is always
set to finishing, regardless of the current state.

With the activity lifecycler, multiple destroy requests are not
tolerated. The second request will lead to an exception due to not
being able to find the activity. Previously, redundant requests were
suppressed client side by simply ignoring them. The state change
leads to inconsistency as the Activity can come back from destroyed.

This changelist addresses these issues by not finishing a
destroying/destroyed activity. Additionally, the changelist now
enforces that state must move forward. Lastly, redundant destroy
requests are not ignored.

Bug: 71506345
Test: atest FrameworksServicesTests:com.android.server.am.ActivityRecordTests#testSetInvalidState
Test: atest FrameworksServicesTests:com.android.server.am.ActivityStackTests#testSuppressMultipleDestroy
Change-Id: I235fa8002f73ad20bb101551fe6ebd5bcc22fa6d
diff --git a/services/tests/servicestests/src/com/android/server/am/ActivityStackTests.java b/services/tests/servicestests/src/com/android/server/am/ActivityStackTests.java
index a98d5a1..32a29a2 100644
--- a/services/tests/servicestests/src/com/android/server/am/ActivityStackTests.java
+++ b/services/tests/servicestests/src/com/android/server/am/ActivityStackTests.java
@@ -24,6 +24,8 @@
 import static android.app.WindowConfiguration.WINDOWING_MODE_SPLIT_SCREEN_PRIMARY;
 import static android.app.WindowConfiguration.WINDOWING_MODE_SPLIT_SCREEN_SECONDARY;
 
+import static com.android.server.am.ActivityStack.ActivityState.DESTROYED;
+import static com.android.server.am.ActivityStack.ActivityState.DESTROYING;
 import static com.android.server.am.ActivityStack.REMOVE_TASK_MODE_DESTROYING;
 
 import static org.junit.Assert.assertEquals;
@@ -31,8 +33,13 @@
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertTrue;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
 
-import android.content.ComponentName;
+import android.app.servertransaction.DestroyActivityItem;
 import android.content.pm.ActivityInfo;
 import android.os.UserHandle;
 import android.platform.test.annotations.Presubmit;
@@ -457,4 +464,26 @@
                 .setCreateTask(true).build();
         return stack;
     }
+
+    @Test
+    public void testSuppressMultipleDestroy() throws Exception {
+        final ActivityRecord r = new ActivityBuilder(mService).setTask(mTask).build();
+        final ClientLifecycleManager lifecycleManager = mock(ClientLifecycleManager.class);
+        final ProcessRecord app = r.app;
+
+        // The mocked lifecycle manager must be set on the ActivityStackSupervisor's reference to
+        // the service rather than mService as mService is a spy and setting the value will not
+        // propagate as ActivityManagerService hands its own reference to the
+        // ActivityStackSupervisor during construction.
+        ((TestActivityManagerService) mSupervisor.mService).setLifecycleManager(lifecycleManager);
+
+        mStack.destroyActivityLocked(r, true, "first invocation");
+        verify(lifecycleManager, times(1)).scheduleTransaction(eq(app.thread),
+                eq(r.appToken), any(DestroyActivityItem.class));
+        assertTrue(r.isState(DESTROYED, DESTROYING));
+
+        mStack.destroyActivityLocked(r, true, "second invocation");
+        verify(lifecycleManager, times(1)).scheduleTransaction(eq(app.thread),
+                eq(r.appToken), any(DestroyActivityItem.class));
+    }
 }