Brian Muramatsu | ac00737 | 2010-08-18 11:02:28 -0700 | [diff] [blame] | 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 | |
Nicholas Sauer | 90f8a23 | 2014-08-17 17:36:53 -0700 | [diff] [blame] | 19 | import com.android.compatibility.common.util.ReportLog; |
| 20 | |
Brian Muramatsu | ac00737 | 2010-08-18 11:02:28 -0700 | [diff] [blame] | 21 | import android.app.Activity; |
| 22 | import android.content.Intent; |
| 23 | |
| 24 | /** |
| 25 | * Object representing the result of a test activity like whether it succeeded or failed. |
Brian Muramatsu | 26e9d0f | 2011-09-21 16:17:09 -0700 | [diff] [blame] | 26 | * Use {@link #setPassedResult(Activity, String, String)} or |
| 27 | * {@link #setFailedResult(Activity, String, String)} from a test activity like you would |
| 28 | * {@link Activity#setResult(int)} so that {@link TestListActivity} |
Jeff Davidson | 112f279 | 2011-08-22 09:46:46 -0700 | [diff] [blame] | 29 | * will persist the test result and update its adapter and thus the list view. |
Brian Muramatsu | ac00737 | 2010-08-18 11:02:28 -0700 | [diff] [blame] | 30 | */ |
| 31 | public class TestResult { |
| 32 | |
| 33 | public static final int TEST_RESULT_NOT_EXECUTED = 0; |
| 34 | public static final int TEST_RESULT_PASSED = 1; |
| 35 | public static final int TEST_RESULT_FAILED = 2; |
| 36 | |
| 37 | private static final String TEST_NAME = "name"; |
| 38 | private static final String TEST_RESULT = "result"; |
Brian Muramatsu | 26e9d0f | 2011-09-21 16:17:09 -0700 | [diff] [blame] | 39 | private static final String TEST_DETAILS = "details"; |
Nicholas Sauer | 90f8a23 | 2014-08-17 17:36:53 -0700 | [diff] [blame] | 40 | private static final String TEST_METRICS = "metrics"; |
Brian Muramatsu | ac00737 | 2010-08-18 11:02:28 -0700 | [diff] [blame] | 41 | |
| 42 | private final String mName; |
Brian Muramatsu | ac00737 | 2010-08-18 11:02:28 -0700 | [diff] [blame] | 43 | private final int mResult; |
Brian Muramatsu | 26e9d0f | 2011-09-21 16:17:09 -0700 | [diff] [blame] | 44 | private final String mDetails; |
Nicholas Sauer | 90f8a23 | 2014-08-17 17:36:53 -0700 | [diff] [blame] | 45 | private final ReportLog mReportLog; |
Brian Muramatsu | ac00737 | 2010-08-18 11:02:28 -0700 | [diff] [blame] | 46 | |
| 47 | /** Sets the test activity's result to pass. */ |
Brian Muramatsu | 26e9d0f | 2011-09-21 16:17:09 -0700 | [diff] [blame] | 48 | public static void setPassedResult(Activity activity, String testId, String testDetails) { |
Nicholas Sauer | 90f8a23 | 2014-08-17 17:36:53 -0700 | [diff] [blame] | 49 | setPassedResult(activity, testId, testDetails, null /*reportLog*/); |
| 50 | } |
| 51 | |
| 52 | /** Sets the test activity's result to pass including a test report log result. */ |
| 53 | public static void setPassedResult(Activity activity, String testId, String testDetails, |
| 54 | ReportLog reportLog) { |
Brian Muramatsu | 26e9d0f | 2011-09-21 16:17:09 -0700 | [diff] [blame] | 55 | activity.setResult(Activity.RESULT_OK, createResult(activity, TEST_RESULT_PASSED, testId, |
Nicholas Sauer | 90f8a23 | 2014-08-17 17:36:53 -0700 | [diff] [blame] | 56 | testDetails, reportLog)); |
Brian Muramatsu | ac00737 | 2010-08-18 11:02:28 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | /** Sets the test activity's result to failed. */ |
Brian Muramatsu | 26e9d0f | 2011-09-21 16:17:09 -0700 | [diff] [blame] | 60 | public static void setFailedResult(Activity activity, String testId, String testDetails) { |
Nicholas Sauer | 90f8a23 | 2014-08-17 17:36:53 -0700 | [diff] [blame] | 61 | setFailedResult(activity, testId, testDetails, null /*reportLog*/); |
| 62 | } |
| 63 | |
| 64 | /** Sets the test activity's result to failed including a test report log result. */ |
| 65 | public static void setFailedResult(Activity activity, String testId, String testDetails, |
| 66 | ReportLog reportLog) { |
Brian Muramatsu | 26e9d0f | 2011-09-21 16:17:09 -0700 | [diff] [blame] | 67 | activity.setResult(Activity.RESULT_OK, createResult(activity, TEST_RESULT_FAILED, testId, |
Nicholas Sauer | 90f8a23 | 2014-08-17 17:36:53 -0700 | [diff] [blame] | 68 | testDetails, reportLog)); |
Brian Muramatsu | ac00737 | 2010-08-18 11:02:28 -0700 | [diff] [blame] | 69 | } |
| 70 | |
Brian Muramatsu | 26e9d0f | 2011-09-21 16:17:09 -0700 | [diff] [blame] | 71 | private static Intent createResult(Activity activity, int testResult, String testName, |
Nicholas Sauer | 90f8a23 | 2014-08-17 17:36:53 -0700 | [diff] [blame] | 72 | String testDetails, ReportLog reportLog) { |
Brian Muramatsu | ac00737 | 2010-08-18 11:02:28 -0700 | [diff] [blame] | 73 | Intent data = new Intent(activity, activity.getClass()); |
Jeff Davidson | 112f279 | 2011-08-22 09:46:46 -0700 | [diff] [blame] | 74 | data.putExtra(TEST_NAME, testName); |
Brian Muramatsu | ac00737 | 2010-08-18 11:02:28 -0700 | [diff] [blame] | 75 | data.putExtra(TEST_RESULT, testResult); |
Brian Muramatsu | 26e9d0f | 2011-09-21 16:17:09 -0700 | [diff] [blame] | 76 | data.putExtra(TEST_DETAILS, testDetails); |
Nicholas Sauer | 90f8a23 | 2014-08-17 17:36:53 -0700 | [diff] [blame] | 77 | data.putExtra(TEST_METRICS, reportLog); |
Brian Muramatsu | ac00737 | 2010-08-18 11:02:28 -0700 | [diff] [blame] | 78 | return data; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Convert the test activity's result into a {@link TestResult}. Only meant to be used by |
| 83 | * {@link TestListActivity}. |
| 84 | */ |
Brian Muramatsu | 26e9d0f | 2011-09-21 16:17:09 -0700 | [diff] [blame] | 85 | static TestResult fromActivityResult(int resultCode, Intent data) { |
Brian Muramatsu | ac00737 | 2010-08-18 11:02:28 -0700 | [diff] [blame] | 86 | String name = data.getStringExtra(TEST_NAME); |
| 87 | int result = data.getIntExtra(TEST_RESULT, TEST_RESULT_NOT_EXECUTED); |
Brian Muramatsu | 26e9d0f | 2011-09-21 16:17:09 -0700 | [diff] [blame] | 88 | String details = data.getStringExtra(TEST_DETAILS); |
Nicholas Sauer | 90f8a23 | 2014-08-17 17:36:53 -0700 | [diff] [blame] | 89 | ReportLog reportLog = (ReportLog) data.getSerializableExtra(TEST_METRICS); |
| 90 | return new TestResult(name, result, details, reportLog); |
Brian Muramatsu | ac00737 | 2010-08-18 11:02:28 -0700 | [diff] [blame] | 91 | } |
| 92 | |
Nicholas Sauer | 90f8a23 | 2014-08-17 17:36:53 -0700 | [diff] [blame] | 93 | private TestResult( |
| 94 | String name, int result, String details, ReportLog reportLog) { |
Brian Muramatsu | ac00737 | 2010-08-18 11:02:28 -0700 | [diff] [blame] | 95 | this.mName = name; |
| 96 | this.mResult = result; |
Brian Muramatsu | 26e9d0f | 2011-09-21 16:17:09 -0700 | [diff] [blame] | 97 | this.mDetails = details; |
Nicholas Sauer | 90f8a23 | 2014-08-17 17:36:53 -0700 | [diff] [blame] | 98 | this.mReportLog = reportLog; |
Brian Muramatsu | ac00737 | 2010-08-18 11:02:28 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | /** Return the name of the test like "com.android.cts.verifier.foo.FooTest" */ |
| 102 | public String getName() { |
| 103 | return mName; |
| 104 | } |
| 105 | |
| 106 | /** Return integer test result. See test result constants. */ |
| 107 | public int getResult() { |
| 108 | return mResult; |
| 109 | } |
Brian Muramatsu | 26e9d0f | 2011-09-21 16:17:09 -0700 | [diff] [blame] | 110 | |
| 111 | /** Return null or string containing test output. */ |
| 112 | public String getDetails() { |
| 113 | return mDetails; |
| 114 | } |
Nicholas Sauer | 90f8a23 | 2014-08-17 17:36:53 -0700 | [diff] [blame] | 115 | |
| 116 | /** @return the {@link ReportLog} or null if not set */ |
| 117 | public ReportLog getReportLog() { |
| 118 | return mReportLog; |
| 119 | } |
Brian Muramatsu | ac00737 | 2010-08-18 11:02:28 -0700 | [diff] [blame] | 120 | } |