ButtonTests.cs 7.0 KB

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