BackupRestoreController


interface BackupRestoreController : AutoCloseable


Controls backup, restore, and verification workflows for a test device.

Provides operations to seed test data with runOnDevice, run backups with performBackup, wipe app data with clearAppData, and restore data with performRestore.

Summary

Public companion properties

String

Class name of the prebuilt action that verifies restored data in app storage.

String

Class name of the prebuilt action that seeds test data into app storage.

List<String>

Default package installation flags: reinstall, allow test, grant all runtime permissions.

Public functions

suspend BackupRestoreController

Clears application sandbox data on the device via pm clear.

ListenableFuture<BackupRestoreController>

Clears application sandbox data asynchronously on the device.

suspend BackupRestoreController

Clears the device logcat buffer via logcat -c.

ListenableFuture<BackupRestoreController>

Clears the device logcat buffer asynchronously.

Unit

Releases the resources held by this controller.

suspend BackupRestoreController
fetchDeviceLogs(destinationPath: Path, duration: Duration)

Saves recent device logcat entries to a local file.

ListenableFuture<BackupRestoreController>
fetchDeviceLogsAsync(destinationPath: Path)

Saves recent device logcat entries asynchronously to a local file.

ListenableFuture<BackupRestoreController>
fetchDeviceLogsAsync(destinationPath: Path, duration: Duration)

Saves recent device logcat entries asynchronously to a local file.

suspend BackupRestoreController
installApk(apkFile: Path, options: List<String>)

Installs an APK from the host onto the device via pm install.

ListenableFuture<BackupRestoreController>

Installs an APK asynchronously from the host onto the device.

ListenableFuture<BackupRestoreController>
installApkAsync(apkFile: Path, options: List<String>)

Installs an APK asynchronously from the host onto the device.

suspend BackupRestoreController
launchApp(
    activityClass: String?,
    intentExtras: Map<String, String>,
    action: String?
)

Starts the target application on the device via am start.

ListenableFuture<BackupRestoreController>

Starts the target application asynchronously on the device.

ListenableFuture<BackupRestoreController>
launchAppAsync(activityClass: String?)

Starts the target application asynchronously on the device.

ListenableFuture<BackupRestoreController>
launchAppAsync(activityClass: String?, intentExtras: Map<String, String>)

Starts the target application asynchronously on the device.

ListenableFuture<BackupRestoreController>
launchAppAsync(
    activityClass: String?,
    intentExtras: Map<String, String>,
    action: String?
)

Starts the target application asynchronously on the device.

suspend Path
performBackup(
    mode: BackupTransportMode,
    outputDir: Path,
    timeout: Duration
)

Captures an application backup archive using the specified transport mode.

ListenableFuture<Path>

Captures an application backup archive asynchronously using the specified transport mode.

ListenableFuture<Path>
performBackupAsync(
    mode: BackupTransportMode,
    outputDir: Path,
    timeout: Duration
)

Captures an application backup archive asynchronously using the specified transport mode.

suspend BackupRestoreController
performRestore(backupFile: Path, timeout: Duration)

Restores application data from a backup archive.

ListenableFuture<BackupRestoreController>

Restores application data asynchronously from a backup archive.

ListenableFuture<BackupRestoreController>
performRestoreAsync(backupFile: Path, timeout: Duration)

Restores application data asynchronously from a backup archive.

suspend BackupRestoreController
pullFile(devicePath: String, hostDestination: Path)

Copies a file from the device to the host machine via adb pull.

ListenableFuture<BackupRestoreController>
pullFileAsync(devicePath: String, hostDestination: Path)

Copies a file asynchronously from the device to the host machine.

suspend BackupRestoreController

Uninstalls and reinstalls the target application from its APK files.

ListenableFuture<BackupRestoreController>

Uninstalls and reinstalls the target application asynchronously from its APK files.

suspend BackupRestoreController
runBackupRestoreFlow(
    storage: StorageDomain,
    outputDir: Path,
    mode: BackupTransportMode,
    restoreOn: BackupRestoreController,
    resetMode: AppResetMode
)

Runs a full backup and restore flow for a single storage domain.

suspend BackupRestoreController
runBackupRestoreFlow(
    storages: List<StorageDomain>,
    outputDir: Path,
    mode: BackupTransportMode,
    restoreOn: BackupRestoreController,
    resetMode: AppResetMode
)

Runs a full backup and restore flow for multiple storage domains.

ListenableFuture<BackupRestoreController>
runBackupRestoreFlowAsync(
    storage: StorageDomain,
    outputDir: Path,
    mode: BackupTransportMode
)

