Add @OnlyOnRavenwood

Also add a bivalent test.

Test: atest RavenwoodBivalentTest && atest RavenwoodBivalentTest_device
Bug: 292141694
Change-Id: I10f258e62e8a7f6a85fad5b39eda27c397e08f55
diff --git a/ravenwood/TEST_MAPPING b/ravenwood/TEST_MAPPING
index 0319848..a5b28ad 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 0000000..acf8533
--- /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 0000000..474c03f
--- /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 0000000..ac4695b
--- /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 0000000..7153555
--- /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 0000000..4b650b4
--- /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 9b7f8f7..a78c5c1 100644
--- a/ravenwood/coretest/Android.bp
+++ b/ravenwood/coretest/Android.bp
@@ -12,9 +12,9 @@
 
     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 0000000..b60bfbf
--- /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 e58c282..2cd585f 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 @@
 
 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 0000000..8ca34ba
--- /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 8d76970..9a4d488 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 @@
 
 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 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 764573d..b90f112 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 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 @@
         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 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 0000000..6b0abe9
--- /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 03cfad5..b477117 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 @@
 @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 96bc275..5ba9b1f 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 0000000..4ceb795
--- /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