Fix task movement if top running activity of focused stack is null

If the target task is non-null and current top task is null (e.g.
the activity may be finishing or use FLAG_ACTIVITY_PREVIOUS_IS_TOP),
we should still consider they are different task so the target task
can be moved to front instead of only move its stack.

Bug: 131589476
Test: atest ActivityStarterTests#testBringTaskToFrontWhenFocusedStackIsFinising
Change-Id: Ibf8befae74aab9e308d82d2149c41cd069615988
diff --git a/services/tests/wmtests/src/com/android/server/wm/ActivityStarterTests.java b/services/tests/wmtests/src/com/android/server/wm/ActivityStarterTests.java
index 45d5219..4cb95d4 100644
--- a/services/tests/wmtests/src/com/android/server/wm/ActivityStarterTests.java
+++ b/services/tests/wmtests/src/com/android/server/wm/ActivityStarterTests.java
@@ -707,6 +707,36 @@
     }
 
     /**
+     * This test ensures that {@link ActivityStarter#setTargetStackAndMoveToFrontIfNeeded} will
+     * move the existing task to front if the current focused stack doesn't have running task.
+     */
+    @Test
+    public void testBringTaskToFrontWhenFocusedStackIsFinising() {
+        // Put 2 tasks in the same stack (simulate the behavior of home stack).
+        final ActivityRecord activity = new ActivityBuilder(mService)
+                .setCreateTask(true).build();
+        new ActivityBuilder(mService)
+                .setStack(activity.getActivityStack())
+                .setCreateTask(true).build();
+
+        // Create a top finishing activity.
+        final ActivityRecord finishingTopActivity = new ActivityBuilder(mService)
+                .setCreateTask(true).build();
+        finishingTopActivity.getActivityStack().moveToFront("finishingTopActivity");
+
+        assertEquals(finishingTopActivity, mRootActivityContainer.topRunningActivity());
+        finishingTopActivity.finishing = true;
+
+        // Launch the bottom task of the target stack.
+        prepareStarter(FLAG_ACTIVITY_NEW_TASK, false /* mockGetLaunchStack */)
+                .setReason("testBringTaskToFrontWhenTopStackIsFinising")
+                .setIntent(activity.intent)
+                .execute();
+        // The hierarchies of the activity should move to front.
+        assertEquals(activity, mRootActivityContainer.topRunningActivity());
+    }
+
+    /**
      * This test ensures that when starting an existing single task activity on secondary display
      * which is not the top focused display, it should deliver new intent to the activity and not
      * create a new stack.