WindowTests.cs 9.5 KB

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