Runs a full backup and restore flow asynchronously for a single storage domain.

ListenableFuture<BackupRestoreController>
runBackupRestoreFlowAsync(
    storages: List<StorageDomain>,
    outputDir: Path,
    mode: BackupTransportMode
)

Runs a full backup and restore flow asynchronously for multiple storage domains.

ListenableFuture<BackupRestoreController>
runBackupRestoreFlowAsync(
    storage: StorageDomain,
    outputDir: Path,
    mode: BackupTransportMode,
    restoreOn: BackupRestoreController
)

Runs a full backup and restore flow asynchronously for a single storage domain, restoring and verifying on restoreOn.

ListenableFuture<BackupRestoreController>
runBackupRestoreFlowAsync(
    storages: List<StorageDomain>,
    outputDir: Path,
    mode: BackupTransportMode,
    restoreOn: BackupRestoreController
)

Runs a full backup and restore flow asynchronously for multiple storage domains, restoring and verifying on restoreOn.

ListenableFuture<BackupRestoreController>
runBackupRestoreFlowAsync(
    storage: StorageDomain,
    outputDir: Path,
    mode: BackupTransportMode,
    restoreOn: BackupRestoreController,
    resetMode: AppResetMode
)

Runs a full backup and restore flow asynchronously for a single storage domain.

ListenableFuture<BackupRestoreController>
runBackupRestoreFlowAsync(
    storages: List<StorageDomain>,
    outputDir: Path,
    mode: BackupTransportMode,
    restoreOn: BackupRestoreController,
    resetMode: AppResetMode
)

Runs a full backup and restore flow asynchronously for multiple storage domains.

suspend BackupActionResult
runOnDevice(
    actionClassName: String,
    args: Map<String, String>,
    timeout: Duration,
    waitForDebugger: Boolean
)

Runs an on-device action inside the target application process.

ListenableFuture<BackupActionResult>
runOnDeviceAsync(actionClassName: String)

Runs an on-device action asynchronously inside the target application process.

ListenableFuture<BackupActionResult>
runOnDeviceAsync(actionClassName: String, args: Map<String, String>)

Runs an on-device action asynchronously inside the target application process.

ListenableFuture<BackupActionResult>
runOnDeviceAsync(
    actionClassName: String,
    args: Map<String, String>,
    timeout: Duration
)

Runs an on-device action asynchronously inside the target application process.

ListenableFuture<BackupActionResult>
runOnDeviceAsync(
    actionClassName: String,
    args: Map<String, String>,
    timeout: Duration,
    waitForDebugger: Boolean
)

Runs an on-device action asynchronously inside the target application process.

suspend BackupRestoreController

Force-stops the target application on the device via am force-stop.

ListenableFuture<BackupRestoreController>

Force-stops the target application asynchronously on the device.

suspend BackupRestoreController

Uninstalls the target application from the device via pm uninstall, if it is installed.

ListenableFuture<BackupRestoreController>

Uninstalls the target application asynchronously from the device, if it is installed.

Public properties

Int

SDK API level of this device or emulator.

String

Application ID of the target package under test.

String

Serial number of this device or emulator.

Public companion properties

ACTION_ASSERT_STORAGE

val ACTION_ASSERT_STORAGE: String

Class name of the prebuilt action that verifies restored data in app storage.

ACTION_POPULATE_STORAGE

val ACTION_POPULATE_STORAGE: String

Class name of the prebuilt action that seeds test data into app storage.

DEFAULT_INSTALL_OPTIONS

val DEFAULT_INSTALL_OPTIONS: List<String>

Default package installation flags: reinstall, allow test, grant all runtime permissions.

Public functions

clearAppData

suspend fun clearAppData(): BackupRestoreController

Clears application sandbox data on the device via pm clear.

Returns
BackupRestoreController

this controller instance

Throws
IOException

if clearing application data fails

clearAppDataAsync

Added in 1.0.0-alpha06
fun clearAppDataAsync(): ListenableFuture<BackupRestoreController>

Clears application sandbox data asynchronously on the device.

Returns
ListenableFuture<BackupRestoreController>

a ListenableFuture with this controller instance

clearDeviceLogs

suspend fun clearDeviceLogs(): BackupRestoreController

Clears the device logcat buffer via logcat -c.

Returns
BackupRestoreController

this controller instance

Throws
IOException

if clearing the logcat buffer fails

clearDeviceLogsAsync

