From 60580256c0c5826f18a65e272826dee63b7adfea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BD=99=E6=B3=93=E9=93=AE?= Date: Tue, 19 Mar 2024 16:27:06 +0800 Subject: [PATCH] fix: error getting window size during screen rotation feat: add log --- hrp/pkg/uixt/android_adb_driver.go | 25 +++++++++++++++++++++++-- hrp/pkg/uixt/android_test.go | 2 +- hrp/pkg/uixt/android_uia2_driver.go | 8 ++++++++ hrp/pkg/uixt/interface.go | 4 ++++ hrp/pkg/uixt/ios_driver.go | 8 ++++++++ 5 files changed, 44 insertions(+), 3 deletions(-) diff --git a/hrp/pkg/uixt/android_adb_driver.go b/hrp/pkg/uixt/android_adb_driver.go index 7910e839..f3161a56 100644 --- a/hrp/pkg/uixt/android_adb_driver.go +++ b/hrp/pkg/uixt/android_adb_driver.go @@ -6,6 +6,7 @@ import ( "io/fs" "io/ioutil" "path/filepath" + "regexp" "strconv" "strings" "time" @@ -91,7 +92,14 @@ func (ad *adbDriver) WindowSize() (size Size, err error) { 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") return } @@ -186,7 +194,20 @@ func (ad *adbDriver) StopCamera() (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 } diff --git a/hrp/pkg/uixt/android_test.go b/hrp/pkg/uixt/android_test.go index cd15736b..9b2d13db 100644 --- a/hrp/pkg/uixt/android_test.go +++ b/hrp/pkg/uixt/android_test.go @@ -18,7 +18,7 @@ var ( func setupAndroid(t *testing.T) { device, err := NewAndroidDevice() checkErr(t, err) - device.UIA2 = true + //device.UIA2 = true driverExt, err = device.NewDriver() checkErr(t, err) } diff --git a/hrp/pkg/uixt/android_uia2_driver.go b/hrp/pkg/uixt/android_uia2_driver.go index b68d09e9..ecd2611d 100644 --- a/hrp/pkg/uixt/android_uia2_driver.go +++ b/hrp/pkg/uixt/android_uia2_driver.go @@ -224,6 +224,14 @@ func (ud *uiaDriver) WindowSize() (size Size, err error) { return Size{}, err } 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 } diff --git a/hrp/pkg/uixt/interface.go b/hrp/pkg/uixt/interface.go index a447d3a3..07885f56 100644 --- a/hrp/pkg/uixt/interface.go +++ b/hrp/pkg/uixt/interface.go @@ -511,6 +511,10 @@ type WebDriver interface { // since the location service needs some time to update the location data. Location() (Location, 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) Screen() (Screen, error) Scale() (float64, error) diff --git a/hrp/pkg/uixt/ios_driver.go b/hrp/pkg/uixt/ios_driver.go index ea2d305f..2c69e984 100644 --- a/hrp/pkg/uixt/ios_driver.go +++ b/hrp/pkg/uixt/ios_driver.go @@ -207,6 +207,14 @@ func (wd *wdaDriver) WindowSize() (size Size, err error) { } size.Height = size.Height * 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 }