Home | History | Annotate | Download | only in metrics
      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.ITestDevice;
     23 import java.util.ArrayList;
     24 import java.util.List;
     25 import java.util.Objects;
     26 
     27 /**
     28  * Helper class to assert <code>DevicePolicyEvent</code> atoms were logged.
     29  */
     30 public final class DevicePolicyEventLogVerifier {
     31 
     32     public interface Action {
     33         void apply() throws Exception;
     34     }
     35 
     36     private static final int WAIT_TIME_SHORT = 500;
     37 
     38     /**
     39      * Asserts that <code>expectedLogs</code> were logged as a result of executing
     40      * <code>action</code>, in the same order.
     41      */
     42     public static void assertMetricsLogged(ITestDevice device, Action action,
     43             DevicePolicyEventWrapper... expectedLogs) throws Exception {
     44         final AtomMetricTester logVerifier = new AtomMetricTester(device);
     45         if (logVerifier.isStatsdDisabled()) {
     46             return;
     47         }
     48         try {
     49             logVerifier.cleanLogs();
     50             logVerifier.createAndUploadConfig(Atom.DEVICE_POLICY_EVENT_FIELD_NUMBER);
     51             action.apply();
     52 
     53             Thread.sleep(WAIT_TIME_SHORT);
     54 
     55             final List<EventMetricData> data = logVerifier.getEventMetricDataList();
     56             for (DevicePolicyEventWrapper expectedLog : expectedLogs) {
     57                 assertExpectedMetricLogged(data, expectedLog);
     58             }
     59         } finally {
     60             logVerifier.cleanLogs();
     61         }
     62     }
     63 
     64     private static void assertExpectedMetricLogged(List<EventMetricData> data,
     65             DevicePolicyEventWrapper expectedLog) {
     66         final List<DevicePolicyEventWrapper> closestMatches = new ArrayList<>();
     67         AtomMetricTester.dropWhileNot(data, atom -> {
     68             final DevicePolicyEventWrapper actualLog =
     69                     DevicePolicyEventWrapper.fromDevicePolicyAtom(atom.getDevicePolicyEvent());
     70             if (actualLog.getEventId() == expectedLog.getEventId()) {
     71                 closestMatches.add(actualLog);
     72             }
     73             return Objects.equals(actualLog, expectedLog);
     74         });
     75         assertWithMessage("Expected metric was not logged.")
     76                 .that(closestMatches).contains(expectedLog);
     77     }
     78 }