Documentation
¶
Overview ¶
Package androidtest is the harness of the device tests (SPEC §24.13): it creates the virtual devices of the emulator in .android-test/ at the module root, starts one headless in a process group of its own under `timeout -k`, with its own adb server and its own HOME so that nothing of the user's ~/.android is read or written, waits for it to boot, and stops it (and the adb server) afterwards. It also installs and starts apps and reads their results.
glutter-android shot uses it too, to start an emulator when no device is given.
Flutter: no counterpart (flutter drive)
Index ¶
- Constants
- Variables
- func Describe(err error) string
- func Leftovers(root string) []string
- func ModuleRoot() (string, error)
- func ReleaseShared(avd string)
- func Run(m *testing.M) int
- func StopShared()
- func ValidationLayerDir() (string, error)
- type AVD
- type Emulator
- func (e *Emulator) BuildAPK(ctx context.Context, pkg, dir string, opts androidtool.BuildOptions) (apk, appID string, err error)
- func (e *Emulator) FullLog(ctx context.Context) (string, error)
- func (e *Emulator) HasActivity(ctx context.Context, appID string) (bool, error)
- func (e *Emulator) Install(ctx context.Context, apk string) error
- func (e *Emulator) Launch(ctx context.Context, appID string, extras map[string]string) error
- func (e *Emulator) Log(ctx context.Context, appID string) (string, error)
- func (e *Emulator) PullAppDir(ctx context.Context, appID, dir string) (map[string][]byte, error)
- func (e *Emulator) RunAs(ctx context.Context, appID string, command ...string) (string, error)
- func (e *Emulator) Screenshot(ctx context.Context, appID string, extras map[string]string, ...) ([]byte, error)
- func (e *Emulator) Stop()
- func (e *Emulator) WaitExit(ctx context.Context, appID string, timeout time.Duration) error
- func (e *Emulator) WaitLog(ctx context.Context, appID, want string, timeout time.Duration) (string, error)
- func (e *Emulator) WaitPid(ctx context.Context, appID string, timeout time.Duration) (int, error)
- type Options
Constants ¶
const ( // TagAndroidTestImageMissing is the tag of the error for a system image that is not // installed; the metadata "install" has the sdkmanager command. TagAndroidTestImageMissing = "AndroidTestImageMissing" // TagAndroidTestAVDFailed is the tag of the error for a virtual device that cannot be written. TagAndroidTestAVDFailed = "AndroidTestAVDFailed" )
The tags of the errors that this file makes (rm4n0s/errors, R5).
const ( // TagAndroidTestEmulatorMissing is the tag of the error for an SDK without the emulator; // the metadata "install" has the sdkmanager command. TagAndroidTestEmulatorMissing = "AndroidTestEmulatorMissing" // TagAndroidTestBootFailed is the tag of the error for an emulator that did not start or // did not boot; the metadata "log" has the end of its output. TagAndroidTestBootFailed = "AndroidTestBootFailed" )
The tags of the errors that this file makes (rm4n0s/errors, R5).
const ScreenshotFile = "files/glutter-screenshot.png"
ScreenshotFile is where the self-capture of an app writes, below the app's directory.
const TagAndroidTestTimeout = "AndroidTestTimeout"
TagAndroidTestTimeout is the tag of the error for something a device test waited for that did not happen in time.
const TagAndroidTestValidationLayerMissing = "AndroidTestValidationLayerMissing"
TagAndroidTestValidationLayerMissing is the tag of the error for a device test of Vulkan that needs the validation layer for Android and does not find it; the metadata "install" says how to get it.
Variables ¶
var AVDs = map[string]AVD{ "glutter-37": {Name: "glutter-37", Package: "system-images;android-37.2;google_apis_ps16k;x86_64", API: "37"}, "glutter-37-16k": {Name: "glutter-37-16k", Package: "system-images;android-37.2;google_apis_ps16k;x86_64", API: "37"}, "glutter-26": {Name: "glutter-26", Package: "system-images;android-26;google_apis;x86_64", API: "26"}, "glutter-37-audio": {Name: "glutter-37-audio", Package: "system-images;android-37.2;google_apis_ps16k;x86_64", API: "37", Audio: true}, }
AVDs are the devices the device tests use (SPEC §24.13), by name.
Deviation: SPEC §24.13 names `system-images;android-37.0;google_apis;x86_64` for glutter-37, but on the emulator 37.2.12 that image does not boot: the system server crashes in a loop with "Invalid feature flag: android.security.aapm_feature_disable_insecure_wifi_autojoin" in AdvancedProtectionService (checked on 2026-10-24). Both API 37 devices use the 16 KB image.
Flutter: no counterpart
Functions ¶
func Describe ¶
Describe is the text of an error for a failed test: its tag, route and metadata (the command and its output), which err.Error() leaves out.
func Leftovers ¶
Leftovers lists the processes (as "pid command") that were started with the environment of the harness for root: the emulator, qemu, the adb server and what they started. A device test checks that there are none after it stopped its emulator. Linux reads /proc.
Flutter: no counterpart
func ModuleRoot ¶
ModuleRoot is the directory of go.mod above the current directory.
func ReleaseShared ¶
func ReleaseShared(avd string)
ReleaseShared stops the emulator of the AVD that Shared started, when its tests are done, so that fewer emulators (about 5 GB of memory each) run at once; a later Shared starts it again.
Flutter: no counterpart
func Run ¶
Run is for the TestMain of a package of device tests: it runs the tests, stops the emulators they started, and returns the exit code.
func TestMain(m *testing.M) { os.Exit(androidtest.Run(m)) }
Flutter: no counterpart
func StopShared ¶
func StopShared()
StopShared stops the emulators that Shared started.
Flutter: no counterpart
func ValidationLayerDir ¶
ValidationLayerDir is the directory with the Khronos validation layer of Android, where the device tests of Vulkan expect it: <module root>/.android-test/vvl, with x86_64/libVkLayer_khronos_validation.so in it (the android-binaries zip of a Vulkan-ValidationLayers release, unzipped there; SPEC §24.13). It fails with AndroidTestValidationLayerMissing, with that instruction, when the library is not there.
Flutter: no counterpart
Types ¶
type AVD ¶
type AVD struct {
// Name is the name of the device, such as "glutter-37".
Name string
// Package is the sdkmanager package of its system image, such as
// "system-images;android-37.2;google_apis_ps16k;x86_64".
Package string
// API is the Android API level of the image, such as "37".
API string
// Audio gives the device an audio output and input. The emulator runs with -no-audio, so the sound goes nowhere (the guest has the
// hardware and the host side is a null device); the other devices have no audio hardware, which keeps their boot as it was.
Audio bool
}
AVD describes a virtual device the harness makes.
Flutter: no counterpart
type Emulator ¶
type Emulator struct {
// Serial is the emulator's adb serial, "emulator-5584".
Serial string
// AVD is the virtual device it runs.
AVD AVD
// Root is the directory of the harness's files.
Root string
// ADB runs adb for this emulator, on its private server.
ADB androidtool.ADB
// SDK is the SDK the emulator came from.
SDK *androidtool.SDK
// contains filtered or unexported fields
}
Emulator is a running emulator with its own adb server.
Flutter: no counterpart
func Shared ¶
Shared returns the emulator of a virtual device for a device test, starting it the first time a test of the package asks: booting takes tens of seconds, so the tests of a package share one, and Run stops it when they are done. A missing emulator or image fails the test: a device test that silently skipped would prove nothing.
Flutter: no counterpart
func Start ¶
Start makes the virtual device if it is not there, starts the emulator headless in a process group of its own under `timeout -k`, waits for it to boot, and returns it. The caller stops it with Stop. It fails with AndroidTestImageMissing (the system image is not installed), AndroidTestEmulatorMissing and AndroidTestBootFailed.
Flutter: no counterpart
func (*Emulator) BuildAPK ¶
func (e *Emulator) BuildAPK(ctx context.Context, pkg, dir string, opts androidtool.BuildOptions) (apk, appID string, err error)
BuildAPK builds a package of the module for x86_64 (the emulator's ABI) into dir and returns the APK and its application ID. The debug key is the harness's own, in root/user, so that the tests never touch the developer's config directory. Extra options (a permission, an app ID) go in opts.
Flutter: no counterpart
func (*Emulator) FullLog ¶
FullLog is the whole log since Launch, every tag: crashes of the runtime and the system's lines about the app included.
func (*Emulator) HasActivity ¶
HasActivity says whether the system still has an activity of the app (`dumpsys activity activities`).
func (*Emulator) Launch ¶
Launch clears the log, stops the app if it runs and starts it with the given extras (the GLUTTER_ environment of the program).
Right after the boot of an old image (API 26) the log cannot be cleared for a while, so clearing it is tried again for up to two minutes.
func (*Emulator) Log ¶
Log is what the program of the app printed since Launch: the lines logcat has under the app's tag (the application ID, SPEC §24.9).
func (*Emulator) PullAppDir ¶
PullAppDir reads every file of a directory of the app's private directory (`run-as ls`, then `run-as cat` of each), keyed by name. The directory has no subdirectories.
func (*Emulator) RunAs ¶
RunAs runs a command as the app (`run-as`, which a debuggable app allows) and returns its output: the way to read the files an app wrote.
func (*Emulator) Screenshot ¶
func (e *Emulator) Screenshot(ctx context.Context, appID string, extras map[string]string, timeout time.Duration) ([]byte, error)
Screenshot starts the app with GLUTTER_SCREENSHOT=1 (and the given extras), waits for it to save the frame of its first idle moment and end, and returns the PNG (proof method E, SPEC §24.13). A test that gets no file reads the app's log with Log.
func (*Emulator) Stop ¶
func (e *Emulator) Stop()
Stop stops the emulator and the adb server: the emulator is asked to quit (`adb emu kill`), and its process group is killed if it is still there after 20 seconds. Nothing of the harness runs afterwards; Leftovers says so.
func (*Emulator) WaitExit ¶
WaitExit waits until the app has no process, for at most timeout. It fails with AndroidTestTimeout when the app still runs.
type Options ¶
type Options struct {
// AVD is the name of the virtual device, a key of AVDs; "glutter-37" when empty.
AVD string
// Root is the directory of the harness's files (virtual devices, HOME of the emulator, its
// log); <module root>/.android-test when empty.
Root string
// GPU is the emulator's -gpu mode; $GLUTTER_ANDROID_GPU, else "swangle_indirect" (ANGLE over SwiftShader: no host GPU and no display needed; swiftshader_indirect crashes the emulator).
GPU string
// Lifetime is how long `timeout -k` lets the emulator live at most; 30 minutes by default.
Lifetime time.Duration
// BootTimeout is how long to wait for sys.boot_completed; 5 minutes by default.
BootTimeout time.Duration
// Args are more arguments for the emulator, such as "-feature", "-Vulkan".
Args []string
}
Options say how an emulator is started.
Flutter: no counterpart