Offer baseline `RavenwoodFlagsValueProvider`.
Many test authors are starting to use `CheckFlagsRule` across suites
like CTS, so instead of recommending that they use `null` when on
Ravenwood, offer them a clear behavior that is either "all-on" or
"all-off".
Eventually we'll explore providing default flag values on Ravenwood,
but let's get something cleaner available now.
Bug: 318841620
Test: ./frameworks/base/ravenwood/run-ravenwood-tests.sh
Change-Id: Id234a375426fad38f64d3a999339bb720eae84bf
diff --git a/Ravenwood.bp b/Ravenwood.bp
index 4791640..f43c37b 100644
--- a/Ravenwood.bp
+++ b/Ravenwood.bp
@@ -205,8 +205,10 @@
// Provide runtime versions of utils linked in below
"junit",
"truth",
+ "flag-junit",
"ravenwood-framework",
"ravenwood-junit-impl",
+ "ravenwood-junit-impl-flag",
"mockito-ravenwood-prebuilt",
"inline-mockito-ravenwood-prebuilt",
],
@@ -220,6 +222,7 @@
libs: [
"junit",
"truth",
+ "flag-junit",
"ravenwood-framework",
"ravenwood-junit",
"mockito-ravenwood-prebuilt",
diff --git a/core/tests/coretests/src/android/os/PowerManagerTest.java b/core/tests/coretests/src/android/os/PowerManagerTest.java
index cb281ff..b9a12ad 100644
--- a/core/tests/coretests/src/android/os/PowerManagerTest.java
+++ b/core/tests/coretests/src/android/os/PowerManagerTest.java
@@ -32,6 +32,7 @@
import android.platform.test.annotations.RequiresFlagsEnabled;
import android.platform.test.flag.junit.CheckFlagsRule;
import android.platform.test.flag.junit.DeviceFlagsValueProvider;
+import android.platform.test.flag.junit.RavenwoodFlagsValueProvider;
import android.platform.test.ravenwood.RavenwoodRule;
import androidx.test.InstrumentationRegistry;
@@ -86,7 +87,8 @@
// Required for RequiresFlagsEnabled and RequiresFlagsDisabled annotations to take effect.
@Rule
- public final CheckFlagsRule mCheckFlagsRule = RavenwoodRule.isUnderRavenwood() ? null
+ public final CheckFlagsRule mCheckFlagsRule = RavenwoodRule.isOnRavenwood()
+ ? RavenwoodFlagsValueProvider.createAllOnCheckFlagsRule()
: DeviceFlagsValueProvider.createCheckFlagsRule();
/**
diff --git a/core/tests/coretests/src/android/os/WorkDurationUnitTest.java b/core/tests/coretests/src/android/os/WorkDurationUnitTest.java
index c70da6e..fcdc590 100644
--- a/core/tests/coretests/src/android/os/WorkDurationUnitTest.java
+++ b/core/tests/coretests/src/android/os/WorkDurationUnitTest.java
@@ -22,6 +22,7 @@
import android.platform.test.annotations.RequiresFlagsEnabled;
import android.platform.test.flag.junit.CheckFlagsRule;
import android.platform.test.flag.junit.DeviceFlagsValueProvider;
+import android.platform.test.flag.junit.RavenwoodFlagsValueProvider;
import android.platform.test.ravenwood.RavenwoodRule;
import androidx.test.runner.AndroidJUnit4;
@@ -40,7 +41,8 @@
// Required for RequiresFlagsEnabled and RequiresFlagsDisabled annotations to take effect.
@Rule
- public final CheckFlagsRule mCheckFlagsRule = RavenwoodRule.isUnderRavenwood() ? null
+ public final CheckFlagsRule mCheckFlagsRule = RavenwoodRule.isOnRavenwood()
+ ? RavenwoodFlagsValueProvider.createAllOnCheckFlagsRule()
: DeviceFlagsValueProvider.createCheckFlagsRule();
@Before
diff --git a/ravenwood/Android.bp b/ravenwood/Android.bp
index e535f0a..178102e 100644
--- a/ravenwood/Android.bp
+++ b/ravenwood/Android.bp
@@ -86,6 +86,21 @@
jarjar_rules: ":ravenwood-services-jarjar-rules",
}
+// Separated out from ravenwood-junit-impl since it needs to compile
+// against `module_current`
+java_library {
+ name: "ravenwood-junit-impl-flag",
+ srcs: [
+ "junit-flag-src/**/*.java",
+ ],
+ sdk_version: "module_current",
+ libs: [
+ "junit",
+ "flag-junit",
+ ],
+ visibility: ["//visibility:public"],
+}
+
// Carefully compiles against only test_current to support tests that
// want to verify they're unbundled. The "impl" library above is what
// ships inside the Ravenwood environment to actually drive any API
@@ -95,10 +110,12 @@
srcs: [
"junit-src/**/*.java",
"junit-stub-src/**/*.java",
+ "junit-flag-src/**/*.java",
],
sdk_version: "test_current",
libs: [
"junit",
+ "flag-junit",
],
visibility: ["//visibility:public"],
}
diff --git a/ravenwood/junit-flag-src/android/platform/test/flag/junit/RavenwoodFlagsValueProvider.java b/ravenwood/junit-flag-src/android/platform/test/flag/junit/RavenwoodFlagsValueProvider.java
new file mode 100644
index 0000000..9d62774
--- /dev/null
+++ b/ravenwood/junit-flag-src/android/platform/test/flag/junit/RavenwoodFlagsValueProvider.java
@@ -0,0 +1,54 @@
+/*
+ * 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.flag.junit;
+
+import android.platform.test.flag.junit.CheckFlagsRule;
+import android.platform.test.flag.junit.IFlagsValueProvider;
+
+/**
+ * Offer to create {@link CheckFlagsRule} instances that are useful on the Ravenwood deviceless
+ * testing environment.
+ *
+ * At the moment, default flag values are not available on Ravenwood, so the only options offered
+ * here are "all-on" and "all-off" options. Tests that want to exercise specific flag states should
+ * use {@link android.platform.test.flag.junit.SetFlagsRule}.
+ */
+public class RavenwoodFlagsValueProvider {
+ /**
+ * Create a {@link CheckFlagsRule} instance where flags are in an "all-on" state.
+ */
+ public static CheckFlagsRule createAllOnCheckFlagsRule() {
+ return new CheckFlagsRule(new IFlagsValueProvider() {
+ @Override
+ public boolean getBoolean(String flag) {
+ return true;
+ }
+ });
+ }
+
+ /**
+ * Create a {@link CheckFlagsRule} instance where flags are in an "all-off" state.
+ */
+ public static CheckFlagsRule createAllOffCheckFlagsRule() {
+ return new CheckFlagsRule(new IFlagsValueProvider() {
+ @Override
+ public boolean getBoolean(String flag) {
+ return false;
+ }
+ });
+ }
+}
diff --git a/ravenwood/test-authors.md b/ravenwood/test-authors.md
index 9179a62..7c0cee8 100644
--- a/ravenwood/test-authors.md
+++ b/ravenwood/test-authors.md
@@ -112,6 +112,24 @@
This naturally composes together well with any `RavenwoodRule` that your test may need.
+While `SetFlagsRule` is generally a best-practice (as it can explicitly confirm behaviors for both "on" and "off" states), you may need to write tests that use `CheckFlagsRule` (such as when writing CTS). Ravenwood currently supports `CheckFlagsRule` by offering "all-on" and "all-off" behaviors:
+
+```
+import android.platform.test.flag.junit.CheckFlagsRule;
+import android.platform.test.flag.junit.DeviceFlagsValueProvider;
+import android.platform.test.flag.junit.RavenwoodFlagsValueProvider;
+import android.platform.test.ravenwood.RavenwoodRule;
+
+@RunWith(AndroidJUnit4.class)
+public class MyCodeTest {
+ @Rule
+ public final CheckFlagsRule mCheckFlagsRule = RavenwoodRule.isUnderRavenwood()
+ ? RavenwoodFlagsValueProvider.createAllOnCheckFlagsRule()
+ : DeviceFlagsValueProvider.createCheckFlagsRule();
+```
+
+Ravenwood currently doesn't have knowledge of the "default" value of any flags, so using `createAllOnCheckFlagsRule()` is recommended to verify the widest possible set of behaviors. The example code above falls back to using default values from `DeviceFlagsValueProvider` when not running on Ravenwood.
+
## Strategies for migration/bivalent tests
Ravenwood aims to support tests that are written in a “bivalent” way, where the same test code can be dual-compiled to run on both a real Android device and under a Ravenwood environment.