Added in 1.0.0-alpha06
fun clearDeviceLogsAsync(): ListenableFuture<BackupRestoreController>

Clears the device logcat buffer asynchronously.

Returns
ListenableFuture<BackupRestoreController>

a ListenableFuture with this controller instance

close

Added in 1.0.0-alpha06
fun close(): Unit

Releases the resources held by this controller.

The ADB session that the controller runs on stays open, since other controllers can share it. The session is closed by whoever created it.

fetchDeviceLogs

suspend fun fetchDeviceLogs(
    destinationPath: Path,
    duration: Duration = Duration.ofSeconds(30)
): BackupRestoreController

Saves recent device logcat entries to a local file.

Parameters
destinationPath: Path

local file path where logs will be written

duration: Duration = Duration.ofSeconds(30)

time window of historical logs to capture

Returns
BackupRestoreController

this controller instance

Throws
IOException

if capturing logs fails

fetchDeviceLogsAsync

Added in 1.0.0-alpha06
fun fetchDeviceLogsAsync(destinationPath: Path): ListenableFuture<BackupRestoreController>

Saves recent device logcat entries asynchronously to a local file.

Parameters
destinationPath: Path

local file path where logs will be written

Returns
ListenableFuture<BackupRestoreController>

a ListenableFuture with this controller instance

fetchDeviceLogsAsync

Added in 1.0.0-alpha06
fun fetchDeviceLogsAsync(destinationPath: Path, duration: Duration): ListenableFuture<BackupRestoreController>

Saves recent device logcat entries asynchronously to a local file.

Parameters
destinationPath: Path

local file path where logs will be written

duration: Duration

time window of historical logs to capture

Returns
ListenableFuture<BackupRestoreController>

a ListenableFuture with this controller instance

installApk

suspend fun installApk(apkFile: Path, options: List<String> = DEFAULT_INSTALL_OPTIONS): BackupRestoreController

Installs an APK from the host onto the device via pm install.

Parameters
apkFile: Path

path to the APK on the host

options: List<String> = DEFAULT_INSTALL_OPTIONS

installation flags to pass to package manager

Returns
BackupRestoreController

this controller instance

Throws
IOException

if package installation fails

installApkAsync

Added in 1.0.0-alpha06
fun installApkAsync(apkFile: Path): ListenableFuture<BackupRestoreController>

Installs an APK asynchronously from the host onto the device.

Parameters
apkFile: Path

path to the APK on the host

Returns
ListenableFuture<BackupRestoreController>

a ListenableFuture with this controller instance

installApkAsync

Added in 1.0.0-alpha06
fun installApkAsync(apkFile: Path, options: List<String>): ListenableFuture<BackupRestoreController>

Installs an APK asynchronously from the host onto the device.

Parameters
apkFile: Path

path to the APK on the host

options: List<String>

installation flags to pass to package manager

Returns
ListenableFuture<BackupRestoreController>

a ListenableFuture with this controller instance

launchApp

suspend fun launchApp(
    activityClass: String? = null,
    intentExtras: Map<String, String> = emptyMap(),
    action: String? = null
): BackupRestoreController

Starts the target application on the device via am start.

Parameters
activityClass: String? = null

activity class to launch, or null for default launcher activity

intentExtras: Map<String, String> = emptyMap()

key-value pairs to pass as intent extras

action: String? = null

intent action string to launch with, or null for MAIN

Returns
BackupRestoreController

this controller instance

Throws
IOException

if starting the application fails

launchAppAsync

Added in 1.0.0-alpha06
fun launchAppAsync(): ListenableFuture<BackupRestoreController>

Starts the target application asynchronously on the device.

Returns
ListenableFuture<BackupRestoreController>

a ListenableFuture with this controller instance

launchAppAsync

Added in 1.0.0-alpha06
fun launchAppAsync(activityClass: String?): ListenableFuture<BackupRestoreController>

Starts the target application asynchronously on the device.

Parameters
activityClass: String?

activity class to launch, or null for default launcher activity

Returns
ListenableFuture<BackupRestoreController>

a ListenableFuture with this controller instance

launchAppAsync

Added in 1.0.0-alpha06
fun launchAppAsync(activityClass: String?, intentExtras: Map<String, String>): ListenableFuture<BackupRestoreController>

Starts the target application asynchronously on the device.

Parameters
activityClass: String?

activity class to launch, or null for default launcher activity

intentExtras: Map<String, String>

key-value pairs to pass as intent extras

Returns
ListenableFuture<BackupRestoreController>

