LogEventChecker.java 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * Copyright (C) 2020 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.android.launcher3.tapl;
  17. import android.os.SystemClock;
  18. import com.android.launcher3.testing.TestProtocol;
  19. import java.util.ArrayList;
  20. import java.util.HashMap;
  21. import java.util.List;
  22. import java.util.Map;
  23. import java.util.regex.Pattern;
  24. /**
  25. * Utility class to verify expected events.
  26. */
  27. public class LogEventChecker {
  28. private final LauncherInstrumentation mLauncher;
  29. // Map from an event sequence name to an ordered list of expected events in that sequence.
  30. private final ListMap<Pattern> mExpectedEvents = new ListMap<>();
  31. LogEventChecker(LauncherInstrumentation launcher) {
  32. mLauncher = launcher;
  33. }
  34. boolean start() {
  35. mExpectedEvents.clear();
  36. return mLauncher.getTestInfo(TestProtocol.REQUEST_START_EVENT_LOGGING) != null;
  37. }
  38. void expectPattern(String sequence, Pattern pattern) {
  39. mExpectedEvents.add(sequence, pattern);
  40. }
  41. // Waits for the expected number of events and returns them.
  42. private ListMap<String> finishSync(long waitForExpectedCountMs) {
  43. final long startTime = SystemClock.uptimeMillis();
  44. // Event strings with '/' separating the sequence and the event.
  45. ArrayList<String> rawEvents;
  46. while (true) {
  47. rawEvents = mLauncher.getTestInfo(TestProtocol.REQUEST_GET_TEST_EVENTS)
  48. .getStringArrayList(TestProtocol.TEST_INFO_RESPONSE_FIELD);
  49. final int expectedCount = mExpectedEvents.entrySet()
  50. .stream().mapToInt(e -> e.getValue().size()).sum();
  51. if (rawEvents.size() >= expectedCount
  52. || SystemClock.uptimeMillis() > startTime + waitForExpectedCountMs) {
  53. break;
  54. }
  55. SystemClock.sleep(100);
  56. }
  57. finishNoWait();
  58. // Parse raw events into a map.
  59. final ListMap<String> eventSequences = new ListMap<>();
  60. for (String rawEvent : rawEvents) {
  61. final String[] split = rawEvent.split("/");
  62. eventSequences.add(split[0], split[1]);
  63. }
  64. return eventSequences;
  65. }
  66. void finishNoWait() {
  67. mLauncher.getTestInfo(TestProtocol.REQUEST_STOP_EVENT_LOGGING);
  68. }
  69. String verify(long waitForExpectedCountMs, boolean successfulGesture) {
  70. final ListMap<String> actualEvents = finishSync(waitForExpectedCountMs);
  71. final StringBuilder sb = new StringBuilder();
  72. boolean hasMismatches = false;
  73. for (Map.Entry<String, List<Pattern>> expectedEvents : mExpectedEvents.entrySet()) {
  74. String sequence = expectedEvents.getKey();
  75. List<String> actual = new ArrayList<>(actualEvents.getNonNull(sequence));
  76. final int mismatchPosition = getMismatchPosition(expectedEvents.getValue(), actual);
  77. hasMismatches = hasMismatches
  78. || mismatchPosition != -1 && !ignoreMistatch(successfulGesture, sequence);
  79. formatSequenceWithMismatch(
  80. sb,
  81. sequence,
  82. expectedEvents.getValue(),
  83. actual,
  84. mismatchPosition);
  85. }
  86. // Check for unexpected event sequences in the actual data.
  87. for (String actualNamedSequence : actualEvents.keySet()) {
  88. if (!mExpectedEvents.containsKey(actualNamedSequence)) {
  89. hasMismatches = hasMismatches
  90. || !ignoreMistatch(successfulGesture, actualNamedSequence);
  91. formatSequenceWithMismatch(
  92. sb,
  93. actualNamedSequence,
  94. new ArrayList<>(),
  95. actualEvents.get(actualNamedSequence),
  96. 0);
  97. }
  98. }
  99. return hasMismatches ? "mismatching events: " + sb.toString() : null;
  100. }
  101. // Workaround for b/154157191
  102. private static boolean ignoreMistatch(boolean successfulGesture, String sequence) {
  103. // b/156287114
  104. return false;
  105. // return TestProtocol.SEQUENCE_TIS.equals(sequence) && successfulGesture;
  106. }
  107. // If the list of actual events matches the list of expected events, returns -1, otherwise
  108. // the position of the mismatch.
  109. private static int getMismatchPosition(List<Pattern> expected, List<String> actual) {
  110. for (int i = 0; i < expected.size(); ++i) {
  111. if (i >= actual.size()
  112. || !expected.get(i).matcher(actual.get(i)).find()) {
  113. return i;
  114. }
  115. }
  116. if (actual.size() > expected.size()) return expected.size();
  117. return -1;
  118. }
  119. private static void formatSequenceWithMismatch(
  120. StringBuilder sb,
  121. String sequenceName,
  122. List<Pattern> expected,
  123. List<String> actualEvents,
  124. int mismatchPosition) {
  125. sb.append("\n>> SEQUENCE " + sequenceName + " - "
  126. + (mismatchPosition == -1 ? "MATCH" : "MISMATCH"));
  127. sb.append("\n EXPECTED:");
  128. formatEventListWithMismatch(sb, expected, mismatchPosition);
  129. sb.append("\n ACTUAL:");
  130. formatEventListWithMismatch(sb, actualEvents, mismatchPosition);
  131. }
  132. private static void formatEventListWithMismatch(StringBuilder sb, List events, int position) {
  133. for (int i = 0; i < events.size(); ++i) {
  134. sb.append("\n | ");
  135. sb.append(i == position ? "---> " : " ");
  136. sb.append(events.get(i).toString());
  137. }
  138. if (position == events.size()) sb.append("\n | ---> (end)");
  139. }
  140. private static class ListMap<T> extends HashMap<String, List<T>> {
  141. void add(String key, T value) {
  142. getNonNull(key).add(value);
  143. }
  144. List<T> getNonNull(String key) {
  145. List<T> list = get(key);
  146. if (list == null) {
  147. list = new ArrayList<>();
  148. put(key, list);
  149. }
  150. return list;
  151. }
  152. }
  153. }