Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
skyline-gleb committed Jun 29, 2015
1 parent 7061a7f commit 49a998f
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 79 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
namespace WpfTestApplication.Tests
{
#region using

using System.Linq;

using NUnit.Framework;
Expand All @@ -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
}
}
4 changes: 2 additions & 2 deletions src/Winium.Desktop.Driver/Automator/Automator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/Winium.Desktop.Driver/Automator/Capabilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
using System;
using System.Linq;

using Winium.Cruciatus;
using Winium.Desktop.Driver.Input;

#endregion

internal class SendKeysToActiveElementExecutor : CommandExecutorBase
Expand Down
33 changes: 16 additions & 17 deletions src/Winium.Desktop.Driver/Input/KeyEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,53 +2,52 @@
{
#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)
{
this.character = 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';
Expand Down
51 changes: 28 additions & 23 deletions src/Winium.Desktop.Driver/Input/KeyboardModifiers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,55 +2,60 @@
{
#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<string>
internal class KeyboardModifiers : List<string>
{
#region Priavte Static Fields

private static readonly List<string> Modifiers = new List<string>() { Keys.Control, Keys.LeftControl, Keys.Shift, Keys.LeftShift, Keys.Alt, Keys.LeftAlt };
private static readonly Dictionary<string, VirtualKeyCode> ModifiersMap = new Dictionary<string, VirtualKeyCode>()
{
{Keys.Control, VirtualKeyCode.CONTROL},
{Keys.Shift, VirtualKeyCode.SHIFT},
{Keys.Alt, VirtualKeyCode.MENU},
};
#region Static Fields

private static readonly List<string> Modifiers = new List<string>
{
Keys.Control,
Keys.LeftControl,
Keys.Shift,
Keys.LeftShift,
Keys.Alt,
Keys.LeftAlt
};

private static readonly Dictionary<string, VirtualKeyCode> ModifiersMap =
new Dictionary<string, VirtualKeyCode>
{
{ 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;
}

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
Expand Down
58 changes: 30 additions & 28 deletions src/Winium.Desktop.Driver/Input/WiniumKeyboard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<KeyEvent> builder = keysToSend.Select(key => new KeyEvent(key)).ToList();
#endregion

this.SendKeys(builder);
}
#region Public Methods and Operators

public void KeyDown(string keyToPress)
{
Expand All @@ -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();
Expand All @@ -65,7 +67,19 @@ protected void ReleaseModifiers()
}
}

private void SendKeys(List<KeyEvent> events)
private void PressOrReleaseModifier(string modifier)
{
if (this.modifiers.Contains(modifier))
{
this.KeyUp(modifier);
}
else
{
this.KeyDown(modifier);
}
}

private void SendKeys(IEnumerable<KeyEvent> events)
{
foreach (var keyEvent in events)
{
Expand All @@ -90,7 +104,7 @@ private void SendKeys(List<KeyEvent> 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))
{
Expand All @@ -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
}
}

0 comments on commit 49a998f

Please sign in to comment.