refactor(android): move PrepareDevice doc onto it; extract tested wakeCommands

The PrepareDevice doc block was stranded above adbArgs, leaving the exported function undocumented under godoc. Move it back and split the wake/keyguard tuples into wakeCommands so they have a unit test.
This commit is contained in:
pj committed 2026-06-11 09:27:35 +05:30
1 parent 600fb71c5b
commit d3182239dc
2 files changed
+26 -14

No files matched your search

+15 -14
View File
@@ -54,15 +54,6 @@ func EnsureDevice(ctx context.Context, serial, avdName string, stdout io.Writer)
return nil return nil
} }
// PrepareDevice keeps the device awake, wakes the screen, and dismisses a
// non-secure keyguard so the launched app stays in the foreground for the whole
// run. The fuzzer drives whatever is on screen, so a sleeping or locked device
// would have it explore system UI instead of the app. A secure lock
// (PIN/password) cannot be dismissed here and must be unlocked out of band.
//
// Every step is best effort: some OEM builds restrict or kill these commands
// (e.g. HyperOS SIGKILLs `svc power stayon`), and none is required for a run to
// proceed, so a failure is logged and skipped rather than aborting the run.
// adbArgs prepends the device selector when a serial is set, so every adb // adbArgs prepends the device selector when a serial is set, so every adb
// invocation targets the chosen device. Without it, `adb` fails on a host with // invocation targets the chosen device. Without it, `adb` fails on a host with
// more than one device attached, which silently disables anything that reads // more than one device attached, which silently disables anything that reads
@@ -74,16 +65,26 @@ func adbArgs(serial string, args ...string) []string {
return append([]string{"-s", serial}, args...) return append([]string{"-s", serial}, args...)
} }
// wakeCommands keep the screen on and unlocked. A secure lock (PIN/password)
// cannot be dismissed here and must be unlocked out of band.
func wakeCommands() [][]string {
return [][]string{
{"svc", "power", "stayon", "true"},
{"input", "keyevent", "KEYCODE_WAKEUP"},
{"wm", "dismiss-keyguard"},
}
}
// PrepareDevice wakes and unlocks the device and disables the background
// freezers that would suspend the driver. Best effort: some OEM builds kill
// these commands (e.g. HyperOS SIGKILLs `svc power stayon`), so a failure is
// logged and skipped rather than aborting the run.
func PrepareDevice(ctx context.Context, serial string, stdout io.Writer) error { func PrepareDevice(ctx context.Context, serial string, stdout io.Writer) error {
adb, err := AdbBinary() adb, err := AdbBinary()
if err != nil { if err != nil {
return err return err
} }
for _, shellCommand := range append([][]string{ for _, shellCommand := range append(wakeCommands(), antiFreezeCommands()...) {
{"svc", "power", "stayon", "true"},
{"input", "keyevent", "KEYCODE_WAKEUP"},
{"wm", "dismiss-keyguard"},
}, antiFreezeCommands()...) {
args := adbArgs(serial, append([]string{"shell"}, shellCommand...)...) args := adbArgs(serial, append([]string{"shell"}, shellCommand...)...)
if err := exec.CommandContext(ctx, adb, args...).Run(); err != nil { if err := exec.CommandContext(ctx, adb, args...).Run(); err != nil {
fmt.Fprintf(stdout, "device prep: skipping `adb %s` (%v)\n", strings.Join(shellCommand, " "), err) fmt.Fprintf(stdout, "device prep: skipping `adb %s` (%v)\n", strings.Join(shellCommand, " "), err)
+11
View File
@@ -7,6 +7,17 @@ import (
"testing" "testing"
) )
func TestWakeCommands(t *testing.T) {
want := [][]string{
{"svc", "power", "stayon", "true"},
{"input", "keyevent", "KEYCODE_WAKEUP"},
{"wm", "dismiss-keyguard"},
}
if got := wakeCommands(); !reflect.DeepEqual(got, want) {
t.Errorf("wakeCommands() = %v, want %v", got, want)
}
}
func TestParseAdbDevices_OnlineOnly(t *testing.T) { func TestParseAdbDevices_OnlineOnly(t *testing.T) {
output := `List of devices attached output := `List of devices attached
emulator-5554 device emulator-5554 device