summaryrefslogtreecommitdiff
path: root/src/main/java/com/android/vts/util/TestRunDetails.java
blob: 584abaff09e13b870691d68239b6a7a5f6fd760f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
 * Copyright (c) 2017 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you
 * may not use this file except in compliance with the License. You may
 * obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */

package com.android.vts.util;

import com.android.vts.entity.TestCaseRunEntity;
import com.android.vts.entity.TestCaseRunEntity.TestCase;
import com.android.vts.proto.VtsReportMessage.TestCaseResult;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

/** Helper object for describing test results data. */
public class TestRunDetails {
    private static final String NAME_KEY = "name";
    private static final String DATA_KEY = "data";

    private class ResultColumn {
        private final String name;
        private final List<String> testCases;

        public ResultColumn(String name) {
            this.name = name;
            this.testCases = new ArrayList<>();
        }

        public void add(String testCase) {
            this.testCases.add(testCase);
        }

        public int size() {
            return this.testCases.size();
        }

        public JsonObject toJson() {
            testCases.sort(Comparator.naturalOrder());
            JsonObject json = new JsonObject();
            json.add(NAME_KEY, new JsonPrimitive(name));
            json.add(DATA_KEY, new Gson().toJsonTree(testCases));
            return json;
        }
    }

    private final ResultColumn[] columns;
    public final int[] resultCounts = new int[TestCaseResult.values().length];

    public TestRunDetails() {
        columns = new ResultColumn[TestCaseResult.values().length];
        for (TestCaseResult r : TestCaseResult.values()) {
            columns[r.getNumber()] = new ResultColumn(r.name());
        }
    }

    /**
     * Add a test case entity to the details object.
     * @param testCaseEntity The TestCaseRunEntity object storing test case results.
     */
    public void addTestCase(TestCaseRunEntity testCaseEntity) {
        for (TestCase testCase : testCaseEntity.getTestCases()) {
            int result = testCase.result;
            if (result > resultCounts.length)
                continue;
            ++resultCounts[result];
            ResultColumn column = columns[result];
            column.add(testCase.name);
        }
    }

    /**
     * Serializes the test run details to json format.
     *
     * @return A JsonObject object representing the details object.
     */
    public JsonElement toJson() {
        List<JsonObject> jsonColumns = new ArrayList<>();
        for (ResultColumn column : columns) {
            if (column.size() > 0) {
                jsonColumns.add(column.toJson());
            }
        }
        return new Gson().toJsonTree(jsonColumns);
    }
}