StatusBarTests.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. using Xunit.Abstractions;
  2. namespace Terminal.Gui.ViewsTests;
  3. #if V2_STATUSBAR
  4. public class StatusBarTests (ITestOutputHelper output)
  5. {
  6. [Fact]
  7. public void AddItemAt_RemoveItem_Replacing ()
  8. {
  9. var sb = new StatusBar (
  10. new StatusItem []
  11. {
  12. new (KeyCode.CtrlMask | KeyCode.Q, "~^O~ Open", null),
  13. new (KeyCode.CtrlMask | KeyCode.Q, "~^S~ Save", null),
  14. new (KeyCode.CtrlMask | KeyCode.Q, "~^Q~ Quit", null)
  15. }
  16. );
  17. sb.AddShortcutAt (2, new (KeyCode.CtrlMask | KeyCode.Q, "~^C~ Close", null));
  18. Assert.Equal ("~^O~ Open", sb.Items [0].Title);
  19. Assert.Equal ("~^S~ Save", sb.Items [1].Title);
  20. Assert.Equal ("~^C~ Close", sb.Items [2].Title);
  21. Assert.Equal ("~^Q~ Quit", sb.Items [^1].Title);
  22. Assert.Equal ("~^S~ Save", sb.RemoveItem (1).Title);
  23. Assert.Equal ("~^O~ Open", sb.Items [0].Title);
  24. Assert.Equal ("~^C~ Close", sb.Items [1].Title);
  25. Assert.Equal ("~^Q~ Quit", sb.Items [^1].Title);
  26. sb.Items [1] = new (KeyCode.CtrlMask | KeyCode.A, "~^A~ Save As", null);
  27. Assert.Equal ("~^O~ Open", sb.Items [0].Title);
  28. Assert.Equal ("~^A~ Save As", sb.Items [1].Title);
  29. Assert.Equal ("~^Q~ Quit", sb.Items [^1].Title);
  30. }
  31. [Fact]
  32. [AutoInitShutdown]
  33. public void CanExecute_ProcessHotKey ()
  34. {
  35. Window win = null;
  36. var statusBar = new StatusBar (
  37. new StatusItem []
  38. {
  39. new (
  40. KeyCode.CtrlMask | KeyCode.N,
  41. "~^N~ New",
  42. New,
  43. CanExecuteNew
  44. ),
  45. new (
  46. KeyCode.CtrlMask | KeyCode.C,
  47. "~^C~ Close",
  48. Close,
  49. CanExecuteClose
  50. )
  51. }
  52. );
  53. Toplevel top = new ();
  54. top.Add (statusBar);
  55. bool CanExecuteNew () { return win == null; }
  56. void New () { win = new (); }
  57. bool CanExecuteClose () { return win != null; }
  58. void Close () { win = null; }
  59. Application.Begin (top);
  60. Assert.Null (win);
  61. Assert.True (CanExecuteNew ());
  62. Assert.False (CanExecuteClose ());
  63. Assert.True (top.NewKeyDownEvent (Key.N.WithCtrl));
  64. Application.MainLoop.RunIteration ();
  65. Assert.NotNull (win);
  66. Assert.False (CanExecuteNew ());
  67. Assert.True (CanExecuteClose ());
  68. top.Dispose ();
  69. }
  70. [Fact]
  71. [AutoInitShutdown]
  72. public void Redraw_Output ()
  73. {
  74. var sb = new StatusBar (
  75. new StatusItem []
  76. {
  77. new (KeyCode.CtrlMask | KeyCode.O, "~^O~ Open", null),
  78. new (Application.QuitKey, $"{Application.QuitKey} to Quit!", null)
  79. }
  80. );
  81. var top = new Toplevel ();
  82. top.Add (sb);
  83. sb.OnDrawContent (sb.Viewport);
  84. var expected = @$"
  85. ^O Open {
  86. CM.Glyphs.VLine
  87. } Ctrl+Q to Quit!
  88. ";
  89. TestHelpers.AssertDriverContentsAre (expected, output);
  90. top.Dispose ();
  91. }
  92. [Fact]
  93. [AutoInitShutdown]
  94. public void Redraw_Output_CTRLQ ()
  95. {
  96. var sb = new StatusBar (
  97. new StatusItem []
  98. {
  99. new (KeyCode.CtrlMask | KeyCode.O, "~CTRL-O~ Open", null),
  100. new (KeyCode.CtrlMask | KeyCode.Q, "~CTRL-Q~ Quit", null)
  101. }
  102. );
  103. var top = new Toplevel ();
  104. top.Add (sb);
  105. sb.OnDrawContent (sb.Viewport);
  106. var expected = @$"
  107. CTRL-O Open {
  108. CM.Glyphs.VLine
  109. } CTRL-Q Quit
  110. ";
  111. TestHelpers.AssertDriverContentsAre (expected, output);
  112. top.Dispose ();
  113. }
  114. [Fact]
  115. [AutoInitShutdown]
  116. public void Run_Action_With_Key_And_Mouse ()
  117. {
  118. var msg = "";
  119. var sb = new StatusBar (
  120. new StatusItem []
  121. {
  122. new (
  123. Application.QuitKey,
  124. $"{Application.QuitKey} to Quit",
  125. () => msg = "Quiting..."
  126. )
  127. }
  128. );
  129. var iteration = 0;
  130. Application.Iteration += (s, a) =>
  131. {
  132. if (iteration == 0)
  133. {
  134. Assert.Equal ("", msg);
  135. sb.NewKeyDownEvent (Key.Q.WithCtrl);
  136. }
  137. else if (iteration == 1)
  138. {
  139. Assert.Equal ("Quiting...", msg);
  140. msg = "";
  141. sb.NewMouseEvent (new() { Position = new (1, 24), Flags = MouseFlags.Button1Clicked });
  142. }
  143. else
  144. {
  145. Assert.Equal ("Quiting...", msg);
  146. Application.RequestStop ();
  147. }
  148. iteration++;
  149. };
  150. Application.Run ().Dispose ();
  151. }
  152. [Fact]
  153. public void StatusBar_Constructor_Default ()
  154. {
  155. var sb = new StatusBar ();
  156. Assert.Empty (sb.Items);
  157. Assert.False (sb.CanFocus);
  158. Assert.Equal (Colors.ColorSchemes ["Menu"], sb.ColorScheme);
  159. Assert.Equal (0, sb.X);
  160. Assert.Equal ("AnchorEnd()", sb.Y.ToString ());
  161. Assert.Equal (Dim.Fill (), sb.Width);
  162. Assert.Equal (1, sb.Height);
  163. }
  164. [Fact]
  165. public void StatusItem_Constructor ()
  166. {
  167. Application.Init ();
  168. var si = new StatusItem (Application.QuitKey, $"{Application.QuitKey} to Quit", null);
  169. Assert.Equal (KeyCode.CtrlMask | KeyCode.Q, si.Shortcut);
  170. Assert.Equal ($"{Application.QuitKey} to Quit", si.Title);
  171. Assert.Null (si.Action);
  172. si = new (Application.QuitKey, $"{Application.QuitKey} to Quit", () => { });
  173. Assert.NotNull (si.Action);
  174. Application.Shutdown ();
  175. }
  176. }
  177. #endif