Home | History | Annotate | Download | only in testtype
      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 com.android.tradefed.testtype;
     17 
     18 import com.android.tradefed.metrics.proto.MetricMeasurement.Metric;
     19 import com.android.tradefed.result.InputStreamSource;
     20 import com.android.tradefed.result.LogDataType;
     21 import com.android.tradefed.result.SnapshotInputStreamSource;
     22 import com.android.tradefed.util.proto.TfMetricProtoUtil;
     23 
     24 import junit.framework.TestCase;
     25 
     26 import java.util.ArrayList;
     27 import java.util.HashMap;
     28 import java.util.List;
     29 
     30 /**
     31  * Extension of {@link TestCase} that allows to log metrics when running as part of TradeFed. Either
     32  * directly as a {@link DeviceTestCase} or as part of a {@link HostTest}. TODO: Evaluate if having
     33  * run metric (not only test metric) make sense for JUnit3 tests.
     34  */
     35 public class MetricTestCase extends TestCase {
     36 
     37     public HashMap<String, Metric> mMetrics = new HashMap<>();
     38     public List<LogHolder> mLogs = new ArrayList<>();
     39 
     40     public MetricTestCase() {
     41         super();
     42     }
     43 
     44     /** Constructs a test case with the given name. Inherited from {@link TestCase} constructor. */
     45     public MetricTestCase(String name) {
     46         super(name);
     47     }
     48 
     49     /**
     50      * Log a metric for the test case.
     51      *
     52      * @param key the key under which the metric will be found.
     53      * @param value associated to the key.
     54      */
     55     public final void addTestMetric(String key, String value) {
     56         mMetrics.put(key, TfMetricProtoUtil.stringToMetric(value));
     57     }
     58 
     59     public final void addTestMetric(String key, Metric metric) {
     60         mMetrics.put(key, metric);
     61     }
     62 
     63     /**
     64      * Callback from JUnit3 forwarder in order to get the logs from a test.
     65      *
     66      * @param dataName a String descriptive name of the data. e.g. "device_logcat". Note dataName
     67      *     may not be unique per invocation. ie implementers must be able to handle multiple calls
     68      *     with same dataName
     69      * @param dataType the LogDataType of the data
     70      * @param dataStream the InputStreamSource of the data. Implementers should call
     71      *     createInputStream to start reading the data, and ensure to close the resulting
     72      *     InputStream when complete. Callers should ensure the source of the data remains present
     73      *     and accessible until the testLog method completes.
     74      */
     75     public final void addTestLog(
     76             String dataName, LogDataType dataType, InputStreamSource dataStream) {
     77         mLogs.add(new LogHolder(dataName, dataType, dataStream));
     78     }
     79 
     80     /** Structure to hold a log file to be reported. */
     81     public static class LogHolder {
     82         public final String mDataName;
     83         public final LogDataType mDataType;
     84         public final InputStreamSource mDataStream;
     85 
     86         public LogHolder(String dataName, LogDataType dataType, InputStreamSource dataStream) {
     87             mDataName = dataName;
     88             mDataType = dataType;
     89             // We hold a copy because the caller will most likely cancel the stream after.
     90             mDataStream =
     91                     new SnapshotInputStreamSource("LogHolder", dataStream.createInputStream());
     92         }
     93     }
     94 }
     95