PageRenderTime 34ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/InputSystem/Tests/KeyboardTests.cs

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