1 /* 2 * Copyright (C) 2022 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 android.car.cts.builtin; 18 19 import com.android.tradefed.device.DeviceNotAvailableException; 20 import com.android.tradefed.log.LogUtil.CLog; 21 import com.android.tradefed.testtype.ITestInformationReceiver; 22 import com.android.tradefed.testtype.junit4.BaseHostJUnit4Test; 23 24 import org.junit.AssumptionViolatedException; 25 import org.junit.Rule; 26 import org.junit.rules.TestRule; 27 import org.junit.runner.Description; 28 import org.junit.runners.model.Statement; 29 30 /** 31 * Base class for all car mainline builtin API host test cases. 32 */ 33 // NOTE: must be public because of @Rules 34 public abstract class CarBuiltinApiHostCtsBase extends BaseHostJUnit4Test { 35 36 @Rule 37 public final RequiredFeatureRule mHasAutomotiveRule = new RequiredFeatureRule(this, 38 "android.hardware.type.automotive"); 39 40 41 protected static final class RequiredFeatureRule implements TestRule { 42 43 private final ITestInformationReceiver mReceiver; 44 private final String mFeature; 45 RequiredFeatureRule(ITestInformationReceiver receiver, String feature)46 RequiredFeatureRule(ITestInformationReceiver receiver, String feature) { 47 mReceiver = receiver; 48 mFeature = feature; 49 } 50 51 @Override apply(Statement base, Description description)52 public Statement apply(Statement base, Description description) { 53 return new Statement() { 54 55 @Override 56 public void evaluate() throws Throwable { 57 boolean hasFeature = false; 58 try { 59 hasFeature = mReceiver.getTestInformation().getDevice() 60 .hasFeature(mFeature); 61 } catch (DeviceNotAvailableException e) { 62 CLog.e("Could not check if device has feature %s: %e", mFeature, e); 63 return; 64 } 65 66 if (!hasFeature) { 67 CLog.d("skipping %s#%s" 68 + " because device does not have feature '%s'", 69 description.getClassName(), description.getMethodName(), mFeature); 70 throw new AssumptionViolatedException("Device does not have feature '" 71 + mFeature + "'"); 72 } 73 base.evaluate(); 74 } 75 }; 76 } 77 78 @Override toString()79 public String toString() { 80 return "RequiredFeatureRule[" + mFeature + "]"; 81 } 82 } 83 } 84