Home | History | Annotate | Download | only in verifier
      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 android.app.Activity;
     20 import android.content.Intent;
     21 
     22 /**
     23  * Object representing the result of a test activity like whether it succeeded or failed.
     24  * Use {@link #setPassedResult(Activity, String, String)} or
     25  * {@link #setFailedResult(Activity, String, String)} from a test activity like you would
     26  * {@link Activity#setResult(int)} so that {@link TestListActivity}
     27  * will persist the test result and update its adapter and thus the list view.
     28  */
     29 public class TestResult {
     30 
     31     public static final int TEST_RESULT_NOT_EXECUTED = 0;
     32     public static final int TEST_RESULT_PASSED = 1;
     33     public static final int TEST_RESULT_FAILED = 2;
     34 
     35     private static final String TEST_NAME = "name";
     36     private static final String TEST_RESULT = "result";
     37     private static final String TEST_DETAILS = "details";
     38 
     39     private final String mName;
     40     private final int mResult;
     41     private final String mDetails;
     42 
     43     /** Sets the test activity's result to pass. */
     44     public static void setPassedResult(Activity activity, String testId, String testDetails) {
     45         activity.setResult(Activity.RESULT_OK, createResult(activity, TEST_RESULT_PASSED, testId,
     46                 testDetails));
     47     }
     48 
     49     /** Sets the test activity's result to failed. */
     50     public static void setFailedResult(Activity activity, String testId, String testDetails) {
     51         activity.setResult(Activity.RESULT_OK, createResult(activity, TEST_RESULT_FAILED, testId,
     52                 testDetails));
     53     }
     54 
     55     private static Intent createResult(Activity activity, int testResult, String testName,
     56             String testDetails) {
     57         Intent data = new Intent(activity, activity.getClass());
     58         data.putExtra(TEST_NAME, testName);
     59         data.putExtra(TEST_RESULT, testResult);
     60         data.putExtra(TEST_DETAILS, testDetails);
     61         return data;
     62     }
     63 
     64     /**
     65      * Convert the test activity's result into a {@link TestResult}. Only meant to be used by
     66      * {@link TestListActivity}.
     67      */
     68     static TestResult fromActivityResult(int resultCode, Intent data) {
     69         String name = data.getStringExtra(TEST_NAME);
     70         int result = data.getIntExtra(TEST_RESULT, TEST_RESULT_NOT_EXECUTED);
     71         String details = data.getStringExtra(TEST_DETAILS);
     72         return new TestResult(name, result, details);
     73     }
     74 
     75     private TestResult(String name, int result, String details) {
     76         this.mName = name;
     77         this.mResult = result;
     78         this.mDetails = details;
     79     }
     80 
     81     /** Return the name of the test like "com.android.cts.verifier.foo.FooTest" */
     82     public String getName() {
     83         return mName;
     84     }
     85 
     86     /** Return integer test result. See test result constants. */
     87     public int getResult() {
     88         return mResult;
     89     }
     90 
     91     /** Return null or string containing test output. */
     92     public String getDetails() {
     93         return mDetails;
     94     }
     95 }
     96