a ListenableFuture with this controller instance

launchAppAsync

Added in 1.0.0-alpha06
fun launchAppAsync(
    activityClass: String?,
    intentExtras: Map<String, String>,
    action: String?
): ListenableFuture<BackupRestoreController>

Starts the target application asynchronously on the device.

Parameters
activityClass: String?

activity class to launch, or null for default launcher activity

intentExtras: Map<String, String>

key-value pairs to pass as intent extras

action: String?

intent action string to launch with

Returns
ListenableFuture<BackupRestoreController>

a ListenableFuture with this controller instance

performBackup

suspend fun performBackup(
    mode: BackupTransportMode,
    outputDir: Path,
    timeout: Duration = Duration.ofMinutes(5)
): Path

Captures an application backup archive using the specified transport mode.

When the app sets android:allowBackup="false", only a BackupTransportMode.DEVICE_TO_DEVICE backup succeeds, and only if the archive contains authentication data, such as a restore credential. In BackupTransportMode.CLOUD_UNENCRYPTED, authentication data is excluded from the archive.

Parameters
mode: BackupTransportMode

transport mode to test

outputDir: Path

directory where the backup archive is saved

timeout: Duration = Duration.ofMinutes(5)

maximum duration to wait for the backup to complete. When it expires, the app is force-stopped and the backup fails with an IOException. Must be positive.

Returns
Path

path to the generated backup archive. A BackupTransportMode.LOCAL archive only marks the backup, whose data stays on the device.

Throws
IOException

if the backup operation fails, or the app does not allow backups and has no authentication data to back up

IllegalArgumentException

if timeout is not positive

performBackupAsync

Added in 1.0.0-alpha06
fun performBackupAsync(mode: BackupTransportMode, outputDir: Path): ListenableFuture<Path>

Captures an application backup archive asynchronously using the specified transport mode.

Parameters
mode: BackupTransportMode

transport mode to test

outputDir: Path

directory where the backup archive is saved

Returns
ListenableFuture<Path>

a ListenableFuture with the generated backup path

performBackupAsync

Added in 1.0.0-alpha06
fun performBackupAsync(
    mode: BackupTransportMode,
    outputDir: Path,
    timeout: Duration
): ListenableFuture<Path>

Captures an application backup archive asynchronously using the specified transport mode.

Parameters
mode: BackupTransportMode

transport mode to test

outputDir: Path

directory where the backup archive is saved

timeout: Duration

maximum duration to wait for the backup to complete. When it expires, the app is force-stopped and the backup fails with an IOException. Must be positive, or the returned future fails with an IllegalArgumentException.

Returns
ListenableFuture<Path>

a ListenableFuture with the generated backup path

performRestore

suspend fun performRestore(backupFile: Path, timeout: Duration = Duration.ofMinutes(5)): BackupRestoreController

Restores application data from a backup archive.

Restores that skip app data because the app's backup is empty or android:allowBackup is false still succeed if the archive contains authentication data, such as a restore credential, and Google Play services restores it to Block Store.

Parameters
backupFile: Path

backup archive generated by performBackup

timeout: Duration = Duration.ofMinutes(5)

maximum duration to wait for the restore to complete. When it expires, the app is force-stopped and the restore fails with an IOException. Must be positive.

Returns
BackupRestoreController

this controller instance

Throws
IOException

if the restore operation fails or restores neither app data nor authentication data, if Google Play services on the device is too old to restore backupFile, or if backupFile is a BackupTransportMode.LOCAL archive of another app or of another device

IllegalArgumentException

if timeout is not positive

performRestoreAsync

Added in 1.0.0-alpha06
fun performRestoreAsync(backupFile: Path): ListenableFuture<BackupRestoreController>

Restores application data asynchronously from a backup archive.

Parameters
backupFile: Path

backup archive generated by performBackup

Returns
ListenableFuture<BackupRestoreController>

a ListenableFuture with this controller instance

performRestoreAsync

Added in 1.0.0-alpha06
fun performRestoreAsync(backupFile: Path, timeout: Duration): ListenableFuture<BackupRestoreController>

Restores application data asynchronously from a backup archive.

Parameters
backupFile: Path

backup archive generated by performBackup

timeout: Duration

maximum duration to wait for the restore to complete. When it expires, the app is force-stopped and the restore fails with an IOException. Must be positive, or the returned future fails with an IllegalArgumentException.

Returns
ListenableFuture<BackupRestoreController>

a ListenableFuture with this controller instance

pullFile

