Return resume result in resumeFocusedStacksTopActivities

The method addToFinishingAndWaitForIdle cannot know the next home
was successful resume because the last result of
resumeTopActivityUncheckedLocked was not returned, so FallbackHome
can be destroy immediately without wait next home activity become
idled.

Add test testCompleteFinishing_lastActivityAboveEmptyHomeStack

Bug: 143199498
Test: atest ActivityRecordTests
Test: atest MultiDisplaySystemDecorationTests ActivityVisibilityTests
SplitScreenTests PinnedStackTests
Change-Id: Icbe436afd66d2c2e5a9b884433e2fea84c119513
(cherry picked from commit a90ef31332bdfe2576994841b312f4bac2ce6bcb)
diff --git a/services/core/java/com/android/server/wm/RootActivityContainer.java b/services/core/java/com/android/server/wm/RootActivityContainer.java
index 9db6dc2..51a3e720 100644
--- a/services/core/java/com/android/server/wm/RootActivityContainer.java
+++ b/services/core/java/com/android/server/wm/RootActivityContainer.java
@@ -1154,7 +1154,7 @@
                 // activity is started and resumed, and no recursion occurs.
                 final ActivityStack focusedStack = display.getFocusedStack();
                 if (focusedStack != null) {
-                    focusedStack.resumeTopActivityUncheckedLocked(target, targetOptions);
+                    result |= focusedStack.resumeTopActivityUncheckedLocked(target, targetOptions);
                 }
             }
         }
diff --git a/services/tests/wmtests/src/com/android/server/wm/ActivityRecordTests.java b/services/tests/wmtests/src/com/android/server/wm/ActivityRecordTests.java
index aceb633..7bd92aa 100644
--- a/services/tests/wmtests/src/com/android/server/wm/ActivityRecordTests.java
+++ b/services/tests/wmtests/src/com/android/server/wm/ActivityRecordTests.java
@@ -836,6 +836,14 @@
         // Set process to 'null' to allow immediate removal, but don't call mActivity.setProcess() -
         // this will cause NPE when updating task's process.
         mActivity.app = null;
+
+        // If mActivity is at the last stack on display, it will wait till idle before destroyed, to
+        // test that it can be destroy, there will need another resumed activity.
+        final ActivityRecord topActivity = new ActivityBuilder(mService).setTask(mTask).build();
+        topActivity.visible = true;
+        topActivity.nowVisible = true;
+        topActivity.setState(RESUMED, "true");
+
         assertEquals("Activity outside of task/stack cannot be finished", FINISH_RESULT_REMOVED,
                 mActivity.finishIfPossible("test", false /* oomAdj */));
         assertTrue(mActivity.finishing);
@@ -1102,8 +1110,11 @@
         // Add another stack to become focused and make the activity there visible. This way it
         // simulates finishing in non-focused stack in split-screen.
         final ActivityStack stack = new StackBuilder(mRootActivityContainer).build();
-        stack.getChildAt(0).getChildAt(0).nowVisible = true;
-        stack.getChildAt(0).getChildAt(0).visible = true;
+        final ActivityRecord focusActivity = stack.getChildAt(0).getChildAt(0);
+        focusActivity.nowVisible = true;
+        focusActivity.visible = true;
+        focusActivity.setState(RESUMED, "test");
+        stack.mResumedActivity = focusActivity;
 
         topActivity.completeFinishing("test");
 
@@ -1151,6 +1162,31 @@
     }
 
     /**
+     * Verify that complete finish request for visible activity must resume next home stack before
+     * destroy it immediately if it is the last running activity on a display with a home stack. We
+     * must wait for home activity to come up to avoid a black flash in this case.
+     */
+    @Test
+    public void testCompleteFinishing_lastActivityAboveEmptyHomeStack() {
+        // Empty the home stack.
+        final ActivityStack homeStack = mActivity.getDisplay().getHomeStack();
+        for (TaskRecord t : homeStack.getAllTasks()) {
+            homeStack.removeTask(t, "test", REMOVE_TASK_MODE_DESTROYING);
+        }
+        mActivity.finishing = true;
+        spyOn(mStack);
+
+        // Try to finish the last activity above the home stack.
+        mActivity.completeFinishing("test");
+
+        // Verify that the activity was not destroyed immediately, but waits for next one to come up
+        // instead.
+        verify(mActivity, never()).destroyImmediately(eq(true) /* removeFromApp */, anyString());
+        assertEquals(FINISHING, mActivity.getState());
+        assertTrue(mActivity.mStackSupervisor.mFinishingActivities.contains(mActivity));
+    }
+
+    /**
      * Test that the activity will be moved to destroying state and the message to destroy will be
      * sent to the client.
      */