WindowTests.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. using Xunit.Abstractions;
  2. namespace Terminal.Gui.ViewsTests;
  3. public class WindowTests
  4. {
  5. private readonly ITestOutputHelper _output;
  6. public WindowTests (ITestOutputHelper output) { _output = output; }
  7. [Fact]
  8. [AutoInitShutdown]
  9. public void Activating_MenuBar_By_Alt_Key_Does_Not_Throw ()
  10. {
  11. var menu = new MenuBar
  12. {
  13. Menus =
  14. [
  15. new MenuBarItem ("Child", new MenuItem [] { new ("_Create Child", "", null) })
  16. ]
  17. };
  18. var win = new Window ();
  19. win.Add (menu);
  20. var top = new Toplevel ();
  21. top.Add (win);
  22. Application.Begin (top);
  23. Exception exception = Record.Exception (() => win.NewKeyDownEvent (KeyCode.AltMask));
  24. Assert.Null (exception);
  25. top.Dispose ();
  26. }
  27. [Fact]
  28. [AutoInitShutdown]
  29. public void MenuBar_And_StatusBar_Inside_Window ()
  30. {
  31. var menu = new MenuBar
  32. {
  33. Menus =
  34. [
  35. new MenuBarItem ("File", new MenuItem [] { new ("Open", "", null), new ("Quit", "", null) }),
  36. new MenuBarItem (
  37. "Edit",
  38. new MenuItem [] { new ("Copy", "", null) }
  39. )
  40. ]
  41. };
  42. var sb = new StatusBar (
  43. new StatusItem []
  44. {
  45. new ((KeyCode)Key.Q.WithCtrl, "~^Q~ Quit", null),
  46. new ((KeyCode)Key.O.WithCtrl, "~^O~ Open", null),
  47. new ((KeyCode)Key.C.WithCtrl, "~^C~ Copy", null)
  48. }
  49. );
  50. var fv = new FrameView { Y = 1, Width = Dim.Fill (), Height = Dim.Fill (1), Title = "Frame View" };
  51. var win = new Window ();
  52. win.Add (menu, sb, fv);
  53. Toplevel top = new ();
  54. top.Add (win);
  55. Application.Begin (top);
  56. ((FakeDriver)Application.Driver).SetBufferSize (20, 10);
  57. TestHelpers.AssertDriverContentsWithFrameAre (
  58. @"
  59. ┌──────────────────┐
  60. │ File Edit │
  61. │┌┤Frame View├────┐│
  62. ││ ││
  63. ││ ││
  64. ││ ││
  65. ││ ││
  66. │└────────────────┘│
  67. │ ^Q Quit │ ^O Open│
  68. └──────────────────┘",
  69. _output
  70. );
  71. ((FakeDriver)Application.Driver).SetBufferSize (40, 20);
  72. TestHelpers.AssertDriverContentsWithFrameAre (
  73. @"
  74. ┌──────────────────────────────────────┐
  75. │ File Edit │
  76. │┌┤Frame View├────────────────────────┐│
  77. ││ ││
  78. ││ ││
  79. ││ ││
  80. ││ ││
  81. ││ ││
  82. ││ ││
  83. ││ ││
  84. ││ ││
  85. ││ ││
  86. ││ ││
  87. ││ ││
  88. ││ ││
  89. ││ ││
  90. ││ ││
  91. │└────────────────────────────────────┘│
  92. │ ^Q Quit │ ^O Open │ ^C Copy │
  93. └──────────────────────────────────────┘",
  94. _output
  95. );
  96. ((FakeDriver)Application.Driver).SetBufferSize (20, 10);
  97. TestHelpers.AssertDriverContentsWithFrameAre (
  98. @"
  99. ┌──────────────────┐
  100. │ File Edit │
  101. │┌┤Frame View├────┐│
  102. ││ ││
  103. ││ ││
  104. ││ ││
  105. ││ ││
  106. │└────────────────┘│
  107. │ ^Q Quit │ ^O Open│
  108. └──────────────────┘",
  109. _output
  110. );
  111. top.Dispose ();
  112. }
  113. [Fact]
  114. public void New_Initializes ()
  115. {
  116. // Parameterless
  117. using var defaultWindow = new Window ();
  118. Assert.NotNull (defaultWindow);
  119. Assert.Equal (string.Empty, defaultWindow.Title);
  120. // Toplevels have Width/Height set to Dim.Fill
  121. // If there's no SuperView, Top, or Driver, the default Fill width is int.MaxValue
  122. Assert.Equal ($"Window(){defaultWindow.Frame}", defaultWindow.ToString ());
  123. Assert.True (defaultWindow.CanFocus);
  124. Assert.False (defaultWindow.HasFocus);
  125. Assert.Equal (new Rectangle (0, 0, 2147483645, 2147483645), defaultWindow.Viewport);
  126. Assert.Equal (new Rectangle (0, 0, 2147483647, 2147483647), defaultWindow.Frame);
  127. Assert.Null (defaultWindow.Focused);
  128. Assert.NotNull (defaultWindow.ColorScheme);
  129. Assert.Equal (0, defaultWindow.X);
  130. Assert.Equal (0, defaultWindow.Y);
  131. Assert.Equal (Dim.Fill (), defaultWindow.Width);
  132. Assert.Equal (Dim.Fill (), defaultWindow.Height);
  133. Assert.False (defaultWindow.IsCurrentTop);
  134. Assert.Empty (defaultWindow.Id);
  135. Assert.False (defaultWindow.WantContinuousButtonPressed);
  136. Assert.False (defaultWindow.WantMousePositionReports);
  137. Assert.Null (defaultWindow.SuperView);
  138. Assert.Null (defaultWindow.MostFocused);
  139. Assert.Equal (TextDirection.LeftRight_TopBottom, defaultWindow.TextDirection);
  140. // Empty Rect
  141. using var windowWithFrameRectEmpty = new Window { Frame = Rectangle.Empty, Title = "title" };
  142. Assert.NotNull (windowWithFrameRectEmpty);
  143. Assert.Equal ("title", windowWithFrameRectEmpty.Title);
  144. Assert.True (windowWithFrameRectEmpty.CanFocus);
  145. Assert.False (windowWithFrameRectEmpty.HasFocus);
  146. Assert.Equal (Rectangle.Empty, windowWithFrameRectEmpty.Viewport);
  147. Assert.Equal (Rectangle.Empty, windowWithFrameRectEmpty.Frame);
  148. Assert.Null (windowWithFrameRectEmpty.Focused);
  149. Assert.NotNull (windowWithFrameRectEmpty.ColorScheme);
  150. Assert.Equal (0, windowWithFrameRectEmpty.X);
  151. Assert.Equal (0, windowWithFrameRectEmpty.Y);
  152. Assert.Equal (0, windowWithFrameRectEmpty.Width);
  153. Assert.Equal (0, windowWithFrameRectEmpty.Height);
  154. Assert.False (windowWithFrameRectEmpty.IsCurrentTop);
  155. #if DEBUG
  156. Assert.Equal (windowWithFrameRectEmpty.Title, windowWithFrameRectEmpty.Id);
  157. #endif
  158. Assert.False (windowWithFrameRectEmpty.WantContinuousButtonPressed);
  159. Assert.False (windowWithFrameRectEmpty.WantMousePositionReports);
  160. Assert.Null (windowWithFrameRectEmpty.SuperView);
  161. Assert.Null (windowWithFrameRectEmpty.MostFocused);
  162. Assert.Equal (TextDirection.LeftRight_TopBottom, windowWithFrameRectEmpty.TextDirection);
  163. // Rect with values
  164. using var windowWithFrame1234 = new Window ( );
  165. windowWithFrame1234.Frame = new (1, 2, 3, 4);
  166. windowWithFrame1234.Title = "title";
  167. Assert.Equal ("title", windowWithFrame1234.Title);
  168. Assert.NotNull (windowWithFrame1234);
  169. #if DEBUG
  170. Assert.Equal ($"Window(title){windowWithFrame1234.Frame}", windowWithFrame1234.ToString ());
  171. #else
  172. Assert.Equal ($"Window(){windowWithFrame1234.Frame}", windowWithFrame1234.ToString ());
  173. #endif
  174. Assert.True (windowWithFrame1234.CanFocus);
  175. Assert.False (windowWithFrame1234.HasFocus);
  176. Assert.Equal (new (0, 0, 1, 2), windowWithFrame1234.Viewport);
  177. Assert.Equal (new (1, 2, 3, 4), windowWithFrame1234.Frame);
  178. Assert.Null (windowWithFrame1234.Focused);
  179. Assert.NotNull (windowWithFrame1234.ColorScheme);
  180. Assert.Equal (1, windowWithFrame1234.X);
  181. Assert.Equal (2, windowWithFrame1234.Y);
  182. Assert.Equal (3, windowWithFrame1234.Width);
  183. Assert.Equal (4, windowWithFrame1234.Height);
  184. Assert.False (windowWithFrame1234.IsCurrentTop);
  185. #if DEBUG
  186. Assert.Equal (windowWithFrame1234.Title, windowWithFrame1234.Id);
  187. #endif
  188. Assert.False (windowWithFrame1234.WantContinuousButtonPressed);
  189. Assert.False (windowWithFrame1234.WantMousePositionReports);
  190. Assert.Null (windowWithFrame1234.SuperView);
  191. Assert.Null (windowWithFrame1234.MostFocused);
  192. Assert.Equal (TextDirection.LeftRight_TopBottom, windowWithFrame1234.TextDirection);
  193. }
  194. [Fact]
  195. [AutoInitShutdown]
  196. public void OnCanFocusChanged_Only_Must_ContentView_Forces_SetFocus_After_IsInitialized_Is_True ()
  197. {
  198. var win1 = new Window { Id = "win1", Width = 10, Height = 1 };
  199. var view1 = new View { Id = "view1", Width = Dim.Fill (), Height = Dim.Fill (), CanFocus = true };
  200. var win2 = new Window { Id = "win2", Y = 6, Width = 10, Height = 1 };
  201. var view2 = new View { Id = "view2", Width = Dim.Fill (), Height = Dim.Fill (), CanFocus = true };
  202. win2.Add (view2);
  203. win1.Add (view1, win2);
  204. Application.Begin (win1);
  205. Assert.True (win1.HasFocus);
  206. Assert.True (view1.HasFocus);
  207. Assert.False (win2.HasFocus);
  208. Assert.False (view2.HasFocus);
  209. win1.Dispose ();
  210. }
  211. }