suspend fun pullFile(devicePath: String, hostDestination: Path): BackupRestoreController

Copies a file from the device to the host machine via adb pull.

Parameters
devicePath: String

path to the file on the device

hostDestination: Path

path to write the file on the host

Returns
BackupRestoreController

this controller instance

Throws
IOException

if copying the file fails

pullFileAsync

Added in 1.0.0-alpha06
fun pullFileAsync(devicePath: String, hostDestination: Path): ListenableFuture<BackupRestoreController>

Copies a file asynchronously from the device to the host machine.

Parameters
devicePath: String

path to the file on the device

hostDestination: Path

path to write the file on the host

Returns
ListenableFuture<BackupRestoreController>

a ListenableFuture with this controller instance

reinstallApp

suspend fun reinstallApp(): BackupRestoreController

Uninstalls and reinstalls the target application from its APK files.

The test runner provides the APK files, as an Android Gradle Plugin backup test suite does. Removes any existing installation of applicationId via pm uninstall so app data and local credentials are cleared, then installs the APK files with DEFAULT_INSTALL_OPTIONS. The test APK stays installed. To install other APK files, or with other options, call uninstallApp and installApk.

On a device whose backup transport is the local transport, Android restores a BackupTransportMode.LOCAL backup of the app as soon as the app is installed again.

Returns
BackupRestoreController

this controller instance

Throws
IllegalStateException

if the test runner provided no APK files for applicationId

IOException

if uninstalling or installing the application fails

reinstallAppAsync

Added in 1.0.0-alpha06
fun reinstallAppAsync(): ListenableFuture<BackupRestoreController>

Uninstalls and reinstalls the target application asynchronously from its APK files.

Installs the APK files that the test runner provides, with DEFAULT_INSTALL_OPTIONS. The returned future fails with an IllegalStateException if the test runner provided no APK files for applicationId, and with an IOException if uninstalling or installing the application fails.

Returns
ListenableFuture<BackupRestoreController>

a ListenableFuture with this controller instance

runBackupRestoreFlow

suspend fun runBackupRestoreFlow(
    storage: StorageDomain,
    outputDir: Path,
    mode: BackupTransportMode,
    restoreOn: BackupRestoreController = this,
    resetMode: AppResetMode = AppResetMode.CLEAR_DATA
): BackupRestoreController

Runs a full backup and restore flow for a single storage domain.

Seeds storage on this device, runs a backup to outputDir using mode, then, on restoreOn, resets the app using resetMode, restores the backup archive, and verifies storage.

Unless storage is a StorageDomain.CustomData, the backup and the restore must carry app data: the flow fails if they have only authentication data, such as a restore credential, as for an app with android:allowBackup="false".

Parameters
storage: StorageDomain

storage domain to seed and verify

outputDir: Path

directory where the generated backup file is saved

mode: BackupTransportMode

transport mode to test

restoreOn: BackupRestoreController = this

device controller on which the app is reset, restored, and verified. Defaults to this controller. Must have the same applicationId as this controller, and for BackupTransportMode.LOCAL, the same serialNumber.

resetMode: AppResetMode = AppResetMode.CLEAR_DATA

how the app is reset on restoreOn before restoring. Defaults to AppResetMode.CLEAR_DATA. AppResetMode.REINSTALL calls reinstallApp on restoreOn, which installs the APK files of the app with DEFAULT_INSTALL_OPTIONS.

Returns
BackupRestoreController

this controller instance, even when the restore ran on restoreOn

Throws
IOException

if any step fails

IllegalArgumentException

if storage is a StorageDomain.ExcludedData, or if restoreOn has a different applicationId, or a different serialNumber with BackupTransportMode.LOCAL, or if resetMode is AppResetMode.REINSTALL with BackupTransportMode.LOCAL

IllegalStateException

if resetMode is AppResetMode.REINSTALL and the test runner provided no APK files of the app for restoreOn

runBackupRestoreFlow

suspend fun runBackupRestoreFlow(
    storages: List<StorageDomain>,
    outputDir: Path,
    mode: BackupTransportMode,
    restoreOn: BackupRestoreController = this,
    resetMode: AppResetMode = AppResetMode.CLEAR_DATA
): BackupRestoreController

Runs a full backup and restore flow for multiple storage domains.

Seeds each domain in storages on this device, runs a backup to outputDir using mode, then, on restoreOn, resets the app using resetMode, restores the backup archive, and verifies each domain.

