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

@@ -96,6 +96,11 @@ type Input interface {
type TapSequence struct {
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.

View File

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

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
// 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 {

View File

@@ -1,6 +1,9 @@
package adb
import "testing"
import (
"reflect"
"testing"
)
func Test_parseScreenResolution(t *testing.T) {
type args struct {
@@ -9,27 +12,24 @@ func Test_parseScreenResolution(t *testing.T) {
tests := []struct {
name string
args args
wantWidth int
wantLength int
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)
}
})
}