fix: resolve conflicts

This commit is contained in:
徐聪
2024-07-19 15:11:16 +08:00
36 changed files with 2018 additions and 337 deletions
+89 -13
View File
@@ -6,6 +6,7 @@ import (
"fmt"
"io"
"os"
"strconv"
"strings"
"time"
@@ -107,25 +108,53 @@ func (d *Device) features() (features Features, err error) {
return features, nil
}
<<<<<<< HEAD
func (d Device) HasAttribute(key string) bool {
=======
func (d *Device) HasAttribute(key string) bool {
>>>>>>> video-release
_, ok := d.attrs[key]
return ok
}
<<<<<<< HEAD
func (d Device) Product() (string, error) {
=======
func (d *Device) Product() (string, error) {
>>>>>>> video-release
if d.HasAttribute("product") {
return d.attrs["product"], nil
}
return "", errors.New("does not have attribute: product")
}
<<<<<<< HEAD
func (d Device) Model() (string, error) {
=======
func (d *Device) Model() (string, error) {
>>>>>>> video-release
if d.HasAttribute("model") {
return d.attrs["model"], nil
}
return "", errors.New("does not have attribute: model")
}
<<<<<<< HEAD
=======
func (d *Device) Brand() (string, error) {
if d.HasAttribute("brand") {
return d.attrs["brand"], nil
}
brand, err := d.RunShellCommand("getprop", "ro.product.brand")
brand = strings.TrimSpace(brand)
if err != nil {
return "", errors.New("does not have attribute: brand")
}
d.attrs["brand"] = brand
return brand, nil
}
>>>>>>> video-release
func (d *Device) Usb() (string, error) {
if d.HasAttribute("usb") {
return d.attrs["usb"], nil
@@ -133,7 +162,11 @@ func (d *Device) Usb() (string, error) {
return "", errors.New("does not have attribute: usb")
}
<<<<<<< HEAD
func (d Device) transportId() (string, error) {
=======
func (d *Device) transportId() (string, error) {
>>>>>>> video-release
if d.HasAttribute("transport_id") {
return d.attrs["transport_id"], nil
}
@@ -168,10 +201,8 @@ func (d *Device) DevicePath() (string, error) {
return resp, err
}
func (d *Device) Forward(localPort int, remoteInterface interface{}, noRebind ...bool) (err error) {
command := ""
func (d *Device) Forward(remoteInterface interface{}, noRebind ...bool) (port int, err error) {
var remote string
local := fmt.Sprintf("tcp:%d", localPort)
switch r := remoteInterface.(type) {
// for unix sockets
case string:
@@ -180,14 +211,28 @@ func (d *Device) Forward(localPort int, remoteInterface interface{}, noRebind ..
remote = fmt.Sprintf("tcp:%d", r)
}
forwardList, err := d.ForwardList()
if err != nil {
return
}
for _, forwardItem := range forwardList {
if forwardItem.Remote == remote {
return strconv.Atoi(forwardItem.Local[4:])
}
}
localPort, err := builtin.GetFreePort()
if err != nil {
return
}
local := fmt.Sprintf("tcp:%d", localPort)
command := fmt.Sprintf("host-serial:%s:forward:%s;%s", d.serial, local, remote)
if len(noRebind) != 0 && noRebind[0] {
command = fmt.Sprintf("host-serial:%s:forward:norebind:%s;%s", d.serial, local, remote)
} else {
command = fmt.Sprintf("host-serial:%s:forward:%s;%s", d.serial, local, remote)
}
_, err = d.adbClient.executeCommand(command, true)
return
return localPort, nil
}
func (d *Device) ForwardList() (deviceForwardList []DeviceForward, err error) {
@@ -524,7 +569,7 @@ func (d *Device) Pull(remotePath string, dest io.Writer) (err error) {
return
}
func (d *Device) installViaABBExec(apk io.ReadSeeker) (raw []byte, err error) {
func (d *Device) installViaABBExec(apk io.ReadSeeker, args ...string) (raw []byte, err error) {
var (
tp transport
filesize int64
@@ -537,8 +582,11 @@ func (d *Device) installViaABBExec(apk io.ReadSeeker) (raw []byte, err error) {
return nil, err
}
defer func() { _ = tp.Close() }()
cmd := fmt.Sprintf("abb_exec:package\x00install\x00-t\x00-S\x00%d", filesize)
cmd := "abb_exec:package\x00install\x00-t"
for _, arg := range args {
cmd += "\x00" + arg
}
cmd += fmt.Sprintf("\x00-S\x00%d", filesize)
if err = tp.SendWithCheck(cmd); err != nil {
return nil, err
}
@@ -555,7 +603,7 @@ func (d *Device) installViaABBExec(apk io.ReadSeeker) (raw []byte, err error) {
return
}
func (d *Device) InstallAPK(apk io.ReadSeeker) (string, error) {
func (d *Device) InstallAPK(apk io.ReadSeeker, args ...string) (string, error) {
haserr := func(ret string) bool {
return strings.Contains(ret, "Failure")
}
@@ -575,8 +623,9 @@ func (d *Device) InstallAPK(apk io.ReadSeeker) (string, error) {
if err != nil {
return "", fmt.Errorf("error pushing: %v", err)
}
res, err := d.RunShellCommand("pm", "install", "-f", remote)
args = append([]string{"install"}, args...)
args = append(args, "-f", remote)
res, err := d.RunShellCommand("pm", args...)
if err != nil {
return "", errors.Wrap(err, "install apk failed")
}
@@ -591,7 +640,7 @@ func (d *Device) Uninstall(packageName string, keepData ...bool) (string, error)
if len(keepData) == 0 {
keepData = []bool{false}
}
packageName = strings.ReplaceAll(packageName, " ", "")
packageName = strings.TrimSpace(packageName)
if len(packageName) == 0 {
return "", fmt.Errorf("invalid package name")
}
@@ -603,6 +652,33 @@ func (d *Device) Uninstall(packageName string, keepData ...bool) (string, error)
return d.RunShellCommand("pm", args...)
}
func (d *Device) ListPackages() ([]string, error) {
args := []string{"list", "packages"}
resRaw, err := d.RunShellCommand("pm", args...)
if err != nil {
return []string{}, err
}
lines := strings.Split(resRaw, "\n")
var packages []string
for _, line := range lines {
packageName := strings.TrimPrefix(line, "package:")
packages = append(packages, packageName)
}
return packages, nil
}
func (d *Device) IsPackagesInstalled(packageName string) bool {
packages, err := d.ListPackages()
if err != nil {
return false
}
packageName = strings.TrimSpace(packageName)
if len(packageName) == 0 {
return false
}
return builtin.Contains(packages, packageName)
}
func (d *Device) ScreenCap() ([]byte, error) {
if d.HasFeature(FeatShellV2) {
return d.RunShellCommandV2WithBytes("screencap", "-p")
+48 -5
View File
@@ -59,7 +59,10 @@ func TestDevice_Product(t *testing.T) {
for i := range devices {
dev := devices[i]
product := dev.Product()
product, err := dev.Product()
if err != nil {
t.Fatal(err)
}
t.Log(dev.Serial(), product)
}
}
@@ -69,7 +72,24 @@ func TestDevice_Model(t *testing.T) {
for i := range devices {
dev := devices[i]
t.Log(dev.Serial(), dev.Model())
model, err := dev.Model()
if err != nil {
t.Fatal(err)
}
t.Log(dev.Serial(), model)
}
}
func TestDevice_Brand(t *testing.T) {
setupDevices(t)
for i := range devices {
dev := devices[i]
brand, err := dev.Brand()
if err != nil {
t.Fatal(err)
}
t.Log(dev.Serial(), brand)
}
}
@@ -78,7 +98,15 @@ func TestDevice_Usb(t *testing.T) {
for i := range devices {
dev := devices[i]
t.Log(dev.Serial(), dev.Usb(), dev.IsUsb())
usb, err := dev.Usb()
if err != nil {
t.Fatal(err)
}
isUsb, err := dev.IsUsb()
if err != nil {
t.Fatal(err)
}
t.Log(dev.Serial(), usb, isUsb)
}
}
@@ -95,8 +123,7 @@ func TestDevice_Forward(t *testing.T) {
setupDevices(t)
for _, device := range devices {
localPort := 61000
err := device.Forward(localPort, 6790)
localPort, err := device.Forward(6790)
if err != nil {
t.Fatal(err)
}
@@ -314,6 +341,22 @@ func TestDevice_InstallAPK(t *testing.T) {
}
}
func TestDevice_ListPackages(t *testing.T) {
setupDevices(t)
for _, dev := range devices {
res, err := dev.ListPackages()
if err != nil {
t.Fatal(err)
}
t.Log(res)
installed := dev.IsPackagesInstalled("io.appium.uiautomator2.server")
if err != nil {
t.Fatal(err)
}
t.Log(installed)
}
}
func TestDevice_HasFeature(t *testing.T) {
setupDevices(t)