From 49a998f955b457ea473911cd86fb4cc8d3d2abc4 Mon Sep 17 00:00:00 2001 From: Golovin Gleb Date: Mon, 29 Jun 2015 10:22:47 +0600 Subject: [PATCH] Code cleanup --- .../ActionChainsTests.cs | 6 +- .../Automator/Automator.cs | 4 +- .../Automator/Capabilities.cs | 6 +- .../SendKeysToActiveElementExecutor.cs | 3 - src/Winium.Desktop.Driver/Input/KeyEvent.cs | 33 +++++------ .../Input/KeyboardModifiers.cs | 51 ++++++++-------- .../Input/WiniumKeyboard.cs | 58 ++++++++++--------- 7 files changed, 82 insertions(+), 79 deletions(-) diff --git a/src/TestApps.Tests/WpfTestApplication.Tests/ActionChainsTests.cs b/src/TestApps.Tests/WpfTestApplication.Tests/ActionChainsTests.cs index 118f080..a714b2b 100644 --- a/src/TestApps.Tests/WpfTestApplication.Tests/ActionChainsTests.cs +++ b/src/TestApps.Tests/WpfTestApplication.Tests/ActionChainsTests.cs @@ -1,7 +1,7 @@ namespace WpfTestApplication.Tests { #region using - + using System.Linq; using NUnit.Framework; @@ -28,10 +28,10 @@ public void KeyUpAndKeyDown() actions.Click(first).KeyDown(Keys.Shift).Click(random).KeyUp(Keys.Shift).Perform(); var selectedItemsCount = list.FindElements(By.ClassName("ListBoxItem")).Count(item => item.Selected); - + Assert.AreEqual(4, selectedItemsCount); } - + #endregion } } diff --git a/src/Winium.Desktop.Driver/Automator/Automator.cs b/src/Winium.Desktop.Driver/Automator/Automator.cs index b9b587b..e10858c 100644 --- a/src/Winium.Desktop.Driver/Automator/Automator.cs +++ b/src/Winium.Desktop.Driver/Automator/Automator.cs @@ -35,12 +35,12 @@ public Automator(string session) public Application Application { get; set; } - public WiniumKeyboard WiniumKeyboard { get; set; } - public ElementStorage Elements { get; private set; } public string Session { get; private set; } + public WiniumKeyboard WiniumKeyboard { get; set; } + #endregion #region Public Methods and Operators diff --git a/src/Winium.Desktop.Driver/Automator/Capabilities.cs b/src/Winium.Desktop.Driver/Automator/Capabilities.cs index 20907ef..a75b0f4 100644 --- a/src/Winium.Desktop.Driver/Automator/Capabilities.cs +++ b/src/Winium.Desktop.Driver/Automator/Capabilities.cs @@ -35,12 +35,12 @@ internal Capabilities() [JsonProperty("innerPort")] public int InnerPort { get; set; } - [JsonProperty("launchDelay")] - public int LaunchDelay { get; set; } - [JsonProperty("keyboardSimulator")] public KeyboardSimulatorType KeyboardSimulator { get; set; } + [JsonProperty("launchDelay")] + public int LaunchDelay { get; set; } + #endregion #region Public Methods and Operators diff --git a/src/Winium.Desktop.Driver/CommandExecutors/SendKeysToActiveElementExecutor.cs b/src/Winium.Desktop.Driver/CommandExecutors/SendKeysToActiveElementExecutor.cs index 31a6204..4228b77 100644 --- a/src/Winium.Desktop.Driver/CommandExecutors/SendKeysToActiveElementExecutor.cs +++ b/src/Winium.Desktop.Driver/CommandExecutors/SendKeysToActiveElementExecutor.cs @@ -5,9 +5,6 @@ using System; using System.Linq; - using Winium.Cruciatus; - using Winium.Desktop.Driver.Input; - #endregion internal class SendKeysToActiveElementExecutor : CommandExecutorBase diff --git a/src/Winium.Desktop.Driver/Input/KeyEvent.cs b/src/Winium.Desktop.Driver/Input/KeyEvent.cs index 62e3b44..c788566 100644 --- a/src/Winium.Desktop.Driver/Input/KeyEvent.cs +++ b/src/Winium.Desktop.Driver/Input/KeyEvent.cs @@ -2,26 +2,21 @@ { #region using - using System; - using System.Collections.Generic; - using System.Linq; - using System.Text; - using System.Threading.Tasks; - using OpenQA.Selenium; #endregion - class KeyEvent + internal class KeyEvent { - #region Private Fields + #region Fields + + private readonly char character; - private char character; - private string unicodeKey; + private readonly string unicodeKey; #endregion - #region Public Methods and Operators + #region Constructors and Destructors public KeyEvent(char ch) { @@ -29,26 +24,30 @@ public KeyEvent(char ch) this.unicodeKey = KeyboardModifiers.GetKeyFromUnicode(this.character); } - public string GetKey() - { - return this.unicodeKey; - } + #endregion + + #region Public Methods and Operators public char GetCharacter() { return this.character; } + public string GetKey() + { + return this.unicodeKey; + } + public bool IsModifier() { return KeyboardModifiers.IsModifier(this.unicodeKey); } - + public bool IsModifierRelease() { return this.GetKey() == Keys.Null; } - + public bool IsNewLine() { return this.GetCharacter() == '\n'; diff --git a/src/Winium.Desktop.Driver/Input/KeyboardModifiers.cs b/src/Winium.Desktop.Driver/Input/KeyboardModifiers.cs index f6a25fb..62ba84f 100644 --- a/src/Winium.Desktop.Driver/Input/KeyboardModifiers.cs +++ b/src/Winium.Desktop.Driver/Input/KeyboardModifiers.cs @@ -2,45 +2,50 @@ { #region using - using System; - using System.Collections; using System.Collections.Generic; - using System.Linq; - using System.Text; - using System.Threading.Tasks; - - using WindowsInput.Native; using OpenQA.Selenium; + using WindowsInput.Native; + #endregion - class KeyboardModifiers : List + internal class KeyboardModifiers : List { - #region Priavte Static Fields - - private static readonly List Modifiers = new List() { Keys.Control, Keys.LeftControl, Keys.Shift, Keys.LeftShift, Keys.Alt, Keys.LeftAlt }; - private static readonly Dictionary ModifiersMap = new Dictionary() - { - {Keys.Control, VirtualKeyCode.CONTROL}, - {Keys.Shift, VirtualKeyCode.SHIFT}, - {Keys.Alt, VirtualKeyCode.MENU}, - }; + #region Static Fields + + private static readonly List Modifiers = new List + { + Keys.Control, + Keys.LeftControl, + Keys.Shift, + Keys.LeftShift, + Keys.Alt, + Keys.LeftAlt + }; + + private static readonly Dictionary ModifiersMap = + new Dictionary + { + { Keys.Control, VirtualKeyCode.CONTROL }, + { Keys.Shift, VirtualKeyCode.SHIFT }, + { Keys.Alt, VirtualKeyCode.MENU }, + }; #endregion - #region Public Static Methods and Operators + #region Public Methods and Operators - public static bool IsModifier(string key) + public static string GetKeyFromUnicode(char key) { - return KeyboardModifiers.Modifiers.Contains(key); + return Modifiers.Find(modifier => modifier[0] == key); } public static VirtualKeyCode GetVirtualKeyCode(string key) { VirtualKeyCode virtualKey; - if (KeyboardModifiers.ModifiersMap.TryGetValue(key, out virtualKey)) + if (ModifiersMap.TryGetValue(key, out virtualKey)) { return virtualKey; } @@ -48,9 +53,9 @@ public static VirtualKeyCode GetVirtualKeyCode(string key) return default(VirtualKeyCode); } - public static string GetKeyFromUnicode(char key) + public static bool IsModifier(string key) { - return KeyboardModifiers.Modifiers.Find(modifier => modifier[0] == key); + return Modifiers.Contains(key); } #endregion diff --git a/src/Winium.Desktop.Driver/Input/WiniumKeyboard.cs b/src/Winium.Desktop.Driver/Input/WiniumKeyboard.cs index 0c91a2e..68cb9b2 100644 --- a/src/Winium.Desktop.Driver/Input/WiniumKeyboard.cs +++ b/src/Winium.Desktop.Driver/Input/WiniumKeyboard.cs @@ -4,42 +4,33 @@ using System; using System.Collections.Generic; - using System.Diagnostics; using System.Linq; - using System.Text; - using System.Threading.Tasks; - - using WindowsInput.Native; using OpenQA.Selenium; using Winium.Cruciatus; - using Winium.Cruciatus.Core; using Winium.Cruciatus.Settings; #endregion - class WiniumKeyboard + internal class WiniumKeyboard { #region Fields - + private readonly KeyboardModifiers modifiers = new KeyboardModifiers(); #endregion - #region Methods and Operators + #region Constructors and Destructors public WiniumKeyboard(KeyboardSimulatorType keyboardSimulatorType) { CruciatusFactory.Settings.KeyboardSimulatorType = keyboardSimulatorType; } - public void SendKeys(char[] keysToSend) - { - List builder = keysToSend.Select(key => new KeyEvent(key)).ToList(); + #endregion - this.SendKeys(builder); - } + #region Public Methods and Operators public void KeyDown(string keyToPress) { @@ -55,6 +46,17 @@ public void KeyUp(string keyToRelease) CruciatusFactory.Keyboard.KeyUp(key); } + public void SendKeys(char[] keysToSend) + { + var builder = keysToSend.Select(key => new KeyEvent(key)).ToList(); + + this.SendKeys(builder); + } + + #endregion + + #region Methods + protected void ReleaseModifiers() { var tmp = this.modifiers.ToList(); @@ -65,7 +67,19 @@ protected void ReleaseModifiers() } } - private void SendKeys(List events) + private void PressOrReleaseModifier(string modifier) + { + if (this.modifiers.Contains(modifier)) + { + this.KeyUp(modifier); + } + else + { + this.KeyDown(modifier); + } + } + + private void SendKeys(IEnumerable events) { foreach (var keyEvent in events) { @@ -90,7 +104,7 @@ private void SendKeys(List events) private void Type(char key) { - String str = Convert.ToString(key); + string str = Convert.ToString(key); if (this.modifiers.Contains(Keys.LeftShift) || this.modifiers.Contains(Keys.Shift)) { @@ -100,18 +114,6 @@ private void Type(char key) CruciatusFactory.Keyboard.SendText(str); } - private void PressOrReleaseModifier(string modifier) - { - if (this.modifiers.Contains(modifier)) - { - this.KeyUp(modifier); - } - else - { - this.KeyDown(modifier); - } - } - #endregion } }