1 /*
2  * Copyright (C) 2010 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.cts.verifier;
18 
19 import static com.android.cts.verifier.TestListActivity.sCurrentDisplayMode;
20 import static com.android.cts.verifier.TestListAdapter.setTestNameSuffix;
21 
22 import com.android.compatibility.common.util.ReportLog;
23 import com.android.compatibility.common.util.TestResultHistory;
24 
25 import android.app.Activity;
26 import android.content.Intent;
27 import android.util.Log;
28 
29 /**
30  * Object representing the result of a test activity like whether it succeeded or failed.
31  * Use {@link #setPassedResult(Activity, String, String)} or
32  * {@link #setFailedResult(Activity, String, String)} from a test activity like you would
33  * {@link Activity#setResult(int)} so that {@link TestListActivity}
34  * will persist the test result and update its adapter and thus the list view.
35  */
36 public class TestResult {
37 
38     private static final String TAG = TestResult.class.getSimpleName();
39 
40     public static final int TEST_RESULT_NOT_EXECUTED = 0;
41     public static final int TEST_RESULT_PASSED = 1;
42     public static final int TEST_RESULT_FAILED = 2;
43 
44     private static final String TEST_NAME = "name";
45     private static final String TEST_RESULT = "result";
46     private static final String TEST_DETAILS = "details";
47     private static final String TEST_METRICS = "metrics";
48     private static final String TEST_HISTORY_COLLECTION = "historyCollection";
49 
50     private final String mName;
51     private final int mResult;
52     private final String mDetails;
53     private final ReportLog mReportLog;
54     private final TestResultHistoryCollection mHistoryCollection;
55 
56     /** Sets the test activity's result to pass. */
setPassedResult(Activity activity, String testId, String testDetails)57     public static void setPassedResult(Activity activity, String testId, String testDetails) {
58         setPassedResult(activity, testId, testDetails, null /*reportLog*/);
59     }
60 
61     /** Sets the test activity's result to pass including a test report log result. */
setPassedResult(Activity activity, String testId, String testDetails, ReportLog reportLog)62     public static void setPassedResult(Activity activity, String testId, String testDetails,
63             ReportLog reportLog) {
64         Log.i(TAG, "setPassedResult(activity=" + activity + ", testId=" + testId
65                 + ", testDetails=" + testDetails);
66         activity.setResult(Activity.RESULT_OK, createResult(activity, TEST_RESULT_PASSED, testId,
67             testDetails, reportLog, null /*history*/));
68     }
69 
70     /** Sets the test activity's result to pass including a test report log result and history. */
setPassedResult(Activity activity, String testId, String testDetails, ReportLog reportLog, TestResultHistoryCollection historyCollection)71     public static void setPassedResult(Activity activity, String testId, String testDetails,
72             ReportLog reportLog, TestResultHistoryCollection historyCollection) {
73         Log.i(TAG, "setPassedResult(activity=" + activity + ", testId=" + testId
74                 + ", testDetails=" + testDetails);
75         activity.setResult(Activity.RESULT_OK, createResult(activity, TEST_RESULT_PASSED, testId,
76                 testDetails, reportLog, historyCollection));
77     }
78 
79     /** Sets the test activity's result to failed. */
setFailedResult(Activity activity, String testId, String testDetails)80     public static void setFailedResult(Activity activity, String testId, String testDetails) {
81         setFailedResult(activity, testId, testDetails, null /*reportLog*/);
82     }
83 
84     /** Sets the test activity's result to failed including a test report log result. */
setFailedResult(Activity activity, String testId, String testDetails, ReportLog reportLog)85     public static void setFailedResult(Activity activity, String testId, String testDetails,
86             ReportLog reportLog) {
87         Log.e(TAG, "setFailedResult(activity=" + activity + ", testId=" + testId
88                 + ", testDetails=" + testDetails);
89         activity.setResult(Activity.RESULT_OK, createResult(activity, TEST_RESULT_FAILED, testId,
90                 testDetails, reportLog, null /*history*/));
91     }
92 
93     /** Sets the test activity's result to failed including a test report log result and history. */
setFailedResult(Activity activity, String testId, String testDetails, ReportLog reportLog, TestResultHistoryCollection historyCollection)94     public static void setFailedResult(Activity activity, String testId, String testDetails,
95             ReportLog reportLog, TestResultHistoryCollection historyCollection) {
96         Log.e(TAG, "setFailedResult(activity=" + activity + ", testId=" + testId
97                 + ", testDetails=" + testDetails);
98         activity.setResult(Activity.RESULT_OK, createResult(activity, TEST_RESULT_FAILED, testId,
99             testDetails, reportLog, historyCollection));
100     }
101 
createResult(Activity activity, int testResult, String testName, String testDetails, ReportLog reportLog, TestResultHistoryCollection historyCollection)102     public static Intent createResult(Activity activity, int testResult, String testName,
103             String testDetails, ReportLog reportLog, TestResultHistoryCollection historyCollection) {
104         Intent data = new Intent(activity, activity.getClass());
105         addResultData(data, testResult, testName, testDetails, reportLog, historyCollection);
106         return data;
107     }
108 
addResultData(Intent intent, int testResult, String testName, String testDetails, ReportLog reportLog, TestResultHistoryCollection historyCollection)109     public static void addResultData(Intent intent, int testResult, String testName,
110             String testDetails, ReportLog reportLog, TestResultHistoryCollection historyCollection) {
111         testName = setTestNameSuffix(sCurrentDisplayMode, testName);
112         intent.putExtra(TEST_NAME, testName);
113         intent.putExtra(TEST_RESULT, testResult);
114         intent.putExtra(TEST_DETAILS, testDetails);
115         intent.putExtra(TEST_METRICS, reportLog);
116         intent.putExtra(TEST_HISTORY_COLLECTION, historyCollection);
117     }
118 
119     /**
120      * Convert the test activity's result into a {@link TestResult}. Only meant to be used by
121      * {@link TestListActivity}.
122      */
fromActivityResult(int resultCode, Intent data)123     static TestResult fromActivityResult(int resultCode, Intent data) {
124         String name = data.getStringExtra(TEST_NAME);
125         int result = data.getIntExtra(TEST_RESULT, TEST_RESULT_NOT_EXECUTED);
126         String details = data.getStringExtra(TEST_DETAILS);
127         ReportLog reportLog = (ReportLog) data.getSerializableExtra(TEST_METRICS);
128         TestResultHistoryCollection historyCollection =
129             (TestResultHistoryCollection) data.getSerializableExtra(TEST_HISTORY_COLLECTION);
130         return new TestResult(name, result, details, reportLog, historyCollection);
131     }
132 
TestResult( String name, int result, String details, ReportLog reportLog, TestResultHistoryCollection historyCollection)133     private TestResult(
134             String name, int result, String details, ReportLog reportLog,
135             TestResultHistoryCollection historyCollection) {
136         this.mName = name;
137         this.mResult = result;
138         this.mDetails = details;
139         this.mReportLog = reportLog;
140         this.mHistoryCollection =
141             historyCollection == null ? new TestResultHistoryCollection() : historyCollection;
142     }
143 
144     /** Return the name of the test like "com.android.cts.verifier.foo.FooTest" */
getName()145     public String getName() {
146         return mName;
147     }
148 
149     /** Return integer test result. See test result constants. */
getResult()150     public int getResult() {
151         return mResult;
152     }
153 
154     /** Return null or string containing test output. */
getDetails()155     public String getDetails() {
156         return mDetails;
157     }
158 
159     /** @return the {@link ReportLog} or null if not set */
getReportLog()160     public ReportLog getReportLog() {
161         return mReportLog;
162     }
163 
164     /** @return the {@link TestResultHistoryCollection} containing test history */
getHistoryCollection()165     public TestResultHistoryCollection getHistoryCollection() {
166         return mHistoryCollection;
167     }
168 }
169