Unless every domain is a StorageDomain.CustomData or a StorageDomain.ExcludedData, the backup and the restore must carry app data: the flow fails if they have only authentication data, such as a restore credential, as for an app with android:allowBackup="false".

Parameters
storages: List<StorageDomain>

storage domains to seed and verify

outputDir: Path

directory where the generated backup file is saved

mode: BackupTransportMode

transport mode to test

restoreOn: BackupRestoreController = this

device controller on which the app is reset, restored, and verified. Defaults to this controller. Must have the same applicationId as this controller, and for BackupTransportMode.LOCAL, the same serialNumber.

resetMode: AppResetMode = AppResetMode.CLEAR_DATA

how the app is reset on restoreOn before restoring. Defaults to AppResetMode.CLEAR_DATA. AppResetMode.REINSTALL calls reinstallApp on restoreOn, which installs the APK files of the app with DEFAULT_INSTALL_OPTIONS.

Returns
BackupRestoreController

this controller instance, even when the restore ran on restoreOn

Throws
IOException

if any step fails

IllegalArgumentException

if storages is empty or has only StorageDomain.ExcludedData, or if restoreOn has a different applicationId, or a different serialNumber with BackupTransportMode.LOCAL, or if resetMode is AppResetMode.REINSTALL with BackupTransportMode.LOCAL

IllegalStateException

if resetMode is AppResetMode.REINSTALL and the test runner provided no APK files of the app for restoreOn

runBackupRestoreFlowAsync

Added in 1.0.0-alpha06
fun runBackupRestoreFlowAsync(
    storage: StorageDomain,
    outputDir: Path,
    mode: BackupTransportMode
): ListenableFuture<BackupRestoreController>

Runs a full backup and restore flow asynchronously for a single storage domain.

Resets the app with AppResetMode.CLEAR_DATA before restoring on this device.

Parameters
storage: StorageDomain

storage domain to seed and verify. Must not be a StorageDomain.ExcludedData, or the returned future fails with an IllegalArgumentException.

outputDir: Path

directory where the generated backup file is saved

mode: BackupTransportMode

transport mode to test

Returns
ListenableFuture<BackupRestoreController>

a ListenableFuture with this controller instance

runBackupRestoreFlowAsync

fun runBackupRestoreFlowAsync(
    storages: List<StorageDomain>,
    outputDir: Path,
    mode: BackupTransportMode
): ListenableFuture<BackupRestoreController>

Runs a full backup and restore flow asynchronously for multiple storage domains.

Resets the app with AppResetMode.CLEAR_DATA before restoring on this device.

Parameters
storages: List<StorageDomain>

storage domains to seed and verify. Must not be empty or hold only StorageDomain.ExcludedData, or the returned future fails with an IllegalArgumentException.

outputDir: Path

directory where the generated backup file is saved

mode: BackupTransportMode

transport mode to test

Returns
ListenableFuture<BackupRestoreController>

a ListenableFuture with this controller instance

runBackupRestoreFlowAsync

Added in 1.0.0-alpha06
fun runBackupRestoreFlowAsync(
    storage: StorageDomain,
    outputDir: Path,
    mode: BackupTransportMode,
    restoreOn: BackupRestoreController
): ListenableFuture<BackupRestoreController>

Runs a full backup and restore flow asynchronously for a single storage domain, restoring and verifying on restoreOn.

Resets the app on restoreOn with AppResetMode.CLEAR_DATA before restoring.

Parameters
storage: StorageDomain

storage domain to seed and verify. Must not be a StorageDomain.ExcludedData, or the returned future fails with an IllegalArgumentException.

outputDir: Path

directory where the generated backup file is saved

mode: BackupTransportMode

transport mode to test

restoreOn: BackupRestoreController

device controller on which app data is cleared, restored, and verified. Must have the same applicationId as this controller, and for BackupTransportMode.LOCAL, the same serialNumber, or the returned future fails with an IllegalArgumentException.

Returns
ListenableFuture<BackupRestoreController>

a ListenableFuture with this controller instance, even when the restore ran on restoreOn

runBackupRestoreFlowAsync

fun runBackupRestoreFlowAsync(
    storages: List<StorageDomain>,
    outputDir: Path,
    mode: BackupTransportMode,
    restoreOn: BackupRestoreController
): ListenableFuture<BackupRestoreController>

Runs a full backup and restore flow asynchronously for multiple storage domains, restoring and verifying on restoreOn.

Resets the app on restoreOn with AppResetMode.CLEAR_DATA before restoring.

Parameters
storages: List<StorageDomain>

