1 /* 2 * Copyright (C) 2018 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.game.qualification.metric; 18 19 import com.android.annotations.Nullable; 20 import com.android.game.qualification.ApkInfo; 21 import com.android.game.qualification.CertificationRequirements; 22 import com.android.game.qualification.proto.ResultDataProto; 23 import com.android.tradefed.device.ITestDevice; 24 import com.android.tradefed.device.metric.BaseDeviceMetricCollector; 25 import com.android.tradefed.device.metric.DeviceMetricData; 26 import com.android.tradefed.invoker.IInvocationContext; 27 import com.android.tradefed.metrics.proto.MetricMeasurement; 28 import com.android.tradefed.result.TestDescription; 29 30 import java.util.Map; 31 32 public abstract class BaseGameQualificationMetricCollector extends BaseDeviceMetricCollector { 33 @Nullable 34 private ApkInfo mTestApk; 35 @Nullable 36 protected CertificationRequirements mCertificationRequirements; 37 protected ResultDataProto.Result mDeviceResultData; 38 protected ITestDevice mDevice; 39 private boolean mEnabled; 40 private boolean mHasError; 41 private String mErrorMessage = ""; 42 setDevice(ITestDevice device)43 public void setDevice(ITestDevice device) { 44 mDevice = device; 45 } 46 47 @Nullable getApkInfo()48 protected ApkInfo getApkInfo() { 49 return mTestApk; 50 } 51 setApkInfo(ApkInfo apk)52 public void setApkInfo(ApkInfo apk) { 53 mTestApk = apk; 54 } 55 setCertificationRequirements(@ullable CertificationRequirements requirements)56 public void setCertificationRequirements(@Nullable CertificationRequirements requirements) { 57 mCertificationRequirements = requirements; 58 } 59 setDeviceResultData(ResultDataProto.Result resultData)60 public void setDeviceResultData(ResultDataProto.Result resultData) { 61 mDeviceResultData = resultData; 62 } 63 hasError()64 public boolean hasError() { 65 return mHasError; 66 } 67 setHasError(boolean hasError)68 protected void setHasError(boolean hasError) { 69 mHasError = hasError; 70 } 71 getErrorMessage()72 public String getErrorMessage() { 73 return mErrorMessage; 74 } 75 setErrorMessage(String msg)76 protected void setErrorMessage(String msg) { 77 mErrorMessage = msg; 78 } 79 isEnabled()80 public boolean isEnabled() { 81 return mEnabled; 82 } 83 enable()84 public void enable() { 85 mEnabled = true; 86 } 87 disable()88 public void disable() { 89 mEnabled = false; 90 } 91 92 // If an exception is thrown, hasError() must return true in order for the host controller to 93 // recognize an error has occurred. Make onTestStart and onTestEnd final to ensure child 94 // classes do not forget to call setHasError(true). Child classes should override onStart and 95 // onEnd instead. 96 97 @Override onTestStart(DeviceMetricData testData)98 public final void onTestStart(DeviceMetricData testData) { 99 super.onTestStart(testData); 100 try { 101 onStart(testData); 102 } catch (Exception e) { 103 setHasError(true); 104 if (getErrorMessage().isEmpty()) { 105 setErrorMessage(e.getMessage()); 106 } 107 throw e; 108 } 109 } 110 111 @Override onTestEnd( DeviceMetricData testData, Map<String, MetricMeasurement.Metric> currentTestCaseMetrics)112 public final void onTestEnd( 113 DeviceMetricData testData, 114 Map<String, MetricMeasurement.Metric> currentTestCaseMetrics) { 115 super.onTestEnd(testData, currentTestCaseMetrics); 116 try { 117 onEnd(testData, currentTestCaseMetrics); 118 } catch (Exception e) { 119 setHasError(true); 120 if (getErrorMessage().isEmpty()) { 121 setErrorMessage(e.getMessage()); 122 } 123 throw e; 124 } 125 } 126 onStart(DeviceMetricData testData)127 protected void onStart(DeviceMetricData testData) { 128 // Do nothing. 129 } 130 onEnd( DeviceMetricData testData, Map<String, MetricMeasurement.Metric> currentTestCaseMetrics)131 protected void onEnd( 132 DeviceMetricData testData, 133 Map<String, MetricMeasurement.Metric> currentTestCaseMetrics) { 134 // Do nothing. 135 } 136 137 138 // Finalize all other methods in the interface to prevent override. Feel free to make 139 // modification if necessary, but ensure that hasError() will return true when Exception is 140 // thrown. 141 142 @Override testModuleStarted(IInvocationContext moduleContext)143 public final void testModuleStarted(IInvocationContext moduleContext) { 144 super.testModuleStarted(moduleContext); 145 } 146 147 @Override testModuleEnded()148 public final void testModuleEnded() { 149 super.testModuleEnded(); 150 } 151 152 @Override testRunStarted(String runName, int testCount, int attemptNumber)153 public final void testRunStarted(String runName, int testCount, int attemptNumber) { 154 super.testRunStarted(runName, testCount , attemptNumber); 155 } 156 157 @Override testRunEnded(long elapsedTimeMillis, Map<String, String> runMetrics)158 public final void testRunEnded(long elapsedTimeMillis, Map<String, String> runMetrics) { 159 super.testRunEnded(elapsedTimeMillis, runMetrics); 160 } 161 162 @Override testEnded(TestDescription test, Map<String, String> testMetrics)163 public final void testEnded(TestDescription test, Map<String, String> testMetrics) { 164 super.testEnded(test, testMetrics); 165 } 166 167 @Override testEnded(TestDescription test, long endTime, Map<String, String> testMetrics)168 public final void testEnded(TestDescription test, long endTime, Map<String, String> testMetrics) { 169 super.testEnded(test, endTime, testMetrics); 170 } 171 }