Skip to content
Snippets Groups Projects
Commit 7037e357 authored by Jeff Sharkey's avatar Jeff Sharkey
Browse files

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
parent 20825592
No related branches found
No related tags found
No related merge requests found
......@@ -205,8 +205,10 @@ android_ravenwood_libgroup {
// 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 @@ android_ravenwood_libgroup {
libs: [
"junit",
"truth",
"flag-junit",
"ravenwood-framework",
"ravenwood-junit",
"mockito-ravenwood-prebuilt",
......
......@@ -32,6 +32,7 @@ import android.platform.test.annotations.IgnoreUnderRavenwood;
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 @@ public class PowerManagerTest {
// 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();
/**
......
......@@ -22,6 +22,7 @@ import android.platform.test.annotations.IgnoreUnderRavenwood;
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 @@ public class WorkDurationUnitTest {
// 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
......
......@@ -86,6 +86,21 @@ java_library {
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 @@ java_library {
srcs: [
"junit-src/**/*.java",
"junit-stub-src/**/*.java",
"junit-flag-src/**/*.java",
],
sdk_version: "test_current",
libs: [
"junit",
"flag-junit",
],
visibility: ["//visibility:public"],
}
......
/*
* 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;
}
});
}
}
......@@ -112,6 +112,24 @@ public class MyCodeTest {
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.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment