1 // Copyright 2022 The Chromium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 package org.chromium.base.test.util; 6 7 import android.os.Build; 8 9 import org.junit.runners.model.FrameworkMethod; 10 11 import org.chromium.base.Log; 12 13 /** Checks the device's SDK level against any specified minimum or maximum requirement. */ 14 public class AndroidSdkLevelSkipCheck extends SkipCheck { 15 private static final String TAG = "base_test"; 16 17 /** 18 * If either {@link MinAndroidSdkLevel} or {@link MaxAndroidSdkLevel} is present, checks its 19 * boundary against the device's SDK level. 20 * 21 * @param testCase The test to check. 22 * @return true if the device's SDK level is below the specified minimum. 23 */ 24 @Override shouldSkip(FrameworkMethod frameworkMethod)25 public boolean shouldSkip(FrameworkMethod frameworkMethod) { 26 int minSdkLevel = 0; 27 for (MinAndroidSdkLevel m : 28 AnnotationProcessingUtils.getAnnotations( 29 frameworkMethod.getMethod(), MinAndroidSdkLevel.class)) { 30 minSdkLevel = Math.max(minSdkLevel, m.value()); 31 } 32 int maxSdkLevel = Integer.MAX_VALUE; 33 for (MaxAndroidSdkLevel m : 34 AnnotationProcessingUtils.getAnnotations( 35 frameworkMethod.getMethod(), MaxAndroidSdkLevel.class)) { 36 maxSdkLevel = Math.min(maxSdkLevel, m.value()); 37 } 38 if (Build.VERSION.SDK_INT < minSdkLevel || Build.VERSION.SDK_INT > maxSdkLevel) { 39 Log.i( 40 TAG, 41 "Test " 42 + frameworkMethod.getDeclaringClass().getName() 43 + "#" 44 + frameworkMethod.getName() 45 + " is not enabled at SDK level " 46 + Build.VERSION.SDK_INT 47 + "."); 48 return true; 49 } 50 return false; 51 } 52 } 53