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 android.app.Activity; 23 import android.content.Intent; 24 import android.util.Log; 25 26 import com.android.compatibility.common.util.ReportLog; 27 28 /** 29 * Object representing the result of a test activity like whether it succeeded or failed. 30 * Use {@link #setPassedResult(Activity, String, String)} or 31 * {@link #setFailedResult(Activity, String, String)} from a test activity like you would 32 * {@link Activity#setResult(int)} so that {@link TestListActivity} 33 * will persist the test result and update its adapter and thus the list view. 34 */ 35 public class TestResult { 36 37 private static final String TAG = TestResult.class.getSimpleName(); 38 39 public static final int TEST_RESULT_NOT_EXECUTED = 0; 40 public static final int TEST_RESULT_PASSED = 1; 41 public static final int TEST_RESULT_FAILED = 2; 42 public static final String TEST_START_TIME = "start_time"; 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 activityIntent = activity.getIntent(); 105 Intent data = new Intent(activity, activity.getClass()); 106 if (activityIntent.hasExtra(TEST_START_TIME)) { 107 data.putExtra(TEST_START_TIME, activityIntent.getLongExtra(TEST_START_TIME, 0)); 108 } 109 addResultData(data, testResult, testName, testDetails, reportLog, historyCollection); 110 return data; 111 } 112 addResultData(Intent intent, int testResult, String testName, String testDetails, ReportLog reportLog, TestResultHistoryCollection historyCollection)113 public static void addResultData(Intent intent, int testResult, String testName, 114 String testDetails, ReportLog reportLog, TestResultHistoryCollection historyCollection) { 115 testName = setTestNameSuffix(sCurrentDisplayMode, testName); 116 intent.putExtra(TEST_NAME, testName); 117 intent.putExtra(TEST_RESULT, testResult); 118 intent.putExtra(TEST_DETAILS, testDetails); 119 intent.putExtra(TEST_METRICS, reportLog); 120 intent.putExtra(TEST_HISTORY_COLLECTION, historyCollection); 121 } 122 123 /** 124 * Convert the test activity's result into a {@link TestResult}. Only meant to be used by 125 * {@link TestListActivity}. 126 */ fromActivityResult(int resultCode, Intent data)127 static TestResult fromActivityResult(int resultCode, Intent data) { 128 String name = data.getStringExtra(TEST_NAME); 129 int result = data.getIntExtra(TEST_RESULT, TEST_RESULT_NOT_EXECUTED); 130 String details = data.getStringExtra(TEST_DETAILS); 131 ReportLog reportLog = (ReportLog) data.getSerializableExtra(TEST_METRICS); 132 TestResultHistoryCollection historyCollection = 133 (TestResultHistoryCollection) data.getSerializableExtra(TEST_HISTORY_COLLECTION); 134 return new TestResult(name, result, details, reportLog, historyCollection); 135 } 136 TestResult( String name, int result, String details, ReportLog reportLog, TestResultHistoryCollection historyCollection)137 private TestResult( 138 String name, int result, String details, ReportLog reportLog, 139 TestResultHistoryCollection historyCollection) { 140 this.mName = name; 141 this.mResult = result; 142 this.mDetails = details; 143 this.mReportLog = reportLog; 144 this.mHistoryCollection = 145 historyCollection == null ? new TestResultHistoryCollection() : historyCollection; 146 } 147 148 /** Return the name of the test like "com.android.cts.verifier.foo.FooTest" */ getName()149 public String getName() { 150 return mName; 151 } 152 153 /** Return integer test result. See test result constants. */ getResult()154 public int getResult() { 155 return mResult; 156 } 157 158 /** Return null or string containing test output. */ getDetails()159 public String getDetails() { 160 return mDetails; 161 } 162 163 /** @return the {@link ReportLog} or null if not set */ getReportLog()164 public ReportLog getReportLog() { 165 return mReportLog; 166 } 167 168 /** @return the {@link TestResultHistoryCollection} containing test history */ getHistoryCollection()169 public TestResultHistoryCollection getHistoryCollection() { 170 return mHistoryCollection; 171 } 172 } 173