StatusBarTests.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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 = Application.Top;
  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. Application.Top.Add (sb);
  82. sb.OnDrawContent (sb.Bounds);
  83. var expected = @$"
  84. ^O Open {
  85. CM.Glyphs.VLine
  86. } Ctrl+Q to Quit!
  87. ";
  88. TestHelpers.AssertDriverContentsAre (expected, output);
  89. }
  90. [Fact]
  91. [AutoInitShutdown]
  92. public void Redraw_Output_CTRLQ ()
  93. {
  94. var sb = new StatusBar (
  95. new StatusItem []
  96. {
  97. new (KeyCode.CtrlMask | KeyCode.O, "~CTRL-O~ Open", null),
  98. new (KeyCode.CtrlMask | KeyCode.Q, "~CTRL-Q~ Quit", null)
  99. }
  100. );
  101. Application.Top.Add (sb);
  102. sb.OnDrawContent (sb.Bounds);
  103. var expected = @$"
  104. CTRL-O Open {
  105. CM.Glyphs.VLine
  106. } CTRL-Q Quit
  107. ";
  108. TestHelpers.AssertDriverContentsAre (expected, output);
  109. }
  110. [Fact]
  111. [AutoInitShutdown]
  112. public void Run_Action_With_Key_And_Mouse ()
  113. {
  114. var msg = "";
  115. var sb = new StatusBar (
  116. new StatusItem []
  117. {
  118. new (
  119. Application.QuitKey,
  120. $"{Application.QuitKey} to Quit",
  121. () => msg = "Quiting..."
  122. )
  123. }
  124. );
  125. Application.Top.Add (sb);
  126. var iteration = 0;
  127. Application.Iteration += (s, a) =>
  128. {
  129. if (iteration == 0)
  130. {
  131. Assert.Equal ("", msg);
  132. sb.NewKeyDownEvent (Key.Q.WithCtrl);
  133. }
  134. else if (iteration == 1)
  135. {
  136. Assert.Equal ("Quiting...", msg);
  137. msg = "";
  138. sb.OnMouseEvent (new MouseEvent { X = 1, Y = 24, Flags = MouseFlags.Button1Clicked });
  139. }
  140. else
  141. {
  142. Assert.Equal ("Quiting...", msg);
  143. Application.RequestStop ();
  144. }
  145. iteration++;
  146. };
  147. Application.Run ();
  148. }
  149. [Fact]
  150. public void StatusBar_Constructor_Default ()
  151. {
  152. var sb = new StatusBar ();
  153. Assert.Empty (sb.Items);
  154. Assert.False (sb.CanFocus);
  155. Assert.Equal (Colors.ColorSchemes ["Menu"], sb.ColorScheme);
  156. Assert.Equal (0, sb.X);
  157. Assert.Equal ("AnchorEnd(1)", sb.Y.ToString ());
  158. Assert.Equal (Dim.Fill (), sb.Width);
  159. Assert.Equal (1, sb.Height);
  160. var driver = new FakeDriver ();
  161. Application.Init (driver);
  162. sb = new StatusBar ();
  163. driver.SetCursorVisibility (CursorVisibility.Default);
  164. driver.GetCursorVisibility (out CursorVisibility cv);
  165. Assert.Equal (CursorVisibility.Default, cv);
  166. Assert.True (FakeConsole.CursorVisible);
  167. Application.Iteration += (s, a) =>
  168. {
  169. Assert.Equal (24, sb.Frame.Y);
  170. driver.SetWindowSize (driver.Cols, 15);
  171. Assert.Equal (14, sb.Frame.Y);
  172. sb.OnEnter (null);
  173. driver.GetCursorVisibility (out cv);
  174. Assert.Equal (CursorVisibility.Invisible, cv);
  175. Assert.False (FakeConsole.CursorVisible);
  176. Application.RequestStop ();
  177. };
  178. Application.Top.Add (sb);
  179. Application.Run ();
  180. Application.Shutdown ();
  181. }
  182. [Fact]
  183. public void StatusItem_Constructor ()
  184. {
  185. Application.Init ();
  186. var si = new StatusItem (Application.QuitKey, $"{Application.QuitKey} to Quit", null);
  187. Assert.Equal (KeyCode.CtrlMask | KeyCode.Q, si.Shortcut);
  188. Assert.Equal ($"{Application.QuitKey} to Quit", si.Title);
  189. Assert.Null (si.Action);
  190. si = new StatusItem (Application.QuitKey, $"{Application.QuitKey} to Quit", () => { });
  191. Assert.NotNull (si.Action);
  192. Application.Shutdown ();
  193. }
  194. }