Fix flaky issue

When getting the accessibility window after changing the focused window
of the activity from default display to virtual display, to make sure
getting the latest one, needing to wait receiving the event,
WINDOWS_CHANGE_FOCUSED or WINDOWS_CHANGE_ACTIVE.

Bug: 144845224
Test: atest --iterations 20 AccessibilityWindowReportingTest#
moveFocusToAnotherDisplay_movesActiveAndFocusWindow

Change-Id: Ic8271a1e0e4cdb76efd237bdc8b33328fd9be5e0
diff --git a/tests/accessibilityservice/src/android/accessibilityservice/cts/AccessibilityWindowReportingTest.java b/tests/accessibilityservice/src/android/accessibilityservice/cts/AccessibilityWindowReportingTest.java
index eb5e2b4..3e253a7 100644
--- a/tests/accessibilityservice/src/android/accessibilityservice/cts/AccessibilityWindowReportingTest.java
+++ b/tests/accessibilityservice/src/android/accessibilityservice/cts/AccessibilityWindowReportingTest.java
@@ -261,22 +261,29 @@
             // Window manager changed the behavior of focused window at a virtual display. A window
             // at virtual display needs to be touched then it becomes to be focused one. Adding this
             // touch event on the activity window of the virtual display to pass this test case.
-            final DisplayMetrics displayMetrics = mActivity.getResources().getDisplayMetrics();
-            final int xOnScreen = displayMetrics.widthPixels / 2;
-            final int yOnScreen = displayMetrics.heightPixels / 2;
-            final long downEventTime = SystemClock.uptimeMillis();
-            final MotionEvent downEvent = MotionEvent.obtain(downEventTime, downEventTime,
-                    MotionEvent.ACTION_DOWN, xOnScreen, yOnScreen, 0);
-            downEvent.setSource(InputDevice.SOURCE_TOUCHSCREEN);
-            downEvent.setDisplayId(virtualDisplayId);
-            sUiAutomation.injectInputEvent(downEvent, true);
+            sUiAutomation.executeAndWaitForEvent(
+                    () -> {
+                        final DisplayMetrics displayMetrics =
+                                mActivity.getResources().getDisplayMetrics();
+                        final int xOnScreen = displayMetrics.widthPixels / 2;
+                        final int yOnScreen = displayMetrics.heightPixels / 2;
+                        final long downEventTime = SystemClock.uptimeMillis();
+                        final MotionEvent downEvent = MotionEvent.obtain(downEventTime,
+                                downEventTime, MotionEvent.ACTION_DOWN, xOnScreen, yOnScreen, 0);
+                        downEvent.setSource(InputDevice.SOURCE_TOUCHSCREEN);
+                        downEvent.setDisplayId(virtualDisplayId);
+                        sUiAutomation.injectInputEvent(downEvent, true);
 
-            final long upEventTime = downEventTime + 10;
-            final MotionEvent upEvent = MotionEvent.obtain(downEventTime, upEventTime,
-                    MotionEvent.ACTION_UP, xOnScreen, yOnScreen, 0);
-            upEvent.setSource(InputDevice.SOURCE_TOUCHSCREEN);
-            upEvent.setDisplayId(virtualDisplayId);
-            sUiAutomation.injectInputEvent(upEvent, true);
+                        final long upEventTime = downEventTime + 10;
+                        final MotionEvent upEvent = MotionEvent.obtain(downEventTime, upEventTime,
+                                MotionEvent.ACTION_UP, xOnScreen, yOnScreen, 0);
+                        upEvent.setSource(InputDevice.SOURCE_TOUCHSCREEN);
+                        upEvent.setDisplayId(virtualDisplayId);
+                        sUiAutomation.injectInputEvent(upEvent, true);
+                    },
+                    filterWindowsChangedWithChangeTypes(WINDOWS_CHANGE_FOCUSED |
+                            WINDOWS_CHANGE_ACTIVE),
+                    TIMEOUT_ASYNC_PROCESSING);
 
             final CharSequence activityTitle = getActivityTitle(sInstrumentation,
                     activityOnVirtualDisplay);
@@ -428,4 +435,4 @@
         });
         return params;
     }
-}
\ No newline at end of file
+}