ViewTests.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. using UnitTests;
  2. using Xunit.Abstractions;
  3. namespace Terminal.Gui.ViewTests;
  4. public class ViewTests
  5. {
  6. private readonly ITestOutputHelper _output;
  7. public ViewTests (ITestOutputHelper output)
  8. {
  9. _output = output;
  10. #if DEBUG_IDISPOSABLE
  11. View.DebugIDisposable = true;
  12. #endif
  13. }
  14. // Generic lifetime (IDisposable) tests
  15. [Fact]
  16. [TestRespondersDisposed]
  17. public void Dispose_Works ()
  18. {
  19. var r = new View ();
  20. #if DEBUG_IDISPOSABLE
  21. Assert.Equal (4, View.Instances.Count);
  22. #endif
  23. r.Dispose ();
  24. #if DEBUG_IDISPOSABLE
  25. Assert.Empty (View.Instances);
  26. #endif
  27. }
  28. [Fact]
  29. public void Disposing_Event_Notify_All_Subscribers_On_The_First_Container ()
  30. {
  31. #if DEBUG_IDISPOSABLE
  32. // Only clear before because need to test after assert
  33. View.Instances.Clear ();
  34. #endif
  35. var container1 = new View { Id = "Container1" };
  36. var count = 0;
  37. var view = new View { Id = "View" };
  38. view.Disposing += ViewDisposing;
  39. container1.Add (view);
  40. Assert.Equal (container1, view.SuperView);
  41. Assert.Single (container1.SubViews);
  42. var container2 = new View { Id = "Container2" };
  43. // BUGBUG: It's not legit to add a View to two SuperViews
  44. container2.Add (view);
  45. Assert.Equal (container2, view.SuperView);
  46. Assert.Equal (container1.SubViews.Count, container2.SubViews.Count);
  47. container2.Dispose ();
  48. Assert.Empty (container1.SubViews);
  49. Assert.Empty (container2.SubViews);
  50. Assert.Equal (1, count);
  51. Assert.Null (view.SuperView);
  52. container1.Dispose ();
  53. #if DEBUG_IDISPOSABLE
  54. Assert.Empty (View.Instances);
  55. #endif
  56. return;
  57. void ViewDisposing (object sender, EventArgs e)
  58. {
  59. count++;
  60. Assert.Equal (view, sender);
  61. container1.Remove ((View)sender);
  62. }
  63. }
  64. [Fact]
  65. public void Disposing_Event_Notify_All_Subscribers_On_The_Second_Container ()
  66. {
  67. #if DEBUG_IDISPOSABLE
  68. // Only clear before because need to test after assert
  69. View.Instances.Clear ();
  70. #endif
  71. var container1 = new View { Id = "Container1" };
  72. var view = new View { Id = "View" };
  73. container1.Add (view);
  74. Assert.Equal (container1, view.SuperView);
  75. Assert.Single (container1.SubViews);
  76. var container2 = new View { Id = "Container2" };
  77. var count = 0;
  78. view.Disposing += View_Disposing;
  79. // BUGBUG: It's not legit to add a View to two SuperViews
  80. container2.Add (view);
  81. Assert.Equal (container2, view.SuperView);
  82. void View_Disposing (object sender, EventArgs e)
  83. {
  84. count++;
  85. Assert.Equal (view, sender);
  86. container2.Remove ((View)sender);
  87. }
  88. Assert.Equal (container1.SubViews.Count, container2.SubViews.Count);
  89. container1.Dispose ();
  90. Assert.Empty (container1.SubViews);
  91. Assert.Empty (container2.SubViews);
  92. Assert.Equal (1, count);
  93. Assert.Null (view.SuperView);
  94. container2.Dispose ();
  95. #if DEBUG_IDISPOSABLE
  96. Assert.Empty (View.Instances);
  97. #endif
  98. }
  99. [Fact]
  100. public void Not_Notifying_Dispose ()
  101. {
  102. // Only clear before because need to test after assert
  103. #if DEBUG_IDISPOSABLE
  104. View.Instances.Clear ();
  105. #endif
  106. var container1 = new View { Id = "Container1" };
  107. var view = new View { Id = "View" };
  108. container1.Add (view);
  109. Assert.Equal (container1, view.SuperView);
  110. Assert.Single (container1.SubViews);
  111. var container2 = new View { Id = "Container2" };
  112. // BUGBUG: It's not legit to add a View to two SuperViews
  113. container2.Add (view);
  114. Assert.Equal (container2, view.SuperView);
  115. Assert.Equal (container1.SubViews.Count, container2.SubViews.Count);
  116. container1.Dispose ();
  117. Assert.Empty (container1.SubViews);
  118. Assert.NotEmpty (container2.SubViews);
  119. Assert.Single (container2.SubViews);
  120. Assert.Null (view.SuperView);
  121. // Trying access disposed properties
  122. #if DEBUG_IDISPOSABLE
  123. Assert.True (container2.SubViews.ElementAt (0).WasDisposed);
  124. #endif
  125. Assert.False (container2.SubViews.ElementAt (0).CanFocus);
  126. Assert.Null (container2.SubViews.ElementAt (0).Margin);
  127. Assert.Null (container2.SubViews.ElementAt (0).Border);
  128. Assert.Null (container2.SubViews.ElementAt (0).Padding);
  129. Assert.Null (view.SuperView);
  130. container2.Dispose ();
  131. #if DEBUG_IDISPOSABLE
  132. Assert.Empty (View.Instances);
  133. #endif
  134. }
  135. [Fact]
  136. [TestRespondersDisposed]
  137. public void Dispose_View ()
  138. {
  139. var view = new View ();
  140. Assert.NotNull (view.Margin);
  141. Assert.NotNull (view.Border);
  142. Assert.NotNull (view.Padding);
  143. #if DEBUG_IDISPOSABLE
  144. Assert.Equal (4, View.Instances.Count);
  145. #endif
  146. view.Dispose ();
  147. Assert.Null (view.Margin);
  148. Assert.Null (view.Border);
  149. Assert.Null (view.Padding);
  150. }
  151. [Fact]
  152. public void Internal_Tests ()
  153. {
  154. var rect = new Rectangle (1, 1, 10, 1);
  155. var view = new View { Frame = rect };
  156. }
  157. [Fact]
  158. [TestRespondersDisposed]
  159. public void New_Initializes ()
  160. {
  161. // Parameterless
  162. var r = new View ();
  163. Assert.NotNull (r);
  164. Assert.True (r.Enabled);
  165. Assert.True (r.Visible);
  166. Assert.Equal ($"View(){r.Viewport}", r.ToString ());
  167. Assert.False (r.CanFocus);
  168. Assert.False (r.HasFocus);
  169. Assert.Equal (new (0, 0, 0, 0), r.Viewport);
  170. Assert.Equal (new (0, 0, 0, 0), r.Frame);
  171. Assert.Null (r.Focused);
  172. Assert.Null (r.ColorScheme);
  173. Assert.Equal (0, r.Width);
  174. Assert.Equal (0, r.Height);
  175. Assert.Equal (0, r.X);
  176. Assert.Equal (0, r.Y);
  177. Assert.False (r.IsCurrentTop);
  178. Assert.Empty (r.Id);
  179. Assert.Empty (r.SubViews);
  180. Assert.False (r.WantContinuousButtonPressed);
  181. Assert.False (r.WantMousePositionReports);
  182. Assert.Null (r.SuperView);
  183. Assert.Null (r.MostFocused);
  184. Assert.Equal (TextDirection.LeftRight_TopBottom, r.TextDirection);
  185. r.Dispose ();
  186. // Empty Rect
  187. r = new () { Frame = Rectangle.Empty };
  188. Assert.NotNull (r);
  189. Assert.Equal ($"View(){r.Viewport}", r.ToString ());
  190. Assert.False (r.CanFocus);
  191. Assert.False (r.HasFocus);
  192. Assert.Equal (new (0, 0, 0, 0), r.Viewport);
  193. Assert.Equal (new (0, 0, 0, 0), r.Frame);
  194. Assert.Null (r.Focused);
  195. Assert.Null (r.ColorScheme);
  196. Assert.Equal (0, r.Width);
  197. Assert.Equal (0, r.Height);
  198. Assert.Equal (0, r.X);
  199. Assert.Equal (0, r.Y);
  200. Assert.False (r.IsCurrentTop);
  201. Assert.Empty (r.Id);
  202. Assert.Empty (r.SubViews);
  203. Assert.False (r.WantContinuousButtonPressed);
  204. Assert.False (r.WantMousePositionReports);
  205. Assert.Null (r.SuperView);
  206. Assert.Null (r.MostFocused);
  207. Assert.Equal (TextDirection.LeftRight_TopBottom, r.TextDirection);
  208. r.Dispose ();
  209. // Rect with values
  210. r = new () { Frame = new (1, 2, 3, 4) };
  211. Assert.NotNull (r);
  212. Assert.Equal ($"View(){r.Frame}", r.ToString ());
  213. Assert.False (r.CanFocus);
  214. Assert.False (r.HasFocus);
  215. Assert.Equal (new (0, 0, 3, 4), r.Viewport);
  216. Assert.Equal (new (1, 2, 3, 4), r.Frame);
  217. Assert.Null (r.Focused);
  218. Assert.Null (r.ColorScheme);
  219. Assert.Equal (3, r.Width);
  220. Assert.Equal (4, r.Height);
  221. Assert.Equal (1, r.X);
  222. Assert.Equal (2, r.Y);
  223. Assert.False (r.IsCurrentTop);
  224. Assert.Empty (r.Id);
  225. Assert.Empty (r.SubViews);
  226. Assert.False (r.WantContinuousButtonPressed);
  227. Assert.False (r.WantMousePositionReports);
  228. Assert.Null (r.SuperView);
  229. Assert.Null (r.MostFocused);
  230. Assert.Equal (TextDirection.LeftRight_TopBottom, r.TextDirection);
  231. r.Dispose ();
  232. // Initializes a view with a vertical direction
  233. r = new ()
  234. {
  235. Text = "Vertical View",
  236. TextDirection = TextDirection.TopBottom_LeftRight,
  237. Width = Dim.Auto (),
  238. Height = Dim.Auto ()
  239. };
  240. r.TextFormatter.WordWrap = false;
  241. Assert.NotNull (r);
  242. r.BeginInit ();
  243. r.EndInit ();
  244. Assert.False (r.CanFocus);
  245. Assert.False (r.HasFocus);
  246. Assert.Equal (new (0, 0, 1, 13), r.Viewport);
  247. Assert.Equal (new (0, 0, 1, 13), r.Frame);
  248. Assert.Null (r.Focused);
  249. Assert.Null (r.ColorScheme);
  250. Assert.False (r.IsCurrentTop);
  251. #if DEBUG
  252. Assert.Equal ("Vertical View", r.Id);
  253. #else
  254. Assert.Equal (string.Empty, r.Id);
  255. #endif
  256. Assert.Empty (r.SubViews);
  257. Assert.False (r.WantContinuousButtonPressed);
  258. Assert.False (r.WantMousePositionReports);
  259. Assert.Null (r.SuperView);
  260. Assert.Null (r.MostFocused);
  261. Assert.Equal (TextDirection.TopBottom_LeftRight, r.TextDirection);
  262. r.Dispose ();
  263. }
  264. [Fact]
  265. [TestRespondersDisposed]
  266. public void New_Methods_Return_False ()
  267. {
  268. var r = new View ();
  269. Assert.False (r.NewKeyDownEvent (Key.Empty));
  270. //Assert.False (r.OnKeyDown (new KeyEventArgs () { Key = Key.Unknown }));
  271. Assert.False (r.NewKeyUpEvent (Key.Empty));
  272. Assert.False (r.NewMouseEvent (new () { Flags = MouseFlags.AllEvents }));
  273. r.Dispose ();
  274. // TODO: Add more
  275. }
  276. [Fact]
  277. [AutoInitShutdown]
  278. public void Test_Nested_Views_With_Height_Equal_To_One ()
  279. {
  280. var v = new View { Width = 11, Height = 3, ColorScheme = new () };
  281. var top = new View { Width = Dim.Fill (), Height = 1 };
  282. var bottom = new View { Width = Dim.Fill (), Height = 1, Y = 2 };
  283. top.Add (new Label { Text = "111" });
  284. v.Add (top);
  285. v.Add (new LineView (Orientation.Horizontal) { Y = 1 });
  286. bottom.Add (new Label { Text = "222" });
  287. v.Add (bottom);
  288. v.BeginInit ();
  289. v.EndInit ();
  290. v.LayoutSubViews ();
  291. v.Draw ();
  292. var looksLike =
  293. @"
  294. 111
  295. ───────────
  296. 222";
  297. DriverAssert.AssertDriverContentsAre (looksLike, _output);
  298. v.Dispose ();
  299. top.Dispose ();
  300. bottom.Dispose ();
  301. }
  302. [Fact]
  303. [TestRespondersDisposed]
  304. public void View_With_No_Difference_Between_An_Object_Initializer_Compute_And_A_Absolute ()
  305. {
  306. // Object Initializer Computed
  307. var view = new View { X = 1, Y = 2, Width = 3, Height = 4 };
  308. // Object Initializer Absolute
  309. var super = new View { Frame = new (0, 0, 10, 10) };
  310. super.Add (view);
  311. super.BeginInit ();
  312. super.EndInit ();
  313. super.LayoutSubViews ();
  314. Assert.Equal (1, view.X);
  315. Assert.Equal (2, view.Y);
  316. Assert.Equal (3, view.Width);
  317. Assert.Equal (4, view.Height);
  318. Assert.False (view.Frame.IsEmpty);
  319. Assert.Equal (new (1, 2, 3, 4), view.Frame);
  320. Assert.False (view.Viewport.IsEmpty);
  321. Assert.Equal (new (0, 0, 3, 4), view.Viewport);
  322. view.LayoutSubViews ();
  323. Assert.Equal (1, view.X);
  324. Assert.Equal (2, view.Y);
  325. Assert.Equal (3, view.Width);
  326. Assert.Equal (4, view.Height);
  327. Assert.False (view.Frame.IsEmpty);
  328. Assert.False (view.Viewport.IsEmpty);
  329. super.Dispose ();
  330. #if DEBUG_IDISPOSABLE
  331. Assert.Empty (View.Instances);
  332. #endif
  333. // Default Constructor
  334. view = new ();
  335. Assert.Equal (0, view.X);
  336. Assert.Equal (0, view.Y);
  337. Assert.Equal (0, view.Width);
  338. Assert.Equal (0, view.Height);
  339. Assert.True (view.Frame.IsEmpty);
  340. Assert.True (view.Viewport.IsEmpty);
  341. view.Dispose ();
  342. // Object Initializer
  343. view = new () { X = 1, Y = 2, Text = "" };
  344. Assert.Equal (1, view.X);
  345. Assert.Equal (2, view.Y);
  346. Assert.Equal (0, view.Width);
  347. Assert.Equal (0, view.Height);
  348. Assert.False (view.Frame.IsEmpty);
  349. Assert.True (view.Viewport.IsEmpty);
  350. view.Dispose ();
  351. // Default Constructor and post assignment equivalent to Object Initializer
  352. view = new ();
  353. view.X = 1;
  354. view.Y = 2;
  355. view.Width = 3;
  356. view.Height = 4;
  357. super = new () { Frame = new (0, 0, 10, 10) };
  358. super.Add (view);
  359. super.BeginInit ();
  360. super.EndInit ();
  361. super.LayoutSubViews ();
  362. Assert.Equal (1, view.X);
  363. Assert.Equal (2, view.Y);
  364. Assert.Equal (3, view.Width);
  365. Assert.Equal (4, view.Height);
  366. Assert.False (view.Frame.IsEmpty);
  367. Assert.Equal (new (1, 2, 3, 4), view.Frame);
  368. Assert.False (view.Viewport.IsEmpty);
  369. Assert.Equal (new (0, 0, 3, 4), view.Viewport);
  370. super.Dispose ();
  371. }
  372. [Fact]
  373. [AutoInitShutdown]
  374. public void Visible_Clear_The_View_Output ()
  375. {
  376. var view = new View { Text = "Testing visibility." }; // use View, not Label to avoid AutoSize == true
  377. Assert.Equal (0, view.Frame.Width);
  378. Assert.Equal (0, view.Height);
  379. var win = new Window ();
  380. win.Add (view);
  381. Toplevel top = new ();
  382. top.Add (win);
  383. RunState rs = Application.Begin (top);
  384. view.Width = Dim.Auto ();
  385. view.Height = Dim.Auto ();
  386. Application.RunIteration (ref rs);
  387. Assert.Equal ("Testing visibility.".Length, view.Frame.Width);
  388. Assert.True (view.Visible);
  389. ((FakeDriver)Application.Driver!).SetBufferSize (30, 5);
  390. DriverAssert.AssertDriverContentsWithFrameAre (
  391. @"
  392. ┌────────────────────────────┐
  393. │Testing visibility. │
  394. │ │
  395. │ │
  396. └────────────────────────────┘
  397. ",
  398. _output
  399. );
  400. view.Visible = false;
  401. var firstIteration = false;
  402. Application.RunIteration (ref rs, firstIteration);
  403. DriverAssert.AssertDriverContentsWithFrameAre (
  404. @"
  405. ┌────────────────────────────┐
  406. │ │
  407. │ │
  408. │ │
  409. └────────────────────────────┘
  410. ",
  411. _output
  412. );
  413. Application.End (rs);
  414. top.Dispose ();
  415. }
  416. [Fact]
  417. [AutoInitShutdown]
  418. public void Visible_Sets_Also_Sets_SubViews ()
  419. {
  420. var button = new Button { Text = "Click Me" };
  421. var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
  422. win.Add (button);
  423. Toplevel top = new ();
  424. top.Add (win);
  425. var iterations = 0;
  426. Application.Iteration += (s, a) =>
  427. {
  428. iterations++;
  429. Assert.True (button.Visible);
  430. Assert.True (button.CanFocus);
  431. Assert.True (button.HasFocus);
  432. Assert.True (win.Visible);
  433. Assert.True (win.CanFocus);
  434. Assert.True (win.HasFocus);
  435. win.Visible = false;
  436. Assert.True (button.Visible);
  437. Assert.True (button.CanFocus);
  438. Assert.False (button.HasFocus);
  439. Assert.False (win.Visible);
  440. Assert.True (win.CanFocus);
  441. Assert.False (win.HasFocus);
  442. button.SetFocus ();
  443. Assert.False (button.HasFocus);
  444. Assert.False (win.HasFocus);
  445. win.SetFocus ();
  446. Assert.False (button.HasFocus);
  447. Assert.False (win.HasFocus);
  448. win.Visible = true;
  449. Assert.True (button.HasFocus);
  450. Assert.True (win.HasFocus);
  451. Application.RequestStop ();
  452. };
  453. Application.Run (top);
  454. top.Dispose ();
  455. Assert.Equal (1, iterations);
  456. }
  457. }