parent
bdb2f0ebe3
commit
41e2ef389e
@ -0,0 +1,20 @@ |
||||
package com.otaliastudios.cameraview.tools; |
||||
|
||||
import java.lang.annotation.ElementType; |
||||
import java.lang.annotation.Retention; |
||||
import java.lang.annotation.RetentionPolicy; |
||||
import java.lang.annotation.Target; |
||||
|
||||
/** |
||||
* Like {@link androidx.test.filters.SdkSuppress}, but with emulatorOnly(). |
||||
*/ |
||||
@Retention(RetentionPolicy.RUNTIME) |
||||
@Target({ElementType.TYPE, ElementType.METHOD}) |
||||
public @interface SdkInclude { |
||||
/** The minimum API level to run (inclusive) */ |
||||
int minSdkVersion() default 1; |
||||
/** The maximum API level to run (inclusive) */ |
||||
int maxSdkVersion() default Integer.MAX_VALUE; |
||||
/** Whether this filter only applies to emulators */ |
||||
boolean emulatorOnly() default false; |
||||
} |
@ -0,0 +1,47 @@ |
||||
package com.otaliastudios.cameraview.tools; |
||||
|
||||
|
||||
import android.os.Build; |
||||
|
||||
import androidx.annotation.Nullable; |
||||
import androidx.test.internal.runner.filters.ParentFilter; |
||||
|
||||
import org.junit.runner.Description; |
||||
|
||||
/** |
||||
* Filter for {@link SdkInclude}, based on |
||||
* {@link androidx.test.internal.runner.TestRequestBuilder}'s SdkSuppressFilter. |
||||
*/ |
||||
public class SdkIncludeFilter extends ParentFilter { |
||||
|
||||
protected boolean evaluateTest(Description description) { |
||||
SdkInclude annotation = getAnnotationForTest(description); |
||||
if (annotation != null) { |
||||
if ((!annotation.emulatorOnly() || Emulator.isEmulator()) |
||||
&& Build.VERSION.SDK_INT >= annotation.minSdkVersion() |
||||
&& Build.VERSION.SDK_INT <= annotation.maxSdkVersion()) { |
||||
return true; // run the test
|
||||
} |
||||
return false; // drop the test
|
||||
} |
||||
return true; // no annotation, run the test
|
||||
} |
||||
|
||||
@Nullable |
||||
private SdkInclude getAnnotationForTest(Description description) { |
||||
final SdkInclude s = description.getAnnotation(SdkInclude.class); |
||||
if (s != null) { |
||||
return s; |
||||
} |
||||
final Class<?> testClass = description.getTestClass(); |
||||
if (testClass != null) { |
||||
return testClass.getAnnotation(SdkInclude.class); |
||||
} |
||||
return null; |
||||
} |
||||
|
||||
@Override |
||||
public String describe() { |
||||
return "Skip tests annotated with SdkInclude"; |
||||
} |
||||
} |
Loading…
Reference in new issue