[CTS] Change the 'wait' scheme for AccessibilityWindowQueryTest

When testing 'split screen' and looking for the window divider,
check for the divider at each WINDOW_CHANGED event until the
expected state is seen (or timeout). This should reduce the typical
execution time and avoid false failures.

The testWindowDockAndUndock_dividerWindowAppearsAndDisappears() test
previously failed on the Emulator.

Bug: 37251132

Test: This is a CTS test

Verified that this modified CTS test passes on Nexus 5X/Bullhead.
(Patch set 3 with Nexus 5X running OPR1.170623.010 dev-keys.)

Change-Id: I5c1d80c81e11f53850a0f1becb81b901eafc4923
diff --git a/tests/accessibilityservice/src/android/accessibilityservice/cts/AccessibilityWindowQueryTest.java b/tests/accessibilityservice/src/android/accessibilityservice/cts/AccessibilityWindowQueryTest.java
index e9a8fd7..31292c3 100755
--- a/tests/accessibilityservice/src/android/accessibilityservice/cts/AccessibilityWindowQueryTest.java
+++ b/tests/accessibilityservice/src/android/accessibilityservice/cts/AccessibilityWindowQueryTest.java
@@ -66,7 +66,22 @@
                     return (event.getEventType() == AccessibilityEvent.TYPE_WINDOWS_CHANGED);
                 }
             };
-
+    private final UiAutomation.AccessibilityEventFilter mDividerPresentFilter =
+            new UiAutomation.AccessibilityEventFilter() {
+                @Override
+                public boolean accept(AccessibilityEvent event) {
+                    return (event.getEventType() == AccessibilityEvent.TYPE_WINDOWS_CHANGED &&
+                            isDividerWindowPresent(getInstrumentation().getUiAutomation())    );
+                }
+            };
+    private final UiAutomation.AccessibilityEventFilter mDividerAbsentFilter =
+            new UiAutomation.AccessibilityEventFilter() {
+                @Override
+                public boolean accept(AccessibilityEvent event) {
+                    return (event.getEventType() == AccessibilityEvent.TYPE_WINDOWS_CHANGED &&
+                            !isDividerWindowPresent(getInstrumentation().getUiAutomation())   );
+                }
+            };
 
     public AccessibilityWindowQueryTest() {
         super(AccessibilityWindowQueryActivity.class);
@@ -620,14 +635,11 @@
             }
         };
 
-        uiAutomation.executeAndWaitForEvent(toggleSplitScreenRunnable, mWindowsChangedFilter,
+        uiAutomation.executeAndWaitForEvent(toggleSplitScreenRunnable, mDividerPresentFilter,
                 TIMEOUT_ASYNC_PROCESSING);
-        waitForIdle();
-        assertTrue(isDividerWindowPresent(uiAutomation));
-        uiAutomation.executeAndWaitForEvent(toggleSplitScreenRunnable, mWindowsChangedFilter,
+
+        uiAutomation.executeAndWaitForEvent(toggleSplitScreenRunnable, mDividerAbsentFilter,
                 TIMEOUT_ASYNC_PROCESSING);
-        waitForIdle();
-        assertFalse(isDividerWindowPresent(uiAutomation));
     }
 
     public void testFindPictureInPictureWindow() throws Exception {