StatusBarTests.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. using Xunit.Abstractions;
  2. namespace Terminal.Gui.ViewsTests;
  3. public class StatusBarTests
  4. {
  5. private readonly ITestOutputHelper output;
  6. public StatusBarTests (ITestOutputHelper output) { this.output = output; }
  7. [Fact]
  8. public void AddItemAt_RemoveItem_Replacing ()
  9. {
  10. var sb = new StatusBar (
  11. new StatusItem []
  12. {
  13. new (KeyCode.CtrlMask | KeyCode.Q, "~^O~ Open", null),
  14. new (KeyCode.CtrlMask | KeyCode.Q, "~^S~ Save", null),
  15. new (KeyCode.CtrlMask | KeyCode.Q, "~^Q~ Quit", null)
  16. }
  17. );
  18. sb.AddItemAt (2, new StatusItem (KeyCode.CtrlMask | KeyCode.Q, "~^C~ Close", null));
  19. Assert.Equal ("~^O~ Open", sb.Items [0].Title);
  20. Assert.Equal ("~^S~ Save", sb.Items [1].Title);
  21. Assert.Equal ("~^C~ Close", sb.Items [2].Title);
  22. Assert.Equal ("~^Q~ Quit", sb.Items [^1].Title);
  23. Assert.Equal ("~^S~ Save", sb.RemoveItem (1).Title);
  24. Assert.Equal ("~^O~ Open", sb.Items [0].Title);
  25. Assert.Equal ("~^C~ Close", sb.Items [1].Title);
  26. Assert.Equal ("~^Q~ Quit", sb.Items [^1].Title);
  27. sb.Items [1] = new StatusItem (KeyCode.CtrlMask | KeyCode.A, "~^A~ Save As", null);
  28. Assert.Equal ("~^O~ Open", sb.Items [0].Title);
  29. Assert.Equal ("~^A~ Save As", sb.Items [1].Title);
  30. Assert.Equal ("~^Q~ Quit", sb.Items [^1].Title);
  31. }
  32. [Fact]
  33. [AutoInitShutdown]
  34. public void CanExecute_ProcessHotKey ()
  35. {
  36. Window win = null;
  37. var statusBar = new StatusBar (
  38. new StatusItem []
  39. {
  40. new (
  41. KeyCode.CtrlMask | KeyCode.N,
  42. "~^N~ New",
  43. New,
  44. CanExecuteNew
  45. ),
  46. new (
  47. KeyCode.CtrlMask | KeyCode.C,
  48. "~^C~ Close",
  49. Close,
  50. CanExecuteClose
  51. )
  52. }
  53. );
  54. Toplevel top = new ();
  55. top.Add (statusBar);
  56. bool CanExecuteNew () { return win == null; }
  57. void New () { win = new Window (); }
  58. bool CanExecuteClose () { return win != null; }
  59. void Close () { win = null; }
  60. Application.Begin (top);
  61. Assert.Null (win);
  62. Assert.True (CanExecuteNew ());
  63. Assert.False (CanExecuteClose ());
  64. Assert.True (top.NewKeyDownEvent (Key.N.WithCtrl));
  65. Application.MainLoop.RunIteration ();
  66. Assert.NotNull (win);
  67. Assert.False (CanExecuteNew ());
  68. Assert.True (CanExecuteClose ());
  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. }
  91. [Fact]
  92. [AutoInitShutdown]
  93. public void Redraw_Output_CTRLQ ()
  94. {
  95. var sb = new StatusBar (
  96. new StatusItem []
  97. {
  98. new (KeyCode.CtrlMask | KeyCode.O, "~CTRL-O~ Open", null),
  99. new (KeyCode.CtrlMask | KeyCode.Q, "~CTRL-Q~ Quit", null)
  100. }
  101. );
  102. var top = new Toplevel ();
  103. top.Add (sb);
  104. sb.OnDrawContent (sb.Viewport);
  105. var expected = @$"
  106. CTRL-O Open {
  107. CM.Glyphs.VLine
  108. } CTRL-Q Quit
  109. ";
  110. TestHelpers.AssertDriverContentsAre (expected, output);
  111. }
  112. [Fact]
  113. [AutoInitShutdown]
  114. public void Run_Action_With_Key_And_Mouse ()
  115. {
  116. var msg = "";
  117. var sb = new StatusBar (
  118. new StatusItem []
  119. {
  120. new (
  121. Application.QuitKey,
  122. $"{Application.QuitKey} to Quit",
  123. () => msg = "Quiting..."
  124. )
  125. }
  126. );
  127. var iteration = 0;
  128. Application.Iteration += (s, a) =>
  129. {
  130. if (iteration == 0)
  131. {
  132. Assert.Equal ("", msg);
  133. sb.NewKeyDownEvent (Key.Q.WithCtrl);
  134. }
  135. else if (iteration == 1)
  136. {
  137. Assert.Equal ("Quiting...", msg);
  138. msg = "";
  139. sb.NewMouseEvent (new MouseEvent { X = 1, Y = 24, Flags = MouseFlags.Button1Clicked });
  140. }
  141. else
  142. {
  143. Assert.Equal ("Quiting...", msg);
  144. Application.RequestStop ();
  145. }
  146. iteration++;
  147. };
  148. Application.Run ().Dispose ();
  149. }
  150. [Fact]
  151. public void StatusBar_Constructor_Default ()
  152. {
  153. var sb = new StatusBar ();
  154. Assert.Empty (sb.Items);
  155. Assert.False (sb.CanFocus);
  156. Assert.Equal (Colors.ColorSchemes ["Menu"], sb.ColorScheme);
  157. Assert.Equal (0, sb.X);
  158. Assert.Equal ("AnchorEnd(1)", sb.Y.ToString ());
  159. Assert.Equal (Dim.Fill (), sb.Width);
  160. Assert.Equal (1, sb.Height);
  161. var driver = new FakeDriver ();
  162. Application.Init (driver);
  163. sb = new StatusBar ();
  164. driver.SetCursorVisibility (CursorVisibility.Default);
  165. driver.GetCursorVisibility (out CursorVisibility cv);
  166. Assert.Equal (CursorVisibility.Default, cv);
  167. Assert.True (FakeConsole.CursorVisible);
  168. Application.Iteration += (s, a) =>
  169. {
  170. Assert.Equal (24, sb.Frame.Y);
  171. driver.SetWindowSize (driver.Cols, 15);
  172. Assert.Equal (14, sb.Frame.Y);
  173. sb.OnEnter (null);
  174. driver.GetCursorVisibility (out cv);
  175. Assert.Equal (CursorVisibility.Invisible, cv);
  176. Assert.False (FakeConsole.CursorVisible);
  177. Application.RequestStop ();
  178. };
  179. var top = new Toplevel ();
  180. top.Add (sb);
  181. Application.Run (top);
  182. top.Dispose ();
  183. Application.Shutdown ();
  184. }
  185. [Fact]
  186. public void StatusItem_Constructor ()
  187. {
  188. Application.Init ();
  189. var si = new StatusItem (Application.QuitKey, $"{Application.QuitKey} to Quit", null);
  190. Assert.Equal (KeyCode.CtrlMask | KeyCode.Q, si.Shortcut);
  191. Assert.Equal ($"{Application.QuitKey} to Quit", si.Title);
  192. Assert.Null (si.Action);
  193. si = new StatusItem (Application.QuitKey, $"{Application.QuitKey} to Quit", () => { });
  194. Assert.NotNull (si.Action);
  195. Application.Shutdown ();
  196. }
  197. }