1 /*
2  * Copyright (C) 2016 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.cts.verifier.sensors.sixdof.Utils;
17 
18 import android.content.Context;
19 import android.os.Build;
20 import android.util.Xml;
21 
22 import org.xmlpull.v1.XmlSerializer;
23 
24 import java.io.ByteArrayOutputStream;
25 import java.io.IOException;
26 import java.text.DateFormat;
27 import java.text.SimpleDateFormat;
28 import java.util.ArrayList;
29 import java.util.Date;
30 import java.util.Locale;
31 
32 /**
33  * Handles all the XML to print to the user.
34  */
35 public class TestReport {
36 
37     public enum TestStatus {
38         NOT_EXECUTED,
39         EXECUTED,
40         PASS,
41         FAIL,
42     }
43 
44     private static final int REPORT_VERSION = 1;
45     private static DateFormat DATE_FORMAT = new SimpleDateFormat(
46             "EEE MMM dd HH:mm:ss z yyyy", Locale.ENGLISH);
47     private static final String TEST_RESULTS_REPORT_TAG = "test-results-report";
48     private static final String VERIFIER_INFO_TAG = "verifier-info";
49     private static final String DEVICE_INFO_TAG = "device-info";
50     private static final String BUILD_INFO_TAG = "build-info";
51     private static final String TEST_RESULTS_TAG = "test-results";
52     private static final String TEST_TAG = "test";
53     private static final String TEST_DETAILS_TAG = "details";
54     private String mTestStatus = "not-executed";
55     private Context mContext;
56     private ArrayList<String> mTestDetails = new ArrayList<>();
57 
58     /**
59      * Sets the context of this test.
60      *
61      * @param context reference to the activity this test is in.
62      */
TestReport(Context context)63     public TestReport(Context context) {
64         mContext = context;
65     }
66 
67     /**
68      * Produces the XML for the test.
69      *
70      * @return the XML of the test to display.
71      */
getContents()72     public String getContents()
73             throws IllegalArgumentException, IllegalStateException, IOException {
74 
75         ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
76 
77         XmlSerializer xml = Xml.newSerializer();
78 
79         xml.setOutput(outputStream, "utf-8");
80         xml.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
81         xml.startDocument("utf-8", true);
82 
83         xml.startTag(null, TEST_RESULTS_REPORT_TAG);
84         xml.attribute(null, "report-version", Integer.toString(REPORT_VERSION));
85         xml.attribute(null, "creation-time", DATE_FORMAT.format(new Date()));
86 
87         xml.startTag(null, VERIFIER_INFO_TAG);
88         xml.attribute(null, "version-name", Version.getVersionName(mContext));
89         xml.attribute(null, "version-code", Integer.toString(Version.getVersionCode(mContext)));
90         xml.endTag(null, VERIFIER_INFO_TAG);
91 
92         xml.startTag(null, DEVICE_INFO_TAG);
93         xml.startTag(null, BUILD_INFO_TAG);
94         xml.attribute(null, "board", Build.BOARD);
95         xml.attribute(null, "brand", Build.BRAND);
96         xml.attribute(null, "device", Build.DEVICE);
97         xml.attribute(null, "display", Build.DISPLAY);
98         xml.attribute(null, "fingerprint", Build.FINGERPRINT);
99         xml.attribute(null, "id", Build.ID);
100         xml.attribute(null, "model", Build.MODEL);
101         xml.attribute(null, "product", Build.PRODUCT);
102         xml.attribute(null, "release", Build.VERSION.RELEASE);
103         xml.attribute(null, "sdk", Integer.toString(Build.VERSION.SDK_INT));
104         xml.endTag(null, BUILD_INFO_TAG);
105         xml.endTag(null, DEVICE_INFO_TAG);
106 
107         xml.startTag(null, TEST_RESULTS_TAG);
108         xml.startTag(null, TEST_TAG);
109         xml.attribute(null, "title", "6dof accuracy test");
110         xml.attribute(null, "class-name", "com.android.cts.verifier.sixdof.Activities.TestActivity");
111 
112         if (mTestDetails.isEmpty()) {
113             xml.attribute(null, "result", mTestStatus);
114         } else {
115             setTestState(TestStatus.FAIL);
116             xml.attribute(null, "result", mTestStatus);
117             xml.startTag(null, TEST_DETAILS_TAG);
118 
119             for (int i = 0; i < mTestDetails.size(); i++) {
120                 xml.text(mTestDetails.get(i));
121             }
122 
123             xml.endTag(null, TEST_DETAILS_TAG);
124         }
125 
126         xml.endTag(null, TEST_TAG);
127         xml.endTag(null, TEST_RESULTS_TAG);
128 
129         xml.endTag(null, TEST_RESULTS_REPORT_TAG);
130         xml.endDocument();
131 
132         return outputStream.toString("utf-8");
133     }
134 
135     /**
136      * Adds the failed results to the details.
137      *
138      * @param failedPart the failed test result.
139      */
setFailDetails(String failedPart)140     public void setFailDetails(String failedPart) {
141         mTestDetails.add(failedPart);
142     }
143 
144     /**
145      * Sets the status the test is currently in.
146      *
147      * @param state the status the test is in.
148      */
setTestState(TestStatus state)149     public void setTestState(TestStatus state) {
150         switch (state) {
151             case EXECUTED:
152                 mTestStatus = "executed";
153                 break;
154             case PASS:
155                 mTestStatus = "passed";
156                 break;
157             case FAIL:
158                 mTestStatus = "failed";
159                 break;
160             case NOT_EXECUTED:
161                 mTestStatus = "not-executed";
162                 break;
163             default:
164                 throw new AssertionError("TestExecuted default we should not be in", null);
165         }
166     }
167 }
168