From 82d0a5934b8aeb32734912a620f5fda068e28d68 Mon Sep 17 00:00:00 2001 From: Tai Groot Date: Tue, 2 Aug 2022 23:34:21 -0700 Subject: [PATCH] Added Resolution type --- capture.go | 7 +++++- examples/injection/main.go | 2 +- examples/tapRecorder/go.mod | 3 +++ examples/tapRecorder/main.go | 45 ++++++++++++++++++++++++++++++++++++ shell.go | 12 +++++----- shell_test.go | 30 ++++++++++++------------ 6 files changed, 76 insertions(+), 23 deletions(-) create mode 100644 examples/tapRecorder/go.mod create mode 100644 examples/tapRecorder/main.go diff --git a/capture.go b/capture.go index 0d91e1a..20e7b9c 100644 --- a/capture.go +++ b/capture.go @@ -95,7 +95,12 @@ type Input interface { } type TapSequence struct { - Events []Input + Events []Input + Resolution Resolution +} +type Resolution struct { + Width int + Height int } // ShortenSleep allows you to shorten all the sleep times between tap and swipe events. diff --git a/examples/injection/main.go b/examples/injection/main.go index af7be0d..99b74d1 100644 --- a/examples/injection/main.go +++ b/examples/injection/main.go @@ -26,7 +26,7 @@ func main() { fmt.Printf("Dev `%s` is not authorized, authorize it to continue.\n", dev.SerialNo) continue } - err := dev.GoHome(ctx) + err := dev.Reboot(ctx) if err != nil { // handle error here fmt.Printf("Error: %v\n", err) diff --git a/examples/tapRecorder/go.mod b/examples/tapRecorder/go.mod new file mode 100644 index 0000000..a20c10d --- /dev/null +++ b/examples/tapRecorder/go.mod @@ -0,0 +1,3 @@ +module github.com/taigrr/adb/examples/tapRecorder + +go 1.19 diff --git a/examples/tapRecorder/main.go b/examples/tapRecorder/main.go new file mode 100644 index 0000000..6855f4d --- /dev/null +++ b/examples/tapRecorder/main.go @@ -0,0 +1,45 @@ +package main + +import ( + "context" + "fmt" + "time" + + "github.com/taigrr/adb" +) + +var command string + +func init() { + // TODO allow for any input to be used as the command + command = "ls" +} + +func main() { + ctx, cancel := context.WithTimeout(context.Background(), time.Second*10) + defer cancel() + devs, err := adb.Devices(ctx) + if err != nil { + fmt.Printf("Error enumerating devices: %v\n", err) + return + } + for _, dev := range devs { + if !dev.IsAuthorized { + fmt.Printf("Dev `%s` is not authorized, authorize it to continue.\n", dev.SerialNo) + continue + } + //w, h, err := dev.GetScreenResolution(ctx) + //if err != nil { + // // handle error here + // fmt.Printf("Error: %v\n", err) + //} + fmt.Printf("Begin tapping on device %s now...\n", dev.SerialNo) + t, err := dev.CaptureSequence(ctx) + if err != nil { + fmt.Printf("Error capturing sequence: %v\n", err) + return + } + fmt.Println("Sequence captured, replaying now...") + dev.ReplayTapSequence(context.TODO(), t) + } +} diff --git a/shell.go b/shell.go index ddb2edf..8fedeec 100644 --- a/shell.go +++ b/shell.go @@ -28,26 +28,26 @@ func (d Device) Shell(ctx context.Context, command string) (stdout string, stder // adb shell wm size // Physical size: 1440x3120 -func (d Device) GetScreenResolution(ctx context.Context) (width int, height int, err error) { +func (d Device) GetScreenResolution(ctx context.Context) (res Resolution, err error) { cmd := []string{"-s", string(d.SerialNo), "shell", "wm", "size"} stdout, _, _, err := execute(ctx, cmd) if err != nil { - return -1, -1, err + return Resolution{Width: -1, Height: -1}, err } return parseScreenResolution(stdout) } // Parses input, example: // Physical size: 1440x3040 -func parseScreenResolution(in string) (int, int, error) { +func parseScreenResolution(in string) (Resolution, error) { r := regexp.MustCompile(`Physical size: ([0-9]+)x([0-9]+)`) resStr := r.FindStringSubmatch(in) if len(resStr) != 3 { - return -1, -1, ErrResolutionParseFail + return Resolution{Width: -1, Height: -1}, ErrResolutionParseFail } w, _ := strconv.Atoi(resStr[1]) h, _ := strconv.Atoi(resStr[2]) - return w, h, nil + return Resolution{Width: w, Height: h}, nil } func (d Device) Tap(ctx context.Context, X, Y int) error { @@ -89,7 +89,7 @@ func (d Device) GoHome(ctx context.Context) error { return err } -//Equivalent to pressing the back button +// Equivalent to pressing the back button // // Calls `input keyevent KEYCODE_BACK` under the hood func (d Device) GoBack(ctx context.Context) error { diff --git a/shell_test.go b/shell_test.go index fb7a4d2..1d8c50f 100644 --- a/shell_test.go +++ b/shell_test.go @@ -1,35 +1,35 @@ package adb -import "testing" +import ( + "reflect" + "testing" +) func Test_parseScreenResolution(t *testing.T) { type args struct { in string } tests := []struct { - name string - args args - wantWidth int - wantLength int - wantErr bool + name string + args args + wantRes Resolution + wantErr bool }{ - {name: "Pixel 4XL", args: args{in: "Physical size: 1440x3040"}, wantWidth: 1440, wantLength: 3040, wantErr: false}, - {name: "Pixel XL", args: args{in: "Physical size: 1440x2560"}, wantWidth: 1440, wantLength: 2560, wantErr: false}, - {name: "garbage", args: args{in: "asdfhjkla"}, wantWidth: -1, wantLength: -1, wantErr: true}, + {name: "Pixel 4XL", args: args{in: "Physical size: 1440x3040"}, wantRes: Resolution{Width: 1440, Height: 3040}, wantErr: false}, + {name: "Pixel XL", args: args{in: "Physical size: 1440x2560"}, wantRes: Resolution{Width: 1440, Height: 2560}, wantErr: false}, + {name: "garbage", args: args{in: "asdfhjkla"}, wantRes: Resolution{Width: -1, Height: -1}, wantErr: true}, // TODO: Add test cases. } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - w, l, err := parseScreenResolution(tt.args.in) + gotRes, err := parseScreenResolution(tt.args.in) if (err != nil) != tt.wantErr { t.Errorf("parseScreenResolution() error = %v, wantErr %v", err, tt.wantErr) return } - if w != tt.wantWidth { - t.Errorf("parseScreenResolution() got = %v, want %v", w, tt.wantWidth) - } - if l != tt.wantLength { - t.Errorf("parseScreenResolution() got1 = %v, want %v", l, tt.wantLength) + + if !reflect.DeepEqual(gotRes, tt.wantRes) { + t.Errorf("Device.GetScreenResolution() = %v, want %v", gotRes, tt.wantRes) } }) }