PageRenderTime 84ms CodeModel.GetById 41ms app.highlight 12ms RepoModel.GetById 28ms app.codeStats 0ms

/InputSystem/Tests/KeyboardTests.cs

#
C# | 211 lines | 166 code | 14 blank | 31 comment | 6 complexity | c35f7af51525b1e38abe2fc1e9c34330 MD5 | raw file
  1using System;
  2using System.Diagnostics;
  3using System.IO;
  4using Delta.Engine;
  5using Delta.InputSystem.Devices;
  6using Delta.Rendering.Basics.Drawing;
  7using Delta.Rendering.Basics.Fonts;
  8using Delta.Utilities;
  9using Delta.Utilities.Datatypes;
 10using Delta.Utilities.Helpers;
 11using NUnit.Framework;
 12
 13namespace Delta.InputSystem.Tests
 14{
 15	/// <summary>
 16	/// Keyboard tests
 17	/// </summary>
 18	[Category("Visual")]
 19	internal class KeyboardTests
 20	{
 21		#region CheckKeyboardStates (Static)
 22		/// <summary>
 23		/// Check Keyboard states
 24		/// </summary>
 25		[Test]
 26		public static void CheckKeyboardStates()
 27		{
 28			Application.Start(delegate
 29			{
 30				Font.Default.Draw(
 31					"Keyboard IsConnected " + Input.Keyboard.IsConnected,
 32					Rectangle.FromCenter(new Point(0.5f, 0.45f), Size.Half));
 33				Font.Default.Draw(
 34					"A is " + Input.Keyboard.GetState(InputButton.A),
 35					Rectangle.FromCenter(new Point(0.5f, 0.50f), Size.Half));
 36				Font.Default.Draw(
 37					"CursorUp is " + Input.Keyboard.GetState(InputButton.CursorUp),
 38					Rectangle.FromCenter(new Point(0.5f, 0.55f), Size.Half));
 39			});
 40		}
 41		#endregion
 42
 43		#region TestVirtualKeyboardCursorPosition (Static)
 44		/// <summary>
 45		/// Test virtual keyboard cursor position
 46		/// </summary>
 47		[Test]
 48		public static void TestVirtualKeyboardCursorPosition()
 49		{
 50			Application.Start(delegate
 51			{
 52				Font.Default.Draw(
 53					"Keyboard Position " + Input.Keyboard.Position,
 54					Rectangle.FromCenter(new Point(0.5f, 0.5f), Size.Half));
 55				Font.Default.Draw(
 56					"Cursor Movement " + Input.Keyboard.CursorMovement,
 57					Rectangle.FromCenter(new Point(0.5f, 0.55f), Size.Half));
 58				// Show the virtual keyboard cursor
 59				Circle.DrawOutline(Input.Keyboard.Position, 0.025f, Color.Red);
 60			});
 61		}
 62		#endregion
 63
 64		#region TestCtrlAltShift (Static)
 65		/// <summary>
 66		/// TestCtrlAltShift, needs an extra test because Control, Alt and Shift
 67		/// are combined keys, we will get them separately from the input classes.
 68		/// </summary>
 69		[Test]
 70		public static void TestCtrlAltShift()
 71		{
 72			Application.Start(delegate
 73			{
 74				Font.Default.Draw(
 75					"WindowsKey " + Input.Keyboard.GetState(InputButton.WindowsKey),
 76					Rectangle.FromCenter(new Point(0.5f, 0.2f), Size.Half));
 77				Font.Default.Draw(
 78					"LeftWindows " + Input.Keyboard.GetState(InputButton.LeftWindows),
 79					Rectangle.FromCenter(new Point(0.5f, 0.25f), Size.Half));
 80				Font.Default.Draw(
 81					"RightWindows " + Input.Keyboard.GetState(InputButton.RightWindows),
 82					Rectangle.FromCenter(new Point(0.5f, 0.3f), Size.Half));
 83				Font.Default.Draw(
 84					"Control " + Input.Keyboard.GetState(InputButton.Control),
 85					Rectangle.FromCenter(new Point(0.5f, 0.35f), Size.Half));
 86				Font.Default.Draw(
 87					"LeftControl " + Input.Keyboard.GetState(InputButton.LeftControl),
 88					Rectangle.FromCenter(new Point(0.5f, 0.4f), Size.Half));
 89				Font.Default.Draw(
 90					"RightControl " + Input.Keyboard.GetState(InputButton.RightControl),
 91					Rectangle.FromCenter(new Point(0.5f, 0.45f), Size.Half));
 92				Font.Default.Draw(
 93					"Alt " + Input.Keyboard.GetState(InputButton.Alt),
 94					Rectangle.FromCenter(new Point(0.5f, 0.5f), Size.Half));
 95				Font.Default.Draw(
 96					"LeftAlt " + Input.Keyboard.GetState(InputButton.LeftAlt),
 97					Rectangle.FromCenter(new Point(0.5f, 0.55f), Size.Half));
 98				Font.Default.Draw(
 99					"RightAlt " + Input.Keyboard.GetState(InputButton.RightAlt),
100					Rectangle.FromCenter(new Point(0.5f, 0.6f), Size.Half));
101				Font.Default.Draw(
102					"Shift " + Input.Keyboard.GetState(InputButton.Shift),
103					Rectangle.FromCenter(new Point(0.5f, 0.65f), Size.Half));
104				Font.Default.Draw(
105					"LeftShift " + Input.Keyboard.GetState(InputButton.LeftShift),
106					Rectangle.FromCenter(new Point(0.5f, 0.7f), Size.Half));
107				Font.Default.Draw(
108					"RightShift " + Input.Keyboard.GetState(InputButton.RightShift),
109					Rectangle.FromCenter(new Point(0.5f, 0.75f), Size.Half));
110			});
111		}
112		#endregion
113
114		#region ResearchStartOSK (Static)
115		/// <summary>
116		/// Research start OSK
117		/// </summary>
118		[Test]
119		public static void ResearchStartOSK()
120		{
121			string winDirectory = Environment.GetEnvironmentVariable("WINDIR");
122
123			// Start with the default path of Windows7 (and Vista ? too)
124			string oskFilePath = Path.Combine(winDirectory, "system32", "osk.exe");
125
126			Assert.True(FileHelper.Exists(oskFilePath));
127
128			Process oskProcess = null;
129			Application.Start(delegate
130			{
131				if (Input.Keyboard.SpaceReleased)
132				{
133					if (oskProcess != null)
134					{
135						oskProcess.Kill();
136						oskProcess = null;
137					} // if
138
139					oskProcess = Process.Start(oskFilePath);
140				} // if
141			});
142		}
143		#endregion
144
145		#region ShowOnScreenKeyboard (Static)
146		/// <summary>
147		/// Tests on screen keyboard, works with Windows and Xna Input module on
148		/// PC and on all mobile and console platforms.
149		/// </summary>
150		[Test]
151		public static void ShowOnScreenKeyboard()
152		{
153			BaseKeyboard keyboard = Input.Keyboard;
154
155			keyboard.ForceShowOnScreenKeyboard();
156			Application.Start(delegate
157			{
158				if (keyboard.CursorUpReleased)
159				{
160					keyboard.ForceShowOnScreenKeyboard();
161				} // if
162				if (keyboard.CursorDownReleased)
163				{
164					keyboard.HideOnScreenKeyboard();
165				} // if
166
167				Font.Default.Draw(
168					"Press 'CursorUp' to show and 'CursorDown' to hide the " +
169					"OnScreen keyboard again",
170					Rectangle.FromCenter(new Point(0.5f, 0.5f), Size.Half));
171			});
172		}
173		#endregion
174
175		#region TestTextboxHandleInput (Static)
176		/// <summary>
177		/// Test textbox handle input, just displays a label and handles it like
178		/// a textbox, which is input controlled by the Input.Keyboard class.
179		/// This test is limited to 10 fps and especially useful to keyboard event
180		/// handling via WindowsKeyboard (as opposed to the polling XnaKeyboard)
181		/// </summary>
182		[Test]
183		public static void TestTextboxHandleInput()
184		{
185			// Limit to 10 fps to really test if we can still type text!
186			//works even fine with 1 fps: Settings.Debug.LimitFramerateNumber = 1;
187			Settings.Extra.LimitFramerateNumber = 10;
188
189			string someText = "";
190			Application.Start(delegate
191			{
192				// Show text and some own little blinking cursor logic
193				Font.Default.Draw(
194					"Enter Text: " + someText +
195					((int)(Time.Milliseconds / 200) % 2 == 0
196					 	? "|"
197					 	: ""),
198					Rectangle.FromCenter(new Point(0.5f, 0.35f), Size.Half));
199				Input.Keyboard.HandleInput(ref someText);
200
201				Font.Default.Draw(
202					"BackSpace " + Input.Keyboard.GetState(InputButton.BackSpace),
203					Rectangle.FromCenter(new Point(0.5f, 0.55f), Size.Half));
204				Font.Default.Draw(
205					"Space " + Input.Keyboard.SpaceIsPressed,
206					Rectangle.FromCenter(new Point(0.5f, 0.6f), Size.Half));
207			});
208		}
209		#endregion
210	}
211}