WindowTests.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. using System;
  2. using Xunit;
  3. using Xunit.Abstractions;
  4. //using GraphViewTests = Terminal.Gui.Views.GraphViewTests;
  5. // Alias Console to MockConsole so we don't accidentally use Console
  6. using Console = Terminal.Gui.FakeConsole;
  7. using NStack;
  8. using Terminal.Gui;
  9. namespace Terminal.Gui.TopLevelTests {
  10. public class WindowTests {
  11. readonly ITestOutputHelper output;
  12. public WindowTests (ITestOutputHelper output)
  13. {
  14. this.output = output;
  15. }
  16. [Fact]
  17. public void New_Initializes ()
  18. {
  19. // Parameterless
  20. var r = new Window ();
  21. Assert.NotNull (r);
  22. Assert.Equal (ustring.Empty, r.Title);
  23. Assert.Equal (LayoutStyle.Computed, r.LayoutStyle);
  24. Assert.Equal ("Window()({X=0,Y=0,Width=0,Height=0})", r.ToString ());
  25. Assert.True (r.CanFocus);
  26. Assert.False (r.HasFocus);
  27. Assert.Equal (new Rect (0, 0, 0, 0), r.Bounds);
  28. Assert.Equal (new Rect (0, 0, 0, 0), r.Frame);
  29. Assert.Null (r.Focused);
  30. Assert.NotNull (r.ColorScheme);
  31. Assert.Equal (Dim.Fill (0), r.Width);
  32. Assert.Equal (Dim.Fill (0), r.Height);
  33. Assert.Null (r.X);
  34. Assert.Null (r.Y);
  35. Assert.False (r.IsCurrentTop);
  36. Assert.Empty (r.Id);
  37. Assert.False (r.WantContinuousButtonPressed);
  38. Assert.False (r.WantMousePositionReports);
  39. Assert.Null (r.SuperView);
  40. Assert.Null (r.MostFocused);
  41. Assert.Equal (TextDirection.LeftRight_TopBottom, r.TextDirection);
  42. // Empty Rect
  43. r = new Window (Rect.Empty, "title");
  44. Assert.NotNull (r);
  45. Assert.Equal ("title", r.Title);
  46. Assert.Equal (LayoutStyle.Absolute, r.LayoutStyle);
  47. Assert.Equal ("Window(title)({X=0,Y=0,Width=0,Height=0})", r.ToString ());
  48. Assert.True (r.CanFocus);
  49. Assert.False (r.HasFocus);
  50. Assert.Equal (new Rect (0, 0, 0, 0), r.Bounds);
  51. Assert.Equal (new Rect (0, 0, 0, 0), r.Frame);
  52. Assert.Null (r.Focused);
  53. Assert.NotNull (r.ColorScheme);
  54. Assert.Null (r.Width); // All view Dim are initialized now in the IsAdded setter,
  55. Assert.Null (r.Height); // avoiding Dim errors.
  56. Assert.Null (r.X); // All view Pos are initialized now in the IsAdded setter,
  57. Assert.Null (r.Y); // avoiding Pos errors.
  58. Assert.False (r.IsCurrentTop);
  59. Assert.Equal (r.Title, r.Id);
  60. Assert.False (r.WantContinuousButtonPressed);
  61. Assert.False (r.WantMousePositionReports);
  62. Assert.Null (r.SuperView);
  63. Assert.Null (r.MostFocused);
  64. Assert.Equal (TextDirection.LeftRight_TopBottom, r.TextDirection);
  65. // Rect with values
  66. r = new Window (new Rect (1, 2, 3, 4), "title");
  67. Assert.Equal ("title", r.Title);
  68. Assert.NotNull (r);
  69. Assert.Equal (LayoutStyle.Absolute, r.LayoutStyle);
  70. Assert.Equal ("Window(title)({X=1,Y=2,Width=3,Height=4})", r.ToString ());
  71. Assert.True (r.CanFocus);
  72. Assert.False (r.HasFocus);
  73. Assert.Equal (new Rect (0, 0, 3, 4), r.Bounds);
  74. Assert.Equal (new Rect (1, 2, 3, 4), r.Frame);
  75. Assert.Null (r.Focused);
  76. Assert.NotNull (r.ColorScheme);
  77. Assert.Null (r.Width);
  78. Assert.Null (r.Height);
  79. Assert.Null (r.X);
  80. Assert.Null (r.Y);
  81. Assert.False (r.IsCurrentTop);
  82. Assert.Equal (r.Title, r.Id);
  83. Assert.False (r.WantContinuousButtonPressed);
  84. Assert.False (r.WantMousePositionReports);
  85. Assert.Null (r.SuperView);
  86. Assert.Null (r.MostFocused);
  87. Assert.Equal (TextDirection.LeftRight_TopBottom, r.TextDirection);
  88. r.Dispose ();
  89. }
  90. [Fact]
  91. public void Set_Title_Fires_TitleChanging ()
  92. {
  93. var r = new Window ();
  94. Assert.Equal (ustring.Empty, r.Title);
  95. string expectedOld = null;
  96. string expectedDuring = null;
  97. string expectedAfter = null;
  98. bool cancel = false;
  99. r.TitleChanging += (s, args) => {
  100. Assert.Equal (expectedOld, args.OldTitle);
  101. Assert.Equal (expectedDuring, args.NewTitle);
  102. args.Cancel = cancel;
  103. };
  104. expectedOld = string.Empty;
  105. r.Title = expectedDuring = expectedAfter = "title";
  106. Assert.Equal (expectedAfter, r.Title.ToString ());
  107. expectedOld = r.Title.ToString ();
  108. r.Title = expectedDuring = expectedAfter = "a different title";
  109. Assert.Equal (expectedAfter, r.Title.ToString ());
  110. // Now setup cancelling the change and change it back to "title"
  111. cancel = true;
  112. expectedOld = r.Title.ToString ();
  113. r.Title = expectedDuring = "title";
  114. Assert.Equal (expectedAfter, r.Title.ToString ());
  115. r.Dispose ();
  116. }
  117. [Fact]
  118. public void Set_Title_Fires_TitleChanged ()
  119. {
  120. var r = new Window ();
  121. Assert.Equal (ustring.Empty, r.Title);
  122. string expectedOld = null;
  123. string expected = null;
  124. r.TitleChanged += (s,args) => {
  125. Assert.Equal (expectedOld, args.OldTitle);
  126. Assert.Equal (r.Title, args.NewTitle);
  127. };
  128. expected = "title";
  129. expectedOld = r.Title.ToString ();
  130. r.Title = expected;
  131. Assert.Equal (expected, r.Title.ToString ());
  132. expected = "another title";
  133. expectedOld = r.Title.ToString ();
  134. r.Title = expected;
  135. Assert.Equal (expected, r.Title.ToString ());
  136. r.Dispose ();
  137. }
  138. [Fact, AutoInitShutdown]
  139. public void MenuBar_And_StatusBar_Inside_Window ()
  140. {
  141. var menu = new MenuBar (new MenuBarItem [] {
  142. new MenuBarItem ("File", new MenuItem [] {
  143. new MenuItem ("Open", "", null),
  144. new MenuItem ("Quit", "", null),
  145. }),
  146. new MenuBarItem ("Edit", new MenuItem [] {
  147. new MenuItem ("Copy", "", null),
  148. })
  149. });
  150. var sb = new StatusBar (new StatusItem [] {
  151. new StatusItem (Key.CtrlMask | Key.Q, "~^Q~ Quit", null),
  152. new StatusItem (Key.CtrlMask | Key.O, "~^O~ Open", null),
  153. new StatusItem (Key.CtrlMask | Key.C, "~^C~ Copy", null),
  154. });
  155. var fv = new FrameView ("Frame View") {
  156. Y = 1,
  157. Width = Dim.Fill (),
  158. Height = Dim.Fill (1)
  159. };
  160. var win = new Window ();
  161. win.Add (menu, sb, fv);
  162. var top = Application.Top;
  163. top.Add (win);
  164. Application.Begin (top);
  165. ((FakeDriver)Application.Driver).SetBufferSize (20, 10);
  166. TestHelpers.AssertDriverContentsWithFrameAre (@"
  167. ┌──────────────────┐
  168. │ File Edit │
  169. │┌┤Frame View├────┐│
  170. ││ ││
  171. ││ ││
  172. ││ ││
  173. ││ ││
  174. │└────────────────┘│
  175. │ ^Q Quit │ ^O Open│
  176. └──────────────────┘", output);
  177. ((FakeDriver)Application.Driver).SetBufferSize (40, 20);
  178. TestHelpers.AssertDriverContentsWithFrameAre (@"
  179. ┌──────────────────────────────────────┐
  180. │ File Edit │
  181. │┌┤Frame View├────────────────────────┐│
  182. ││ ││
  183. ││ ││
  184. ││ ││
  185. ││ ││
  186. ││ ││
  187. ││ ││
  188. ││ ││
  189. ││ ││
  190. ││ ││
  191. ││ ││
  192. ││ ││
  193. ││ ││
  194. ││ ││
  195. ││ ││
  196. │└────────────────────────────────────┘│
  197. │ ^Q Quit │ ^O Open │ ^C Copy │
  198. └──────────────────────────────────────┘", output);
  199. ((FakeDriver)Application.Driver).SetBufferSize (20, 10);
  200. TestHelpers.AssertDriverContentsWithFrameAre (@"
  201. ┌──────────────────┐
  202. │ File Edit │
  203. │┌┤Frame View├────┐│
  204. ││ ││
  205. ││ ││
  206. ││ ││
  207. ││ ││
  208. │└────────────────┘│
  209. │ ^Q Quit │ ^O Open│
  210. └──────────────────┘", output);
  211. }
  212. }
  213. }