LayoutTests.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750
  1. using Xunit.Abstractions;
  2. namespace Terminal.Gui.LayoutTests;
  3. public class LayoutTests (ITestOutputHelper output)
  4. {
  5. private readonly ITestOutputHelper _output = output;
  6. [Fact]
  7. [AutoInitShutdown]
  8. public void Screen_Size_Change_Causes_Layout ()
  9. {
  10. Application.Top = new ();
  11. var view = new View
  12. {
  13. X = 3,
  14. Y = 2,
  15. Width = 10,
  16. Height = 1,
  17. Text = "0123456789"
  18. };
  19. Application.Top.Add (view);
  20. var rs = Application.Begin (Application.Top);
  21. Assert.Equal (new (0, 0, 80, 25), new Rectangle (0, 0, View.Driver.Cols, View.Driver.Rows));
  22. Assert.Equal (new (0, 0, View.Driver.Cols, View.Driver.Rows), Application.Top.Frame);
  23. Assert.Equal (new (0, 0, 80, 25), Application.Top.Frame);
  24. ((FakeDriver)Application.Driver!).SetBufferSize (20, 10);
  25. Assert.Equal (new (0, 0, View.Driver.Cols, View.Driver.Rows), Application.Top.Frame);
  26. Assert.Equal (new (0, 0, 20, 10), Application.Top.Frame);
  27. Application.End (rs);
  28. }
  29. [Fact]
  30. [TestRespondersDisposed]
  31. public void LayoutSubviews ()
  32. {
  33. var superRect = new Rectangle (0, 0, 100, 100);
  34. var super = new View { Frame = superRect, Text = "super" };
  35. var v1 = new View { X = 0, Y = 0, Width = 10, Height = 10 };
  36. var v2 = new View { X = 10, Y = 10, Width = 10, Height = 10 };
  37. super.Add (v1, v2);
  38. super.LayoutSubviews ();
  39. Assert.Equal (new (0, 0, 10, 10), v1.Frame);
  40. Assert.Equal (new (10, 10, 10, 10), v2.Frame);
  41. super.Dispose ();
  42. }
  43. [Fact]
  44. public void LayoutSubviews_No_SuperView ()
  45. {
  46. var root = new View ();
  47. var first = new View
  48. {
  49. Id = "first",
  50. X = 1,
  51. Y = 2,
  52. Height = 3,
  53. Width = 4
  54. };
  55. root.Add (first);
  56. var second = new View { Id = "second" };
  57. root.Add (second);
  58. second.X = Pos.Right (first) + 1;
  59. root.LayoutSubviews ();
  60. Assert.Equal (6, second.Frame.X);
  61. root.Dispose ();
  62. first.Dispose ();
  63. second.Dispose ();
  64. }
  65. [Fact]
  66. public void Add_Does_Not_Call_Layout ()
  67. {
  68. var superView = new View { Id = "superView" };
  69. var view = new View { Id = "view" };
  70. bool layoutStartedRaised = false;
  71. bool layoutCompleteRaised = false;
  72. superView.LayoutStarted += (sender, e) => layoutStartedRaised = true;
  73. superView.LayoutComplete += (sender, e) => layoutCompleteRaised = true;
  74. superView.Add (view);
  75. Assert.False (layoutStartedRaised);
  76. Assert.False (layoutCompleteRaised);
  77. superView.Remove (view);
  78. superView.Add (view);
  79. Assert.False (layoutStartedRaised);
  80. Assert.False (layoutCompleteRaised);
  81. }
  82. [Fact]
  83. public void LayoutSubViews_Raises_LayoutStarted_LayoutComplete ()
  84. {
  85. var superView = new View { Id = "superView" };
  86. int layoutStartedRaised = 0;
  87. int layoutCompleteRaised = 0;
  88. superView.LayoutStarted += (sender, e) => layoutStartedRaised++;
  89. superView.LayoutComplete += (sender, e) => layoutCompleteRaised++;
  90. superView.LayoutSubviews ();
  91. Assert.Equal (1, layoutStartedRaised);
  92. Assert.Equal (1, layoutCompleteRaised);
  93. superView.BeginInit ();
  94. superView.EndInit ();
  95. superView.LayoutSubviews ();
  96. Assert.Equal (2, layoutStartedRaised);
  97. Assert.Equal (2, layoutCompleteRaised);
  98. }
  99. [Fact]
  100. public void LayoutSubviews_RootHas_SuperView ()
  101. {
  102. var top = new View ();
  103. var root = new View ();
  104. top.Add (root);
  105. var first = new View
  106. {
  107. Id = "first",
  108. X = 1,
  109. Y = 2,
  110. Height = 3,
  111. Width = 4
  112. };
  113. root.Add (first);
  114. var second = new View { Id = "second" };
  115. root.Add (second);
  116. second.X = Pos.Right (first) + 1;
  117. root.LayoutSubviews ();
  118. Assert.Equal (6, second.Frame.X);
  119. root.Dispose ();
  120. top.Dispose ();
  121. first.Dispose ();
  122. second.Dispose ();
  123. }
  124. [Fact]
  125. public void LayoutSubviews_ViewThatRefsSubView_Throws ()
  126. {
  127. var root = new View ();
  128. var super = new View ();
  129. root.Add (super);
  130. var sub = new View ();
  131. super.Add (sub);
  132. super.Width = Dim.Width (sub);
  133. Assert.Throws<InvalidOperationException> (() => root.LayoutSubviews ());
  134. root.Dispose ();
  135. super.Dispose ();
  136. }
  137. [Fact]
  138. public void TopologicalSort_Missing_Add ()
  139. {
  140. var root = new View ();
  141. var sub1 = new View ();
  142. root.Add (sub1);
  143. var sub2 = new View ();
  144. sub1.Width = Dim.Width (sub2);
  145. Assert.Throws<InvalidOperationException> (() => root.LayoutSubviews ());
  146. sub2.Width = Dim.Width (sub1);
  147. Assert.Throws<InvalidOperationException> (() => root.LayoutSubviews ());
  148. root.Dispose ();
  149. sub1.Dispose ();
  150. sub2.Dispose ();
  151. }
  152. [Fact]
  153. public void TopologicalSort_Recursive_Ref ()
  154. {
  155. var root = new View ();
  156. var sub1 = new View ();
  157. root.Add (sub1);
  158. var sub2 = new View ();
  159. root.Add (sub2);
  160. sub2.Width = Dim.Width (sub2);
  161. Exception exception = Record.Exception (root.LayoutSubviews);
  162. Assert.Null (exception);
  163. root.Dispose ();
  164. sub1.Dispose ();
  165. sub2.Dispose ();
  166. }
  167. [Fact]
  168. public void LayoutSubviews_Uses_ContentSize ()
  169. {
  170. var superView = new View ()
  171. {
  172. Width = 5,
  173. Height = 5,
  174. };
  175. superView.SetContentSize (new (10, 10));
  176. var view = new View ()
  177. {
  178. X = Pos.Center ()
  179. };
  180. superView.Add (view);
  181. superView.LayoutSubviews ();
  182. Assert.Equal (5, view.Frame.X);
  183. superView.Dispose ();
  184. }
  185. // Test OnLayoutStarted/OnLayoutComplete - ensure that they are called at right times
  186. [Fact]
  187. public void LayoutSubviews_LayoutStarted_Complete ()
  188. {
  189. var superView = new View ();
  190. var view = new View ();
  191. var layoutStartedCount = 0;
  192. var layoutCompleteCount = 0;
  193. var borderLayoutStartedCount = 0;
  194. var borderLayoutCompleteCount = 0;
  195. view.LayoutStarted += (sender, e) => layoutStartedCount++;
  196. view.LayoutComplete += (sender, e) => layoutCompleteCount++;
  197. view.Border.LayoutStarted += (sender, e) => borderLayoutStartedCount++;
  198. view.Border.LayoutComplete += (sender, e) => borderLayoutCompleteCount++;
  199. superView.Add (view);
  200. Assert.Equal (0, borderLayoutStartedCount);
  201. Assert.Equal (0, borderLayoutCompleteCount);
  202. Assert.Equal (0, layoutStartedCount);
  203. Assert.Equal (0, layoutCompleteCount);
  204. superView.BeginInit ();
  205. Assert.Equal (0, borderLayoutStartedCount);
  206. Assert.Equal (0, borderLayoutCompleteCount);
  207. Assert.Equal (0, layoutStartedCount);
  208. Assert.Equal (0, layoutCompleteCount);
  209. superView.EndInit ();
  210. Assert.Equal (1, borderLayoutStartedCount);
  211. Assert.Equal (1, borderLayoutCompleteCount);
  212. Assert.Equal (1, layoutStartedCount);
  213. Assert.Equal (1, layoutCompleteCount);
  214. superView.LayoutSubviews ();
  215. Assert.Equal (1, borderLayoutStartedCount);
  216. Assert.Equal (1, borderLayoutCompleteCount);
  217. Assert.Equal (1, layoutStartedCount);
  218. Assert.Equal (1, layoutCompleteCount);
  219. superView.SetLayoutNeeded ();
  220. superView.LayoutSubviews ();
  221. Assert.Equal (2, borderLayoutStartedCount);
  222. Assert.Equal (2, borderLayoutCompleteCount);
  223. Assert.Equal (2, layoutStartedCount);
  224. Assert.Equal (2, layoutCompleteCount);
  225. superView.Dispose ();
  226. }
  227. [Fact]
  228. public void LayoutSubviews__Honors_IsLayoutNeeded ()
  229. {
  230. var superView = new View ();
  231. var view = new View ();
  232. var layoutStartedCount = 0;
  233. var layoutCompleteCount = 0;
  234. var borderLayoutStartedCount = 0;
  235. var borderLayoutCompleteCount = 0;
  236. view.LayoutStarted += (sender, e) => layoutStartedCount++;
  237. view.LayoutComplete += (sender, e) => layoutCompleteCount++;
  238. view.Border.LayoutStarted += (sender, e) => borderLayoutStartedCount++;
  239. view.Border.LayoutComplete += (sender, e) => borderLayoutCompleteCount++;
  240. superView.Add (view);
  241. superView.LayoutSubviews ();
  242. Assert.Equal (1, borderLayoutStartedCount);
  243. Assert.Equal (1, borderLayoutCompleteCount);
  244. Assert.Equal (1, layoutStartedCount);
  245. Assert.Equal (1, layoutCompleteCount);
  246. superView.LayoutSubviews ();
  247. Assert.Equal (1, borderLayoutStartedCount);
  248. Assert.Equal (1, borderLayoutCompleteCount);
  249. Assert.Equal (1, layoutStartedCount);
  250. Assert.Equal (1, layoutCompleteCount);
  251. superView.SetLayoutNeeded ();
  252. superView.LayoutSubviews ();
  253. Assert.Equal (2, borderLayoutStartedCount);
  254. Assert.Equal (2, borderLayoutCompleteCount);
  255. Assert.Equal (2, layoutStartedCount);
  256. Assert.Equal (2, layoutCompleteCount);
  257. superView.Dispose ();
  258. }
  259. [Fact]
  260. public void Set_X_Does_Not_Change_Frame_Until_Layout ()
  261. {
  262. var v = new View ();
  263. Assert.Equal (0, v.Frame.X);
  264. v.Layout ();
  265. Assert.Equal (0, v.Frame.X);
  266. v.X = 1;
  267. Assert.Equal (0, v.Frame.X);
  268. v.Layout ();
  269. Assert.Equal (1, v.Frame.X);
  270. v.X = 2;
  271. Assert.Equal (1, v.Frame.X);
  272. v.Layout ();
  273. Assert.Equal (2, v.Frame.X);
  274. }
  275. [Fact]
  276. public void Set_Y_Does_Not_Change_Frame_Until_Layout ()
  277. {
  278. var v = new View ();
  279. Assert.Equal (0, v.Frame.Y);
  280. v.Layout ();
  281. Assert.Equal (0, v.Frame.Y);
  282. v.Y = 1;
  283. Assert.Equal (0, v.Frame.Y);
  284. v.Layout ();
  285. Assert.Equal (1, v.Frame.Y);
  286. v.Y = 2;
  287. Assert.Equal (1, v.Frame.Y);
  288. v.Layout ();
  289. Assert.Equal (2, v.Frame.Y);
  290. }
  291. [Fact]
  292. public void Set_Width_Does_Not_Change_Frame_Until_Layout ()
  293. {
  294. var v = new View ();
  295. Assert.Equal (0, v.Frame.Width);
  296. v.Layout ();
  297. Assert.Equal (0, v.Frame.Width);
  298. v.Width = 1;
  299. Assert.Equal (0, v.Frame.Width);
  300. v.Layout ();
  301. Assert.Equal (1, v.Frame.Width);
  302. v.Width = 2;
  303. Assert.Equal (1, v.Frame.Width);
  304. v.Layout ();
  305. Assert.Equal (2, v.Frame.Width);
  306. }
  307. [Fact]
  308. public void Set_Height_Does_Not_Change_Frame_Until_Layout ()
  309. {
  310. var v = new View ();
  311. Assert.Equal (0, v.Frame.Height);
  312. v.Layout ();
  313. Assert.Equal (0, v.Frame.Height);
  314. v.Height = 1;
  315. Assert.Equal (0, v.Frame.Height);
  316. v.Layout ();
  317. Assert.Equal (1, v.Frame.Height);
  318. v.Height = 2;
  319. Assert.Equal (1, v.Frame.Height);
  320. v.Layout ();
  321. Assert.Equal (2, v.Frame.Height);
  322. }
  323. [Fact]
  324. [TestRespondersDisposed]
  325. public void Change_Height_or_Width_MakesComputed ()
  326. {
  327. var v = new View { Frame = Rectangle.Empty };
  328. v.Height = Dim.Fill ();
  329. v.Width = Dim.Fill ();
  330. v.Dispose ();
  331. }
  332. [Fact]
  333. [TestRespondersDisposed]
  334. public void Change_X_or_Y_Absolute ()
  335. {
  336. var frame = new Rectangle (1, 2, 3, 4);
  337. var newFrame = new Rectangle (10, 20, 3, 4);
  338. var v = new View { Frame = frame };
  339. v.X = newFrame.X;
  340. v.Y = newFrame.Y;
  341. v.Layout ();
  342. Assert.Equal (newFrame, v.Frame);
  343. Assert.Equal (
  344. new (0, 0, newFrame.Width, newFrame.Height),
  345. v.Viewport
  346. ); // With Absolute Viewport *is* deterministic before Layout
  347. Assert.Equal ($"Absolute({newFrame.X})", v.X.ToString ());
  348. Assert.Equal ($"Absolute({newFrame.Y})", v.Y.ToString ());
  349. Assert.Equal (Dim.Absolute (3), v.Width);
  350. Assert.Equal (Dim.Absolute (4), v.Height);
  351. v.Dispose ();
  352. }
  353. [Fact]
  354. [TestRespondersDisposed]
  355. public void Change_X_or_Y_MakesComputed ()
  356. {
  357. var v = new View { Frame = Rectangle.Empty };
  358. v.X = Pos.Center ();
  359. v.Y = Pos.Center ();
  360. v.Dispose ();
  361. }
  362. [Fact]
  363. [TestRespondersDisposed]
  364. public void Change_X_Y_Height_Width_Absolute ()
  365. {
  366. var v = new View { Frame = Rectangle.Empty };
  367. v.X = 1;
  368. v.Y = 2;
  369. v.Height = 3;
  370. v.Width = 4;
  371. v.Dispose ();
  372. v = new () { Frame = Rectangle.Empty };
  373. v.X = Pos.Center ();
  374. v.Y = Pos.Center ();
  375. v.Width = Dim.Fill ();
  376. v.Height = Dim.Fill ();
  377. v.Dispose ();
  378. v = new () { Frame = Rectangle.Empty };
  379. v.X = Pos.Center ();
  380. v.Y = Pos.Center ();
  381. v.Width = Dim.Fill ();
  382. v.Height = Dim.Fill ();
  383. v.X = 1;
  384. v.Dispose ();
  385. v = new () { Frame = Rectangle.Empty };
  386. v.X = Pos.Center ();
  387. v.Y = Pos.Center ();
  388. v.Width = Dim.Fill ();
  389. v.Height = Dim.Fill ();
  390. v.Y = 2;
  391. v.Dispose ();
  392. v = new () { Frame = Rectangle.Empty };
  393. v.X = Pos.Center ();
  394. v.Y = Pos.Center ();
  395. v.Width = Dim.Fill ();
  396. v.Height = Dim.Fill ();
  397. v.Width = 3;
  398. v.Dispose ();
  399. v = new () { Frame = Rectangle.Empty };
  400. v.X = Pos.Center ();
  401. v.Y = Pos.Center ();
  402. v.Width = Dim.Fill ();
  403. v.Height = Dim.Fill ();
  404. v.Height = 3;
  405. v.Dispose ();
  406. v = new () { Frame = Rectangle.Empty };
  407. v.X = Pos.Center ();
  408. v.Y = Pos.Center ();
  409. v.Width = Dim.Fill ();
  410. v.Height = Dim.Fill ();
  411. v.X = 1;
  412. v.Y = 2;
  413. v.Height = 3;
  414. v.Width = 4;
  415. v.Dispose ();
  416. }
  417. [Fact]
  418. public void Constructor ()
  419. {
  420. var v = new View ();
  421. v.Dispose ();
  422. var frame = Rectangle.Empty;
  423. v = new () { Frame = frame };
  424. v.Layout ();
  425. Assert.Equal (frame, v.Frame);
  426. Assert.Equal (
  427. new (0, 0, frame.Width, frame.Height),
  428. v.Viewport
  429. ); // With Absolute Viewport *is* deterministic before Layout
  430. Assert.Equal (Pos.Absolute (0), v.X);
  431. Assert.Equal (Pos.Absolute (0), v.Y);
  432. Assert.Equal (Dim.Absolute (0), v.Width);
  433. Assert.Equal (Dim.Absolute (0), v.Height);
  434. v.Dispose ();
  435. frame = new (1, 2, 3, 4);
  436. v = new () { Frame = frame };
  437. v.Layout ();
  438. Assert.Equal (frame, v.Frame);
  439. Assert.Equal (
  440. new (0, 0, frame.Width, frame.Height),
  441. v.Viewport
  442. ); // With Absolute Viewport *is* deterministic before Layout
  443. Assert.Equal (Pos.Absolute (1), v.X);
  444. Assert.Equal (Pos.Absolute (2), v.Y);
  445. Assert.Equal (Dim.Absolute (3), v.Width);
  446. Assert.Equal (Dim.Absolute (4), v.Height);
  447. v.Dispose ();
  448. v = new () { Frame = frame, Text = "v" };
  449. v.Layout ();
  450. Assert.Equal (frame, v.Frame);
  451. Assert.Equal (
  452. new (0, 0, frame.Width, frame.Height),
  453. v.Viewport
  454. ); // With Absolute Viewport *is* deterministic before Layout
  455. Assert.Equal (Pos.Absolute (1), v.X);
  456. Assert.Equal (Pos.Absolute (2), v.Y);
  457. Assert.Equal (Dim.Absolute (3), v.Width);
  458. Assert.Equal (Dim.Absolute (4), v.Height);
  459. v.Dispose ();
  460. v = new () { X = frame.X, Y = frame.Y, Text = "v" };
  461. v.Layout ();
  462. Assert.Equal (new (frame.X, frame.Y, 0, 0), v.Frame);
  463. Assert.Equal (new (0, 0, 0, 0), v.Viewport); // With Absolute Viewport *is* deterministic before Layout
  464. Assert.Equal (Pos.Absolute (1), v.X);
  465. Assert.Equal (Pos.Absolute (2), v.Y);
  466. Assert.Equal (Dim.Absolute (0), v.Width);
  467. Assert.Equal (Dim.Absolute (0), v.Height);
  468. v.Dispose ();
  469. v = new ();
  470. v.Layout ();
  471. Assert.Equal (new (0, 0, 0, 0), v.Frame);
  472. Assert.Equal (new (0, 0, 0, 0), v.Viewport); // With Absolute Viewport *is* deterministic before Layout
  473. Assert.Equal (Pos.Absolute (0), v.X);
  474. Assert.Equal (Pos.Absolute (0), v.Y);
  475. Assert.Equal (Dim.Absolute (0), v.Width);
  476. Assert.Equal (Dim.Absolute (0), v.Height);
  477. v.Dispose ();
  478. v = new () { X = frame.X, Y = frame.Y, Width = frame.Width, Height = frame.Height };
  479. v.Layout ();
  480. Assert.Equal (new (frame.X, frame.Y, 3, 4), v.Frame);
  481. Assert.Equal (new (0, 0, 3, 4), v.Viewport); // With Absolute Viewport *is* deterministic before Layout
  482. Assert.Equal (Pos.Absolute (1), v.X);
  483. Assert.Equal (Pos.Absolute (2), v.Y);
  484. Assert.Equal (Dim.Absolute (3), v.Width);
  485. Assert.Equal (Dim.Absolute (4), v.Height);
  486. v.Dispose ();
  487. }
  488. /// <summary>
  489. /// This tests the special case in LayoutSubviews. See https://github.com/gui-cs/Terminal.Gui/issues/2461</summary>
  490. [Fact]
  491. public void Nested_SubViews_Ref_Topmost_SuperView ()
  492. {
  493. var superView = new View { Width = 80, Height = 25, Text = "superView" };
  494. var subView1 = new View
  495. {
  496. Id = "subView1 - refs superView",
  497. Width = Dim.Width (superView) - 2, // 78
  498. Height = Dim.Height (superView) - 2 // 23
  499. };
  500. superView.Add (subView1);
  501. var subView1OfSubView1 = new View ()
  502. {
  503. Id = "subView1OfSubView1 - refs superView",
  504. Width = Dim.Width (superView) - 4, // 76
  505. Height = Dim.Height (superView) - 4 // 21
  506. };
  507. subView1.Add (subView1OfSubView1);
  508. superView.Layout ();
  509. Assert.Equal (80, superView.Frame.Width);
  510. Assert.Equal (25, superView.Frame.Height);
  511. Assert.Equal (78, subView1.Frame.Width);
  512. Assert.Equal (23, subView1.Frame.Height);
  513. //Assert.Equal (76, subView2.Frame.Width);
  514. //Assert.Equal (21, subView2.Frame.Height);
  515. Assert.Equal (76, subView1OfSubView1.Frame.Width);
  516. Assert.Equal (21, subView1OfSubView1.Frame.Height);
  517. superView.Dispose ();
  518. }
  519. /// <summary>This is an intentionally obtuse test. See https://github.com/gui-cs/Terminal.Gui/issues/2461</summary>
  520. [Fact]
  521. public void Does_Not_Throw_If_Nested_SubViews_Ref_Topmost_SuperView ()
  522. {
  523. var t = new View { Width = 80, Height = 25, Text = "top" };
  524. var w = new Window
  525. {
  526. Width = Dim.Width (t) - 2, // 78
  527. Height = Dim.Height (t) - 2 // 23
  528. };
  529. var f = new FrameView ();
  530. var v1 = new View
  531. {
  532. Width = Dim.Width (w) - 2, // 76
  533. Height = Dim.Height (w) - 2 // 21
  534. };
  535. var v2 = new View
  536. {
  537. Width = Dim.Width (v1) - 2, // 74
  538. Height = Dim.Height (v1) - 2 // 19
  539. };
  540. f.Width = Dim.Width (t) - Dim.Width (w) + 4; // 80 - 74 = 6
  541. f.Height = Dim.Height (t) - Dim.Height (w) + 4; // 25 - 19 = 6
  542. f.Add (v1, v2);
  543. w.Add (f);
  544. t.Add (w);
  545. t.BeginInit ();
  546. t.EndInit ();
  547. // f references t and w here; t is f's super-superview and w is f's superview. This is supported!
  548. Exception exception = Record.Exception (() => t.Layout ());
  549. Assert.Null (exception);
  550. Assert.Equal (80, t.Frame.Width);
  551. Assert.Equal (25, t.Frame.Height);
  552. Assert.Equal (78, w.Frame.Width);
  553. Assert.Equal (23, w.Frame.Height);
  554. Assert.Equal (6, f.Frame.Width);
  555. Assert.Equal (6, f.Frame.Height);
  556. Assert.Equal (76, v1.Frame.Width);
  557. Assert.Equal (21, v1.Frame.Height);
  558. Assert.Equal (74, v2.Frame.Width);
  559. Assert.Equal (19, v2.Frame.Height);
  560. t.Dispose ();
  561. }
  562. /// <summary>This is an intentionally obtuse test. See https://github.com/gui-cs/Terminal.Gui/issues/2461</summary>
  563. [Fact]
  564. [TestRespondersDisposed]
  565. public void Throw_If_SuperView_Refs_SubView ()
  566. {
  567. var topSuperView = new View { Width = 80, Height = 25 };
  568. var superViewRefsTopSuperView = new Window
  569. {
  570. Width = Dim.Width (topSuperView) - 2, // 78
  571. Height = Dim.Height (topSuperView) - 2 // 23
  572. };
  573. var v1 = new View
  574. {
  575. Width = Dim.Width (superViewRefsTopSuperView) - 2, // 76
  576. Height = Dim.Height (superViewRefsTopSuperView) - 2 // 21
  577. };
  578. var v2 = new View
  579. {
  580. Width = Dim.Width (v1) - 2, // 74
  581. Height = Dim.Height (v1) - 2 // 19
  582. };
  583. var superView = new FrameView ();
  584. superView.Width = Dim.Width (topSuperView) - Dim.Width (v2); // 80 - 74 = 6
  585. superView.Height = Dim.Height (topSuperView) - Dim.Height (v2); // 25 - 19 = 6
  586. superView.Add (v1, v2);
  587. superViewRefsTopSuperView.Add (superView);
  588. topSuperView.Add (superViewRefsTopSuperView);
  589. topSuperView.SetRelativeLayout (new (100, 100));
  590. Assert.Throws<InvalidOperationException> (() => topSuperView.LayoutSubviews());
  591. Assert.Equal (80, topSuperView.Frame.Width);
  592. Assert.Equal (25, topSuperView.Frame.Height);
  593. Assert.Equal (78, superViewRefsTopSuperView.Frame.Width);
  594. Assert.Equal (23, superViewRefsTopSuperView.Frame.Height);
  595. Assert.Equal (6, superView.Frame.Width);
  596. Assert.Equal (6, superView.Frame.Height);
  597. Assert.Equal (76, v1.Frame.Width);
  598. Assert.Equal (21, v1.Frame.Height);
  599. Assert.Equal (74, v2.Frame.Width);
  600. Assert.Equal (19, v2.Frame.Height);
  601. topSuperView.Dispose ();
  602. }
  603. }