Fix testTurnScreenOnSingleTask flaky.

In gce-presubmit-am "no-retries" tests, found testTurnScreenOnSingleTask
when resume single task activity after screen on, we cannot guarantee
the display state change will be done right after activity visible since
display state change is from another async thread.

Calling waitForDefaultDisplayState to wait for expected display state
to fix timing case.

Bug: 113638150
Test: atest ActivityManagerActivityVisibilityTests#testTurnScreenOnSingleTask
       to test 100 times without fail.

Change-Id: I4be748c624c2e30b871aadf058f2b4d119277440
Merged-In: I4be748c624c2e30b871aadf058f2b4d119277440
(cherry picked from commit 9ac763316d1717708e3dd1058f6b3c8bf98fc305)
diff --git a/tests/framework/base/activitymanager/src/android/server/am/ActivityManagerActivityVisibilityTests.java b/tests/framework/base/activitymanager/src/android/server/am/ActivityManagerActivityVisibilityTests.java
index ee16237..1adc553 100644
--- a/tests/framework/base/activitymanager/src/android/server/am/ActivityManagerActivityVisibilityTests.java
+++ b/tests/framework/base/activitymanager/src/android/server/am/ActivityManagerActivityVisibilityTests.java
@@ -50,6 +50,7 @@
 import static android.server.am.Components.TURN_SCREEN_ON_SINGLE_TASK_ACTIVITY;
 import static android.server.am.Components.TURN_SCREEN_ON_WITH_RELAYOUT_ACTIVITY;
 import static android.server.am.UiDeviceUtils.pressBackButton;
+import static android.server.am.VirtualDisplayHelper.waitForDefaultDisplayState;
 
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotEquals;
@@ -444,6 +445,9 @@
             logSeparator = separateLogs();
             launchActivity(TURN_SCREEN_ON_SINGLE_TASK_ACTIVITY);
             mAmWmState.assertVisibility(TURN_SCREEN_ON_SINGLE_TASK_ACTIVITY, true);
+            // Wait more for display state change since turning the display ON may take longer
+            // and reported after the activity launch.
+            waitForDefaultDisplayState(true /* wantOn */);
             assertTrue("Display turns on", isDisplayOn());
             assertSingleStart(TURN_SCREEN_ON_SINGLE_TASK_ACTIVITY, logSeparator);
         }