WindowTests.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. }
  26. [Fact]
  27. [AutoInitShutdown]
  28. public void MenuBar_And_StatusBar_Inside_Window ()
  29. {
  30. var menu = new MenuBar
  31. {
  32. Menus =
  33. [
  34. new MenuBarItem ("File", new MenuItem [] { new ("Open", "", null), new ("Quit", "", null) }),
  35. new MenuBarItem (
  36. "Edit",
  37. new MenuItem [] { new ("Copy", "", null) }
  38. )
  39. ]
  40. };
  41. var sb = new StatusBar (
  42. new StatusItem []
  43. {
  44. new ((KeyCode)Key.Q.WithCtrl, "~^Q~ Quit", null),
  45. new ((KeyCode)Key.O.WithCtrl, "~^O~ Open", null),
  46. new ((KeyCode)Key.C.WithCtrl, "~^C~ Copy", null)
  47. }
  48. );
  49. var fv = new FrameView { Y = 1, Width = Dim.Fill (), Height = Dim.Fill (1), Title = "Frame View" };
  50. var win = new Window ();
  51. win.Add (menu, sb, fv);
  52. Toplevel top = new ();
  53. top.Add (win);
  54. Application.Begin (top);
  55. ((FakeDriver)Application.Driver).SetBufferSize (20, 10);
  56. TestHelpers.AssertDriverContentsWithFrameAre (
  57. @"
  58. ┌──────────────────┐
  59. │ File Edit │
  60. │┌┤Frame View├────┐│
  61. ││ ││
  62. ││ ││
  63. ││ ││
  64. ││ ││
  65. │└────────────────┘│
  66. │ ^Q Quit │ ^O Open│
  67. └──────────────────┘",
  68. _output
  69. );
  70. ((FakeDriver)Application.Driver).SetBufferSize (40, 20);
  71. TestHelpers.AssertDriverContentsWithFrameAre (
  72. @"
  73. ┌──────────────────────────────────────┐
  74. │ File Edit │
  75. │┌┤Frame View├────────────────────────┐│
  76. ││ ││
  77. ││ ││
  78. ││ ││
  79. ││ ││
  80. ││ ││
  81. ││ ││
  82. ││ ││
  83. ││ ││
  84. ││ ││
  85. ││ ││
  86. ││ ││
  87. ││ ││
  88. ││ ││
  89. ││ ││
  90. │└────────────────────────────────────┘│
  91. │ ^Q Quit │ ^O Open │ ^C Copy │
  92. └──────────────────────────────────────┘",
  93. _output
  94. );
  95. ((FakeDriver)Application.Driver).SetBufferSize (20, 10);
  96. TestHelpers.AssertDriverContentsWithFrameAre (
  97. @"
  98. ┌──────────────────┐
  99. │ File Edit │
  100. │┌┤Frame View├────┐│
  101. ││ ││
  102. ││ ││
  103. ││ ││
  104. ││ ││
  105. │└────────────────┘│
  106. │ ^Q Quit │ ^O Open│
  107. └──────────────────┘",
  108. _output
  109. );
  110. }
  111. [Fact]
  112. public void New_Initializes ()
  113. {
  114. // Parameterless
  115. using var defaultWindow = new Window ();
  116. Assert.NotNull (defaultWindow);
  117. Assert.Equal (string.Empty, defaultWindow.Title);
  118. // Toplevels have Width/Height set to Dim.Fill
  119. Assert.Equal (LayoutStyle.Computed, defaultWindow.LayoutStyle);
  120. // If there's no SuperView, Top, or Driver, the default Fill width is int.MaxValue
  121. Assert.Equal ($"Window(){defaultWindow.Frame}", defaultWindow.ToString ());
  122. Assert.True (defaultWindow.CanFocus);
  123. Assert.False (defaultWindow.HasFocus);
  124. Assert.Equal (new Rectangle (0, 0, 2147483645, 2147483645), defaultWindow.Viewport);
  125. Assert.Equal (new Rectangle (0, 0, 2147483647, 2147483647), defaultWindow.Frame);
  126. Assert.Null (defaultWindow.Focused);
  127. Assert.NotNull (defaultWindow.ColorScheme);
  128. Assert.Equal (0, defaultWindow.X);
  129. Assert.Equal (0, defaultWindow.Y);
  130. Assert.Equal (Dim.Fill (), defaultWindow.Width);
  131. Assert.Equal (Dim.Fill (), defaultWindow.Height);
  132. Assert.False (defaultWindow.IsCurrentTop);
  133. Assert.Empty (defaultWindow.Id);
  134. Assert.False (defaultWindow.WantContinuousButtonPressed);
  135. Assert.False (defaultWindow.WantMousePositionReports);
  136. Assert.Null (defaultWindow.SuperView);
  137. Assert.Null (defaultWindow.MostFocused);
  138. Assert.Equal (TextDirection.LeftRight_TopBottom, defaultWindow.TextDirection);
  139. // Empty Rect
  140. using var windowWithFrameRectEmpty = new Window { Frame = Rectangle.Empty, Title = "title" };
  141. Assert.NotNull (windowWithFrameRectEmpty);
  142. Assert.Equal ("title", windowWithFrameRectEmpty.Title);
  143. Assert.Equal (LayoutStyle.Absolute, windowWithFrameRectEmpty.LayoutStyle);
  144. Assert.Equal ("title", windowWithFrameRectEmpty.Title);
  145. Assert.Equal (LayoutStyle.Absolute, windowWithFrameRectEmpty.LayoutStyle);
  146. // TODO: Fix things so that this works in release and debug
  147. // BUG: This also looks like it might be unintended behavior.
  148. // Can actually also be removed, since the tests below make it redundant.
  149. #if DEBUG
  150. Assert.Equal ($"Window(title){windowWithFrameRectEmpty.Frame}", windowWithFrameRectEmpty.ToString ());
  151. #else
  152. Assert.Equal ($"Window(){windowWithFrameRectEmpty.Frame}", windowWithFrameRectEmpty.ToString ());
  153. #endif
  154. Assert.True (windowWithFrameRectEmpty.CanFocus);
  155. Assert.False (windowWithFrameRectEmpty.HasFocus);
  156. Assert.Equal (Rectangle.Empty, windowWithFrameRectEmpty.Viewport);
  157. Assert.Equal (Rectangle.Empty, windowWithFrameRectEmpty.Frame);
  158. Assert.Null (windowWithFrameRectEmpty.Focused);
  159. Assert.NotNull (windowWithFrameRectEmpty.ColorScheme);
  160. Assert.Equal (0, windowWithFrameRectEmpty.X);
  161. Assert.Equal (0, windowWithFrameRectEmpty.Y);
  162. Assert.Equal (0, windowWithFrameRectEmpty.Width);
  163. Assert.Equal (0, windowWithFrameRectEmpty.Height);
  164. Assert.False (windowWithFrameRectEmpty.IsCurrentTop);
  165. #if DEBUG
  166. Assert.Equal (windowWithFrameRectEmpty.Title, windowWithFrameRectEmpty.Id);
  167. #endif
  168. Assert.False (windowWithFrameRectEmpty.WantContinuousButtonPressed);
  169. Assert.False (windowWithFrameRectEmpty.WantMousePositionReports);
  170. Assert.Null (windowWithFrameRectEmpty.SuperView);
  171. Assert.Null (windowWithFrameRectEmpty.MostFocused);
  172. Assert.Equal (TextDirection.LeftRight_TopBottom, windowWithFrameRectEmpty.TextDirection);
  173. // Rect with values
  174. using var windowWithFrame1234 = new Window ( );
  175. windowWithFrame1234.Frame = new (1, 2, 3, 4);
  176. windowWithFrame1234.Title = "title";
  177. Assert.Equal ("title", windowWithFrame1234.Title);
  178. Assert.NotNull (windowWithFrame1234);
  179. Assert.Equal (LayoutStyle.Absolute, windowWithFrame1234.LayoutStyle);
  180. Assert.Equal (LayoutStyle.Absolute, windowWithFrame1234.LayoutStyle);
  181. #if DEBUG
  182. Assert.Equal ($"Window(title){windowWithFrame1234.Frame}", windowWithFrame1234.ToString ());
  183. #else
  184. Assert.Equal ($"Window(){windowWithFrame1234.Frame}", windowWithFrame1234.ToString ());
  185. #endif
  186. Assert.True (windowWithFrame1234.CanFocus);
  187. Assert.False (windowWithFrame1234.HasFocus);
  188. Assert.Equal (new (0, 0, 1, 2), windowWithFrame1234.Viewport);
  189. Assert.Equal (new (1, 2, 3, 4), windowWithFrame1234.Frame);
  190. Assert.Null (windowWithFrame1234.Focused);
  191. Assert.NotNull (windowWithFrame1234.ColorScheme);
  192. Assert.Equal (1, windowWithFrame1234.X);
  193. Assert.Equal (2, windowWithFrame1234.Y);
  194. Assert.Equal (3, windowWithFrame1234.Width);
  195. Assert.Equal (4, windowWithFrame1234.Height);
  196. Assert.False (windowWithFrame1234.IsCurrentTop);
  197. #if DEBUG
  198. Assert.Equal (windowWithFrame1234.Title, windowWithFrame1234.Id);
  199. #endif
  200. Assert.False (windowWithFrame1234.WantContinuousButtonPressed);
  201. Assert.False (windowWithFrame1234.WantMousePositionReports);
  202. Assert.Null (windowWithFrame1234.SuperView);
  203. Assert.Null (windowWithFrame1234.MostFocused);
  204. Assert.Equal (TextDirection.LeftRight_TopBottom, windowWithFrame1234.TextDirection);
  205. }
  206. [Fact]
  207. [AutoInitShutdown]
  208. public void OnCanFocusChanged_Only_Must_ContentView_Forces_SetFocus_After_IsInitialized_Is_True ()
  209. {
  210. var win1 = new Window { Id = "win1", Width = 10, Height = 1 };
  211. var view1 = new View { Id = "view1", Width = Dim.Fill (), Height = Dim.Fill (), CanFocus = true };
  212. var win2 = new Window { Id = "win2", Y = 6, Width = 10, Height = 1 };
  213. var view2 = new View { Id = "view2", Width = Dim.Fill (), Height = Dim.Fill (), CanFocus = true };
  214. win2.Add (view2);
  215. win1.Add (view1, win2);
  216. Application.Begin (win1);
  217. Assert.True (win1.HasFocus);
  218. Assert.True (view1.HasFocus);
  219. Assert.False (win2.HasFocus);
  220. Assert.False (view2.HasFocus);
  221. }
  222. }