ButtonTests.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Xunit;
  7. namespace Terminal.Gui.Views {
  8. public class ButtonTests {
  9. [Fact, AutoInitShutdown]
  10. public void Constructors_Defaults ()
  11. {
  12. var btn = new Button ();
  13. Assert.Equal (string.Empty, btn.Text);
  14. Application.Top.Add (btn);
  15. btn.Redraw (btn.Bounds);
  16. Assert.Equal ("[ ]", GetContents (btn.Bounds.Width));
  17. Assert.False (btn.IsDefault);
  18. Assert.Equal (TextAlignment.Centered, btn.TextAlignment);
  19. Assert.Equal ('_', btn.HotKeySpecifier);
  20. Assert.True (btn.CanFocus);
  21. Assert.Equal (new Rect (0, 0, 4, 1), btn.Frame);
  22. Assert.Equal (Key.Null, btn.HotKey);
  23. btn = new Button ("Test", true);
  24. Assert.Equal ("Test", btn.Text);
  25. Application.Top.Add (btn);
  26. btn.Redraw (btn.Bounds);
  27. Assert.Equal ("[◦ Test ◦]", GetContents (btn.Bounds.Width));
  28. Assert.True (btn.IsDefault);
  29. Assert.Equal (TextAlignment.Centered, btn.TextAlignment);
  30. Assert.Equal ('_', btn.HotKeySpecifier);
  31. Assert.True (btn.CanFocus);
  32. Assert.Equal (new Rect (0, 0, 10, 1), btn.Frame);
  33. Assert.Equal (Key.T, btn.HotKey);
  34. btn = new Button (3, 4, "Test", true);
  35. Assert.Equal ("Test", btn.Text);
  36. Application.Top.Add (btn);
  37. btn.Redraw (btn.Bounds);
  38. Assert.Equal ("[◦ Test ◦]", GetContents (btn.Bounds.Width));
  39. Assert.True (btn.IsDefault);
  40. Assert.Equal (TextAlignment.Centered, btn.TextAlignment);
  41. Assert.Equal ('_', btn.HotKeySpecifier);
  42. Assert.True (btn.CanFocus);
  43. Assert.Equal (new Rect (3, 4, 10, 1), btn.Frame);
  44. Assert.Equal (Key.T, btn.HotKey);
  45. }
  46. private string GetContents (int width)
  47. {
  48. string output = "";
  49. for (int i = 0; i < width; i++) {
  50. output += (char)Application.Driver.Contents [0, i, 0];
  51. }
  52. return output;
  53. }
  54. [Fact]
  55. [AutoInitShutdown]
  56. public void KeyBindings_Command ()
  57. {
  58. var clicked = false;
  59. Button btn = new Button ("Test");
  60. btn.Clicked += () => clicked = true;
  61. Application.Top.Add (btn);
  62. Application.Begin (Application.Top);
  63. Assert.Equal (Key.T, btn.HotKey);
  64. Assert.False (btn.ProcessHotKey (new KeyEvent (Key.T, new KeyModifiers ())));
  65. Assert.False (clicked);
  66. Assert.True (btn.ProcessHotKey (new KeyEvent (Key.T | Key.AltMask, new KeyModifiers () { Alt = true })));
  67. Assert.True (clicked);
  68. clicked = false;
  69. Assert.False (btn.IsDefault);
  70. Assert.False (btn.ProcessColdKey (new KeyEvent (Key.Enter, new KeyModifiers ())));
  71. Assert.False (clicked);
  72. btn.IsDefault = true;
  73. Assert.True (btn.ProcessColdKey (new KeyEvent (Key.Enter, new KeyModifiers ())));
  74. Assert.True (clicked);
  75. clicked = false;
  76. Assert.True (btn.ProcessColdKey (new KeyEvent (Key.AltMask | Key.T, new KeyModifiers ())));
  77. Assert.True (clicked);
  78. clicked = false;
  79. Assert.True (btn.ProcessKey (new KeyEvent (Key.Enter, new KeyModifiers ())));
  80. Assert.True (clicked);
  81. clicked = false;
  82. Assert.True (btn.ProcessKey (new KeyEvent (Key.Space, new KeyModifiers ())));
  83. Assert.True (clicked);
  84. clicked = false;
  85. Assert.True (btn.ProcessKey (new KeyEvent ((Key)'t', new KeyModifiers ())));
  86. Assert.True (clicked);
  87. clicked = false;
  88. Assert.True (btn.ProcessKey (new KeyEvent (Key.Space | btn.HotKey, new KeyModifiers ())));
  89. Assert.True (clicked);
  90. btn.Text = "Te_st";
  91. clicked = false;
  92. Assert.True (btn.ProcessKey (new KeyEvent (Key.Space | btn.HotKey, new KeyModifiers ())));
  93. Assert.True (clicked);
  94. }
  95. [Fact]
  96. [AutoInitShutdown]
  97. public void ChangeHotKey ()
  98. {
  99. var clicked = false;
  100. Button btn = new Button ("Test");
  101. btn.Clicked += () => clicked = true;
  102. Application.Top.Add (btn);
  103. Application.Begin (Application.Top);
  104. Assert.Equal (Key.T, btn.HotKey);
  105. Assert.False (btn.ProcessHotKey (new KeyEvent (Key.T, new KeyModifiers ())));
  106. Assert.False (clicked);
  107. Assert.True (btn.ProcessHotKey (new KeyEvent (Key.T | Key.AltMask, new KeyModifiers () { Alt = true })));
  108. Assert.True (clicked);
  109. clicked = false;
  110. btn.HotKey = Key.E;
  111. Assert.True (btn.ProcessHotKey (new KeyEvent (Key.E | Key.AltMask, new KeyModifiers () { Alt = true })));
  112. Assert.True (clicked);
  113. }
  114. /// <summary>
  115. /// This test demonstrates how to change the activation key for Button
  116. /// as described in the README.md keyboard handling section
  117. /// </summary>
  118. [Fact]
  119. [AutoInitShutdown]
  120. public void KeyBindingExample ()
  121. {
  122. int pressed = 0;
  123. var btn = new Button ("Press Me");
  124. btn.Clicked += () => pressed++;
  125. // The Button class supports the Accept command
  126. Assert.Contains (Command.Accept, btn.GetSupportedCommands ());
  127. Application.Top.Add (btn);
  128. Application.Begin (Application.Top);
  129. // default keybinding is Enter which results in keypress
  130. Application.Driver.SendKeys ('\n', ConsoleKey.Enter, false, false, false);
  131. Assert.Equal (1, pressed);
  132. // remove the default keybinding (Enter)
  133. btn.ClearKeybinding (Command.Accept);
  134. // After clearing the default keystroke the Enter button no longer does anything for the Button
  135. Application.Driver.SendKeys ('\n', ConsoleKey.Enter, false, false, false);
  136. Assert.Equal (1, pressed);
  137. // Set a new binding of b for the click (Accept) event
  138. btn.AddKeyBinding (Key.b, Command.Accept);
  139. // now pressing B should call the button click event
  140. Application.Driver.SendKeys ('b', ConsoleKey.B, false, false, false);
  141. Assert.Equal (2, pressed);
  142. }
  143. [Fact]
  144. public void TestAssignTextToButton ()
  145. {
  146. View b = new Button ();
  147. b.Text = "heya";
  148. Assert.Equal ("heya", b.Text);
  149. // with cast
  150. Assert.Equal ("heya", ((Button)b).Text);
  151. }
  152. [Fact]
  153. public void Setting_Empty_Text_Sets_HoKey_To_KeyNull ()
  154. {
  155. var btn = new Button ("Test");
  156. Assert.Equal ("Test", btn.Text);
  157. Assert.Equal (Key.T, btn.HotKey);
  158. btn.Text = string.Empty;
  159. Assert.Equal ("", btn.Text);
  160. Assert.Equal (Key.Null, btn.HotKey);
  161. btn.Text = "Te_st";
  162. Assert.Equal ("Te_st", btn.Text);
  163. Assert.Equal (Key.S, btn.HotKey);
  164. }
  165. }
  166. }