# Media3FlagsRule

Artifact: [androidx.media3:media3-test-utils](https://developer.android.com/jetpack/androidx/releases/media3) [View Source](https://cs.android.com/search?q=file:androidx/media3/test/utils/Media3FlagsRule.java+class:androidx.media3.test.utils.Media3FlagsRule)

*** ** * ** ***


```
@UnstableApi
public final class Media3FlagsRule implements TestRule
```

<br />

*** ** * ** ***

A JUnit `https://junit.org/junit4/javadoc/latest/org/junit/rules/TestRule.html` for managing `https://developer.android.com/reference/androidx/media3/common/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 `https://developer.android.com/reference/androidx/media3/test/utils/BindFlag` and `https://developer.android.com/reference/androidx/media3/test/utils/BindCanaryMode`, and configures `https://developer.android.com/reference/androidx/media3/common/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 `https://developer.android.com/reference/androidx/media3/common/Flags#enableFlag(int)` or `https://developer.android.com/reference/androidx/media3/common/Flags#setCanaryModeEnabled(boolean)`) 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 `https://developer.android.com/reference/androidx/media3/test/utils/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 `https://developer.android.com/reference/androidx/media3/test/utils/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 |
|---|
| `https://developer.android.com/reference/androidx/media3/test/utils/Media3FlagsRule#Media3FlagsRule(java.lang.Object)(https://developer.android.com/reference/java/lang/Object.html target)` Creates a rule that binds annotated fields on the `target` before each test and resets flags after each test. |

| ### Public methods |
|---|---|
| `https://junit.org/junit4/javadoc/latest/org/junit/runners/model/Statement.html` | `https://developer.android.com/reference/androidx/media3/test/utils/Media3FlagsRule#apply(org.junit.runners.model.Statement,org.junit.runner.Description)(https://junit.org/junit4/javadoc/latest/org/junit/runners/model/Statement.html base, https://junit.org/junit4/javadoc/latest/org/junit/runner/Description.html 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 |
|---|---|
| `https://developer.android.com/reference/java/lang/Object.html target` | The test instance (usually `this`) containing fields annotated with `https://developer.android.com/reference/androidx/media3/test/utils/BindFlag` or `https://developer.android.com/reference/androidx/media3/test/utils/BindCanaryMode`, or simply the test instance to ensure flags are reset. |

## Public methods

### apply

```
public Statement apply(Statement base, Description description)
```