1 /* 2 * Copyright (C) 2017 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 android.support.test.metricshelper; 17 18 import static junit.framework.Assert.assertTrue; 19 20 import android.metrics.LogMaker; 21 import android.metrics.MetricsReader; 22 23 import com.android.internal.logging.nano.MetricsProto.MetricsEvent; 24 25 import java.util.Collection; 26 import java.util.Iterator; 27 import java.util.LinkedList; 28 import java.util.Queue; 29 30 /** 31 * Useful test utilities for metrics tests. 32 */ 33 public class MetricsAsserts { 34 35 /** 36 * Assert unless there is a log with the matching category and with ACTION type. 37 */ assertHasActionLog(String message, MetricsReader reader, int view)38 public static void assertHasActionLog(String message, MetricsReader reader, int view) { 39 reader.read(0); 40 assertHasActionLog(message, new ReaderQueue(reader), view); 41 } 42 /** 43 * Assert unless there is a log with the matching category and with ACTION type. 44 */ assertHasActionLog(String message, Queue<LogMaker> queue, int view)45 public static void assertHasActionLog(String message, Queue<LogMaker> queue, int view) { 46 Queue<LogMaker> logs = findMatchingLogs(queue, 47 new LogMaker(view) 48 .setType(MetricsEvent.TYPE_ACTION)); 49 assertTrue(message, !logs.isEmpty()); 50 } 51 52 /** 53 * Assert unless there is a log with the matching category and with visibility type. 54 */ assertHasVisibilityLog(String message, MetricsReader reader, int view, boolean visible)55 public static void assertHasVisibilityLog(String message, MetricsReader reader, 56 int view, boolean visible) { 57 reader.read(0); 58 assertHasVisibilityLog(message, new ReaderQueue(reader), view, visible); 59 } 60 61 /** 62 * Assert unless there is a log with the matching category and with visibility type. 63 */ assertHasVisibilityLog(String message, Queue<LogMaker> queue, int view, boolean visible)64 public static void assertHasVisibilityLog(String message, Queue<LogMaker> queue, 65 int view, boolean visible) { 66 Queue<LogMaker> logs = findMatchingLogs(queue, 67 new LogMaker(view) 68 .setType(visible ? MetricsEvent.TYPE_OPEN : MetricsEvent.TYPE_CLOSE)); 69 assertTrue(message, !logs.isEmpty()); 70 } 71 72 /** 73 * @returns logs that have at least all the matching fields in the template. 74 */ findMatchingLogs(MetricsReader reader, LogMaker template)75 public static Queue<LogMaker> findMatchingLogs(MetricsReader reader, LogMaker template) { 76 reader.read(0); 77 return findMatchingLogs(new ReaderQueue(reader), template); 78 } 79 80 /** 81 * @returns logs that have at least all the matching fields in the template. 82 */ findMatchingLogs(Queue<LogMaker> queue, LogMaker template)83 public static Queue<LogMaker> findMatchingLogs(Queue<LogMaker> queue, LogMaker template) { 84 LinkedList<LogMaker> logs = new LinkedList<>(); 85 if (template == null) { 86 return logs; 87 } 88 while (!queue.isEmpty()) { 89 LogMaker b = queue.poll(); 90 if (template.isSubsetOf(b)) { 91 logs.push(b); 92 } 93 } 94 return logs; 95 } 96 97 /** 98 * Assert unless there is at least one log that matches the template. 99 */ assertHasLog(String message, MetricsReader reader, LogMaker expected)100 public static void assertHasLog(String message, MetricsReader reader, LogMaker expected) { 101 reader.read(0); 102 assertHasLog(message, new ReaderQueue(reader), expected); 103 } 104 105 /** 106 * Assert unless there is at least one log that matches the template. 107 */ assertHasLog(String message, Queue<LogMaker> queue, LogMaker expected)108 public static void assertHasLog(String message, Queue<LogMaker> queue, LogMaker expected) { 109 assertTrue(message, !findMatchingLogs(queue, expected).isEmpty()); 110 } 111 112 private static class ReaderQueue implements Queue<LogMaker> { 113 114 private final MetricsReader mMetricsReader; 115 ReaderQueue(MetricsReader metricsReader)116 ReaderQueue(MetricsReader metricsReader) { 117 mMetricsReader = metricsReader; 118 } 119 120 @Override isEmpty()121 public boolean isEmpty() { 122 return !mMetricsReader.hasNext(); 123 } 124 125 @Override poll()126 public LogMaker poll() { 127 return mMetricsReader.next(); 128 } 129 130 @Override add(LogMaker logMaker)131 public boolean add(LogMaker logMaker) { 132 throw new UnsupportedOperationException("unimplemented fake method"); 133 } 134 135 @Override addAll(Collection<? extends LogMaker> collection)136 public boolean addAll(Collection<? extends LogMaker> collection) { 137 throw new UnsupportedOperationException("unimplemented fake method"); 138 } 139 140 @Override clear()141 public void clear() { 142 throw new UnsupportedOperationException("unimplemented fake method"); 143 } 144 145 @Override contains(Object object)146 public boolean contains(Object object) { 147 throw new UnsupportedOperationException("unimplemented fake method"); 148 } 149 150 @Override containsAll(Collection<?> collection)151 public boolean containsAll(Collection<?> collection) { 152 throw new UnsupportedOperationException("unimplemented fake method"); 153 } 154 155 @Override iterator()156 public Iterator<LogMaker> iterator() { 157 throw new UnsupportedOperationException("unimplemented fake method"); 158 } 159 160 @Override remove(Object object)161 public boolean remove(Object object) { 162 throw new UnsupportedOperationException("unimplemented fake method"); 163 } 164 165 @Override removeAll(Collection<?> collection)166 public boolean removeAll(Collection<?> collection) { 167 throw new UnsupportedOperationException("unimplemented fake method"); 168 } 169 170 @Override retainAll(Collection<?> collection)171 public boolean retainAll(Collection<?> collection) { 172 throw new UnsupportedOperationException("unimplemented fake method"); 173 } 174 175 @Override size()176 public int size() { 177 throw new UnsupportedOperationException("unimplemented fake method"); 178 } 179 180 @Override toArray()181 public Object[] toArray() { 182 throw new UnsupportedOperationException("unimplemented fake method"); 183 } 184 185 @Override toArray(T[] array)186 public <T> T[] toArray(T[] array) { 187 throw new UnsupportedOperationException("unimplemented fake method"); 188 } 189 190 @Override offer(LogMaker logMaker)191 public boolean offer(LogMaker logMaker) { 192 throw new UnsupportedOperationException("unimplemented fake method"); 193 } 194 195 @Override remove()196 public LogMaker remove() { 197 throw new UnsupportedOperationException("unimplemented fake method"); 198 } 199 200 @Override element()201 public LogMaker element() { 202 throw new UnsupportedOperationException("unimplemented fake method"); 203 } 204 205 @Override peek()206 public LogMaker peek() { 207 throw new UnsupportedOperationException("unimplemented fake method"); 208 } 209 } 210 } 211