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