1 /* 2 * Copyright (C) 2018 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.cts.devicepolicy.metrics; 17 18 import static com.google.common.truth.Truth.assertWithMessage; 19 20 import com.android.os.AtomsProto.Atom; 21 import com.android.os.StatsLog.EventMetricData; 22 import com.android.tradefed.device.DeviceNotAvailableException; 23 import com.android.tradefed.device.ITestDevice; 24 import java.util.ArrayList; 25 import java.util.List; 26 import java.util.Objects; 27 28 /** 29 * Helper class to assert <code>DevicePolicyEvent</code> atoms were logged. 30 */ 31 public final class DevicePolicyEventLogVerifier { 32 33 public interface Action { apply()34 void apply() throws Exception; 35 } 36 37 private static final int WAIT_TIME_SHORT = 500; 38 39 /** 40 * Asserts that <code>expectedLogs</code> were logged as a result of executing 41 * <code>action</code>, in the same order. 42 */ assertMetricsLogged(ITestDevice device, Action action, DevicePolicyEventWrapper... expectedLogs)43 public static void assertMetricsLogged(ITestDevice device, Action action, 44 DevicePolicyEventWrapper... expectedLogs) throws Exception { 45 final AtomMetricTester logVerifier = new AtomMetricTester(device); 46 try { 47 logVerifier.cleanLogs(); 48 logVerifier.createAndUploadConfig(Atom.DEVICE_POLICY_EVENT_FIELD_NUMBER); 49 action.apply(); 50 51 Thread.sleep(WAIT_TIME_SHORT); 52 53 final List<EventMetricData> data = logVerifier.getEventMetricDataList(); 54 for (DevicePolicyEventWrapper expectedLog : expectedLogs) { 55 assertExpectedMetricLogged(data, expectedLog); 56 } 57 } finally { 58 logVerifier.cleanLogs(); 59 } 60 } 61 62 /** 63 * Asserts that <code>expectedLogs</code> were not logged as a result of executing 64 * <code>action</code>. 65 */ assertMetricsNotLogged(ITestDevice device, Action action, DevicePolicyEventWrapper... expectedLogs)66 public static void assertMetricsNotLogged(ITestDevice device, Action action, 67 DevicePolicyEventWrapper... expectedLogs) throws Exception { 68 final AtomMetricTester logVerifier = new AtomMetricTester(device); 69 try { 70 logVerifier.cleanLogs(); 71 logVerifier.createAndUploadConfig(Atom.DEVICE_POLICY_EVENT_FIELD_NUMBER); 72 action.apply(); 73 74 Thread.sleep(WAIT_TIME_SHORT); 75 76 final List<EventMetricData> data = logVerifier.getEventMetricDataList(); 77 for (DevicePolicyEventWrapper expectedLog : expectedLogs) { 78 assertExpectedMetricNotLogged(data, expectedLog); 79 } 80 } finally { 81 logVerifier.cleanLogs(); 82 } 83 } 84 assertExpectedMetricLogged(List<EventMetricData> data, DevicePolicyEventWrapper expectedLog)85 private static void assertExpectedMetricLogged(List<EventMetricData> data, 86 DevicePolicyEventWrapper expectedLog) { 87 assertWithMessage("Expected metric was not logged.") 88 .that(isExpectedMetricLogged(data, expectedLog)).isTrue(); 89 } 90 assertExpectedMetricNotLogged(List<EventMetricData> data, DevicePolicyEventWrapper expectedLog)91 private static void assertExpectedMetricNotLogged(List<EventMetricData> data, 92 DevicePolicyEventWrapper expectedLog) { 93 assertWithMessage("Expected metric was logged.") 94 .that(isExpectedMetricLogged(data, expectedLog)).isFalse(); 95 } 96 isExpectedMetricLogged(List<EventMetricData> data, DevicePolicyEventWrapper expectedLog)97 private static boolean isExpectedMetricLogged(List<EventMetricData> data, 98 DevicePolicyEventWrapper expectedLog) { 99 final List<DevicePolicyEventWrapper> closestMatches = new ArrayList<>(); 100 AtomMetricTester.dropWhileNot(data, atom -> { 101 final DevicePolicyEventWrapper actualLog = 102 DevicePolicyEventWrapper.fromDevicePolicyAtom(atom.getDevicePolicyEvent()); 103 if (actualLog.getEventId() == expectedLog.getEventId()) { 104 closestMatches.add(actualLog); 105 } 106 return Objects.equals(actualLog, expectedLog); 107 }); 108 return closestMatches.contains(expectedLog); 109 } 110 } 111