1 /* 2 * Copyright (C) 2019 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.compatibility.common.util; 18 19 import static org.junit.Assume.assumeTrue; 20 21 import android.content.res.Resources; 22 import android.text.TextUtils; 23 import android.util.Log; 24 25 import androidx.annotation.NonNull; 26 import androidx.annotation.Nullable; 27 28 import org.junit.AssumptionViolatedException; 29 import org.junit.rules.TestRule; 30 import org.junit.runner.Description; 31 import org.junit.runners.model.Statement; 32 33 /** 34 * Custom JUnit4 rule that does not run a test case if the device does not define the given system 35 * resource. 36 * 37 * <p>The tests are skipped by throwing a {@link AssumptionViolatedException}. CTS test runners 38 * will report this as a {@code ASSUMPTION_FAILED}. 39 */ 40 public class RequiredSystemResourceRule implements TestRule { 41 42 private static final String TAG = "RequiredSystemResourceRule"; 43 44 @NonNull private final String mName; 45 private final boolean mHasResource; 46 47 /** 48 * Creates a rule for the given system resource. 49 * 50 * @param resourceId resource per se 51 * @param name resource name used for debugging purposes 52 */ RequiredSystemResourceRule(@onNull String name)53 public RequiredSystemResourceRule(@NonNull String name) { 54 mName = name; 55 mHasResource = !TextUtils.isEmpty(getSystemResource(name)); 56 } 57 58 @Override apply(Statement base, Description description)59 public Statement apply(Statement base, Description description) { 60 return new Statement() { 61 62 @Override 63 public void evaluate() throws Throwable { 64 if (!mHasResource) { 65 Log.d(TAG, "skipping " 66 + description.getClassName() + "#" + description.getMethodName() 67 + " because device does not have system resource '" + mName + "'"); 68 assumeTrue("Device does not have system resource '" + mName + "'", 69 mHasResource); 70 return; 71 } 72 base.evaluate(); 73 } 74 }; 75 } 76 77 /** 78 * Gets the given system resource. 79 */ 80 @Nullable 81 public static String getSystemResource(@NonNull String name) { 82 try { 83 final int resourceId = Resources.getSystem().getIdentifier(name, "string", "android"); 84 return Resources.getSystem().getString(resourceId); 85 } catch (Exception e) { 86 Log.e(TAG, "could not get value of resource '" + name + "': ", e); 87 } 88 return null; 89 } 90 91 @Override 92 public String toString() { 93 return "RequiredSystemResourceRule[" + mName + "]"; 94 } 95 } 96