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 com.android.compatibility.common.util.ReportLog;
20 
21 import android.app.Activity;
22 import android.content.Intent;
23 
24 /**
25  * Object representing the result of a test activity like whether it succeeded or failed.
26  * Use {@link #setPassedResult(Activity, String, String)} or
27  * {@link #setFailedResult(Activity, String, String)} from a test activity like you would
28  * {@link Activity#setResult(int)} so that {@link TestListActivity}
29  * will persist the test result and update its adapter and thus the list view.
30  */
31 public class TestResult {
32 
33     public static final int TEST_RESULT_NOT_EXECUTED = 0;
34     public static final int TEST_RESULT_PASSED = 1;
35     public static final int TEST_RESULT_FAILED = 2;
36 
37     private static final String TEST_NAME = "name";
38     private static final String TEST_RESULT = "result";
39     private static final String TEST_DETAILS = "details";
40     private static final String TEST_METRICS = "metrics";
41 
42     private final String mName;
43     private final int mResult;
44     private final String mDetails;
45     private final ReportLog mReportLog;
46 
47     /** Sets the test activity's result to pass. */
setPassedResult(Activity activity, String testId, String testDetails)48     public static void setPassedResult(Activity activity, String testId, String testDetails) {
49         setPassedResult(activity, testId, testDetails, null /*reportLog*/);
50     }
51 
52     /** Sets the test activity's result to pass including a test report log result. */
setPassedResult(Activity activity, String testId, String testDetails, ReportLog reportLog)53     public static void setPassedResult(Activity activity, String testId, String testDetails,
54             ReportLog reportLog) {
55         activity.setResult(Activity.RESULT_OK, createResult(activity, TEST_RESULT_PASSED, testId,
56                 testDetails, reportLog));
57     }
58 
59     /** Sets the test activity's result to failed. */
setFailedResult(Activity activity, String testId, String testDetails)60     public static void setFailedResult(Activity activity, String testId, String testDetails) {
61         setFailedResult(activity, testId, testDetails, null /*reportLog*/);
62     }
63 
64     /** Sets the test activity's result to failed including a test report log result. */
setFailedResult(Activity activity, String testId, String testDetails, ReportLog reportLog)65     public static void setFailedResult(Activity activity, String testId, String testDetails,
66             ReportLog reportLog) {
67         activity.setResult(Activity.RESULT_OK, createResult(activity, TEST_RESULT_FAILED, testId,
68                 testDetails, reportLog));
69     }
70 
createResult(Activity activity, int testResult, String testName, String testDetails, ReportLog reportLog)71     private static Intent createResult(Activity activity, int testResult, String testName,
72             String testDetails, ReportLog reportLog) {
73         Intent data = new Intent(activity, activity.getClass());
74         addResultData(data, testResult, testName, testDetails, reportLog);
75         return data;
76     }
77 
addResultData(Intent intent, int testResult, String testName, String testDetails, ReportLog reportLog)78     public static void addResultData(Intent intent, int testResult, String testName,
79             String testDetails, ReportLog reportLog) {
80         intent.putExtra(TEST_NAME, testName);
81         intent.putExtra(TEST_RESULT, testResult);
82         intent.putExtra(TEST_DETAILS, testDetails);
83         intent.putExtra(TEST_METRICS, reportLog);
84     }
85 
86     /**
87      * Convert the test activity's result into a {@link TestResult}. Only meant to be used by
88      * {@link TestListActivity}.
89      */
fromActivityResult(int resultCode, Intent data)90     static TestResult fromActivityResult(int resultCode, Intent data) {
91         String name = data.getStringExtra(TEST_NAME);
92         int result = data.getIntExtra(TEST_RESULT, TEST_RESULT_NOT_EXECUTED);
93         String details = data.getStringExtra(TEST_DETAILS);
94         ReportLog reportLog = (ReportLog) data.getSerializableExtra(TEST_METRICS);
95         return new TestResult(name, result, details, reportLog);
96     }
97 
TestResult( String name, int result, String details, ReportLog reportLog)98     private TestResult(
99             String name, int result, String details, ReportLog reportLog) {
100         this.mName = name;
101         this.mResult = result;
102         this.mDetails = details;
103         this.mReportLog = reportLog;
104     }
105 
106     /** Return the name of the test like "com.android.cts.verifier.foo.FooTest" */
getName()107     public String getName() {
108         return mName;
109     }
110 
111     /** Return integer test result. See test result constants. */
getResult()112     public int getResult() {
113         return mResult;
114     }
115 
116     /** Return null or string containing test output. */
getDetails()117     public String getDetails() {
118         return mDetails;
119     }
120 
121     /** @return the {@link ReportLog} or null if not set */
getReportLog()122     public ReportLog getReportLog() {
123         return mReportLog;
124     }
125 }
126