WindowTests.cs 11 KB

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