diff --git a/CHANGELOG.md b/CHANGELOG.md index dc2bd89..132b6e2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,8 @@ - Add dynamic port assigment for InnerServer #39 - Add `autoLaunch` capability: whether to launch the app automatically. Default `true` - Add `--version` option to a driver CLI. Fix exit codes +- Add `SetOrientation` command (Note that orientation is preserved between sessions) +- Fix `GetOrientation` command ## v1.2.0 diff --git a/Winium/Winium.StoreApps.Driver/Automator/Automator.cs b/Winium/Winium.StoreApps.Driver/Automator/Automator.cs index 9374752..d9f4281 100644 --- a/Winium/Winium.StoreApps.Driver/Automator/Automator.cs +++ b/Winium/Winium.StoreApps.Driver/Automator/Automator.cs @@ -211,27 +211,6 @@ public void InitializeApp() return new Point(x, y); } - /// - /// This method should be called before any EmulatorController methods that move cursor or use screen size. - /// Orientation value is used by EmulatorController to translate phone screen coordinates into virtual machine coordinates - /// - public void UpdatedOrientationForEmulatorController() - { - var command = new Command(DriverCommand.GetOrientation); - var responseBody = this.CommandForwarder.ForwardCommand(command); - var deserializeObject = JsonConvert.DeserializeObject(responseBody); - if (deserializeObject.Status != ResponseStatus.Success) - { - return; - } - - EmulatorController.PhoneOrientation orientation; - if (Enum.TryParse(deserializeObject.Value.ToString(), true, out orientation)) - { - this.emulatorController.PhoneOrientationToUse = orientation; - } - } - #endregion #region Methods diff --git a/Winium/Winium.StoreApps.Driver/CommandExecutors/ClickElementExecutor.cs b/Winium/Winium.StoreApps.Driver/CommandExecutors/ClickElementExecutor.cs index e1c8322..c1885d4 100644 --- a/Winium/Winium.StoreApps.Driver/CommandExecutors/ClickElementExecutor.cs +++ b/Winium/Winium.StoreApps.Driver/CommandExecutors/ClickElementExecutor.cs @@ -20,7 +20,6 @@ internal static bool ClickElement(Automator automator, string elementId) return false; } - automator.UpdatedOrientationForEmulatorController(); automator.EmulatorController.LeftButtonClick(location.Value); return true; diff --git a/Winium/Winium.StoreApps.Driver/CommandExecutors/CloseExecutor.cs b/Winium/Winium.StoreApps.Driver/CommandExecutors/CloseExecutor.cs index ebd747c..6717022 100644 --- a/Winium/Winium.StoreApps.Driver/CommandExecutors/CloseExecutor.cs +++ b/Winium/Winium.StoreApps.Driver/CommandExecutors/CloseExecutor.cs @@ -9,6 +9,7 @@ protected override string DoImpl() if (!this.Automator.ActualCapabilities.DebugConnectToRunningApp) { // TODO close should only close app, not uninstall + this.Automator.EmulatorController.Disconnect(); this.Automator.Deployer.Uninstall(); } diff --git a/Winium/Winium.StoreApps.Driver/CommandExecutors/GetOrientationExecutor.cs b/Winium/Winium.StoreApps.Driver/CommandExecutors/GetOrientationExecutor.cs index e7c7360..e9cd001 100644 --- a/Winium/Winium.StoreApps.Driver/CommandExecutors/GetOrientationExecutor.cs +++ b/Winium/Winium.StoreApps.Driver/CommandExecutors/GetOrientationExecutor.cs @@ -2,11 +2,8 @@ { #region - using System; - - using Newtonsoft.Json; - using Winium.StoreApps.Common; + using Winium.StoreApps.Driver.EmulatorHelpers; #endregion @@ -16,18 +13,13 @@ internal class GetOrientationExecutor : CommandExecutorBase protected override string DoImpl() { - var orientation = string.Empty; - var responseBody = this.Automator.CommandForwarder.ForwardCommand(this.ExecutedCommand); - var deserializeObject = JsonConvert.DeserializeObject(responseBody); - if (deserializeObject.Status == ResponseStatus.Success) - { - var value = deserializeObject.Value.ToString(); - orientation = value.StartsWith("landscape", StringComparison.OrdinalIgnoreCase) - ? "LANDSCAPE" - : "PORTRAIT"; - } + var orientation = this.Automator.EmulatorController.Orientation; + + var orientationName = (orientation == EmulatorController.PhoneOrientation.Portrait) + ? "PORTRAIT" + : "LANDSCAPE"; - return this.JsonResponse(ResponseStatus.Success, orientation); + return this.JsonResponse(ResponseStatus.Success, orientationName); } #endregion diff --git a/Winium/Winium.StoreApps.Driver/CommandExecutors/GetWindowSizeExecutor.cs b/Winium/Winium.StoreApps.Driver/CommandExecutors/GetWindowSizeExecutor.cs index f1cebd1..afb23f6 100644 --- a/Winium/Winium.StoreApps.Driver/CommandExecutors/GetWindowSizeExecutor.cs +++ b/Winium/Winium.StoreApps.Driver/CommandExecutors/GetWindowSizeExecutor.cs @@ -14,7 +14,6 @@ internal class GetWindowSizeExecutor : CommandExecutorBase protected override string DoImpl() { - this.Automator.UpdatedOrientationForEmulatorController(); var phoneScreenSize = this.Automator.EmulatorController.PhoneScreenSize; return this.JsonResponse( diff --git a/Winium/Winium.StoreApps.Driver/CommandExecutors/MouseMoveToExecutor.cs b/Winium/Winium.StoreApps.Driver/CommandExecutors/MouseMoveToExecutor.cs index c76730f..a21aa13 100644 --- a/Winium/Winium.StoreApps.Driver/CommandExecutors/MouseMoveToExecutor.cs +++ b/Winium/Winium.StoreApps.Driver/CommandExecutors/MouseMoveToExecutor.cs @@ -16,8 +16,6 @@ internal class MouseMoveToExecutor : CommandExecutorBase protected override string DoImpl() { - this.Automator.UpdatedOrientationForEmulatorController(); - var elementId = Automator.GetValue(this.ExecutedCommand.Parameters, "element"); Point coordinates; if (elementId != null) @@ -31,7 +29,6 @@ protected override string DoImpl() coordinates = new Point(xOffset, yOffset); } - this.Automator.UpdatedOrientationForEmulatorController(); this.Automator.EmulatorController.MoveCursorTo(coordinates); return this.JsonResponse(); diff --git a/Winium/Winium.StoreApps.Driver/CommandExecutors/QuitExecutor.cs b/Winium/Winium.StoreApps.Driver/CommandExecutors/QuitExecutor.cs index ad514f4..a085b9c 100644 --- a/Winium/Winium.StoreApps.Driver/CommandExecutors/QuitExecutor.cs +++ b/Winium/Winium.StoreApps.Driver/CommandExecutors/QuitExecutor.cs @@ -9,6 +9,7 @@ protected override string DoImpl() if (!this.Automator.ActualCapabilities.DebugConnectToRunningApp) { // TODO quit should close all open windows (apps) and possible close the emulator + this.Automator.EmulatorController.Disconnect(); this.Automator.Deployer.Uninstall(); } diff --git a/Winium/Winium.StoreApps.Driver/CommandExecutors/SetOrientationExecutor.cs b/Winium/Winium.StoreApps.Driver/CommandExecutors/SetOrientationExecutor.cs new file mode 100644 index 0000000..09b008d --- /dev/null +++ b/Winium/Winium.StoreApps.Driver/CommandExecutors/SetOrientationExecutor.cs @@ -0,0 +1,29 @@ +namespace Winium.StoreApps.Driver.CommandExecutors +{ + #region + + using System; + + using Winium.StoreApps.Driver.EmulatorHelpers; + + #endregion + + internal class SetOrientationExecutor : CommandExecutorBase + { + #region Methods + + protected override string DoImpl() + { + var value = this.ExecutedCommand.Parameters["orientation"].ToObject(); + + EmulatorController.PhoneOrientation orientation; + Enum.TryParse(value, true, out orientation); + + this.Automator.EmulatorController.Orientation = orientation; + + return this.JsonResponse(); + } + + #endregion + } +} diff --git a/Winium/Winium.StoreApps.Driver/CommandExecutors/TouchFlickExecutor.cs b/Winium/Winium.StoreApps.Driver/CommandExecutors/TouchFlickExecutor.cs index 5ae574e..6964080 100644 --- a/Winium/Winium.StoreApps.Driver/CommandExecutors/TouchFlickExecutor.cs +++ b/Winium/Winium.StoreApps.Driver/CommandExecutors/TouchFlickExecutor.cs @@ -17,8 +17,6 @@ internal class TouchFlickExecutor : CommandExecutorBase protected override string DoImpl() { - this.Automator.UpdatedOrientationForEmulatorController(); - var screen = this.Automator.EmulatorController.PhoneScreenSize; var startPoint = new Point(screen.Width / 2, screen.Height / 2); var elementId = Automator.GetValue(this.ExecutedCommand.Parameters, "element"); diff --git a/Winium/Winium.StoreApps.Driver/CommandExecutors/TouchScrollExecutor.cs b/Winium/Winium.StoreApps.Driver/CommandExecutors/TouchScrollExecutor.cs index 065de06..bb6f824 100644 --- a/Winium/Winium.StoreApps.Driver/CommandExecutors/TouchScrollExecutor.cs +++ b/Winium/Winium.StoreApps.Driver/CommandExecutors/TouchScrollExecutor.cs @@ -17,8 +17,6 @@ internal class TouchScrollExecutor : CommandExecutorBase protected override string DoImpl() { - this.Automator.UpdatedOrientationForEmulatorController(); - var screen = this.Automator.EmulatorController.PhoneScreenSize; var startPoint = new Point(screen.Width / 2, screen.Height / 2); diff --git a/Winium/Winium.StoreApps.Driver/CommandExecutors/TouchSingleTapExecutor.cs b/Winium/Winium.StoreApps.Driver/CommandExecutors/TouchSingleTapExecutor.cs index 2f28d6f..df2d9db 100644 --- a/Winium/Winium.StoreApps.Driver/CommandExecutors/TouchSingleTapExecutor.cs +++ b/Winium/Winium.StoreApps.Driver/CommandExecutors/TouchSingleTapExecutor.cs @@ -13,8 +13,6 @@ internal class TouchSingleTapExecutor : CommandExecutorBase protected override string DoImpl() { - this.Automator.UpdatedOrientationForEmulatorController(); - var elementId = Automator.GetValue(this.ExecutedCommand.Parameters, "element"); if (elementId == null) { diff --git a/Winium/Winium.StoreApps.Driver/EmulatorHelpers/EmulatorController.cs b/Winium/Winium.StoreApps.Driver/EmulatorHelpers/EmulatorController.cs index 5e8de96..e4a6008 100644 --- a/Winium/Winium.StoreApps.Driver/EmulatorHelpers/EmulatorController.cs +++ b/Winium/Winium.StoreApps.Driver/EmulatorHelpers/EmulatorController.cs @@ -11,6 +11,7 @@ using System.Windows.Forms; using Microsoft.Xde.Common; + using Microsoft.Xde.Interface; using Microsoft.Xde.Wmi; using Winium.StoreApps.Common; @@ -22,6 +23,8 @@ internal class EmulatorController { #region Fields + private readonly IXdeAutomation client; + private readonly IXdeVirtualMachine emulatorVm; private Point cursor; @@ -35,8 +38,8 @@ internal class EmulatorController public EmulatorController(string emulatorName) { this.emulatorVm = GetEmulatorVm(emulatorName); + this.client = AutomationClient.CreateAutomationClient(this.emulatorVm.Name); this.cursor = new Point(0, 0); - this.PhoneOrientationToUse = PhoneOrientation.Landscape; if (this.emulatorVm == null) { @@ -51,22 +54,11 @@ public EmulatorController(string emulatorName) #region Enums - [Flags] public enum PhoneOrientation { - None = 0, - Portrait = 1, Landscape = 2, - - PortraitUp = 5, - - PortraitDown = 9, - - LandscapeLeft = 18, - - LandscapeRight = 34, } #endregion @@ -77,13 +69,30 @@ public enum PhoneOrientation /// Current phone orientation to be used when translating phone screen coordinates into virtual machine screen coordinates. /// Should be set before using PhoneScreenSize or any mouse moving methods /// - public PhoneOrientation PhoneOrientationToUse { get; set; } + public PhoneOrientation Orientation + { + get + { + return this.client.DisplayOrientation == DisplayOrientation.Portrait + ? PhoneOrientation.Portrait + : PhoneOrientation.Landscape; + } + + set + { + this.client.DisplayOrientation = (value == PhoneOrientation.Portrait) + ? DisplayOrientation.Portrait + : DisplayOrientation.LandscapeLeft; + Thread.Sleep(1500); + } + } public Size PhoneScreenSize { get { - return (this.PhoneOrientationToUse & PhoneOrientation.Landscape) == PhoneOrientation.Landscape + var landscape = this.Orientation.HasFlag(PhoneOrientation.Landscape); + return landscape ? new Size(this.virtualScreenSize.Height, this.virtualScreenSize.Width) : this.virtualScreenSize; } @@ -94,10 +103,24 @@ private set } } + public string VmName + { + get + { + return this.emulatorVm.Name; + } + } + #endregion #region Public Methods and Operators + public void Disconnect() + { + this.client.Dispose(); + this.emulatorVm.Dispose(); + } + public string GetIpAddress() { var nic = this.emulatorVm.InternalNic; @@ -122,6 +145,7 @@ public void LeftButtonClick(Point? point = null) } this.LeftButtonDown(); + Thread.Sleep(250); this.LeftButtonUp(); } @@ -159,12 +183,6 @@ public void PerformGesture(IGesture gesture) this.LeftButtonUp(); } - public bool PhonePointVisibleOnScreen(Point phonePoint) - { - var phoneScreen = new Rectangle(new Point(0, 0), this.PhoneScreenSize); - return phoneScreen.Contains(phonePoint); - } - public string TakeScreenshot() { var size = this.virtualScreenSize; @@ -223,19 +241,14 @@ private static string ImageToBase64String(Image image, ImageFormat imageFormat) private Point TranslatePhoneToVirtualmachinePoint(Point location) { var translatedPoint = location; - switch (this.PhoneOrientationToUse) + switch (this.client.DisplayOrientation) { - case PhoneOrientation.PortraitDown: - translatedPoint.X = this.virtualScreenSize.Width - location.X; - translatedPoint.Y = this.virtualScreenSize.Height - location.Y; - break; - - case PhoneOrientation.LandscapeLeft: + case DisplayOrientation.LandscapeLeft: translatedPoint.X = this.virtualScreenSize.Width - location.Y; translatedPoint.Y = location.X; break; - case PhoneOrientation.LandscapeRight: + case DisplayOrientation.LandscapeRight: translatedPoint.X = location.Y; translatedPoint.Y = this.virtualScreenSize.Height - location.X; break; diff --git a/Winium/Winium.StoreApps.Driver/Winium.StoreApps.Driver.csproj b/Winium/Winium.StoreApps.Driver/Winium.StoreApps.Driver.csproj index 65a2025..c8b263b 100644 --- a/Winium/Winium.StoreApps.Driver/Winium.StoreApps.Driver.csproj +++ b/Winium/Winium.StoreApps.Driver/Winium.StoreApps.Driver.csproj @@ -68,6 +68,10 @@ False ..\..\..\..\..\..\..\Program Files (x86)\Microsoft XDE\8.1\Microsoft.Xde.Common.dll + + False + C:\Program Files (x86)\Microsoft XDE\8.1\Microsoft.Xde.Interface.dll + C:\Program Files (x86)\Microsoft XDE\8.1\Microsoft.Xde.Wmi.dll @@ -93,6 +97,7 @@ +