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 setPassedResult(activity, testId, testDetails, reportLog, null /*history*/); 65 } 66 67 /** 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)68 public static void setPassedResult(Activity activity, String testId, String testDetails, 69 ReportLog reportLog, TestResultHistoryCollection historyCollection) { 70 Log.i(TAG, "setPassedResult(activity=" + activity + ", testId=" + testId 71 + ", testDetails=" + testDetails); 72 activity.setResult(Activity.RESULT_OK, createResult(activity, TEST_RESULT_PASSED, testId, 73 testDetails, reportLog, historyCollection)); 74 } 75 76 /** Sets the test activity's result to failed. */ setFailedResult(Activity activity, String testId, String testDetails)77 public static void setFailedResult(Activity activity, String testId, String testDetails) { 78 setFailedResult(activity, testId, testDetails, null /*reportLog*/); 79 } 80 81 /** Sets the test activity's result to failed including a test report log result. */ setFailedResult(Activity activity, String testId, String testDetails, ReportLog reportLog)82 public static void setFailedResult(Activity activity, String testId, String testDetails, 83 ReportLog reportLog) { 84 setFailedResult(activity, testId, testDetails, reportLog, null /*history*/); 85 } 86 87 /** 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)88 public static void setFailedResult(Activity activity, String testId, String testDetails, 89 ReportLog reportLog, TestResultHistoryCollection historyCollection) { 90 Log.e(TAG, "setFailedResult(activity=" + activity + ", testId=" + testId 91 + ", testDetails=" + testDetails); 92 activity.setResult(Activity.RESULT_OK, createResult(activity, TEST_RESULT_FAILED, testId, 93 testDetails, reportLog, historyCollection)); 94 } 95 createResult(Activity activity, int testResult, String testName, String testDetails, ReportLog reportLog, TestResultHistoryCollection historyCollection)96 public static Intent createResult(Activity activity, int testResult, String testName, 97 String testDetails, ReportLog reportLog, TestResultHistoryCollection historyCollection) { 98 Intent activityIntent = activity.getIntent(); 99 Intent data = new Intent(activity, activity.getClass()); 100 if (activityIntent.hasExtra(TEST_START_TIME)) { 101 data.putExtra(TEST_START_TIME, activityIntent.getLongExtra(TEST_START_TIME, 0)); 102 } 103 addResultData(data, testResult, testName, testDetails, reportLog, historyCollection); 104 return data; 105 } 106 addResultData(Intent intent, int testResult, String testName, String testDetails, ReportLog reportLog, TestResultHistoryCollection historyCollection)107 public static void addResultData(Intent intent, int testResult, String testName, 108 String testDetails, ReportLog reportLog, TestResultHistoryCollection historyCollection) { 109 testName = setTestNameSuffix(sCurrentDisplayMode, testName); 110 intent.putExtra(TEST_NAME, testName); 111 intent.putExtra(TEST_RESULT, testResult); 112 intent.putExtra(TEST_DETAILS, testDetails); 113 intent.putExtra(TEST_METRICS, reportLog); 114 intent.putExtra(TEST_HISTORY_COLLECTION, historyCollection); 115 } 116 117 /** 118 * Convert the test activity's result into a {@link TestResult}. Only meant to be used by 119 * {@link TestListActivity}. 120 */ fromActivityResult(int resultCode, Intent data)121 static TestResult fromActivityResult(int resultCode, Intent data) { 122 String name = setTestNameSuffix(sCurrentDisplayMode, data.getStringExtra(TEST_NAME)); 123 int result = data.getIntExtra(TEST_RESULT, TEST_RESULT_NOT_EXECUTED); 124 String details = data.getStringExtra(TEST_DETAILS); 125 ReportLog reportLog = (ReportLog) data.getSerializableExtra(TEST_METRICS); 126 TestResultHistoryCollection historyCollection = 127 (TestResultHistoryCollection) data.getSerializableExtra(TEST_HISTORY_COLLECTION); 128 return new TestResult(name, result, details, reportLog, historyCollection); 129 } 130 TestResult( String name, int result, String details, ReportLog reportLog, TestResultHistoryCollection historyCollection)131 private TestResult( 132 String name, int result, String details, ReportLog reportLog, 133 TestResultHistoryCollection historyCollection) { 134 this.mName = name; 135 this.mResult = result; 136 this.mDetails = details; 137 this.mReportLog = reportLog; 138 this.mHistoryCollection = 139 historyCollection == null ? new TestResultHistoryCollection() : historyCollection; 140 } 141 142 /** Return the name of the test like "com.android.cts.verifier.foo.FooTest" */ getName()143 public String getName() { 144 return mName; 145 } 146 147 /** Return integer test result. See test result constants. */ getResult()148 public int getResult() { 149 return mResult; 150 } 151 152 /** Return null or string containing test output. */ getDetails()153 public String getDetails() { 154 return mDetails; 155 } 156 157 /** @return the {@link ReportLog} or null if not set */ getReportLog()158 public ReportLog getReportLog() { 159 return mReportLog; 160 } 161 162 /** @return the {@link TestResultHistoryCollection} containing test history */ getHistoryCollection()163 public TestResultHistoryCollection getHistoryCollection() { 164 return mHistoryCollection; 165 } 166 } 167