Home | History | Annotate | Download | only in reporting
      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 com.android.cts.verifier.R;
     21 
     22 import org.junit.runner.Result;
     23 
     24 import android.content.Context;
     25 import android.hardware.cts.helpers.SensorTestStateNotSupportedException;
     26 
     27 /**
     28  * A class that holds the result of a Sensor test execution.
     29  */
     30 public class SensorTestDetails {
     31     private final String mName;
     32     private final ResultCode mResultCode;
     33     private final String mSummary;
     34 
     35     public enum ResultCode {
     36         SKIPPED,
     37         PASS,
     38         FAIL,
     39         INTERRUPTED
     40     }
     41 
     42     public SensorTestDetails(String name, ResultCode resultCode) {
     43         this(name, resultCode, null /* summary */);
     44     }
     45 
     46     public SensorTestDetails(String name, ResultCode resultCode, String summary) {
     47         mName = name;
     48         mResultCode = resultCode;
     49         mSummary = summary;
     50     }
     51 
     52     public SensorTestDetails(
     53             Context context,
     54             String name,
     55             int passCount,
     56             int skipCount,
     57             int failCount) {
     58         ResultCode resultCode = ResultCode.PASS;
     59         if (failCount > 0) {
     60             resultCode = ResultCode.FAIL;
     61         } else if (skipCount > 0) {
     62             resultCode = ResultCode.SKIPPED;
     63         }
     64 
     65         mName = name;
     66         mResultCode = resultCode;
     67         mSummary = context.getString(R.string.snsr_test_summary, passCount, skipCount, failCount);
     68     }
     69 
     70     public SensorTestDetails(Context context, String name, Result result) {
     71         this(context,
     72                 name,
     73                 result.getRunCount() - result.getFailureCount() - result.getIgnoreCount(),
     74                 result.getIgnoreCount(),
     75                 result.getFailureCount());
     76     }
     77 
     78     public SensorTestDetails(String name, String tag, Throwable cause) {
     79         ResultCode resultCode = ResultCode.FAIL;
     80         if (cause instanceof InterruptedException) {
     81             resultCode = ResultCode.INTERRUPTED;
     82             // the interrupted status must be restored, so other routines can consume it
     83             Thread.currentThread().interrupt();
     84         } else if (cause instanceof SensorTestStateNotSupportedException) {
     85             resultCode = ResultCode.SKIPPED;
     86         }
     87 
     88         mName = name;
     89         mResultCode = resultCode;
     90         mSummary = String.format("[%s] %s", tag, cause.getMessage());
     91     }
     92 
     93     public String getName() {
     94         return mName;
     95     }
     96 
     97     public ResultCode getResultCode() {
     98         return mResultCode;
     99     }
    100 
    101     public String getSummary() {
    102         return mSummary;
    103     }
    104 
    105     public SensorTestDetails cloneAndChangeResultCode(ResultCode resultCode) {
    106         return new SensorTestDetails(mName, resultCode, mSummary);
    107     }
    108 
    109     @Override
    110     public String toString() {
    111         return String.format("%s|%s|%s", mName, mResultCode.name(), mSummary);
    112     }
    113 }
    114