Implementing Test Harness Mode

Test Harness Mode is a feature added in Android 10 for developers that wish to automate a device or a fleet of devices. The feature provides a method to wipe all user data on an Android device, retain ADB keys, and run a UI test immediately after startup without any user interaction.

Customization

Test Harness Mode can be customized by checking ActivityManager.isRunningInUserTestHarness() although this feature is targeted at third-party developers. Aside from skipping setup screens (like on the keyboard or setup wizard) that would break UI tests or require manual interaction, a little to no work should be done.

Implementation

The default implementation of PersistentDataBlockManagerInternal is in PersistentDataBlockService. Test Harness Mode is implemented in TestHarnessModeService.

The documentation for PersistentDataBlockManagerInternal and TestHarnessModeService is in the source.

The default implementation of Test Harness Mode uses the same storage mechanism as Factory Reset Protection to store the ADB keys temporarily in a persistent partition. If a persistent partition with Factory Reset Protection is already implemented on the device, a little to no work is necessary to support the feature.

OEMs that don't have a persistent partition set up need to implement PersistentDataBlockManagerInternal for TestHarnessModeService to work.

The public method ActivityManager.isRunningInUserTestHarness() tells apps if they're being run under Test Harness Mode.

Run test harness mode

Run the adb command to run the test harness mode.

adb shell cmd testharness enable

The command should be run when the user wants to wipe all of the device data and set it up for testing.

Enabling Test Harness Mode:

  • Wipes everything from the device.
  • Sets the device up for testing (skips dialogs, etc).

After it's enabled, ActivityManager.isRunningInUserTestHarness() returns true, so apps know that they're running in the Test Harness Mode.

The command should only be run when the user wants to erase all of the data from their device and set it up again. For device farms, this is after every app that they run. It's up to the user when they want to wipe all of their data.