Added Resolution type

This commit is contained in:
2022-08-02 23:34:21 -07:00
parent 3e348641d8
commit 82d0a5934b
6 changed files with 76 additions and 23 deletions

View File

@@ -95,7 +95,12 @@ type Input interface {
} }
type TapSequence struct { 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. // ShortenSleep allows you to shorten all the sleep times between tap and swipe events.

View File

@@ -26,7 +26,7 @@ func main() {
fmt.Printf("Dev `%s` is not authorized, authorize it to continue.\n", dev.SerialNo) fmt.Printf("Dev `%s` is not authorized, authorize it to continue.\n", dev.SerialNo)
continue continue
} }
err := dev.GoHome(ctx) err := dev.Reboot(ctx)
if err != nil { if err != nil {
// handle error here // handle error here
fmt.Printf("Error: %v\n", err) fmt.Printf("Error: %v\n", err)

View File

@@ -0,0 +1,3 @@
module github.com/taigrr/adb/examples/tapRecorder
go 1.19

View File

@@ -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)
}
}

View File

@@ -28,26 +28,26 @@ func (d Device) Shell(ctx context.Context, command string) (stdout string, stder
// adb shell wm size // adb shell wm size
// Physical size: 1440x3120 // 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"} cmd := []string{"-s", string(d.SerialNo), "shell", "wm", "size"}
stdout, _, _, err := execute(ctx, cmd) stdout, _, _, err := execute(ctx, cmd)
if err != nil { if err != nil {
return -1, -1, err return Resolution{Width: -1, Height: -1}, err
} }
return parseScreenResolution(stdout) return parseScreenResolution(stdout)
} }
// Parses input, example: // Parses input, example:
// Physical size: 1440x3040 // 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]+)`) r := regexp.MustCompile(`Physical size: ([0-9]+)x([0-9]+)`)
resStr := r.FindStringSubmatch(in) resStr := r.FindStringSubmatch(in)
if len(resStr) != 3 { if len(resStr) != 3 {
return -1, -1, ErrResolutionParseFail return Resolution{Width: -1, Height: -1}, ErrResolutionParseFail
} }
w, _ := strconv.Atoi(resStr[1]) w, _ := strconv.Atoi(resStr[1])
h, _ := strconv.Atoi(resStr[2]) 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 { 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 return err
} }
//Equivalent to pressing the back button // Equivalent to pressing the back button
// //
// Calls `input keyevent KEYCODE_BACK` under the hood // Calls `input keyevent KEYCODE_BACK` under the hood
func (d Device) GoBack(ctx context.Context) error { func (d Device) GoBack(ctx context.Context) error {

View File

@@ -1,35 +1,35 @@
package adb package adb
import "testing" import (
"reflect"
"testing"
)
func Test_parseScreenResolution(t *testing.T) { func Test_parseScreenResolution(t *testing.T) {
type args struct { type args struct {
in string in string
} }
tests := []struct { tests := []struct {
name string name string
args args args args
wantWidth int wantRes Resolution
wantLength int wantErr bool
wantErr bool
}{ }{
{name: "Pixel 4XL", args: args{in: "Physical size: 1440x3040"}, wantWidth: 1440, wantLength: 3040, wantErr: false}, {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"}, wantWidth: 1440, wantLength: 2560, 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"}, wantWidth: -1, wantLength: -1, wantErr: true}, {name: "garbage", args: args{in: "asdfhjkla"}, wantRes: Resolution{Width: -1, Height: -1}, wantErr: true},
// TODO: Add test cases. // TODO: Add test cases.
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { 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 { if (err != nil) != tt.wantErr {
t.Errorf("parseScreenResolution() error = %v, wantErr %v", err, tt.wantErr) t.Errorf("parseScreenResolution() error = %v, wantErr %v", err, tt.wantErr)
return return
} }
if w != tt.wantWidth {
t.Errorf("parseScreenResolution() got = %v, want %v", w, tt.wantWidth) if !reflect.DeepEqual(gotRes, tt.wantRes) {
} t.Errorf("Device.GetScreenResolution() = %v, want %v", gotRes, tt.wantRes)
if l != tt.wantLength {
t.Errorf("parseScreenResolution() got1 = %v, want %v", l, tt.wantLength)
} }
}) })
} }