# BackupRestoreExtension

Artifact: [androidx.test.backup:backup-host](https://developer.android.com/jetpack/androidx/releases/test-backup) [View Source](https://cs.android.com/search?q=file:androidx/test/backup/host/BackupRestoreExtension.kt+class:androidx.test.backup.host.BackupRestoreExtension) Added in [1.0.0-alpha04](https://developer.android.com/jetpack/androidx/releases/test-backup#1.0.0-alpha04)

*** ** * ** ***


```
class BackupRestoreExtension : BeforeAllCallback, AfterAllCallback, BeforeEachCallback, AfterEachCallback, ParameterResolver
```

<br />

*** ** * ** ***

A JUnit 5 Jupiter extension that resolves `https://developer.android.com/reference/kotlin/androidx/test/backup/host/BackupRestoreController` test parameters.

It automatically initializes ADB sessions, selects an online Android device matching target API and serial constraints (specified via system properties or `https://developer.android.com/reference/kotlin/androidx/test/backup/host/Device` annotations), handles automatic APK installation for tested and test packages, proactively dismisses lockscreens/keyguards, and sets up data isolation (e.g. running `pm clear`) prior to test execution.

Each controller targets the app named by the test class's `https://developer.android.com/reference/kotlin/androidx/test/backup/host/BackupRestoreConfig`. Without one, it targets the tested app that the test suite properties name.

When a test fails, the extension saves the logs of each device that the test used while it ran, and names the saved files in the test failure. Logs aren't saved for aborted tests, such as a failed assumption. To also save them when a `@BeforeAll` or `@AfterAll` method fails, register the extension on the test class, with `@ExtendWith` or a static `@RegisterExtension` field. To turn off saving logs, set the system property `androidx.test.backup.device.logs.enabled` to `false`.

The files are in `suite-details` in the results directory of the test suite. Outside of a test suite, they are in a new temporary directory, which the extension keeps after the test run.

## Summary

| ### Public constructors |
|---|
| `https://developer.android.com/reference/kotlin/androidx/test/backup/host/BackupRestoreExtension#BackupRestoreExtension()()` Creates an extension, which manages its own ADB session. |

| ### Public functions |
|---|---|
| `open https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/test/backup/host/BackupRestoreExtension#afterAll(org.junit.jupiter.api.extension.ExtensionContext)(context: https://docs.junit.org/current/api/org.junit.jupiter.api/org/junit/jupiter/api/extension/ExtensionContext.html)` |
| `open https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/test/backup/host/BackupRestoreExtension#afterEach(org.junit.jupiter.api.extension.ExtensionContext)(context: https://docs.junit.org/current/api/org.junit.jupiter.api/org/junit/jupiter/api/extension/ExtensionContext.html)` |
| `open https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/test/backup/host/BackupRestoreExtension#beforeAll(org.junit.jupiter.api.extension.ExtensionContext)(context: https://docs.junit.org/current/api/org.junit.jupiter.api/org/junit/jupiter/api/extension/ExtensionContext.html)` |
| `open https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/test/backup/host/BackupRestoreExtension#beforeEach(org.junit.jupiter.api.extension.ExtensionContext)(context: https://docs.junit.org/current/api/org.junit.jupiter.api/org/junit/jupiter/api/extension/ExtensionContext.html)` |
| `open https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html` | `https://developer.android.com/reference/kotlin/androidx/test/backup/host/BackupRestoreExtension#resolveParameter(org.junit.jupiter.api.extension.ParameterContext,org.junit.jupiter.api.extension.ExtensionContext)( parameterContext: https://docs.junit.org/current/api/org.junit.jupiter.api/org/junit/jupiter/api/extension/ParameterContext.html?, extensionContext: https://docs.junit.org/current/api/org.junit.jupiter.api/org/junit/jupiter/api/extension/ExtensionContext.html? )` |
| `open https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | `https://developer.android.com/reference/kotlin/androidx/test/backup/host/BackupRestoreExtension#supportsParameter(org.junit.jupiter.api.extension.ParameterContext,org.junit.jupiter.api.extension.ExtensionContext)( parameterContext: https://docs.junit.org/current/api/org.junit.jupiter.api/org/junit/jupiter/api/extension/ParameterContext.html?, extensionContext: https://docs.junit.org/current/api/org.junit.jupiter.api/org/junit/jupiter/api/extension/ExtensionContext.html? )` |

## Public constructors

### BackupRestoreExtension

Added in [1.0.0-alpha04](https://developer.android.com/jetpack/androidx/releases/test-backup#1.0.0-alpha04)

```
BackupRestoreExtension()
```

Creates an extension, which manages its own ADB session.

All extensions in a test run share one session. It is created when a test first requests a `https://developer.android.com/reference/kotlin/androidx/test/backup/host/BackupRestoreController` and closed when the test run ends.

## Public functions

### afterAll

Added in [1.0.0-alpha04](https://developer.android.com/jetpack/androidx/releases/test-backup#1.0.0-alpha04)

```
open fun afterAll(context: ExtensionContext): Unit
```

### afterEach

Added in [1.0.0-alpha04](https://developer.android.com/jetpack/androidx/releases/test-backup#1.0.0-alpha04)

```
open fun afterEach(context: ExtensionContext): Unit
```

### beforeAll

Added in [1.0.0-alpha04](https://developer.android.com/jetpack/androidx/releases/test-backup#1.0.0-alpha04)

```
open fun beforeAll(context: ExtensionContext): Unit
```

### beforeEach

Added in [1.0.0-alpha04](https://developer.android.com/jetpack/androidx/releases/test-backup#1.0.0-alpha04)

```
open fun beforeEach(context: ExtensionContext): Unit
```

### resolveParameter

Added in [1.0.0-alpha04](https://developer.android.com/jetpack/androidx/releases/test-backup#1.0.0-alpha04)

```
open fun resolveParameter(
    parameterContext: ParameterContext?,
    extensionContext: ExtensionContext?
): Any
```

### supportsParameter

Added in [1.0.0-alpha04](https://developer.android.com/jetpack/androidx/releases/test-backup#1.0.0-alpha04)

```
open fun supportsParameter(
    parameterContext: ParameterContext?,
    extensionContext: ExtensionContext?
): Boolean
```