From 41e2ef389e0ef3f5118085e75ead45873a5b55ed Mon Sep 17 00:00:00 2001 From: Mattia Iavarone Date: Fri, 13 Dec 2019 18:06:04 +0100 Subject: [PATCH] Test --- .github/workflows/build.yml | 24 ++++++++-- cameraview/build.gradle | 4 +- .../engine/CameraIntegrationTest.java | 6 +++ .../cameraview/tools/SdkExclude.java | 2 + .../cameraview/tools/SdkExcludeFilter.java | 9 ++-- .../cameraview/tools/SdkInclude.java | 20 ++++++++ .../cameraview/tools/SdkIncludeFilter.java | 47 +++++++++++++++++++ 7 files changed, 102 insertions(+), 10 deletions(-) create mode 100644 cameraview/src/androidTest/java/com/otaliastudios/cameraview/tools/SdkInclude.java create mode 100644 cameraview/src/androidTest/java/com/otaliastudios/cameraview/tools/SdkIncludeFilter.java diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 3c7d62d7..29625815 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -38,19 +38,33 @@ jobs: name: Emulator Tests runs-on: macOS-latest strategy: + fail-fast: false matrix: # TODO 29 fails due to Mockito issues, probably reproducible locally. - # 22, 23, 24, 25, 26, 27, 28 work - some of them, with SdkExclude restrictions. - # EMULATOR_API: [22, 23, 24, 25, 26, 27, 28] - EMULATOR_API: [25] - EMULATOR_ARCH: [x86_64] + # 22-28 work (some of them, with SdkExclude restrictions) + EMULATOR_API: [22, 23, 24, 25, 26, 27, 28] + include: + - EMULATOR_API: 28 + EMULATOR_ARCH: x86_64 + - EMULATOR_API: 27 + EMULATOR_ARCH: x86_64 + - EMULATOR_API: 26 + EMULATOR_ARCH: x86_64 + - EMULATOR_API: 25 + EMULATOR_ARCH: x86 + - EMULATOR_API: 24 + EMULATOR_ARCH: x86 + - EMULATOR_API: 23 + EMULATOR_ARCH: x86 + - EMULATOR_API: 22 + EMULATOR_ARCH: x86 steps: - uses: actions/checkout@v1 - uses: actions/setup-java@v1 with: java-version: 1.8 - name: Execute emulator tests - uses: reactivecircus/android-emulator-runner@v2 + uses: reactivecircus/android-emulator-runner@v2.0.0 with: api-level: ${{ matrix.EMULATOR_API }} arch: ${{ matrix.EMULATOR_ARCH }} diff --git a/cameraview/build.gradle b/cameraview/build.gradle index ba9846d1..692e9baa 100644 --- a/cameraview/build.gradle +++ b/cameraview/build.gradle @@ -17,7 +17,9 @@ android { versionCode 1 versionName project.version testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" - testInstrumentationRunnerArgument "filter", "com.otaliastudios.cameraview.tools.SdkExcludeFilter" + testInstrumentationRunnerArgument "filter", "" + + "com.otaliastudios.cameraview.tools.SdkExcludeFilter," + + "com.otaliastudios.cameraview.tools.SdkIncludeFilter" } buildTypes { diff --git a/cameraview/src/androidTest/java/com/otaliastudios/cameraview/engine/CameraIntegrationTest.java b/cameraview/src/androidTest/java/com/otaliastudios/cameraview/engine/CameraIntegrationTest.java index 43228223..710533de 100644 --- a/cameraview/src/androidTest/java/com/otaliastudios/cameraview/engine/CameraIntegrationTest.java +++ b/cameraview/src/androidTest/java/com/otaliastudios/cameraview/engine/CameraIntegrationTest.java @@ -36,6 +36,7 @@ import com.otaliastudios.cameraview.overlay.Overlay; import com.otaliastudios.cameraview.size.Size; import com.otaliastudios.cameraview.tools.Retry; import com.otaliastudios.cameraview.tools.RetryRule; +import com.otaliastudios.cameraview.tools.SdkExclude; import androidx.annotation.NonNull; import androidx.annotation.Nullable; @@ -613,6 +614,7 @@ public abstract class CameraIntegrationTest extends Base @Test @Retry(emulatorOnly = true) + @SdkExclude(maxSdkVersion = 25, emulatorOnly = true) public void testStartEndVideo() { camera.setMode(Mode.VIDEO); openSync(true); @@ -631,6 +633,7 @@ public abstract class CameraIntegrationTest extends Base @Test @Retry(emulatorOnly = true) + @SdkExclude(maxSdkVersion = 25, emulatorOnly = true) public void testStartEndVideo_withManualStop() { camera.setMode(Mode.VIDEO); openSync(true); @@ -679,6 +682,7 @@ public abstract class CameraIntegrationTest extends Base @Test @Retry(emulatorOnly = true) + @SdkExclude(maxSdkVersion = 25, emulatorOnly = true) public void testEndVideo_withMaxSize() { camera.setMode(Mode.VIDEO); camera.setVideoSize(SizeSelectors.maxArea(480 * 360)); @@ -710,6 +714,7 @@ public abstract class CameraIntegrationTest extends Base @Test @Retry(emulatorOnly = true) + @SdkExclude(maxSdkVersion = 25, emulatorOnly = true) public void testEndVideo_withMaxDuration() { camera.setMode(Mode.VIDEO); camera.setVideoMaxDuration(4000); @@ -1006,6 +1011,7 @@ public abstract class CameraIntegrationTest extends Base @Test @Retry(emulatorOnly = true) + @SdkExclude(maxSdkVersion = 25, emulatorOnly = true) public void testFrameProcessing_afterVideo() throws Exception { FrameProcessor processor = mock(FrameProcessor.class); camera.addFrameProcessor(processor); diff --git a/cameraview/src/androidTest/java/com/otaliastudios/cameraview/tools/SdkExclude.java b/cameraview/src/androidTest/java/com/otaliastudios/cameraview/tools/SdkExclude.java index 23a2d86e..8e4bdebb 100644 --- a/cameraview/src/androidTest/java/com/otaliastudios/cameraview/tools/SdkExclude.java +++ b/cameraview/src/androidTest/java/com/otaliastudios/cameraview/tools/SdkExclude.java @@ -15,4 +15,6 @@ public @interface SdkExclude { int minSdkVersion() default 1; /** The maximum API level to drop (inclusive) */ int maxSdkVersion() default Integer.MAX_VALUE; + /** Whether this filter only applies to emulators */ + boolean emulatorOnly() default false; } \ No newline at end of file diff --git a/cameraview/src/androidTest/java/com/otaliastudios/cameraview/tools/SdkExcludeFilter.java b/cameraview/src/androidTest/java/com/otaliastudios/cameraview/tools/SdkExcludeFilter.java index 4978e74e..0cb621f3 100644 --- a/cameraview/src/androidTest/java/com/otaliastudios/cameraview/tools/SdkExcludeFilter.java +++ b/cameraview/src/androidTest/java/com/otaliastudios/cameraview/tools/SdkExcludeFilter.java @@ -15,10 +15,11 @@ import org.junit.runner.Description; public class SdkExcludeFilter extends ParentFilter { protected boolean evaluateTest(Description description) { - final SdkExclude sdkSuppress = getAnnotationForTest(description); - if (sdkSuppress != null) { - if (Build.VERSION.SDK_INT >= sdkSuppress.minSdkVersion() - && Build.VERSION.SDK_INT <= sdkSuppress.maxSdkVersion()) { + SdkExclude annotation = getAnnotationForTest(description); + if (annotation != null) { + if ((!annotation.emulatorOnly() || Emulator.isEmulator()) + && Build.VERSION.SDK_INT >= annotation.minSdkVersion() + && Build.VERSION.SDK_INT <= annotation.maxSdkVersion()) { return false; // exclude the test } return true; // run the test diff --git a/cameraview/src/androidTest/java/com/otaliastudios/cameraview/tools/SdkInclude.java b/cameraview/src/androidTest/java/com/otaliastudios/cameraview/tools/SdkInclude.java new file mode 100644 index 00000000..4083c56b --- /dev/null +++ b/cameraview/src/androidTest/java/com/otaliastudios/cameraview/tools/SdkInclude.java @@ -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; +} \ No newline at end of file diff --git a/cameraview/src/androidTest/java/com/otaliastudios/cameraview/tools/SdkIncludeFilter.java b/cameraview/src/androidTest/java/com/otaliastudios/cameraview/tools/SdkIncludeFilter.java new file mode 100644 index 00000000..aa0937fb --- /dev/null +++ b/cameraview/src/androidTest/java/com/otaliastudios/cameraview/tools/SdkIncludeFilter.java @@ -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"; + } +}