1 /* 2 * Copyright (C) 2009 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 * use this file except in compliance with the License. You may obtain a copy of 6 * 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, WITHOUT 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 * License for the specific language governing permissions and limitations under 14 * the License. 15 */ 16 17 package com.android.cts; 18 19 import junit.framework.TestFailure; 20 import junit.framework.TestResult; 21 22 import java.util.Enumeration; 23 import java.util.HashMap; 24 25 26 /** 27 * Store the result of a specific test. 28 */ 29 public class CtsTestResult { 30 private int mResultCode; 31 private String mFailedMessage; 32 private String mStackTrace; 33 34 public static final int CODE_INIT = -1; 35 public static final int CODE_NOT_EXECUTED = 0; 36 public static final int CODE_PASS = 1; 37 public static final int CODE_FAIL = 2; 38 public static final int CODE_ERROR = 3; 39 public static final int CODE_TIMEOUT = 4; 40 public static final int CODE_FIRST = CODE_INIT; 41 public static final int CODE_LAST = CODE_TIMEOUT; 42 43 public static final String STR_ERROR = "error"; 44 public static final String STR_TIMEOUT = "timeout"; 45 public static final String STR_NOT_EXECUTED = "notExecuted"; 46 public static final String STR_FAIL = "fail"; 47 public static final String STR_PASS = "pass"; 48 49 private static HashMap<Integer, String> sCodeToResultMap; 50 private static HashMap<String, Integer> sResultToCodeMap; 51 static { 52 sCodeToResultMap = new HashMap<Integer, String>(); sCodeToResultMap.put(CODE_NOT_EXECUTED, STR_NOT_EXECUTED)53 sCodeToResultMap.put(CODE_NOT_EXECUTED, STR_NOT_EXECUTED); sCodeToResultMap.put(CODE_PASS, STR_PASS)54 sCodeToResultMap.put(CODE_PASS, STR_PASS); sCodeToResultMap.put(CODE_FAIL, STR_FAIL)55 sCodeToResultMap.put(CODE_FAIL, STR_FAIL); sCodeToResultMap.put(CODE_ERROR, STR_ERROR)56 sCodeToResultMap.put(CODE_ERROR, STR_ERROR); sCodeToResultMap.put(CODE_TIMEOUT, STR_TIMEOUT)57 sCodeToResultMap.put(CODE_TIMEOUT, STR_TIMEOUT); 58 sResultToCodeMap = new HashMap<String, Integer>(); 59 for (int code : sCodeToResultMap.keySet()) { sCodeToResultMap.get(code)60 sResultToCodeMap.put(sCodeToResultMap.get(code), code); 61 } 62 } 63 CtsTestResult(int resCode)64 public CtsTestResult(int resCode) { 65 mResultCode = resCode; 66 } 67 CtsTestResult(int resCode, final String failedMessage, final String stackTrace)68 public CtsTestResult(int resCode, final String failedMessage, final String stackTrace) { 69 mResultCode = resCode; 70 mFailedMessage = failedMessage; 71 mStackTrace = stackTrace; 72 } 73 CtsTestResult(final String result, final String failedMessage, final String stackTrace)74 public CtsTestResult(final String result, final String failedMessage, 75 final String stackTrace) throws InvalidTestResultStringException { 76 if (!sResultToCodeMap.containsKey(result)) { 77 throw new InvalidTestResultStringException(result); 78 } 79 80 mResultCode = sResultToCodeMap.get(result); 81 mFailedMessage = failedMessage; 82 mStackTrace = stackTrace; 83 } 84 85 /** 86 * Check if the result indicates failure. 87 * 88 * @return If failed, return true; else, return false. 89 */ isFail()90 public boolean isFail() { 91 return mResultCode == CODE_FAIL; 92 } 93 94 /** 95 * Check if the result indicates pass. 96 * 97 * @return If pass, return true; else, return false. 98 */ isPass()99 public boolean isPass() { 100 return mResultCode == CODE_PASS; 101 } 102 103 /** 104 * Check if the result indicates not executed. 105 * 106 * @return If not executed, return true; else, return false. 107 */ isNotExecuted()108 public boolean isNotExecuted() { 109 return mResultCode == CODE_NOT_EXECUTED; 110 } 111 112 /** 113 * Get result code of the test. 114 * 115 * @return The result code of the test. 116 * The following is the possible result codes: 117 * <ul> 118 * <li> notExecuted 119 * <li> pass 120 * <li> fail 121 * <li> error 122 * <li> timeout 123 * </ul> 124 */ getResultCode()125 public int getResultCode() { 126 return mResultCode; 127 } 128 129 /** 130 * Get the failed message. 131 * 132 * @return The failed message. 133 */ getFailedMessage()134 public String getFailedMessage() { 135 return mFailedMessage; 136 } 137 138 /** 139 * Get the stack trace. 140 * 141 * @return The stack trace. 142 */ getStackTrace()143 public String getStackTrace() { 144 return mStackTrace; 145 } 146 147 /** 148 * Set the result. 149 * 150 * @param testResult The result in the form of JUnit test result. 151 */ 152 @SuppressWarnings("unchecked") setResult(TestResult testResult)153 public void setResult(TestResult testResult) { 154 int resCode = CODE_PASS; 155 String failedMessage = null; 156 String stackTrace = null; 157 if ((testResult != null) && (testResult.failureCount() > 0)) { 158 resCode = CODE_FAIL; 159 Enumeration<TestFailure> failures = testResult.failures(); 160 while (failures.hasMoreElements()) { 161 TestFailure failure = failures.nextElement(); 162 failedMessage += failure.exceptionMessage(); 163 stackTrace += failure.trace(); 164 } 165 } 166 mResultCode = resCode; 167 mFailedMessage = failedMessage; 168 mStackTrace = stackTrace; 169 } 170 171 /** 172 * Reverse the result code. 173 */ reverse()174 public void reverse() { 175 if (isPass()) { 176 mResultCode = CtsTestResult.CODE_FAIL; 177 } else if (isFail()){ 178 mResultCode = CtsTestResult.CODE_PASS; 179 } 180 } 181 182 /** 183 * Get the test result as string. 184 * 185 * @return The readable result string. 186 */ getResultString()187 public String getResultString() { 188 return sCodeToResultMap.get(mResultCode); 189 } 190 191 /** 192 * Check if the given resultType is a valid result type defined.. 193 * 194 * @param resultType The result type to be checked. 195 * @return If valid, return true; else, return false. 196 */ isValidResultType(final String resultType)197 static public boolean isValidResultType(final String resultType) { 198 return sResultToCodeMap.containsKey(resultType); 199 } 200 } 201