Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit df2bf84c authored by John Wu's avatar John Wu
Browse files

[Ravenwood] Remove usage of RavenwoodFlagsValueProvider

Flag: EXEMPT host test change only
Bug: 379740361
Test: $ANDROID_BUILD_TOP/frameworks/base/ravenwood/scripts/run-ravenwood-tests.sh
Change-Id: I0bde8e4e7d84a9c82d45c05fc58f2c14922cb19b
parent d26ba0f3
Loading
Loading
Loading
Loading
+4 −10
Original line number Diff line number Diff line
@@ -28,11 +28,10 @@ import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import android.content.Context;
import android.platform.test.annotations.IgnoreUnderRavenwood;
import android.platform.test.annotations.DisabledOnRavenwood;
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;
@@ -52,7 +51,7 @@ import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

@RunWith(AndroidJUnit4.class)
@IgnoreUnderRavenwood(blockedBy = PowerManager.class)
@DisabledOnRavenwood(blockedBy = PowerManager.class)
public class PowerManagerTest {

    private static final String TAG = "PowerManagerTest";
@@ -77,19 +76,14 @@ public class PowerManagerTest {
            String[] keys, String[] values);

    static {
        if (!RavenwoodRule.isUnderRavenwood()) {
        if (!RavenwoodRule.isOnRavenwood()) {
            System.loadLibrary("powermanagertest_jni");
        }
    }

    @Rule
    public final RavenwoodRule mRavenwood = new RavenwoodRule();

    // Required for RequiresFlagsEnabled and RequiresFlagsDisabled annotations to take effect.
    @Rule
    public final CheckFlagsRule mCheckFlagsRule = RavenwoodRule.isOnRavenwood()
            ? RavenwoodFlagsValueProvider.createAllOnCheckFlagsRule()
            : DeviceFlagsValueProvider.createCheckFlagsRule();
    public final CheckFlagsRule mCheckFlagsRule = DeviceFlagsValueProvider.createCheckFlagsRule();

    /**
     * Setup any common data for the upcoming tests.
+3 −10
Original line number Diff line number Diff line
@@ -18,12 +18,10 @@ package android.os;

import static org.junit.Assert.assertThrows;

import android.platform.test.annotations.IgnoreUnderRavenwood;
import android.platform.test.annotations.DisabledOnRavenwood;
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;

@@ -34,16 +32,11 @@ import org.junit.runner.RunWith;
import org.mockito.MockitoAnnotations;

@RunWith(AndroidJUnit4.class)
@IgnoreUnderRavenwood(blockedBy = WorkDuration.class)
@DisabledOnRavenwood(blockedBy = WorkDuration.class)
public class WorkDurationUnitTest {
    @Rule
    public final RavenwoodRule mRavenwood = new RavenwoodRule();

    // Required for RequiresFlagsEnabled and RequiresFlagsDisabled annotations to take effect.
    @Rule
    public final CheckFlagsRule mCheckFlagsRule = RavenwoodRule.isOnRavenwood()
            ? RavenwoodFlagsValueProvider.createAllOnCheckFlagsRule()
            : DeviceFlagsValueProvider.createCheckFlagsRule();
    public final CheckFlagsRule mCheckFlagsRule = DeviceFlagsValueProvider.createCheckFlagsRule();

    @Before
    public void setUp() {
+0 −39
Original line number Diff line number Diff line
@@ -106,45 +106,6 @@ You can also run your new tests automatically via `TEST_MAPPING` rules like this

> **Note:** There's a known bug #308854804 where `TEST_MAPPING` is not being applied, so we're currently planning to run all Ravenwood tests unconditionally in presubmit for changes to `frameworks/base/` and `cts/` until there is a better path forward.

## Strategies for feature flags

Ravenwood supports writing tests against logic that uses feature flags through the existing `SetFlagsRule` infrastructure maintained by the feature flagging team:

```
import android.platform.test.flag.junit.SetFlagsRule;

@RunWith(AndroidJUnit4.class)
public class MyCodeTest {
    @Rule
    public final SetFlagsRule mSetFlagsRule = new SetFlagsRule(SetFlagsRule.DefaultInitValueType.NULL_DEFAULT);

    @Test
    public void testEnabled() {
        mSetFlagsRule.enableFlags(Flags.FLAG_MY_FLAG);
        // verify test logic that depends on flag being enabled
    }
```

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.
+2 −9
Original line number Diff line number Diff line
@@ -29,8 +29,6 @@ import android.os.UserHandle;
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 android.provider.DeviceConfig;

import androidx.test.InstrumentationRegistry;
@@ -59,13 +57,8 @@ import java.util.regex.Pattern;
@LargeTest
@android.platform.test.annotations.DisabledOnRavenwood(reason = "Integration test")
public class CpuPowerStatsCollectorValidationTest {
    @Rule(order = 0)
    public final RavenwoodRule mRavenwood = new RavenwoodRule();

    @Rule(order = 1)
    public final CheckFlagsRule mCheckFlagsRule = RavenwoodRule.isOnRavenwood()
            ? RavenwoodFlagsValueProvider.createAllOnCheckFlagsRule()
            : DeviceFlagsValueProvider.createCheckFlagsRule();
    @Rule
    public final CheckFlagsRule mCheckFlagsRule = DeviceFlagsValueProvider.createCheckFlagsRule();

    private static final int WORK_DURATION_MS = 2000;
    private static final String TEST_PKG = "com.android.coretests.apps.bstatstestapp";
+2 −9
Original line number Diff line number Diff line
@@ -31,8 +31,6 @@ import android.os.Process;
import android.platform.test.annotations.RequiresFlagsDisabled;
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.filters.SmallTest;

@@ -58,18 +56,13 @@ import java.util.Collection;
@SuppressWarnings("GuardedBy")
public class SystemServicePowerCalculatorTest {
    @Rule(order = 0)
    public final RavenwoodRule mRavenwood = new RavenwoodRule();

    @Rule(order = 1)
    public final CheckFlagsRule mCheckFlagsRule = RavenwoodRule.isOnRavenwood()
            ? RavenwoodFlagsValueProvider.createAllOnCheckFlagsRule()
            : DeviceFlagsValueProvider.createCheckFlagsRule();
    public final CheckFlagsRule mCheckFlagsRule = DeviceFlagsValueProvider.createCheckFlagsRule();

    private static final double PRECISION = 0.000001;
    private static final int APP_UID1 = 100;
    private static final int APP_UID2 = 200;

    @Rule(order = 2)
    @Rule(order = 1)
    public final BatteryUsageStatsRule mStatsRule = new BatteryUsageStatsRule()
            .setAveragePower(PowerProfile.POWER_CPU_ACTIVE, 720)
            .setCpuScalingPolicy(0, new int[]{0, 1}, new int[]{100, 200})