storage domains to seed and verify. Must not be empty or hold only StorageDomain.ExcludedData, or the returned future fails with an IllegalArgumentException.

outputDir: Path

directory where the generated backup file is saved

mode: BackupTransportMode

transport mode to test

restoreOn: BackupRestoreController

device controller on which app data is cleared, restored, and verified. Must have the same applicationId as this controller, and for BackupTransportMode.LOCAL, the same serialNumber, or the returned future fails with an IllegalArgumentException.

Returns
ListenableFuture<BackupRestoreController>

a ListenableFuture with this controller instance, even when the restore ran on restoreOn

runBackupRestoreFlowAsync

Added in 1.0.0-alpha06
fun runBackupRestoreFlowAsync(
    storage: StorageDomain,
    outputDir: Path,
    mode: BackupTransportMode,
    restoreOn: BackupRestoreController,
    resetMode: AppResetMode
): ListenableFuture<BackupRestoreController>

Runs a full backup and restore flow asynchronously for a single storage domain.

Resets the app on restoreOn with resetMode before restoring.

Parameters
storage: StorageDomain

storage domain to seed and verify. Must not be a StorageDomain.ExcludedData, or the returned future fails with an IllegalArgumentException.

outputDir: Path

directory where the generated backup file is saved

mode: BackupTransportMode

transport mode to test

restoreOn: BackupRestoreController

device controller on which the app is reset, restored, and verified. Must have the same applicationId as this controller, and for BackupTransportMode.LOCAL, the same serialNumber, or the returned future fails with an IllegalArgumentException.

resetMode: AppResetMode

how the app is reset on restoreOn before restoring. AppResetMode.REINSTALL calls reinstallApp on restoreOn, which installs the APK files of the app with DEFAULT_INSTALL_OPTIONS. With it, the returned future fails with an IllegalStateException if the test runner provided no APK files of the app for restoreOn, and with an IllegalArgumentException if mode is BackupTransportMode.LOCAL.

Returns
ListenableFuture<BackupRestoreController>

a ListenableFuture with this controller instance, even when the restore ran on restoreOn

runBackupRestoreFlowAsync

fun runBackupRestoreFlowAsync(
    storages: List<StorageDomain>,
    outputDir: Path,
    mode: BackupTransportMode,
    restoreOn: BackupRestoreController,
    resetMode: AppResetMode
): ListenableFuture<BackupRestoreController>

Runs a full backup and restore flow asynchronously for multiple storage domains.

Resets the app on restoreOn with resetMode before restoring.

Parameters
storages: List<StorageDomain>

storage domains to seed and verify. Must not be empty or hold only StorageDomain.ExcludedData, or the returned future fails with an IllegalArgumentException.

outputDir: Path

directory where the generated backup file is saved

mode: BackupTransportMode

transport mode to test

restoreOn: BackupRestoreController

device controller on which the app is reset, restored, and verified. Must have the same applicationId as this controller, and for BackupTransportMode.LOCAL, the same serialNumber, or the returned future fails with an IllegalArgumentException.

resetMode: AppResetMode

how the app is reset on restoreOn before restoring. AppResetMode.REINSTALL calls reinstallApp on restoreOn, which installs the APK files of the app with DEFAULT_INSTALL_OPTIONS. With it, the returned future fails with an IllegalStateException if the test runner provided no APK files of the app for restoreOn, and with an IllegalArgumentException if mode is BackupTransportMode.LOCAL.

Returns
ListenableFuture<BackupRestoreController>

a ListenableFuture with this controller instance, even when the restore ran on restoreOn

runOnDevice

suspend fun runOnDevice(
    actionClassName: String,
    args: Map<String, String> = emptyMap(),
    timeout: Duration = Duration.ofMinutes(1),
    waitForDebugger: Boolean = false
): BackupActionResult

Runs an on-device action inside the target application process.

Parameters
actionClassName: String

class name of the androidx.test.backup.BackupDeviceAction to run

args: Map<String, String> = emptyMap()

arguments to pass to the action. The keys actionClass, argsFile, debug and redirect_dir are reserved for the runner.

timeout: Duration = Duration.ofMinutes(1)

maximum duration to wait for the action to complete. When it expires, the app is force-stopped and a BackupActionResult.Failure is returned. Must be positive.

waitForDebugger: Boolean = false

whether the runner waits for a debugger to attach before running

Returns
BackupActionResult

result of the action execution

Throws
IOException

if communicating with the device fails

IllegalArgumentException

if timeout is not positive, or if args uses a reserved key

runOnDeviceAsync

