fix: error getting window size during screen rotation

feat: add log
This commit is contained in:
余泓铮
2024-03-19 16:51:57 +08:00
parent 80c50707f3
commit 60580256c0
5 changed files with 44 additions and 3 deletions
+23 -2
View File
@@ -6,6 +6,7 @@ import (
"io/fs" "io/fs"
"io/ioutil" "io/ioutil"
"path/filepath" "path/filepath"
"regexp"
"strconv" "strconv"
"strings" "strings"
"time" "time"
@@ -91,7 +92,14 @@ func (ad *adbDriver) WindowSize() (size Size, err error) {
return Size{Width: width, Height: height}, nil return Size{Width: width, Height: height}, nil
} }
} }
orientation, err := ad.Orientation()
if err != nil {
log.Warn().Err(err).Msgf("window size get orientation failed, use default orientation")
orientation = OrientationPortrait
}
if orientation != OrientationPortrait {
size.Width, size.Height = size.Height, size.Width
}
err = errors.New("physical window size not found by adb") err = errors.New("physical window size not found by adb")
return return
} }
@@ -186,7 +194,20 @@ func (ad *adbDriver) StopCamera() (err error) {
} }
func (ad *adbDriver) Orientation() (orientation Orientation, err error) { func (ad *adbDriver) Orientation() (orientation Orientation, err error) {
err = errDriverNotImplemented output, err := ad.adbClient.RunShellCommand("dumpsys", "input", "|", "grep", "'SurfaceOrientation'")
if err != nil {
return
}
re := regexp.MustCompile(`SurfaceOrientation: (\d)`)
matches := re.FindStringSubmatch(output)
if len(matches) > 1 { // 确保找到了匹配项
if matches[1] == "0" || matches[1] == "2" {
return OrientationPortrait, nil
} else if matches[1] == "1" || matches[1] == "3" {
return OrientationLandscapeLeft, nil
}
}
err = fmt.Errorf("not found SurfaceOrientation value")
return return
} }
+1 -1
View File
@@ -18,7 +18,7 @@ var (
func setupAndroid(t *testing.T) { func setupAndroid(t *testing.T) {
device, err := NewAndroidDevice() device, err := NewAndroidDevice()
checkErr(t, err) checkErr(t, err)
device.UIA2 = true //device.UIA2 = true
driverExt, err = device.NewDriver() driverExt, err = device.NewDriver()
checkErr(t, err) checkErr(t, err)
} }
+8
View File
@@ -224,6 +224,14 @@ func (ud *uiaDriver) WindowSize() (size Size, err error) {
return Size{}, err return Size{}, err
} }
size = reply.Value.Size size = reply.Value.Size
orientation, err := ud.Orientation()
if err != nil {
log.Warn().Err(err).Msgf("window size get orientation failed, use default orientation")
orientation = OrientationPortrait
}
if orientation != OrientationPortrait {
size.Width, size.Height = size.Height, size.Width
}
return return
} }
+4
View File
@@ -511,6 +511,10 @@ type WebDriver interface {
// since the location service needs some time to update the location data. // since the location service needs some time to update the location data.
Location() (Location, error) Location() (Location, error)
BatteryInfo() (BatteryInfo, error) BatteryInfo() (BatteryInfo, error)
// WindowSize Return the width and height in portrait mode.
// when getting the window size in wda/ui2/adb, if the device is in landscape mode,
// the width and height will be reversed.
WindowSize() (Size, error) WindowSize() (Size, error)
Screen() (Screen, error) Screen() (Screen, error)
Scale() (float64, error) Scale() (float64, error)
+8
View File
@@ -207,6 +207,14 @@ func (wd *wdaDriver) WindowSize() (size Size, err error) {
} }
size.Height = size.Height * int(scale) size.Height = size.Height * int(scale)
size.Width = size.Width * int(scale) size.Width = size.Width * int(scale)
orientation, err := wd.Orientation()
if err != nil {
log.Warn().Err(err).Msgf("window size get orientation failed, use default orientation")
orientation = OrientationPortrait
}
if orientation != OrientationPortrait {
size.Width, size.Height = size.Height, size.Width
}
return return
} }