From 8e4eb7ed4d16c7ea847d5e21717389db8dd021de Mon Sep 17 00:00:00 2001 From: Makoto Onuki <omakoto@google.com> Date: Thu, 1 Feb 2024 15:34:37 -0800 Subject: [PATCH] Add @OnlyOnRavenwood Also add a bivalent test. Test: atest RavenwoodBivalentTest && atest RavenwoodBivalentTest_device Bug: 292141694 Change-Id: I10f258e62e8a7f6a85fad5b39eda27c397e08f55 --- ravenwood/TEST_MAPPING | 11 +++++ ravenwood/bivalenttest/Android.bp | 47 +++++++++++++++++++ ravenwood/bivalenttest/AndroidManifest.xml | 28 +++++++++++ ravenwood/bivalenttest/AndroidTest.xml | 29 ++++++++++++ ravenwood/bivalenttest/README.md | 3 ++ .../bivalenttest/RavenwoodRuleTest.java | 47 +++++++++++++++++++ ravenwood/coretest/Android.bp | 2 +- ravenwood/coretest/README.md | 3 ++ .../RavenwoodTestRunnerValidationTest.java | 2 +- .../annotations/DisabledOnNonRavenwood.java | 47 +++++++++++++++++++ .../test/ravenwood/RavenwoodClassRule.java | 2 + .../test/ravenwood/RavenwoodRule.java | 11 +++++ ravenwood/minimum-test/README.md | 3 ++ .../ravenwood/RavenwoodMinimumTest.java | 2 +- ravenwood/mockito/AndroidTest.xml | 2 - ravenwood/mockito/README.md | 3 ++ 16 files changed, 237 insertions(+), 5 deletions(-) create mode 100644 ravenwood/bivalenttest/Android.bp create mode 100644 ravenwood/bivalenttest/AndroidManifest.xml create mode 100644 ravenwood/bivalenttest/AndroidTest.xml create mode 100644 ravenwood/bivalenttest/README.md create mode 100644 ravenwood/bivalenttest/test/com/android/platform/test/ravenwood/bivalenttest/RavenwoodRuleTest.java create mode 100644 ravenwood/coretest/README.md create mode 100644 ravenwood/junit-src/android/platform/test/annotations/DisabledOnNonRavenwood.java create mode 100644 ravenwood/minimum-test/README.md create mode 100644 ravenwood/mockito/README.md diff --git a/ravenwood/TEST_MAPPING b/ravenwood/TEST_MAPPING index 031984829e77..a5b28ad550ba 100644 --- a/ravenwood/TEST_MAPPING +++ b/ravenwood/TEST_MAPPING @@ -2,6 +2,9 @@ "presubmit": [ { "name": "RavenwoodMockitoTest_device" + }, + { + "name": "RavenwoodBivalentTest_device" } ], "ravenwood-presubmit": [ @@ -16,6 +19,14 @@ { "name": "CtsUtilTestCasesRavenwood", "host": true + }, + { + "name": "RavenwoodCoreTest", + "host": true + }, + { + "name": "RavenwoodBivalentTest", + "host": true } ] } diff --git a/ravenwood/bivalenttest/Android.bp b/ravenwood/bivalenttest/Android.bp new file mode 100644 index 000000000000..acf85331601e --- /dev/null +++ b/ravenwood/bivalenttest/Android.bp @@ -0,0 +1,47 @@ +package { + // See: http://go/android-license-faq + // A large-scale-change added 'default_applicable_licenses' to import + // all of the 'license_kinds' from "frameworks_base_license" + // to get the below license kinds: + // SPDX-license-identifier-Apache-2.0 + default_applicable_licenses: ["frameworks_base_license"], +} + +android_ravenwood_test { + name: "RavenwoodBivalentTest", + + static_libs: [ + "androidx.annotation_annotation", + "androidx.test.ext.junit", + "androidx.test.rules", + ], + srcs: [ + "test/**/*.java", + ], + sdk_version: "test_current", + auto_gen_config: true, +} + +android_test { + name: "RavenwoodBivalentTest_device", + + srcs: [ + "test/**/*.java", + ], + static_libs: [ + "junit", + "truth", + + "androidx.annotation_annotation", + "androidx.test.ext.junit", + "androidx.test.rules", + + "ravenwood-junit", + ], + test_suites: [ + "device-tests", + ], + optimize: { + enabled: false, + }, +} diff --git a/ravenwood/bivalenttest/AndroidManifest.xml b/ravenwood/bivalenttest/AndroidManifest.xml new file mode 100644 index 000000000000..474c03f4f3c9 --- /dev/null +++ b/ravenwood/bivalenttest/AndroidManifest.xml @@ -0,0 +1,28 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- Copyright (C) 2024 The Android Open Source Project + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +--> + +<manifest xmlns:android="http://schemas.android.com/apk/res/android" + package="com.android.ravenwood.bivalenttest"> + + <application android:debuggable="true" > + <uses-library android:name="android.test.runner" /> + </application> + + <instrumentation + android:name="androidx.test.runner.AndroidJUnitRunner" + android:targetPackage="com.android.ravenwood.bivalenttest" + /> +</manifest> diff --git a/ravenwood/bivalenttest/AndroidTest.xml b/ravenwood/bivalenttest/AndroidTest.xml new file mode 100644 index 000000000000..ac4695bb9a50 --- /dev/null +++ b/ravenwood/bivalenttest/AndroidTest.xml @@ -0,0 +1,29 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- Copyright (C) 2024 The Android Open Source Project + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +--> +<configuration description="Runs Frameworks Services Tests."> + <option name="test-suite-tag" value="apct" /> + <option name="test-suite-tag" value="apct-instrumentation" /> + + <target_preparer class="com.android.tradefed.targetprep.suite.SuiteApkInstaller"> + <option name="cleanup-apks" value="true" /> + <option name="test-file-name" value="RavenwoodBivalentTest_device.apk" /> + </target_preparer> + + <test class="com.android.tradefed.testtype.AndroidJUnitTest" > + <option name="package" value="com.android.ravenwood.bivalenttest" /> + <option name="runner" value="androidx.test.runner.AndroidJUnitRunner" /> + </test> +</configuration> diff --git a/ravenwood/bivalenttest/README.md b/ravenwood/bivalenttest/README.md new file mode 100644 index 000000000000..71535556ec75 --- /dev/null +++ b/ravenwood/bivalenttest/README.md @@ -0,0 +1,3 @@ +# Ravenwood bivalent test + +This test contains bivalent tests for Ravenwood itself. \ No newline at end of file diff --git a/ravenwood/bivalenttest/test/com/android/platform/test/ravenwood/bivalenttest/RavenwoodRuleTest.java b/ravenwood/bivalenttest/test/com/android/platform/test/ravenwood/bivalenttest/RavenwoodRuleTest.java new file mode 100644 index 000000000000..4b650b4bc5e1 --- /dev/null +++ b/ravenwood/bivalenttest/test/com/android/platform/test/ravenwood/bivalenttest/RavenwoodRuleTest.java @@ -0,0 +1,47 @@ +/* + * Copyright (C) 2024 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.android.platform.test.ravenwood.bivalenttest; + +import android.platform.test.annotations.DisabledOnNonRavenwood; +import android.platform.test.annotations.DisabledOnRavenwood; +import android.platform.test.ravenwood.RavenwoodRule; + +import androidx.test.ext.junit.runners.AndroidJUnit4; + +import org.junit.Assert; +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; + +@RunWith(AndroidJUnit4.class) +public class RavenwoodRuleTest { + @Rule + public final RavenwoodRule mRavenwood = new RavenwoodRule(); + + @Test + @DisabledOnRavenwood + public void testDeviceOnly() { + Assert.assertFalse(RavenwoodRule.isOnRavenwood()); + } + + @Test + @DisabledOnNonRavenwood + public void testRavenwoodOnly() { + Assert.assertTrue(RavenwoodRule.isOnRavenwood()); + } + + // TODO: Add more tests +} diff --git a/ravenwood/coretest/Android.bp b/ravenwood/coretest/Android.bp index 9b7f8f7d2171..a78c5c1e8227 100644 --- a/ravenwood/coretest/Android.bp +++ b/ravenwood/coretest/Android.bp @@ -12,9 +12,9 @@ android_ravenwood_test { static_libs: [ "androidx.annotation_annotation", + "androidx.test.ext.junit", "androidx.test.rules", ], - srcs: [ "test/**/*.java", ], diff --git a/ravenwood/coretest/README.md b/ravenwood/coretest/README.md new file mode 100644 index 000000000000..b60bfbfcb6f4 --- /dev/null +++ b/ravenwood/coretest/README.md @@ -0,0 +1,3 @@ +# Ravenwood core test + +This test contains (non-bivalent) tests for Ravenwood itself -- e.g. tests for the ravenwood rules. \ No newline at end of file diff --git a/ravenwood/coretest/test/com/android/platform/test/ravenwood/coretest/RavenwoodTestRunnerValidationTest.java b/ravenwood/coretest/test/com/android/platform/test/ravenwood/coretest/RavenwoodTestRunnerValidationTest.java index e58c282fb690..2cd585ff6c9c 100644 --- a/ravenwood/coretest/test/com/android/platform/test/ravenwood/coretest/RavenwoodTestRunnerValidationTest.java +++ b/ravenwood/coretest/test/com/android/platform/test/ravenwood/coretest/RavenwoodTestRunnerValidationTest.java @@ -17,7 +17,7 @@ package com.android.platform.test.ravenwood.coretest; import android.platform.test.ravenwood.RavenwoodRule; -import androidx.test.runner.AndroidJUnit4; +import androidx.test.runner.AndroidJUnit4; // Intentionally use the deprecated one. import org.junit.Assume; import org.junit.Rule; diff --git a/ravenwood/junit-src/android/platform/test/annotations/DisabledOnNonRavenwood.java b/ravenwood/junit-src/android/platform/test/annotations/DisabledOnNonRavenwood.java new file mode 100644 index 000000000000..8ca34bafc2c6 --- /dev/null +++ b/ravenwood/junit-src/android/platform/test/annotations/DisabledOnNonRavenwood.java @@ -0,0 +1,47 @@ +/* + * Copyright (C) 2024 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.platform.test.annotations; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Inherited; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Tests marked with this annotation are only executed when running on Ravenwood, but not + * on a device. + * + * This is basically equivalent to the opposite of {@link DisabledOnRavenwood}, but in order to + * avoid complex structure, and there's no equivalent to the opposite {@link EnabledOnRavenwood}, + * which means if a test class has this annotation, you can't negate it in subclasses or + * on a per-method basis. + * + * The {@code RAVENWOOD_RUN_DISABLED_TESTS} environmental variable won't work because it won't be + * propagated to the device. (We may support it in the future, possibly using a debug. sysprop.) + * + * @hide + */ +@Inherited +@Target({ElementType.METHOD, ElementType.TYPE}) +@Retention(RetentionPolicy.RUNTIME) +public @interface DisabledOnNonRavenwood { + /** + * General free-form description of why this test is being ignored. + */ + String reason() default ""; +} diff --git a/ravenwood/junit-src/android/platform/test/ravenwood/RavenwoodClassRule.java b/ravenwood/junit-src/android/platform/test/ravenwood/RavenwoodClassRule.java index 8d76970f9ad4..9a4d4886d6f0 100644 --- a/ravenwood/junit-src/android/platform/test/ravenwood/RavenwoodClassRule.java +++ b/ravenwood/junit-src/android/platform/test/ravenwood/RavenwoodClassRule.java @@ -18,6 +18,7 @@ package android.platform.test.ravenwood; import static android.platform.test.ravenwood.RavenwoodRule.ENABLE_PROBE_IGNORED; import static android.platform.test.ravenwood.RavenwoodRule.IS_ON_RAVENWOOD; +import static android.platform.test.ravenwood.RavenwoodRule.shouldEnableOnDevice; import static android.platform.test.ravenwood.RavenwoodRule.shouldEnableOnRavenwood; import static android.platform.test.ravenwood.RavenwoodRule.shouldStillIgnoreInProbeIgnoreMode; @@ -42,6 +43,7 @@ public class RavenwoodClassRule implements TestRule { public Statement apply(Statement base, Description description) { // No special treatment when running outside Ravenwood; run tests as-is if (!IS_ON_RAVENWOOD) { + Assume.assumeTrue(shouldEnableOnDevice(description)); return base; } diff --git a/ravenwood/junit-src/android/platform/test/ravenwood/RavenwoodRule.java b/ravenwood/junit-src/android/platform/test/ravenwood/RavenwoodRule.java index 764573defd7a..b90f112c1655 100644 --- a/ravenwood/junit-src/android/platform/test/ravenwood/RavenwoodRule.java +++ b/ravenwood/junit-src/android/platform/test/ravenwood/RavenwoodRule.java @@ -22,6 +22,7 @@ import static android.os.UserHandle.USER_SYSTEM; import static org.junit.Assert.fail; +import android.platform.test.annotations.DisabledOnNonRavenwood; import android.platform.test.annotations.DisabledOnRavenwood; import android.platform.test.annotations.EnabledOnRavenwood; import android.platform.test.annotations.IgnoreUnderRavenwood; @@ -211,6 +212,15 @@ public class RavenwoodRule implements TestRule { return IS_ON_RAVENWOOD; } + static boolean shouldEnableOnDevice(Description description) { + if (description.isTest()) { + if (description.getAnnotation(DisabledOnNonRavenwood.class) != null) { + return false; + } + } + return true; + } + /** * Determine if the given {@link Description} should be enabled when running on the * Ravenwood test environment. @@ -271,6 +281,7 @@ public class RavenwoodRule implements TestRule { public Statement apply(Statement base, Description description) { // No special treatment when running outside Ravenwood; run tests as-is if (!IS_ON_RAVENWOOD) { + Assume.assumeTrue(shouldEnableOnDevice(description)); return base; } diff --git a/ravenwood/minimum-test/README.md b/ravenwood/minimum-test/README.md new file mode 100644 index 000000000000..6b0abe968053 --- /dev/null +++ b/ravenwood/minimum-test/README.md @@ -0,0 +1,3 @@ +# Ravenwood minimum test + +This directory contains a minimum possible ravenwood test -- no extra dependencies, etc. \ No newline at end of file diff --git a/ravenwood/minimum-test/test/com/android/ravenwood/RavenwoodMinimumTest.java b/ravenwood/minimum-test/test/com/android/ravenwood/RavenwoodMinimumTest.java index 03cfad5c7a70..b4771171815a 100644 --- a/ravenwood/minimum-test/test/com/android/ravenwood/RavenwoodMinimumTest.java +++ b/ravenwood/minimum-test/test/com/android/ravenwood/RavenwoodMinimumTest.java @@ -28,7 +28,7 @@ import org.junit.runner.RunWith; @RunWith(AndroidJUnit4.class) public class RavenwoodMinimumTest { @Rule - public RavenwoodRule mRavenwood = new RavenwoodRule.Builder() + public final RavenwoodRule mRavenwood = new RavenwoodRule.Builder() .setProcessApp() .build(); diff --git a/ravenwood/mockito/AndroidTest.xml b/ravenwood/mockito/AndroidTest.xml index 96bc2752fe95..5ba9b1ff2cd8 100644 --- a/ravenwood/mockito/AndroidTest.xml +++ b/ravenwood/mockito/AndroidTest.xml @@ -22,8 +22,6 @@ <option name="test-file-name" value="RavenwoodMockitoTest_device.apk" /> </target_preparer> - <option name="test-tag" value="FrameworksMockingServicesTests" /> - <test class="com.android.tradefed.testtype.AndroidJUnitTest" > <option name="package" value="com.android.ravenwood.mockitotest" /> <option name="runner" value="androidx.test.runner.AndroidJUnitRunner" /> diff --git a/ravenwood/mockito/README.md b/ravenwood/mockito/README.md new file mode 100644 index 000000000000..4ceb795fe14a --- /dev/null +++ b/ravenwood/mockito/README.md @@ -0,0 +1,3 @@ +# Ravenwood mockito test + +This directory contains a sample bivalent test using Mockito. \ No newline at end of file -- GitLab