WindowTests.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. using Terminal.Gui;
  2. using Xunit;
  3. using Xunit.Abstractions;
  4. namespace TerminalGui.ViewsTests;
  5. public class WindowTests {
  6. readonly ITestOutputHelper _output;
  7. public WindowTests (ITestOutputHelper output) => _output = output;
  8. [Fact]
  9. public void New_Initializes ()
  10. {
  11. // Parameterless
  12. var r = new Window ();
  13. Assert.NotNull (r);
  14. Assert.Equal (string.Empty, r.Title);
  15. Assert.Equal (LayoutStyle.Computed, r.LayoutStyle);
  16. Assert.Equal ("Window()(0,0,0,0)", r.ToString ());
  17. Assert.True (r.CanFocus);
  18. Assert.False (r.HasFocus);
  19. Assert.Equal (new Rect (0, 0, 0, 0), r.Bounds);
  20. Assert.Equal (new Rect (0, 0, 0, 0), r.Frame);
  21. Assert.Null (r.Focused);
  22. Assert.NotNull (r.ColorScheme);
  23. Assert.Equal (Dim.Fill (), r.Width);
  24. Assert.Equal (Dim.Fill (), r.Height);
  25. Assert.Null (r.X);
  26. Assert.Null (r.Y);
  27. Assert.False (r.IsCurrentTop);
  28. Assert.Empty (r.Id);
  29. Assert.False (r.WantContinuousButtonPressed);
  30. Assert.False (r.WantMousePositionReports);
  31. Assert.Null (r.SuperView);
  32. Assert.Null (r.MostFocused);
  33. Assert.Equal (TextDirection.LeftRight_TopBottom, r.TextDirection);
  34. // Empty Rect
  35. r = new Window (Rect.Empty) { Title = "title" };
  36. Assert.NotNull (r);
  37. Assert.Equal ("title", r.Title);
  38. Assert.Equal (LayoutStyle.Absolute, r.LayoutStyle);
  39. Assert.Equal ("Window(title)(0,0,0,0)", r.ToString ());
  40. Assert.True (r.CanFocus);
  41. Assert.False (r.HasFocus);
  42. Assert.Equal (new Rect (0, 0, 0, 0), r.Bounds);
  43. Assert.Equal (new Rect (0, 0, 0, 0), r.Frame);
  44. Assert.Null (r.Focused);
  45. Assert.NotNull (r.ColorScheme);
  46. Assert.Null (r.Width); // All view Dim are initialized now in the IsAdded setter,
  47. Assert.Null (r.Height); // avoiding Dim errors.
  48. Assert.Null (r.X); // All view Pos are initialized now in the IsAdded setter,
  49. Assert.Null (r.Y); // avoiding Pos errors.
  50. Assert.False (r.IsCurrentTop);
  51. Assert.Equal (r.Title, r.Id);
  52. Assert.False (r.WantContinuousButtonPressed);
  53. Assert.False (r.WantMousePositionReports);
  54. Assert.Null (r.SuperView);
  55. Assert.Null (r.MostFocused);
  56. Assert.Equal (TextDirection.LeftRight_TopBottom, r.TextDirection);
  57. // Rect with values
  58. r = new Window (new Rect (1, 2, 3, 4)) { Title = "title" };
  59. Assert.Equal ("title", r.Title);
  60. Assert.NotNull (r);
  61. Assert.Equal (LayoutStyle.Absolute, r.LayoutStyle);
  62. Assert.Equal ("Window(title)(1,2,3,4)", r.ToString ());
  63. Assert.True (r.CanFocus);
  64. Assert.False (r.HasFocus);
  65. Assert.Equal (new Rect (0, 0, 1, 2), r.Bounds);
  66. Assert.Equal (new Rect (1, 2, 3, 4), r.Frame);
  67. Assert.Null (r.Focused);
  68. Assert.NotNull (r.ColorScheme);
  69. Assert.Null (r.Width);
  70. Assert.Null (r.Height);
  71. Assert.Null (r.X);
  72. Assert.Null (r.Y);
  73. Assert.False (r.IsCurrentTop);
  74. Assert.Equal (r.Title, r.Id);
  75. Assert.False (r.WantContinuousButtonPressed);
  76. Assert.False (r.WantMousePositionReports);
  77. Assert.Null (r.SuperView);
  78. Assert.Null (r.MostFocused);
  79. Assert.Equal (TextDirection.LeftRight_TopBottom, r.TextDirection);
  80. r.Dispose ();
  81. }
  82. [Fact]
  83. [AutoInitShutdown]
  84. public void MenuBar_And_StatusBar_Inside_Window ()
  85. {
  86. var menu = new MenuBar (new MenuBarItem [] {
  87. new ("File", new MenuItem [] {
  88. new ("Open", "", null),
  89. new ("Quit", "", null)
  90. }),
  91. new ("Edit", new MenuItem [] {
  92. new ("Copy", "", null)
  93. })
  94. });
  95. var sb = new StatusBar (new StatusItem [] {
  96. new (KeyCode.CtrlMask | KeyCode.Q, "~^Q~ Quit", null),
  97. new (KeyCode.CtrlMask | KeyCode.O, "~^O~ Open", null),
  98. new (KeyCode.CtrlMask | KeyCode.C, "~^C~ Copy", null)
  99. });
  100. var fv = new FrameView ("Frame View") {
  101. Y = 1,
  102. Width = Dim.Fill (),
  103. Height = Dim.Fill (1)
  104. };
  105. var win = new Window ();
  106. win.Add (menu, sb, fv);
  107. var top = Application.Top;
  108. top.Add (win);
  109. Application.Begin (top);
  110. ((FakeDriver)Application.Driver).SetBufferSize (20, 10);
  111. TestHelpers.AssertDriverContentsWithFrameAre (@"
  112. ┌──────────────────┐
  113. │ File Edit │
  114. │┌┤Frame View├────┐│
  115. ││ ││
  116. ││ ││
  117. ││ ││
  118. ││ ││
  119. │└────────────────┘│
  120. │ ^Q Quit │ ^O Open│
  121. └──────────────────┘", _output);
  122. ((FakeDriver)Application.Driver).SetBufferSize (40, 20);
  123. TestHelpers.AssertDriverContentsWithFrameAre (@"
  124. ┌──────────────────────────────────────┐
  125. │ File Edit │
  126. │┌┤Frame View├────────────────────────┐│
  127. ││ ││
  128. ││ ││
  129. ││ ││
  130. ││ ││
  131. ││ ││
  132. ││ ││
  133. ││ ││
  134. ││ ││
  135. ││ ││
  136. ││ ││
  137. ││ ││
  138. ││ ││
  139. ││ ││
  140. ││ ││
  141. │└────────────────────────────────────┘│
  142. │ ^Q Quit │ ^O Open │ ^C Copy │
  143. └──────────────────────────────────────┘", _output);
  144. ((FakeDriver)Application.Driver).SetBufferSize (20, 10);
  145. TestHelpers.AssertDriverContentsWithFrameAre (@"
  146. ┌──────────────────┐
  147. │ File Edit │
  148. │┌┤Frame View├────┐│
  149. ││ ││
  150. ││ ││
  151. ││ ││
  152. ││ ││
  153. │└────────────────┘│
  154. │ ^Q Quit │ ^O Open│
  155. └──────────────────┘", _output);
  156. }
  157. [Fact]
  158. [AutoInitShutdown]
  159. public void OnCanFocusChanged_Only_Must_ContentView_Forces_SetFocus_After_IsInitialized_Is_True ()
  160. {
  161. var win1 = new Window { Id = "win1", Width = 10, Height = 1 };
  162. var view1 = new View { Id = "view1", Width = Dim.Fill (), Height = Dim.Fill (), CanFocus = true };
  163. var win2 = new Window { Id = "win2", Y = 6, Width = 10, Height = 1 };
  164. var view2 = new View { Id = "view2", Width = Dim.Fill (), Height = Dim.Fill (), CanFocus = true };
  165. win2.Add (view2);
  166. win1.Add (view1, win2);
  167. Application.Begin (win1);
  168. Assert.True (win1.HasFocus);
  169. Assert.True (view1.HasFocus);
  170. Assert.False (win2.HasFocus);
  171. Assert.False (view2.HasFocus);
  172. }
  173. [Fact]
  174. [AutoInitShutdown]
  175. public void Activating_MenuBar_By_Alt_Key_Does_Not_Throw ()
  176. {
  177. var menu = new MenuBar (new MenuBarItem [] {
  178. new ("Child", new MenuItem [] {
  179. new ("_Create Child", "", null)
  180. })
  181. });
  182. var win = new Window ();
  183. win.Add (menu);
  184. Application.Top.Add (win);
  185. Application.Begin (Application.Top);
  186. var exception = Record.Exception (() => win.NewKeyDownEvent (new Key (KeyCode.AltMask)));
  187. Assert.Null (exception);
  188. }
  189. }