blob: d8a675c6e7f96fbd3e69b29c96c2107539744aba [file] [log] [blame]
Brian Muramatsuac007372010-08-18 11:02:28 -07001/*
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
17package com.android.cts.verifier;
18
Nicholas Sauer90f8a232014-08-17 17:36:53 -070019import com.android.compatibility.common.util.ReportLog;
20
Brian Muramatsuac007372010-08-18 11:02:28 -070021import android.app.Activity;
22import android.content.Intent;
23
24/**
25 * Object representing the result of a test activity like whether it succeeded or failed.
Brian Muramatsu26e9d0f2011-09-21 16:17:09 -070026 * 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 Davidson112f2792011-08-22 09:46:46 -070029 * will persist the test result and update its adapter and thus the list view.
Brian Muramatsuac007372010-08-18 11:02:28 -070030 */
31public 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 Muramatsu26e9d0f2011-09-21 16:17:09 -070039 private static final String TEST_DETAILS = "details";
Nicholas Sauer90f8a232014-08-17 17:36:53 -070040 private static final String TEST_METRICS = "metrics";
Brian Muramatsuac007372010-08-18 11:02:28 -070041
42 private final String mName;
Brian Muramatsuac007372010-08-18 11:02:28 -070043 private final int mResult;
Brian Muramatsu26e9d0f2011-09-21 16:17:09 -070044 private final String mDetails;
Nicholas Sauer90f8a232014-08-17 17:36:53 -070045 private final ReportLog mReportLog;
Brian Muramatsuac007372010-08-18 11:02:28 -070046
47 /** Sets the test activity's result to pass. */
Brian Muramatsu26e9d0f2011-09-21 16:17:09 -070048 public static void setPassedResult(Activity activity, String testId, String testDetails) {
Nicholas Sauer90f8a232014-08-17 17:36:53 -070049 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 Muramatsu26e9d0f2011-09-21 16:17:09 -070055 activity.setResult(Activity.RESULT_OK, createResult(activity, TEST_RESULT_PASSED, testId,
Nicholas Sauer90f8a232014-08-17 17:36:53 -070056 testDetails, reportLog));
Brian Muramatsuac007372010-08-18 11:02:28 -070057 }
58
59 /** Sets the test activity's result to failed. */
Brian Muramatsu26e9d0f2011-09-21 16:17:09 -070060 public static void setFailedResult(Activity activity, String testId, String testDetails) {
Nicholas Sauer90f8a232014-08-17 17:36:53 -070061 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 Muramatsu26e9d0f2011-09-21 16:17:09 -070067 activity.setResult(Activity.RESULT_OK, createResult(activity, TEST_RESULT_FAILED, testId,
Nicholas Sauer90f8a232014-08-17 17:36:53 -070068 testDetails, reportLog));
Brian Muramatsuac007372010-08-18 11:02:28 -070069 }
70
Brian Muramatsu26e9d0f2011-09-21 16:17:09 -070071 private static Intent createResult(Activity activity, int testResult, String testName,
Nicholas Sauer90f8a232014-08-17 17:36:53 -070072 String testDetails, ReportLog reportLog) {
Brian Muramatsuac007372010-08-18 11:02:28 -070073 Intent data = new Intent(activity, activity.getClass());
Jeff Davidson112f2792011-08-22 09:46:46 -070074 data.putExtra(TEST_NAME, testName);
Brian Muramatsuac007372010-08-18 11:02:28 -070075 data.putExtra(TEST_RESULT, testResult);
Brian Muramatsu26e9d0f2011-09-21 16:17:09 -070076 data.putExtra(TEST_DETAILS, testDetails);
Nicholas Sauer90f8a232014-08-17 17:36:53 -070077 data.putExtra(TEST_METRICS, reportLog);
Brian Muramatsuac007372010-08-18 11:02:28 -070078 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 Muramatsu26e9d0f2011-09-21 16:17:09 -070085 static TestResult fromActivityResult(int resultCode, Intent data) {
Brian Muramatsuac007372010-08-18 11:02:28 -070086 String name = data.getStringExtra(TEST_NAME);
87 int result = data.getIntExtra(TEST_RESULT, TEST_RESULT_NOT_EXECUTED);
Brian Muramatsu26e9d0f2011-09-21 16:17:09 -070088 String details = data.getStringExtra(TEST_DETAILS);
Nicholas Sauer90f8a232014-08-17 17:36:53 -070089 ReportLog reportLog = (ReportLog) data.getSerializableExtra(TEST_METRICS);
90 return new TestResult(name, result, details, reportLog);
Brian Muramatsuac007372010-08-18 11:02:28 -070091 }
92
Nicholas Sauer90f8a232014-08-17 17:36:53 -070093 private TestResult(
94 String name, int result, String details, ReportLog reportLog) {
Brian Muramatsuac007372010-08-18 11:02:28 -070095 this.mName = name;
96 this.mResult = result;
Brian Muramatsu26e9d0f2011-09-21 16:17:09 -070097 this.mDetails = details;
Nicholas Sauer90f8a232014-08-17 17:36:53 -070098 this.mReportLog = reportLog;
Brian Muramatsuac007372010-08-18 11:02:28 -070099 }
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 Muramatsu26e9d0f2011-09-21 16:17:09 -0700110
111 /** Return null or string containing test output. */
112 public String getDetails() {
113 return mDetails;
114 }
Nicholas Sauer90f8a232014-08-17 17:36:53 -0700115
116 /** @return the {@link ReportLog} or null if not set */
117 public ReportLog getReportLog() {
118 return mReportLog;
119 }
Brian Muramatsuac007372010-08-18 11:02:28 -0700120}