WindowTests.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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.NotEmpty (r.Subviews);
  38. Assert.False (r.WantContinuousButtonPressed);
  39. Assert.False (r.WantMousePositionReports);
  40. Assert.Null (r.SuperView);
  41. Assert.Null (r.MostFocused);
  42. Assert.Equal (TextDirection.LeftRight_TopBottom, r.TextDirection);
  43. // Empty Rect
  44. r = new Window (Rect.Empty, "title");
  45. Assert.NotNull (r);
  46. Assert.Equal ("title", r.Title);
  47. Assert.Equal (LayoutStyle.Absolute, r.LayoutStyle);
  48. Assert.Equal ("Window()({X=0,Y=0,Width=0,Height=0})", r.ToString ());
  49. Assert.True (r.CanFocus);
  50. Assert.False (r.HasFocus);
  51. Assert.Equal (new Rect (0, 0, 0, 0), r.Bounds);
  52. Assert.Equal (new Rect (0, 0, 0, 0), r.Frame);
  53. Assert.Null (r.Focused);
  54. Assert.NotNull (r.ColorScheme);
  55. Assert.Null (r.Width); // All view Dim are initialized now in the IsAdded setter,
  56. Assert.Null (r.Height); // avoiding Dim errors.
  57. Assert.Null (r.X); // All view Pos are initialized now in the IsAdded setter,
  58. Assert.Null (r.Y); // avoiding Pos errors.
  59. Assert.False (r.IsCurrentTop);
  60. Assert.Empty (r.Id);
  61. Assert.NotEmpty (r.Subviews);
  62. Assert.False (r.WantContinuousButtonPressed);
  63. Assert.False (r.WantMousePositionReports);
  64. Assert.Null (r.SuperView);
  65. Assert.Null (r.MostFocused);
  66. Assert.Equal (TextDirection.LeftRight_TopBottom, r.TextDirection);
  67. // Rect with values
  68. r = new Window (new Rect (1, 2, 3, 4), "title");
  69. Assert.Equal ("title", r.Title);
  70. Assert.NotNull (r);
  71. Assert.Equal (LayoutStyle.Absolute, r.LayoutStyle);
  72. Assert.Equal ("Window()({X=1,Y=2,Width=3,Height=4})", r.ToString ());
  73. Assert.True (r.CanFocus);
  74. Assert.False (r.HasFocus);
  75. Assert.Equal (new Rect (0, 0, 3, 4), r.Bounds);
  76. Assert.Equal (new Rect (1, 2, 3, 4), r.Frame);
  77. Assert.Null (r.Focused);
  78. Assert.NotNull (r.ColorScheme);
  79. Assert.Null (r.Width);
  80. Assert.Null (r.Height);
  81. Assert.Null (r.X);
  82. Assert.Null (r.Y);
  83. Assert.False (r.IsCurrentTop);
  84. Assert.Empty (r.Id);
  85. Assert.NotEmpty (r.Subviews);
  86. Assert.False (r.WantContinuousButtonPressed);
  87. Assert.False (r.WantMousePositionReports);
  88. Assert.Null (r.SuperView);
  89. Assert.Null (r.MostFocused);
  90. Assert.Equal (TextDirection.LeftRight_TopBottom, r.TextDirection);
  91. r.Dispose ();
  92. }
  93. [Fact]
  94. public void Set_Title_Fires_TitleChanging ()
  95. {
  96. var r = new Window ();
  97. Assert.Equal (ustring.Empty, r.Title);
  98. string expectedOld = null;
  99. string expectedDuring = null;
  100. string expectedAfter = null;
  101. bool cancel = false;
  102. r.TitleChanging += (args) => {
  103. Assert.Equal (expectedOld, args.OldTitle);
  104. Assert.Equal (expectedDuring, args.NewTitle);
  105. args.Cancel = cancel;
  106. };
  107. expectedOld = string.Empty;
  108. r.Title = expectedDuring = expectedAfter = "title";
  109. Assert.Equal (expectedAfter, r.Title.ToString ());
  110. expectedOld = r.Title.ToString ();
  111. r.Title = expectedDuring = expectedAfter = "a different title";
  112. Assert.Equal (expectedAfter, r.Title.ToString ());
  113. // Now setup cancelling the change and change it back to "title"
  114. cancel = true;
  115. expectedOld = r.Title.ToString ();
  116. r.Title = expectedDuring = "title";
  117. Assert.Equal (expectedAfter, r.Title.ToString ());
  118. r.Dispose ();
  119. }
  120. [Fact]
  121. public void Set_Title_Fires_TitleChanged ()
  122. {
  123. var r = new Window ();
  124. Assert.Equal (ustring.Empty, r.Title);
  125. string expectedOld = null;
  126. string expected = null;
  127. r.TitleChanged += (args) => {
  128. Assert.Equal (expectedOld, args.OldTitle);
  129. Assert.Equal (r.Title, args.NewTitle);
  130. };
  131. expected = "title";
  132. expectedOld = r.Title.ToString ();
  133. r.Title = expected;
  134. Assert.Equal (expected, r.Title.ToString ());
  135. expected = "another title";
  136. expectedOld = r.Title.ToString ();
  137. r.Title = expected;
  138. Assert.Equal (expected, r.Title.ToString ());
  139. r.Dispose ();
  140. }
  141. [Fact, AutoInitShutdown]
  142. public void MenuBar_And_StatusBar_Inside_Window ()
  143. {
  144. var menu = new MenuBar (new MenuBarItem [] {
  145. new MenuBarItem ("File", new MenuItem [] {
  146. new MenuItem ("Open", "", null),
  147. new MenuItem ("Quit", "", null),
  148. }),
  149. new MenuBarItem ("Edit", new MenuItem [] {
  150. new MenuItem ("Copy", "", null),
  151. })
  152. });
  153. var sb = new StatusBar (new StatusItem [] {
  154. new StatusItem (Key.CtrlMask | Key.Q, "~^Q~ Quit", null),
  155. new StatusItem (Key.CtrlMask | Key.O, "~^O~ Open", null),
  156. new StatusItem (Key.CtrlMask | Key.C, "~^C~ Copy", null),
  157. });
  158. var fv = new FrameView ("Frame View") {
  159. Y = 1,
  160. Width = Dim.Fill (),
  161. Height = Dim.Fill (1)
  162. };
  163. var win = new Window ();
  164. win.Add (menu, sb, fv);
  165. var top = Application.Top;
  166. top.Add (win);
  167. Application.Begin (top);
  168. ((FakeDriver)Application.Driver).SetBufferSize (20, 10);
  169. TestHelpers.AssertDriverContentsWithFrameAre (@"
  170. ┌──────────────────┐
  171. │ File Edit │
  172. │┌ Frame View ────┐│
  173. ││ ││
  174. ││ ││
  175. ││ ││
  176. ││ ││
  177. │└────────────────┘│
  178. │ ^Q Quit │ ^O Open│
  179. └──────────────────┘", output);
  180. ((FakeDriver)Application.Driver).SetBufferSize (40, 20);
  181. TestHelpers.AssertDriverContentsWithFrameAre (@"
  182. ┌──────────────────────────────────────┐
  183. │ File Edit │
  184. │┌ Frame View ────────────────────────┐│
  185. ││ ││
  186. ││ ││
  187. ││ ││
  188. ││ ││
  189. ││ ││
  190. ││ ││
  191. ││ ││
  192. ││ ││
  193. ││ ││
  194. ││ ││
  195. ││ ││
  196. ││ ││
  197. ││ ││
  198. ││ ││
  199. │└────────────────────────────────────┘│
  200. │ ^Q Quit │ ^O Open │ ^C Copy │
  201. └──────────────────────────────────────┘", output);
  202. ((FakeDriver)Application.Driver).SetBufferSize (20, 10);
  203. TestHelpers.AssertDriverContentsWithFrameAre (@"
  204. ┌──────────────────┐
  205. │ File Edit │
  206. │┌ Frame View ────┐│
  207. ││ ││
  208. ││ ││
  209. ││ ││
  210. ││ ││
  211. │└────────────────┘│
  212. │ ^Q Quit │ ^O Open│
  213. └──────────────────┘", output);
  214. }
  215. [Fact, AutoInitShutdown]
  216. public void Activating_MenuBar_By_Alt_Key_Does_Not_Throw ()
  217. {
  218. var menu = new MenuBar (new MenuBarItem [] {
  219. new MenuBarItem ("Child", new MenuItem [] {
  220. new MenuItem ("_Create Child", "", null)
  221. })
  222. });
  223. var win = new Window ();
  224. win.Add (menu);
  225. Application.Top.Add (win);
  226. Application.Begin (Application.Top);
  227. var exception = Record.Exception (() => win.ProcessHotKey (new KeyEvent (Key.AltMask, new KeyModifiers { Alt = true })));
  228. Assert.Null (exception);
  229. }
  230. [Fact, AutoInitShutdown]
  231. public void Adding_Center_Window_Child_To_Window_Parent_Always_LayoutSubviews ()
  232. {
  233. var parentWin = new Window ();
  234. View childWin = null;
  235. var menu = new MenuBar (new MenuBarItem [] {
  236. new MenuBarItem ("Child", new MenuItem [] {
  237. new MenuItem ("_Create Child", "", () => {
  238. childWin = new Window () {
  239. X = Pos.Center (),
  240. Y = Pos.Center (),
  241. Width = 10,
  242. Height = 10
  243. };
  244. parentWin.Add (childWin);
  245. })
  246. }),
  247. new MenuBarItem ("View", new MenuBarItem [] {
  248. new MenuBarItem ("Create", new MenuItem [] {
  249. new MenuItem("_TextField", "", () => {
  250. var tf = new TextField ("Test") {
  251. X = Pos.Center (),
  252. Y = Pos.Center (),
  253. Width = 5
  254. };
  255. childWin.Add (tf);
  256. })
  257. })
  258. })
  259. });
  260. parentWin.Add (menu);
  261. Application.Top.Add (parentWin);
  262. Application.Begin (Application.Top);
  263. ((FakeDriver)Application.Driver).SetBufferSize (20, 20);
  264. TestHelpers.AssertDriverContentsWithFrameAre (@"
  265. ┌──────────────────┐
  266. │ Child View │
  267. │ │
  268. │ │
  269. │ │
  270. │ │
  271. │ │
  272. │ │
  273. │ │
  274. │ │
  275. │ │
  276. │ │
  277. │ │
  278. │ │
  279. │ │
  280. │ │
  281. │ │
  282. │ │
  283. │ │
  284. └──────────────────┘", output);
  285. Assert.True (parentWin.ProcessHotKey (new KeyEvent (Key.C | Key.AltMask, new KeyModifiers { Alt = true })));
  286. Application.MainLoop.MainIteration ();
  287. parentWin.Redraw (parentWin.Bounds);
  288. TestHelpers.AssertDriverContentsWithFrameAre (@"
  289. ┌──────────────────┐
  290. │ Child View │
  291. │ │
  292. │ │
  293. │ │
  294. │ ┌────────┐ │
  295. │ │ │ │
  296. │ │ │ │
  297. │ │ │ │
  298. │ │ │ │
  299. │ │ │ │
  300. │ │ │ │
  301. │ │ │ │
  302. │ │ │ │
  303. │ └────────┘ │
  304. │ │
  305. │ │
  306. │ │
  307. │ │
  308. └──────────────────┘", output);
  309. Assert.True (parentWin.ProcessHotKey (new KeyEvent (Key.T | Key.AltMask, new KeyModifiers { Alt = true })));
  310. Application.MainLoop.MainIteration ();
  311. childWin.Redraw (childWin.Bounds);
  312. TestHelpers.AssertDriverContentsWithFrameAre (@"
  313. ┌──────────────────┐
  314. │ Child View │
  315. │ │
  316. │ │
  317. │ │
  318. │ ┌────────┐ │
  319. │ │ │ │
  320. │ │ │ │
  321. │ │ │ │
  322. │ │ Test │ │
  323. │ │ │ │
  324. │ │ │ │
  325. │ │ │ │
  326. │ │ │ │
  327. │ └────────┘ │
  328. │ │
  329. │ │
  330. │ │
  331. │ │
  332. └──────────────────┘", output);
  333. }
  334. [Fact, AutoInitShutdown]
  335. public void Window_On_Non_Toplevel_Superview_Cannot_Overflows_His_Bounds ()
  336. {
  337. var win = new Window () { Width = Dim.Fill (5), Height = Dim.Fill (5) };
  338. var view = new View () { X = 3, Y = 3, Width = 10, Height = 10 };
  339. view.Add (win);
  340. Application.Top.Add (view);
  341. Application.Begin (Application.Top);
  342. ReflectionTools.InvokePrivate (
  343. typeof (Application),
  344. "ProcessMouseEvent",
  345. new MouseEvent () {
  346. X = 3,
  347. Y = 3,
  348. Flags = MouseFlags.Button1Pressed
  349. });
  350. Assert.Equal (win, Application.MouseGrabView);
  351. ReflectionTools.InvokePrivate (
  352. typeof (Application),
  353. "ProcessMouseEvent",
  354. new MouseEvent () {
  355. X = 2,
  356. Y = 2,
  357. Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  358. });
  359. Assert.Equal (win, Application.MouseGrabView);
  360. Assert.Equal (new Rect (0, 0, 5, 5), win.Frame);
  361. ReflectionTools.InvokePrivate (
  362. typeof (Application),
  363. "ProcessMouseEvent",
  364. new MouseEvent () {
  365. X = 14,
  366. Y = 14,
  367. Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  368. });
  369. Assert.Equal (win, Application.MouseGrabView);
  370. Assert.Equal (new Rect (4, 4, 5, 5), win.Frame);
  371. }
  372. }
  373. }