StatusBarTests.cs 7.1 KB

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