1 /* 2 3 * Copyright (C) 2014 The Android Open Source Project 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package com.android.cts.verifier.sensors.reporting; 19 20 import android.content.Context; 21 import android.hardware.cts.helpers.SensorTestStateNotSupportedException; 22 import android.hardware.cts.helpers.SensorTestWarningException; 23 24 import com.android.cts.verifier.R; 25 26 import org.junit.runner.Result; 27 28 /** 29 * A class that holds the result of a Sensor test execution. 30 */ 31 public class SensorTestDetails { 32 private final String mName; 33 private final ResultCode mResultCode; 34 private final String mSummary; 35 36 public enum ResultCode { 37 SKIPPED, 38 PASS, 39 FAIL, 40 INTERRUPTED, 41 WARNING 42 } 43 SensorTestDetails(String name, ResultCode resultCode)44 public SensorTestDetails(String name, ResultCode resultCode) { 45 this(name, resultCode, null /* summary */); 46 } 47 SensorTestDetails(String name, ResultCode resultCode, String summary)48 public SensorTestDetails(String name, ResultCode resultCode, String summary) { 49 mName = name; 50 mResultCode = resultCode; 51 mSummary = summary; 52 } 53 SensorTestDetails( Context context, String name, int passCount, int skipCount, int failCount)54 public SensorTestDetails( 55 Context context, 56 String name, 57 int passCount, 58 int skipCount, 59 int failCount) { 60 ResultCode resultCode = ResultCode.PASS; 61 if (failCount > 0) { 62 resultCode = ResultCode.FAIL; 63 } else if (skipCount > 0) { 64 resultCode = ResultCode.SKIPPED; 65 } 66 67 mName = name; 68 mResultCode = resultCode; 69 mSummary = context.getString(R.string.snsr_test_summary, passCount, skipCount, failCount); 70 } 71 SensorTestDetails(Context context, String name, Result result)72 public SensorTestDetails(Context context, String name, Result result) { 73 this(context, 74 name, 75 result.getRunCount() - result.getFailureCount() - result.getIgnoreCount(), 76 result.getIgnoreCount(), 77 result.getFailureCount()); 78 } 79 SensorTestDetails(String name, String tag, Throwable cause)80 public SensorTestDetails(String name, String tag, Throwable cause) { 81 ResultCode resultCode = ResultCode.FAIL; 82 if (cause instanceof InterruptedException) { 83 resultCode = ResultCode.INTERRUPTED; 84 // the interrupted status must be restored, so other routines can consume it 85 Thread.currentThread().interrupt(); 86 } else if (cause instanceof SensorTestStateNotSupportedException) { 87 resultCode = ResultCode.SKIPPED; 88 } else if (cause instanceof SensorTestWarningException) { 89 resultCode = ResultCode.WARNING; 90 tag = "TestWarning"; 91 } 92 93 mName = name; 94 mResultCode = resultCode; 95 mSummary = String.format("[%s] %s", tag, cause.getMessage()); 96 } 97 getName()98 public String getName() { 99 return mName; 100 } 101 getResultCode()102 public ResultCode getResultCode() { 103 return mResultCode; 104 } 105 getSummary()106 public String getSummary() { 107 return mSummary; 108 } 109 cloneAndChangeResultCode(ResultCode resultCode)110 public SensorTestDetails cloneAndChangeResultCode(ResultCode resultCode) { 111 return new SensorTestDetails(mName, resultCode, mSummary); 112 } 113 114 @Override toString()115 public String toString() { 116 return String.format("%s|%s|%s", mName, mResultCode.name(), mSummary); 117 } 118 } 119