Home | History | Annotate | Download | only in util
      1 /*
      2  * Copyright (c) 2017 Google Inc. All Rights Reserved.
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License"); you
      5  * may not use this file except in compliance with the License. You may
      6  * 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
     13  * implied. See the License for the specific language governing
     14  * permissions and limitations under the License.
     15  */
     16 
     17 package com.android.vts.util;
     18 
     19 import com.android.vts.entity.DeviceInfoEntity;
     20 import com.android.vts.entity.TestRunEntity;
     21 import com.google.gson.JsonObject;
     22 import com.google.gson.JsonPrimitive;
     23 import java.util.ArrayList;
     24 import java.util.List;
     25 import org.apache.commons.lang.StringUtils;
     26 
     27 /** Helper object for describing test results data. */
     28 public class TestRunMetadata {
     29     private static final String TEST_RUN = "testRun";
     30     private static final String TEST_DETAILS = "testDetails";
     31     private static final String DEVICE_INFO = "deviceInfo";
     32     private static final String ABI_INFO = "abiInfo";
     33     public final TestRunEntity testRun;
     34 
     35     private final List<DeviceInfoEntity> devices;
     36     private String deviceInfo;
     37     private String abiInfo;
     38     private TestRunDetails details;
     39 
     40     /**
     41      * Create a test metadata object.
     42      *
     43      * @param testName The name of the test.
     44      * @param testRun The test run entity storing run information.
     45      * @param devices The list of device entities describing the test run.
     46      */
     47     public TestRunMetadata(String testName, TestRunEntity testRun, List<DeviceInfoEntity> devices) {
     48         this.testRun = testRun;
     49         this.deviceInfo = "";
     50         this.abiInfo = "";
     51         this.details = null;
     52         this.devices = devices;
     53     }
     54 
     55     /**
     56      * Create a test metadata object.
     57      *
     58      * @param testName The name of the test.
     59      * @param testRun The test run entity storing run information.
     60      */
     61     public TestRunMetadata(String testName, TestRunEntity testRun) {
     62         this(testName, testRun, new ArrayList<DeviceInfoEntity>());
     63     }
     64 
     65     /**
     66      * Add a device info to the test run metadata.
     67      *
     68      * @param device The entity to add.
     69      */
     70     public void addDevice(DeviceInfoEntity device) {
     71         this.devices.add(device);
     72     }
     73 
     74     /** Get device information for the test run and add it to the metadata message. */
     75     private void processDeviceInfo() {
     76         List<String> deviceInfoList = new ArrayList<>();
     77         List<String> abiInfoList = new ArrayList<>();
     78 
     79         for (DeviceInfoEntity device : this.devices) {
     80             String abi = "";
     81             String abiName = device.abiName;
     82             String abiBitness = device.abiBitness;
     83             if (abiName.length() > 0) {
     84                 abi += abiName;
     85                 if (abiBitness.length() > 0) {
     86                     abi += " (" + abiBitness + " bit)";
     87                 }
     88             }
     89             abiInfoList.add(abi);
     90             deviceInfoList.add(device.getFingerprint());
     91         }
     92         this.abiInfo = StringUtils.join(abiInfoList, ", ");
     93         this.deviceInfo = StringUtils.join(deviceInfoList, ", ");
     94     }
     95 
     96     /**
     97      * Get the device info string in the test metadata.
     98      *
     99      * @return The string descriptor of the devices used in the test run.
    100      */
    101     public String getDeviceInfo() {
    102         return this.deviceInfo;
    103     }
    104 
    105     /**
    106      * Get the test run details (e.g. test case information) for the test run.
    107      *
    108      * @return The TestRunDetails object stored in the metadata, or null if not set.
    109      */
    110     public TestRunDetails getDetails() {
    111         return this.details;
    112     }
    113 
    114     /**
    115      * Add test case details to the metadata object.
    116      *
    117      * <p>Used for prefetching details on initial page load.
    118      *
    119      * @param details The TestRunDetails object storing test case results for the test run.
    120      */
    121     public void addDetails(TestRunDetails details) {
    122         this.details = details;
    123     }
    124 
    125     /**
    126      * Serializes the test run metadata to json format.
    127      *
    128      * @return A JsonElement object representing the details object.
    129      */
    130     public JsonObject toJson() {
    131         processDeviceInfo();
    132         JsonObject json = new JsonObject();
    133         json.add(DEVICE_INFO, new JsonPrimitive(this.deviceInfo));
    134         json.add(ABI_INFO, new JsonPrimitive(this.abiInfo));
    135         json.add(TEST_RUN, this.testRun.toJson());
    136         if (this.details != null) {
    137             json.add(TEST_DETAILS, this.details.toJson());
    138         }
    139         return json;
    140     }
    141 }
    142