Added in 1.0.0-alpha06
fun runOnDeviceAsync(actionClassName: String): ListenableFuture<BackupActionResult>

Runs an on-device action asynchronously inside the target application process.

Parameters
actionClassName: String

class name of the androidx.test.backup.BackupDeviceAction to run

Returns
ListenableFuture<BackupActionResult>

a ListenableFuture with the action result

runOnDeviceAsync

Added in 1.0.0-alpha06
fun runOnDeviceAsync(actionClassName: String, args: Map<String, String>): ListenableFuture<BackupActionResult>

Runs an on-device action asynchronously inside the target application process.

Parameters
actionClassName: String

class name of the androidx.test.backup.BackupDeviceAction to run

args: Map<String, String>

arguments to pass to the action. The keys actionClass, argsFile, debug and redirect_dir are reserved for the runner; with one of them, the returned future fails with an IllegalArgumentException.

Returns
ListenableFuture<BackupActionResult>

a ListenableFuture with the action result

runOnDeviceAsync

Added in 1.0.0-alpha06
fun runOnDeviceAsync(
    actionClassName: String,
    args: Map<String, String>,
    timeout: Duration
): ListenableFuture<BackupActionResult>

Runs an on-device action asynchronously inside the target application process.

Parameters
actionClassName: String

class name of the androidx.test.backup.BackupDeviceAction to run

args: Map<String, String>

arguments to pass to the action. The keys actionClass, argsFile, debug and redirect_dir are reserved for the runner; with one of them, the returned future fails with an IllegalArgumentException.

timeout: Duration

maximum duration to wait for the action to complete. When it expires, the app is force-stopped and a BackupActionResult.Failure is returned. Must be positive, or the returned future fails with an IllegalArgumentException.

Returns
ListenableFuture<BackupActionResult>

a ListenableFuture with the action result

runOnDeviceAsync

Added in 1.0.0-alpha06
fun runOnDeviceAsync(
    actionClassName: String,
    args: Map<String, String>,
    timeout: Duration,
    waitForDebugger: Boolean
): ListenableFuture<BackupActionResult>

Runs an on-device action asynchronously inside the target application process.

Parameters
actionClassName: String

class name of the androidx.test.backup.BackupDeviceAction to run

args: Map<String, String>

arguments to pass to the action. The keys actionClass, argsFile, debug and redirect_dir are reserved for the runner; with one of them, the returned future fails with an IllegalArgumentException.

timeout: Duration

maximum duration to wait for the action to complete. When it expires, the app is force-stopped and a BackupActionResult.Failure is returned. Must be positive, or the returned future fails with an IllegalArgumentException.

waitForDebugger: Boolean

whether the runner waits for a debugger to attach before running

Returns
ListenableFuture<BackupActionResult>

a ListenableFuture with the action result

stopApp

suspend fun stopApp(): BackupRestoreController

Force-stops the target application on the device via am force-stop.

Returns
BackupRestoreController

this controller instance

Throws
IOException

if stopping the application fails

stopAppAsync

Added in 1.0.0-alpha06
fun stopAppAsync(): ListenableFuture<BackupRestoreController>

Force-stops the target application asynchronously on the device.

Returns
ListenableFuture<BackupRestoreController>

a ListenableFuture with this controller instance

uninstallApp

suspend fun uninstallApp(): BackupRestoreController

Uninstalls the target application from the device via pm uninstall, if it is installed.

The test APK stays installed. Until installApk or reinstallApp installs the app again, the calls that need it, such as runOnDevice, runBackupRestoreFlow and launchApp, fail.

Returns
BackupRestoreController

this controller instance

Throws
IOException

if uninstalling the application fails

uninstallAppAsync

Added in 1.0.0-alpha06
fun uninstallAppAsync(): ListenableFuture<BackupRestoreController>

Uninstalls the target application asynchronously from the device, if it is installed.

The test APK stays installed. Until installApkAsync or reinstallAppAsync installs the app again, the calls that need it, such as runOnDeviceAsync, runBackupRestoreFlowAsync and launchAppAsync, fail.

Returns
ListenableFuture<BackupRestoreController>

a ListenableFuture with this controller instance

Public properties

apiLevel

Added in 1.0.0-alpha06
val apiLevel: Int

SDK API level of this device or emulator.

applicationId

Added in 1.0.0-alpha06
val applicationId: String

Application ID of the target package under test.

serialNumber

Added in 1.0.0-alpha06
val serialNumber: String

Serial number of this device or emulator.