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.TestCaseRunEntity;
20 import com.android.vts.entity.TestCaseRunEntity.TestCase;
21 import com.android.vts.proto.VtsReportMessage.TestCaseResult;
22 import com.google.gson.Gson;
23 import com.google.gson.JsonElement;
24 import com.google.gson.JsonObject;
25 import com.google.gson.JsonPrimitive;
26 import java.util.ArrayList;
27 import java.util.Comparator;
28 import java.util.List;
29 
30 /** Helper object for describing test results data. */
31 public class TestRunDetails {
32     private static final String NAME_KEY = "name";
33     private static final String DATA_KEY = "data";
34 
35     private class ResultColumn {
36         private final String name;
37         private final List<String> testCases;
38 
ResultColumn(String name)39         public ResultColumn(String name) {
40             this.name = name;
41             this.testCases = new ArrayList<>();
42         }
43 
add(String testCase)44         public void add(String testCase) {
45             this.testCases.add(testCase);
46         }
47 
size()48         public int size() {
49             return this.testCases.size();
50         }
51 
toJson()52         public JsonObject toJson() {
53             testCases.sort(Comparator.naturalOrder());
54             JsonObject json = new JsonObject();
55             json.add(NAME_KEY, new JsonPrimitive(name));
56             json.add(DATA_KEY, new Gson().toJsonTree(testCases));
57             return json;
58         }
59     }
60 
61     private final ResultColumn[] columns;
62     public final int[] resultCounts = new int[TestCaseResult.values().length];
63 
TestRunDetails()64     public TestRunDetails() {
65         columns = new ResultColumn[TestCaseResult.values().length];
66         for (TestCaseResult r : TestCaseResult.values()) {
67             columns[r.getNumber()] = new ResultColumn(r.name());
68         }
69     }
70 
71     /**
72      * Add a test case entity to the details object.
73      * @param testCaseEntity The TestCaseRunEntity object storing test case results.
74      */
addTestCase(TestCaseRunEntity testCaseEntity)75     public void addTestCase(TestCaseRunEntity testCaseEntity) {
76         for (TestCase testCase : testCaseEntity.getTestCases()) {
77             int result = testCase.result;
78             if (result > resultCounts.length)
79                 continue;
80             ++resultCounts[result];
81             ResultColumn column = columns[result];
82             column.add(testCase.name);
83         }
84     }
85 
86     /**
87      * Serializes the test run details to json format.
88      *
89      * @return A JsonObject object representing the details object.
90      */
toJson()91     public JsonElement toJson() {
92         List<JsonObject> jsonColumns = new ArrayList<>();
93         for (ResultColumn column : columns) {
94             if (column.size() > 0) {
95                 jsonColumns.add(column.toJson());
96             }
97         }
98         return new Gson().toJsonTree(jsonColumns);
99     }
100 }
101