TAPL: Optimization: avoid getting actual events twice

Getting events is an expensive operation involving reading logcat. We
did this al least twice even if the number of events was already
sufficient. Fixing this.

Change-Id: I5bfb57e3b573c4fc3f0327512327750dbb35af2f
diff --git a/tests/tapl/com/android/launcher3/tapl/LauncherInstrumentation.java b/tests/tapl/com/android/launcher3/tapl/LauncherInstrumentation.java
index 196c6b7..bd3671b 100644
--- a/tests/tapl/com/android/launcher3/tapl/LauncherInstrumentation.java
+++ b/tests/tapl/com/android/launcher3/tapl/LauncherInstrumentation.java
@@ -385,7 +385,7 @@
         log("Hierarchy dump for: " + message);
         dumpViewHierarchy();
 
-        final String eventMismatch = getEventMismatchMessage();
+        final String eventMismatch = getEventMismatchMessage(false);
 
         if (eventMismatch != null) {
             message = message + ",\nhaving produced wrong events:\n    " + eventMismatch;
@@ -1170,14 +1170,7 @@
                 return; // There was a failure. Noo need to report another one.
             }
 
-            // Wait until Launcher generates expected number of events.
-            final long endTime = SystemClock.uptimeMillis() + WAIT_TIME_MS;
-            while (SystemClock.uptimeMillis() < endTime
-                    && getEvents().size() < mExpectedEvents.size()) {
-                SystemClock.sleep(100);
-            }
-
-            final String message = getEventMismatchMessage();
+            final String message = getEventMismatchMessage(true);
             if (message != null) {
                 Assert.fail(formatSystemHealthMessage(
                         "http://go/tapl : unexpected event sequence: " + message));
@@ -1189,11 +1182,21 @@
         if (mExpectedEvents != null) mExpectedEvents.add(expected);
     }
 
-    private String getEventMismatchMessage() {
+    private String getEventMismatchMessage(boolean waitForExpectedCount) {
         if (mExpectedEvents == null) return null;
 
         try {
-            final List<String> actual = getEvents();
+            List<String> actual = getEvents();
+
+            if (waitForExpectedCount) {
+                // Wait until Launcher generates the expected number of events.
+                final long endTime = SystemClock.uptimeMillis() + WAIT_TIME_MS;
+                while (SystemClock.uptimeMillis() < endTime
+                        && actual.size() < mExpectedEvents.size()) {
+                    SystemClock.sleep(100);
+                    actual = getEvents();
+                }
+            }
 
             for (int i = 0; i < mExpectedEvents.size(); ++i) {
                 if (i >= actual.size()) {