Home | History | Annotate | Download | only in util
      1 /*
      2  * Copyright (C) 2014 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 
     17 package com.android.compatibility.common.util;
     18 
     19 import android.app.Instrumentation;
     20 import android.os.Bundle;
     21 import android.os.Environment;
     22 import android.util.Log;
     23 
     24 import java.io.File;
     25 import java.io.IOException;
     26 import java.util.List;
     27 
     28 import androidx.test.InstrumentationRegistry;
     29 
     30 /**
     31  * Handles adding results to the report for device side tests.
     32  *
     33  * NOTE: tests MUST call {@link #submit(Instrumentation)} if and only if the test passes in order to
     34  * send the results to the runner.
     35  */
     36 public class DeviceReportLog extends ReportLog {
     37     private static final String TAG = DeviceReportLog.class.getSimpleName();
     38     private static final String RESULT = "COMPATIBILITY_TEST_RESULT";
     39     private static final int INST_STATUS_ERROR = -1;
     40     private static final int INST_STATUS_IN_PROGRESS = 2;
     41 
     42     private ReportLogDeviceInfoStore store;
     43 
     44     public DeviceReportLog(String reportLogName, String streamName) {
     45         this(reportLogName, streamName, new File(InstrumentationRegistry
     46                 .getInstrumentation().getTargetContext()
     47                 .getExternalFilesDir(null).getPath(),
     48                 "report-log-files"));
     49     }
     50 
     51     public DeviceReportLog(String reportLogName, String streamName, File logDirectory) {
     52         super(reportLogName, streamName);
     53         try {
     54             // dir value must match the src-dir value configured in ReportLogCollector target
     55             // preparer in cts/harness/tools/cts-tradefed/res/config/cts-preconditions.xml
     56             if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
     57                 throw new IOException("External storage is not mounted");
     58             } else if ((!logDirectory.exists() && !logDirectory.mkdirs())
     59                     || (logDirectory.exists() && !logDirectory.isDirectory())) {
     60                 throw new IOException("Cannot create directory for device info files");
     61             } else {
     62                 File jsonFile = new File(logDirectory, mReportLogName + ".reportlog.json");
     63                 store = new ReportLogDeviceInfoStore(jsonFile, mStreamName);
     64                 store.open();
     65             }
     66         } catch (Exception e) {
     67             Log.e(TAG, "Could not create report log file.", e);
     68         }
     69     }
     70 
     71     /**
     72      * Adds a double metric to the report.
     73      */
     74     @Override
     75     public void addValue(String source, String message, double value, ResultType type,
     76             ResultUnit unit) {
     77         super.addValue(source, message, value, type, unit);
     78         try {
     79             store.addResult(message, value);
     80         } catch (Exception e) {
     81             Log.e(TAG, "Could not log metric.", e);
     82         }
     83     }
     84 
     85     /**
     86      * Adds a double metric to the report.
     87      */
     88     @Override
     89     public void addValue(String message, double value, ResultType type, ResultUnit unit) {
     90         super.addValue(message, value, type, unit);
     91         try {
     92             store.addResult(message, value);
     93         } catch (Exception e) {
     94             Log.e(TAG, "Could not log metric.", e);
     95         }
     96     }
     97 
     98     /**
     99      * Adds a double array of metrics to the report.
    100      */
    101     @Override
    102     public void addValues(String source, String message, double[] values, ResultType type,
    103             ResultUnit unit) {
    104         super.addValues(source, message, values, type, unit);
    105         try {
    106             store.addArrayResult(message, values);
    107         } catch (Exception e) {
    108             Log.e(TAG, "Could not log metric.", e);
    109         }
    110     }
    111 
    112     /**
    113      * Adds a double array of metrics to the report.
    114      */
    115     @Override
    116     public void addValues(String message, double[] values, ResultType type, ResultUnit unit) {
    117         super.addValues(message, values, type, unit);
    118         try {
    119             store.addArrayResult(message, values);
    120         } catch (Exception e) {
    121             Log.e(TAG, "Could not log metric.", e);
    122         }
    123     }
    124 
    125     /**
    126      * Adds an int metric to the report.
    127      */
    128     @Override
    129     public void addValue(String message, int value, ResultType type, ResultUnit unit) {
    130         try {
    131             store.addResult(message, value);
    132         } catch (Exception e) {
    133             Log.e(TAG, "Could not log metric.", e);
    134         }
    135     }
    136 
    137     /**
    138      * Adds a long metric to the report.
    139      */
    140     @Override
    141     public void addValue(String message, long value, ResultType type, ResultUnit unit) {
    142         try {
    143             store.addResult(message, value);
    144         } catch (Exception e) {
    145             Log.e(TAG, "Could not log metric.", e);
    146         }
    147     }
    148 
    149     /**
    150      * Adds a float metric to the report.
    151      */
    152     @Override
    153     public void addValue(String message, float value, ResultType type, ResultUnit unit) {
    154         try {
    155             store.addResult(message, value);
    156         } catch (Exception e) {
    157             Log.e(TAG, "Could not log metric.", e);
    158         }
    159     }
    160 
    161     /**
    162      * Adds a boolean metric to the report.
    163      */
    164     @Override
    165     public void addValue(String message, boolean value, ResultType type, ResultUnit unit) {
    166         try {
    167             store.addResult(message, value);
    168         } catch (Exception e) {
    169             Log.e(TAG, "Could not log metric.", e);
    170         }
    171     }
    172 
    173     /**
    174      * Adds a String metric to the report.
    175      */
    176     @Override
    177     public void addValue(String message, String value, ResultType type, ResultUnit unit) {
    178         try {
    179             store.addResult(message, value);
    180         } catch (Exception e) {
    181             Log.e(TAG, "Could not log metric.", e);
    182         }
    183     }
    184 
    185     /**
    186      * Adds an int array of metrics to the report.
    187      */
    188     @Override
    189     public void addValues(String message, int[] values, ResultType type, ResultUnit unit) {
    190         try {
    191             store.addArrayResult(message, values);
    192         } catch (Exception e) {
    193             Log.e(TAG, "Could not log metric.", e);
    194         }
    195     }
    196 
    197     /**
    198      * Adds a long array of metrics to the report.
    199      */
    200     @Override
    201     public void addValues(String message, long[] values, ResultType type, ResultUnit unit) {
    202         try {
    203             store.addArrayResult(message, values);
    204         } catch (Exception e) {
    205             Log.e(TAG, "Could not log metric.", e);
    206         }
    207     }
    208 
    209     /**
    210      * Adds a float array of metrics to the report.
    211      */
    212     @Override
    213     public void addValues(String message, float[] values, ResultType type, ResultUnit unit) {
    214         try {
    215             store.addArrayResult(message, values);
    216         } catch (Exception e) {
    217             Log.e(TAG, "Could not log metric.", e);
    218         }
    219     }
    220 
    221     /**
    222      * Adds a boolean array of metrics to the report.
    223      */
    224     @Override
    225     public void addValues(String message, boolean[] values, ResultType type, ResultUnit unit) {
    226         try {
    227             store.addArrayResult(message, values);
    228         } catch (Exception e) {
    229             Log.e(TAG, "Could not log metric.", e);
    230         }
    231     }
    232 
    233     /**
    234      * Adds a String List of metrics to the report.
    235      */
    236     @Override
    237     public void addValues(String message, List<String> values, ResultType type, ResultUnit unit) {
    238         try {
    239             store.addListResult(message, values);
    240         } catch (Exception e) {
    241             Log.e(TAG, "Could not log metric.", e);
    242         }
    243     }
    244 
    245     /**
    246      * Sets the summary double metric of the report.
    247      *
    248      * NOTE: messages over {@value Metric#MAX_MESSAGE_LENGTH} chars will be trimmed.
    249      */
    250     @Override
    251     public void setSummary(String message, double value, ResultType type, ResultUnit unit) {
    252         super.setSummary(message, value, type, unit);
    253         try {
    254             store.addResult(message, value);
    255         } catch (Exception e) {
    256             Log.e(TAG, "Could not log metric.", e);
    257         }
    258     }
    259 
    260     /**
    261      * Closes report file and submits report to instrumentation.
    262      */
    263     public void submit(Instrumentation instrumentation) {
    264         try {
    265             store.close();
    266             Bundle output = new Bundle();
    267             output.putString(RESULT, serialize(this));
    268             instrumentation.sendStatus(INST_STATUS_IN_PROGRESS, output);
    269         } catch (Exception e) {
    270             Log.e(TAG, "ReportLog Submit Failed", e);
    271             instrumentation.sendStatus(INST_STATUS_ERROR, null);
    272         }
    273     }
    274 
    275     /**
    276      * Closes report file. Static functions that do not have access to instrumentation can
    277      * use this to close report logs. Summary, if present, is not reported to instrumentation, hence
    278      * does not appear in the result XML.
    279      */
    280     public void submit() {
    281         try {
    282             store.close();
    283         } catch (Exception e) {
    284             Log.e(TAG, "ReportLog Submit Failed", e);
    285         }
    286     }
    287 }
    288