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.removeTestNameSuffix;
21 import static com.android.cts.verifier.TestListAdapter.setTestNameSuffix;
22 
23 import android.app.Activity;
24 import android.content.Intent;
25 import android.util.Log;
26 
27 import com.android.compatibility.common.util.ReportLog;
28 import com.android.cts.verifier.TestListActivity.DisplayMode;
29 
30 /**
31  * Object representing the result of a test activity like whether it succeeded or failed.
32  * Use {@link #setPassedResult(Activity, String, String)} or
33  * {@link #setFailedResult(Activity, String, String)} from a test activity like you would
34  * {@link Activity#setResult(int)} so that {@link TestListActivity}
35  * will persist the test result and update its adapter and thus the list view.
36  */
37 public class TestResult {
38 
39     private static final String TAG = TestResult.class.getSimpleName();
40 
41     public static final int TEST_RESULT_NOT_EXECUTED = 0;
42     public static final int TEST_RESULT_PASSED = 1;
43     public static final int TEST_RESULT_FAILED = 2;
44     public static final String TEST_START_TIME = "start_time";
45 
46     private static final String TEST_NAME = "name";
47     private static final String TEST_RESULT = "result";
48     private static final String TEST_DETAILS = "details";
49     private static final String TEST_METRICS = "metrics";
50     private static final String TEST_HISTORY_COLLECTION = "historyCollection";
51 
52     private final String mName;
53     private final int mResult;
54     private final String mDetails;
55     private final ReportLog mReportLog;
56     private final TestResultHistoryCollection mHistoryCollection;
57 
58     /** Sets the test activity's result to pass. */
setPassedResult(Activity activity, String testId, String testDetails)59     public static void setPassedResult(Activity activity, String testId, String testDetails) {
60         setPassedResult(activity, testId, testDetails, null /*reportLog*/);
61     }
62 
63     /** Sets the test activity's result to pass including a test report log result. */
setPassedResult(Activity activity, String testId, String testDetails, ReportLog reportLog)64     public static void setPassedResult(Activity activity, String testId, String testDetails,
65             ReportLog reportLog) {
66         setPassedResult(activity, testId, testDetails, reportLog, null /*history*/);
67     }
68 
69     /** 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)70     public static void setPassedResult(Activity activity, String testId, String testDetails,
71             ReportLog reportLog, TestResultHistoryCollection historyCollection) {
72         Log.i(TAG, "setPassedResult(activity=" + activity + ", testId=" + testId
73                 + ", testDetails=" + testDetails);
74 
75         // We store results here straight into the content provider so it can be fetched by the
76         // CTSInteractive host
77         TestResultsProvider.setTestResult(
78                 activity, testId, 1, testDetails, reportLog, historyCollection,
79                 null);
80 
81         activity.setResult(Activity.RESULT_OK, createResult(activity, TEST_RESULT_PASSED, testId,
82                 testDetails, reportLog, historyCollection));
83     }
84 
85     /** Sets the test activity's result to failed. */
setFailedResult(Activity activity, String testId, String testDetails)86     public static void setFailedResult(Activity activity, String testId, String testDetails) {
87         setFailedResult(activity, testId, testDetails, null /*reportLog*/);
88     }
89 
90     /** Sets the test activity's result to failed including a test report log result. */
setFailedResult(Activity activity, String testId, String testDetails, ReportLog reportLog)91     public static void setFailedResult(Activity activity, String testId, String testDetails,
92             ReportLog reportLog) {
93         setFailedResult(activity, testId, testDetails, reportLog, null /*history*/);
94     }
95 
96     /** 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)97     public static void setFailedResult(Activity activity, String testId, String testDetails,
98             ReportLog reportLog, TestResultHistoryCollection historyCollection) {
99         Log.e(TAG, "setFailedResult(activity=" + activity + ", testId=" + testId
100                 + ", testDetails=" + testDetails);
101 
102         // We store results here straight into the content provider so it can be fetched by the
103         // CTSInteractive host
104         TestResultsProvider.setTestResult(
105                 activity, testId, 2, testDetails, reportLog, historyCollection,
106                 null);
107 
108         activity.setResult(Activity.RESULT_OK, createResult(activity, TEST_RESULT_FAILED, testId,
109                 testDetails, reportLog, historyCollection));
110     }
111 
createResult(Activity activity, int testResult, String testName, String testDetails, ReportLog reportLog, TestResultHistoryCollection historyCollection)112     public static Intent createResult(Activity activity, int testResult, String testName,
113             String testDetails, ReportLog reportLog, TestResultHistoryCollection historyCollection) {
114         Intent activityIntent = activity.getIntent();
115         Intent data = new Intent(activity, activity.getClass());
116         if (activityIntent.hasExtra(TEST_START_TIME)) {
117             data.putExtra(TEST_START_TIME, activityIntent.getLongExtra(TEST_START_TIME, 0));
118         }
119         addResultData(data, testResult, testName, testDetails, reportLog, historyCollection);
120         return data;
121     }
122 
addResultData(Intent intent, int testResult, String testName, String testDetails, ReportLog reportLog, TestResultHistoryCollection historyCollection)123     public static void addResultData(Intent intent, int testResult, String testName,
124             String testDetails, ReportLog reportLog, TestResultHistoryCollection historyCollection) {
125         testName = setTestNameSuffix(sCurrentDisplayMode, testName);
126         intent.putExtra(TEST_NAME, testName);
127         intent.putExtra(TEST_RESULT, testResult);
128         intent.putExtra(TEST_DETAILS, testDetails);
129         intent.putExtra(TEST_METRICS, reportLog);
130         intent.putExtra(TEST_HISTORY_COLLECTION, historyCollection);
131     }
132 
133     /**
134      * Convert the test activity's result into a {@link TestResult}. Only meant to be used by
135      * {@link TestListActivity}.
136      */
fromActivityResult(int resultCode, Intent data)137     static TestResult fromActivityResult(int resultCode, Intent data) {
138         String name = setTestNameSuffix(sCurrentDisplayMode, data.getStringExtra(TEST_NAME));
139         return fromActivityResultWithTestName(resultCode, data, name);
140     }
141 
142     /**
143      * Convert the test activity's result into a {@link TestResult} with the given test name.
144      * Only meant to be used by {@link TestResult}.
145      */
fromActivityResultWithTestName( int resultCode, Intent data, String name)146     static TestResult fromActivityResultWithTestName(
147             int resultCode, Intent data, String name) {
148         int result = data.getIntExtra(TEST_RESULT, TEST_RESULT_NOT_EXECUTED);
149         String details = data.getStringExtra(TEST_DETAILS);
150         ReportLog reportLog = (ReportLog) data.getSerializableExtra(TEST_METRICS);
151         TestResultHistoryCollection historyCollection =
152                 (TestResultHistoryCollection) data.getSerializableExtra(TEST_HISTORY_COLLECTION);
153         return new TestResult(name, result, details, reportLog, historyCollection);
154     }
155 
156     /**
157      * Convert the test activity's result into a {@link TestResult} with the given display mode.
158      * Only meant to be used by {@link TestListActivity}.
159      */
fromActivityResultWithDisplayMode( int resultCode, Intent data, String mode)160     static TestResult fromActivityResultWithDisplayMode(
161             int resultCode, Intent data, String mode) {
162         if (mode.equalsIgnoreCase(DisplayMode.UNFOLDED.toString())) {
163             return fromActivityResultWithTestName(
164                     resultCode,
165                     data,
166                     removeTestNameSuffix(mode, data.getStringExtra(TEST_NAME))
167             );
168         }
169         return fromActivityResultWithTestName(
170                 resultCode,
171                 data,
172                 setTestNameSuffix(mode, data.getStringExtra(TEST_NAME))
173         );
174     }
175 
TestResult( String name, int result, String details, ReportLog reportLog, TestResultHistoryCollection historyCollection)176     private TestResult(
177             String name, int result, String details, ReportLog reportLog,
178             TestResultHistoryCollection historyCollection) {
179         this.mName = name;
180         this.mResult = result;
181         this.mDetails = details;
182         this.mReportLog = reportLog;
183         this.mHistoryCollection =
184             historyCollection == null ? new TestResultHistoryCollection() : historyCollection;
185     }
186 
187     /** Return the name of the test like "com.android.cts.verifier.foo.FooTest" */
getName()188     public String getName() {
189         return mName;
190     }
191 
192     /** Return integer test result. See test result constants. */
getResult()193     public int getResult() {
194         return mResult;
195     }
196 
197     /** Return null or string containing test output. */
getDetails()198     public String getDetails() {
199         return mDetails;
200     }
201 
202     /** @return the {@link ReportLog} or null if not set */
getReportLog()203     public ReportLog getReportLog() {
204         return mReportLog;
205     }
206 
207     /** @return the {@link TestResultHistoryCollection} containing test history */
getHistoryCollection()208     public TestResultHistoryCollection getHistoryCollection() {
209         return mHistoryCollection;
210     }
211 }
212