@UnstableApi
public final class Media3FlagsRule implements TestRule


A JUnit TestRule for managing Flags states in tests.

This rule ensures that all flag overrides and canary mode configurations are reset after each test to prevent test pollution.

It automatically inspects the target object (usually this) and its superclasses for fields annotated with BindFlag and BindCanaryMode, and configures Flags before each test runs.

Usage Examples:

1. Automatic flag reset for manual flag overrides

Applying the rule ensures that any flag or canary mode modifications made during test execution (e.g. via enableFlag or setCanaryModeEnabled) are automatically reset after each test finishes:

public class MyPlaybackTest {

  @Rule public final Media3FlagsRule flagsRule = new Media3FlagsRule(this);

  @Test
  public void playback_withFeatureFlagEnabled() {
    Flags.enableFlag(Flags.FLAG_SOME_FEATURE);
    // Test behavior with the flag enabled...
  }
}

2. Parameterized test on a feature flag with BindFlag

The rule automatically enables or disables the flag before each test iteration according to the parameter field's value:

@RunWith(ParameterizedRobolectricTestRunner.class)
public class MyPlaybackTest {

  @Parameter(0)
  @BindFlag(Flags.FLAG_SOME_FEATURE)
  public boolean someFeatureEnabled;

  @Rule public final Media3FlagsRule flagsRule = new Media3FlagsRule(this);

  @Test
  public void playback() {
    // Runs once with the flag disabled, and once with the flag enabled.
  }
}

3. Parameterized test on canary mode with BindCanaryMode

The rule enables or disables canary mode before each test iteration:

@RunWith(ParameterizedRobolectricTestRunner.class)
public class MyPlaybackTest {

  @Parameter(0)
  @BindCanaryMode
  public boolean canaryMode;

  @Rule public final Media3FlagsRule flagsRule = new Media3FlagsRule(this);

  @Test
  public void playback() {
    // Runs once with canary mode disabled, and once with canary mode enabled.
  }
}

4. Multiple flags or combined flag and canary mode bindings

Multiple annotated fields can be declared to test combinations of flags:

@RunWith(ParameterizedRobolectricTestRunner.class)
public class MyPlaybackTest {

  @Parameter(0)
  @BindFlag(Flags.FLAG_FEATURE_A)
  public boolean featureA;

  @Parameter(1)
  @BindFlag(Flags.FLAG_FEATURE_B)
  public boolean featureB;

  @Rule public final Media3FlagsRule flagsRule = new Media3FlagsRule(this);

  @Test
  public void playback() {
    // Runs for each combination of flags.
  }
}

Summary

Public constructors

Creates a rule that binds annotated fields on the target before each test and resets flags after each test.

Public methods

Statement
apply(Statement base, Description description)

Public constructors

Media3FlagsRule

public Media3FlagsRule(Object target)

Creates a rule that binds annotated fields on the target before each test and resets flags after each test.

Parameters
Object target

The test instance (usually this) containing fields annotated with BindFlag or BindCanaryMode, or simply the test instance to ensure flags are reset.

Public methods

apply

public Statement apply(Statement base, Description description)