WindowTests.cs 9.9 KB

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