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 try { 100 onStart(testData); 101 } catch (Exception e) { 102 setHasError(true); 103 if (getErrorMessage().isEmpty()) { 104 setErrorMessage(e.getMessage()); 105 } 106 throw e; 107 } 108 } 109 110 @Override onTestEnd( DeviceMetricData testData, Map<String, MetricMeasurement.Metric> currentTestCaseMetrics)111 public final void onTestEnd( 112 DeviceMetricData testData, 113 Map<String, MetricMeasurement.Metric> currentTestCaseMetrics) { 114 try { 115 onEnd(testData, currentTestCaseMetrics); 116 } catch (Exception e) { 117 setHasError(true); 118 if (getErrorMessage().isEmpty()) { 119 setErrorMessage(e.getMessage()); 120 } 121 throw e; 122 } 123 } 124 onStart(DeviceMetricData testData)125 protected void onStart(DeviceMetricData testData) { 126 // Do nothing. 127 } 128 onEnd( DeviceMetricData testData, Map<String, MetricMeasurement.Metric> currentTestCaseMetrics)129 protected void onEnd( 130 DeviceMetricData testData, 131 Map<String, MetricMeasurement.Metric> currentTestCaseMetrics) { 132 // Do nothing. 133 } 134 135 } 136