WindowTests.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. using UnitTests;
  2. using Xunit.Abstractions;
  3. namespace Terminal.Gui.ViewsTests;
  4. public class WindowTests (ITestOutputHelper output)
  5. {
  6. [Fact]
  7. [AutoInitShutdown]
  8. public void Activating_MenuBar_By_Alt_Key_Does_Not_Throw ()
  9. {
  10. var menu = new MenuBar
  11. {
  12. Menus =
  13. [
  14. new MenuBarItem ("Child", new MenuItem [] { new ("_Create Child", "", null) })
  15. ]
  16. };
  17. var win = new Window ();
  18. win.Add (menu);
  19. var top = new Toplevel ();
  20. top.Add (win);
  21. Application.Begin (top);
  22. Exception exception = Record.Exception (() => win.NewKeyDownEvent (KeyCode.AltMask));
  23. Assert.Null (exception);
  24. top.Dispose ();
  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. var fv = new FrameView { Y = 1, Width = Dim.Fill (), Height = Dim.Fill (1), Title = "Frame View" };
  43. var win = new Window ();
  44. win.Add (menu, sb, fv);
  45. Toplevel top = new ();
  46. top.Add (win);
  47. Application.Begin (top);
  48. ((FakeDriver)Application.Driver!).SetBufferSize (20, 10);
  49. DriverAssert.AssertDriverContentsWithFrameAre (
  50. @"
  51. ┌──────────────────┐
  52. │ File Edit │
  53. │┌┤Frame View├────┐│
  54. ││ ││
  55. ││ ││
  56. ││ ││
  57. ││ ││
  58. │└────────────────┘│
  59. │ │
  60. └──────────────────┘",
  61. output
  62. );
  63. ((FakeDriver)Application.Driver!).SetBufferSize (40, 20);
  64. DriverAssert.AssertDriverContentsWithFrameAre (
  65. @"
  66. ┌──────────────────────────────────────┐
  67. │ File Edit │
  68. │┌┤Frame View├────────────────────────┐│
  69. ││ ││
  70. ││ ││
  71. ││ ││
  72. ││ ││
  73. ││ ││
  74. ││ ││
  75. ││ ││
  76. ││ ││
  77. ││ ││
  78. ││ ││
  79. ││ ││
  80. ││ ││
  81. ││ ││
  82. ││ ││
  83. │└────────────────────────────────────┘│
  84. │ │
  85. └──────────────────────────────────────┘",
  86. output
  87. );
  88. ((FakeDriver)Application.Driver!).SetBufferSize (20, 10);
  89. DriverAssert.AssertDriverContentsWithFrameAre (
  90. @"
  91. ┌──────────────────┐
  92. │ File Edit │
  93. │┌┤Frame View├────┐│
  94. ││ ││
  95. ││ ││
  96. ││ ││
  97. ││ ││
  98. │└────────────────┘│
  99. │ │
  100. └──────────────────┘",
  101. output
  102. );
  103. top.Dispose ();
  104. }
  105. [Fact]
  106. public void New_Initializes ()
  107. {
  108. // Parameterless
  109. using var defaultWindow = new Window ();
  110. defaultWindow.Layout ();
  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. Assert.Equal (ViewArrangement.Overlapped, defaultWindow.Arrangement);
  134. // Empty Rect
  135. using var windowWithFrameRectEmpty = new Window { Frame = Rectangle.Empty, Title = "title" };
  136. windowWithFrameRectEmpty.Layout ();
  137. Assert.NotNull (windowWithFrameRectEmpty);
  138. Assert.Equal ("title", windowWithFrameRectEmpty.Title);
  139. Assert.True (windowWithFrameRectEmpty.CanFocus);
  140. Assert.False (windowWithFrameRectEmpty.HasFocus);
  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. }