DimTests.cs 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Text;
  5. using System.Threading;
  6. using Xunit;
  7. using Xunit.Abstractions;
  8. // Alias Console to MockConsole so we don't accidentally use Console
  9. using Console = Terminal.Gui.FakeConsole;
  10. namespace Terminal.Gui.ViewTests;
  11. public class DimTests {
  12. readonly ITestOutputHelper _output;
  13. readonly string [] expecteds = new string [21] {
  14. @"
  15. ┌────────────────────┐
  16. │View with long text │
  17. │ │
  18. └────────────────────┘",
  19. @"
  20. ┌────────────────────┐
  21. │View with long text │
  22. │Label 0 │
  23. │Label 0 │
  24. └────────────────────┘",
  25. @"
  26. ┌────────────────────┐
  27. │View with long text │
  28. │Label 0 │
  29. │Label 1 │
  30. │Label 1 │
  31. └────────────────────┘",
  32. @"
  33. ┌────────────────────┐
  34. │View with long text │
  35. │Label 0 │
  36. │Label 1 │
  37. │Label 2 │
  38. │Label 2 │
  39. └────────────────────┘",
  40. @"
  41. ┌────────────────────┐
  42. │View with long text │
  43. │Label 0 │
  44. │Label 1 │
  45. │Label 2 │
  46. │Label 3 │
  47. │Label 3 │
  48. └────────────────────┘",
  49. @"
  50. ┌────────────────────┐
  51. │View with long text │
  52. │Label 0 │
  53. │Label 1 │
  54. │Label 2 │
  55. │Label 3 │
  56. │Label 4 │
  57. │Label 4 │
  58. └────────────────────┘",
  59. @"
  60. ┌────────────────────┐
  61. │View with long text │
  62. │Label 0 │
  63. │Label 1 │
  64. │Label 2 │
  65. │Label 3 │
  66. │Label 4 │
  67. │Label 5 │
  68. │Label 5 │
  69. └────────────────────┘",
  70. @"
  71. ┌────────────────────┐
  72. │View with long text │
  73. │Label 0 │
  74. │Label 1 │
  75. │Label 2 │
  76. │Label 3 │
  77. │Label 4 │
  78. │Label 5 │
  79. │Label 6 │
  80. │Label 6 │
  81. └────────────────────┘",
  82. @"
  83. ┌────────────────────┐
  84. │View with long text │
  85. │Label 0 │
  86. │Label 1 │
  87. │Label 2 │
  88. │Label 3 │
  89. │Label 4 │
  90. │Label 5 │
  91. │Label 6 │
  92. │Label 7 │
  93. │Label 7 │
  94. └────────────────────┘",
  95. @"
  96. ┌────────────────────┐
  97. │View with long text │
  98. │Label 0 │
  99. │Label 1 │
  100. │Label 2 │
  101. │Label 3 │
  102. │Label 4 │
  103. │Label 5 │
  104. │Label 6 │
  105. │Label 7 │
  106. │Label 8 │
  107. │Label 8 │
  108. └────────────────────┘",
  109. @"
  110. ┌────────────────────┐
  111. │View with long text │
  112. │Label 0 │
  113. │Label 1 │
  114. │Label 2 │
  115. │Label 3 │
  116. │Label 4 │
  117. │Label 5 │
  118. │Label 6 │
  119. │Label 7 │
  120. │Label 8 │
  121. │Label 9 │
  122. │Label 9 │
  123. └────────────────────┘",
  124. @"
  125. ┌────────────────────┐
  126. │View with long text │
  127. │Label 0 │
  128. │Label 1 │
  129. │Label 2 │
  130. │Label 3 │
  131. │Label 4 │
  132. │Label 5 │
  133. │Label 6 │
  134. │Label 7 │
  135. │Label 8 │
  136. │Label 9 │
  137. │Label 10 │
  138. │Label 10 │
  139. └────────────────────┘",
  140. @"
  141. ┌────────────────────┐
  142. │View with long text │
  143. │Label 0 │
  144. │Label 1 │
  145. │Label 2 │
  146. │Label 3 │
  147. │Label 4 │
  148. │Label 5 │
  149. │Label 6 │
  150. │Label 7 │
  151. │Label 8 │
  152. │Label 9 │
  153. │Label 10 │
  154. │Label 11 │
  155. │Label 11 │
  156. └────────────────────┘",
  157. @"
  158. ┌────────────────────┐
  159. │View with long text │
  160. │Label 0 │
  161. │Label 1 │
  162. │Label 2 │
  163. │Label 3 │
  164. │Label 4 │
  165. │Label 5 │
  166. │Label 6 │
  167. │Label 7 │
  168. │Label 8 │
  169. │Label 9 │
  170. │Label 10 │
  171. │Label 11 │
  172. │Label 12 │
  173. │Label 12 │
  174. └────────────────────┘",
  175. @"
  176. ┌────────────────────┐
  177. │View with long text │
  178. │Label 0 │
  179. │Label 1 │
  180. │Label 2 │
  181. │Label 3 │
  182. │Label 4 │
  183. │Label 5 │
  184. │Label 6 │
  185. │Label 7 │
  186. │Label 8 │
  187. │Label 9 │
  188. │Label 10 │
  189. │Label 11 │
  190. │Label 12 │
  191. │Label 13 │
  192. │Label 13 │
  193. └────────────────────┘",
  194. @"
  195. ┌────────────────────┐
  196. │View with long text │
  197. │Label 0 │
  198. │Label 1 │
  199. │Label 2 │
  200. │Label 3 │
  201. │Label 4 │
  202. │Label 5 │
  203. │Label 6 │
  204. │Label 7 │
  205. │Label 8 │
  206. │Label 9 │
  207. │Label 10 │
  208. │Label 11 │
  209. │Label 12 │
  210. │Label 13 │
  211. │Label 14 │
  212. │Label 14 │
  213. └────────────────────┘",
  214. @"
  215. ┌────────────────────┐
  216. │View with long text │
  217. │Label 0 │
  218. │Label 1 │
  219. │Label 2 │
  220. │Label 3 │
  221. │Label 4 │
  222. │Label 5 │
  223. │Label 6 │
  224. │Label 7 │
  225. │Label 8 │
  226. │Label 9 │
  227. │Label 10 │
  228. │Label 11 │
  229. │Label 12 │
  230. │Label 13 │
  231. │Label 14 │
  232. │Label 15 │
  233. │Label 15 │
  234. └────────────────────┘",
  235. @"
  236. ┌────────────────────┐
  237. │View with long text │
  238. │Label 0 │
  239. │Label 1 │
  240. │Label 2 │
  241. │Label 3 │
  242. │Label 4 │
  243. │Label 5 │
  244. │Label 6 │
  245. │Label 7 │
  246. │Label 8 │
  247. │Label 9 │
  248. │Label 10 │
  249. │Label 11 │
  250. │Label 12 │
  251. │Label 13 │
  252. │Label 14 │
  253. │Label 15 │
  254. │Label 16 │
  255. │Label 16 │
  256. └────────────────────┘",
  257. @"
  258. ┌────────────────────┐
  259. │View with long text │
  260. │Label 0 │
  261. │Label 1 │
  262. │Label 2 │
  263. │Label 3 │
  264. │Label 4 │
  265. │Label 5 │
  266. │Label 6 │
  267. │Label 7 │
  268. │Label 8 │
  269. │Label 9 │
  270. │Label 10 │
  271. │Label 11 │
  272. │Label 12 │
  273. │Label 13 │
  274. │Label 14 │
  275. │Label 15 │
  276. │Label 16 │
  277. │Label 17 │
  278. │Label 17 │
  279. └────────────────────┘",
  280. @"
  281. ┌────────────────────┐
  282. │View with long text │
  283. │Label 0 │
  284. │Label 1 │
  285. │Label 2 │
  286. │Label 3 │
  287. │Label 4 │
  288. │Label 5 │
  289. │Label 6 │
  290. │Label 7 │
  291. │Label 8 │
  292. │Label 9 │
  293. │Label 10 │
  294. │Label 11 │
  295. │Label 12 │
  296. │Label 13 │
  297. │Label 14 │
  298. │Label 15 │
  299. │Label 16 │
  300. │Label 17 │
  301. │Label 18 │
  302. │Label 18 │
  303. └────────────────────┘",
  304. @"
  305. ┌────────────────────┐
  306. │View with long text │
  307. │Label 0 │
  308. │Label 1 │
  309. │Label 2 │
  310. │Label 3 │
  311. │Label 4 │
  312. │Label 5 │
  313. │Label 6 │
  314. │Label 7 │
  315. │Label 8 │
  316. │Label 9 │
  317. │Label 10 │
  318. │Label 11 │
  319. │Label 12 │
  320. │Label 13 │
  321. │Label 14 │
  322. │Label 15 │
  323. │Label 16 │
  324. │Label 17 │
  325. │Label 18 │
  326. │Label 19 │
  327. │Label 19 │
  328. └────────────────────┘"
  329. };
  330. public DimTests (ITestOutputHelper output)
  331. {
  332. _output = output;
  333. Console.OutputEncoding = Encoding.Default;
  334. // Change current culture
  335. var culture = CultureInfo.CreateSpecificCulture ("en-US");
  336. Thread.CurrentThread.CurrentCulture = culture;
  337. Thread.CurrentThread.CurrentUICulture = culture;
  338. }
  339. [Fact]
  340. public void New_Works ()
  341. {
  342. var dim = new Dim ();
  343. Assert.Equal ("Terminal.Gui.Dim", dim.ToString ());
  344. }
  345. [Fact]
  346. public void Sized_SetsValue ()
  347. {
  348. var dim = Dim.Sized (0);
  349. Assert.Equal ("Absolute(0)", dim.ToString ());
  350. var testVal = 5;
  351. dim = Dim.Sized (testVal);
  352. Assert.Equal ($"Absolute({testVal})", dim.ToString ());
  353. testVal = -1;
  354. dim = Dim.Sized (testVal);
  355. Assert.Equal ($"Absolute({testVal})", dim.ToString ());
  356. }
  357. [Fact]
  358. public void Sized_Equals ()
  359. {
  360. var n1 = 0;
  361. var n2 = 0;
  362. var dim1 = Dim.Sized (n1);
  363. var dim2 = Dim.Sized (n2);
  364. Assert.Equal (dim1, dim2);
  365. n1 = n2 = 1;
  366. dim1 = Dim.Sized (n1);
  367. dim2 = Dim.Sized (n2);
  368. Assert.Equal (dim1, dim2);
  369. n1 = n2 = -1;
  370. dim1 = Dim.Sized (n1);
  371. dim2 = Dim.Sized (n2);
  372. Assert.Equal (dim1, dim2);
  373. n1 = 0;
  374. n2 = 1;
  375. dim1 = Dim.Sized (n1);
  376. dim2 = Dim.Sized (n2);
  377. Assert.NotEqual (dim1, dim2);
  378. }
  379. [Fact]
  380. public void Width_Set_To_Null_Throws ()
  381. {
  382. var dim = Dim.Width (null);
  383. Assert.Throws<NullReferenceException> (() => dim.ToString ());
  384. }
  385. [Fact]
  386. [TestRespondersDisposed]
  387. public void SetsValue ()
  388. {
  389. var testVal = Rect.Empty;
  390. var testValView = new View (testVal);
  391. var dim = Dim.Width (testValView);
  392. Assert.Equal ($"View(Width,View(){testVal})", dim.ToString ());
  393. testValView.Dispose ();
  394. testVal = new Rect (1, 2, 3, 4);
  395. testValView = new View (testVal);
  396. dim = Dim.Width (testValView);
  397. Assert.Equal ($"View(Width,View(){testVal})", dim.ToString ());
  398. testValView.Dispose ();
  399. }
  400. [Fact]
  401. [TestRespondersDisposed]
  402. public void Width_Equals ()
  403. {
  404. var testRect1 = Rect.Empty;
  405. var view1 = new View (testRect1);
  406. var testRect2 = Rect.Empty;
  407. var view2 = new View (testRect2);
  408. var dim1 = Dim.Width (view1);
  409. var dim2 = Dim.Width (view1);
  410. // FIXED: Dim.Width should support Equals() and this should change to Equal.
  411. Assert.Equal (dim1, dim2);
  412. dim2 = Dim.Width (view2);
  413. Assert.NotEqual (dim1, dim2);
  414. testRect1 = new Rect (0, 1, 2, 3);
  415. view1 = new View (testRect1);
  416. testRect2 = new Rect (0, 1, 2, 3);
  417. dim1 = Dim.Width (view1);
  418. dim2 = Dim.Width (view1);
  419. // FIXED: Dim.Width should support Equals() and this should change to Equal.
  420. Assert.Equal (dim1, dim2);
  421. testRect1 = new Rect (0, -1, 2, 3);
  422. view1 = new View (testRect1);
  423. testRect2 = new Rect (0, -1, 2, 3);
  424. dim1 = Dim.Width (view1);
  425. dim2 = Dim.Width (view1);
  426. // FIXED: Dim.Width should support Equals() and this should change to Equal.
  427. Assert.Equal (dim1, dim2);
  428. testRect1 = new Rect (0, -1, 2, 3);
  429. view1 = new View (testRect1);
  430. testRect2 = Rect.Empty;
  431. view2 = new View (testRect2);
  432. dim1 = Dim.Width (view1);
  433. dim2 = Dim.Width (view2);
  434. Assert.NotEqual (dim1, dim2);
  435. #if DEBUG_IDISPOSABLE
  436. // HACK: Force clean up of Responders to avoid having to Dispose all the Views created above.
  437. Responder.Instances.Clear ();
  438. Assert.Empty (Responder.Instances);
  439. #endif
  440. }
  441. [Fact]
  442. public void Height_Set_To_Null_Throws ()
  443. {
  444. var dim = Dim.Height (null);
  445. Assert.Throws<NullReferenceException> (() => dim.ToString ());
  446. }
  447. [Fact]
  448. [TestRespondersDisposed]
  449. public void Height_SetsValue ()
  450. {
  451. var testVal = Rect.Empty;
  452. var testValview = new View (testVal);
  453. var dim = Dim.Height (testValview);
  454. Assert.Equal ($"View(Height,View(){testVal})", dim.ToString ());
  455. testValview.Dispose ();
  456. testVal = new Rect (1, 2, 3, 4);
  457. testValview = new View (testVal);
  458. dim = Dim.Height (testValview);
  459. Assert.Equal ($"View(Height,View(){testVal})", dim.ToString ());
  460. testValview.Dispose ();
  461. }
  462. // TODO: Other Dim.Height tests (e.g. Equal?)
  463. [Fact]
  464. public void Fill_SetsValue ()
  465. {
  466. var testMargin = 0;
  467. var dim = Dim.Fill ();
  468. Assert.Equal ($"Fill({testMargin})", dim.ToString ());
  469. testMargin = 0;
  470. dim = Dim.Fill (testMargin);
  471. Assert.Equal ($"Fill({testMargin})", dim.ToString ());
  472. testMargin = 5;
  473. dim = Dim.Fill (testMargin);
  474. Assert.Equal ($"Fill({testMargin})", dim.ToString ());
  475. }
  476. [Fact]
  477. public void Fill_Equal ()
  478. {
  479. var margin1 = 0;
  480. var margin2 = 0;
  481. var dim1 = Dim.Fill (margin1);
  482. var dim2 = Dim.Fill (margin2);
  483. Assert.Equal (dim1, dim2);
  484. }
  485. [Fact]
  486. public void Percent_SetsValue ()
  487. {
  488. float f = 0;
  489. var dim = Dim.Percent (f);
  490. Assert.Equal ($"Factor({f / 100:0.###},{false})", dim.ToString ());
  491. f = 0.5F;
  492. dim = Dim.Percent (f);
  493. Assert.Equal ($"Factor({f / 100:0.###},{false})", dim.ToString ());
  494. f = 100;
  495. dim = Dim.Percent (f);
  496. Assert.Equal ($"Factor({f / 100:0.###},{false})", dim.ToString ());
  497. }
  498. [Fact]
  499. public void Percent_Equals ()
  500. {
  501. float n1 = 0;
  502. float n2 = 0;
  503. var dim1 = Dim.Percent (n1);
  504. var dim2 = Dim.Percent (n2);
  505. Assert.Equal (dim1, dim2);
  506. n1 = n2 = 1;
  507. dim1 = Dim.Percent (n1);
  508. dim2 = Dim.Percent (n2);
  509. Assert.Equal (dim1, dim2);
  510. n1 = n2 = 0.5f;
  511. dim1 = Dim.Percent (n1);
  512. dim2 = Dim.Percent (n2);
  513. Assert.Equal (dim1, dim2);
  514. n1 = n2 = 100f;
  515. dim1 = Dim.Percent (n1);
  516. dim2 = Dim.Percent (n2);
  517. Assert.Equal (dim1, dim2);
  518. n1 = n2 = 0.3f;
  519. dim1 = Dim.Percent (n1, true);
  520. dim2 = Dim.Percent (n2, true);
  521. Assert.Equal (dim1, dim2);
  522. n1 = n2 = 0.3f;
  523. dim1 = Dim.Percent (n1);
  524. dim2 = Dim.Percent (n2, true);
  525. Assert.NotEqual (dim1, dim2);
  526. n1 = 0;
  527. n2 = 1;
  528. dim1 = Dim.Percent (n1);
  529. dim2 = Dim.Percent (n2);
  530. Assert.NotEqual (dim1, dim2);
  531. n1 = 0.5f;
  532. n2 = 1.5f;
  533. dim1 = Dim.Percent (n1);
  534. dim2 = Dim.Percent (n2);
  535. Assert.NotEqual (dim1, dim2);
  536. }
  537. [Fact]
  538. public void Percent_Invalid_Throws ()
  539. {
  540. var dim = Dim.Percent (0);
  541. Assert.Throws<ArgumentException> (() => dim = Dim.Percent (-1));
  542. Assert.Throws<ArgumentException> (() => dim = Dim.Percent (101));
  543. Assert.Throws<ArgumentException> (() => dim = Dim.Percent (100.0001F));
  544. Assert.Throws<ArgumentException> (() => dim = Dim.Percent (1000001));
  545. }
  546. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  547. // TODO: A new test that calls SetRelativeLayout directly is needed.
  548. [Fact]
  549. [TestRespondersDisposed]
  550. public void Dim_Validation_Do_Not_Throws_If_NewValue_Is_DimAbsolute_And_OldValue_Is_Null ()
  551. {
  552. var t = new View ("top") { Width = 80, Height = 25 };
  553. var w = new Window (new Rect (1, 2, 4, 5)) { Title = "w" };
  554. t.Add (w);
  555. t.LayoutSubviews ();
  556. Assert.Equal (3, w.Width = 3);
  557. Assert.Equal (4, w.Height = 4);
  558. t.Dispose ();
  559. }
  560. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  561. // TODO: A new test that calls SetRelativeLayout directly is needed.
  562. [Fact]
  563. [TestRespondersDisposed]
  564. public void Dim_Validation_Do_Not_Throws_If_NewValue_Is_DimAbsolute_And_OldValue_Is_Another_Type_After_Sets_To_LayoutStyle_Absolute ()
  565. {
  566. var t = new View ("top") { Width = 80, Height = 25 };
  567. var w = new Window {
  568. Width = Dim.Fill (),
  569. Height = Dim.Sized (10)
  570. };
  571. var v = new View ("v") {
  572. Width = Dim.Width (w) - 2,
  573. Height = Dim.Percent (10)
  574. };
  575. w.Add (v);
  576. t.Add (w);
  577. Assert.Equal (LayoutStyle.Absolute, t.LayoutStyle);
  578. Assert.Equal (LayoutStyle.Computed, w.LayoutStyle);
  579. Assert.Equal (LayoutStyle.Computed, v.LayoutStyle);
  580. t.LayoutSubviews ();
  581. Assert.Equal (2, v.Width = 2);
  582. Assert.Equal (2, v.Height = 2);
  583. // Force v to be LayoutStyle.Absolute;
  584. v.Frame = new Rect (0, 1, 3, 4);
  585. Assert.Equal (LayoutStyle.Absolute, v.LayoutStyle);
  586. t.LayoutSubviews ();
  587. Assert.Equal (2, v.Width = 2);
  588. Assert.Equal (2, v.Height = 2);
  589. t.Dispose ();
  590. }
  591. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  592. // TODO: A new test that calls SetRelativeLayout directly is needed.
  593. [Fact]
  594. [AutoInitShutdown]
  595. public void Only_DimAbsolute_And_DimFactor_As_A_Different_Procedure_For_Assigning_Value_To_Width_Or_Height ()
  596. {
  597. // Testing with the Button because it properly handles the Dim class.
  598. var t = Application.Top;
  599. var w = new Window {
  600. Width = 100,
  601. Height = 100
  602. };
  603. var f1 = new FrameView ("f1") {
  604. X = 0,
  605. Y = 0,
  606. Width = Dim.Percent (50),
  607. Height = 5
  608. };
  609. var f2 = new FrameView ("f2") {
  610. X = Pos.Right (f1),
  611. Y = 0,
  612. Width = Dim.Fill (),
  613. Height = 5
  614. };
  615. var v1 = new Button ("v1") {
  616. AutoSize = false,
  617. X = Pos.X (f1) + 2,
  618. Y = Pos.Bottom (f1) + 2,
  619. Width = Dim.Width (f1) - 2,
  620. Height = Dim.Fill () - 2,
  621. ValidatePosDim = true
  622. };
  623. var v2 = new Button ("v2") {
  624. AutoSize = false,
  625. X = Pos.X (f2) + 2,
  626. Y = Pos.Bottom (f2) + 2,
  627. Width = Dim.Width (f2) - 2,
  628. Height = Dim.Fill () - 2,
  629. ValidatePosDim = true
  630. };
  631. var v3 = new Button ("v3") {
  632. AutoSize = false,
  633. Width = Dim.Percent (10),
  634. Height = Dim.Percent (10),
  635. ValidatePosDim = true
  636. };
  637. var v4 = new Button ("v4") {
  638. AutoSize = false,
  639. Width = Dim.Sized (50),
  640. Height = Dim.Sized (50),
  641. ValidatePosDim = true
  642. };
  643. var v5 = new Button ("v5") {
  644. AutoSize = false,
  645. Width = Dim.Width (v1) - Dim.Width (v3),
  646. Height = Dim.Height (v1) - Dim.Height (v3),
  647. ValidatePosDim = true
  648. };
  649. var v6 = new Button ("v6") {
  650. AutoSize = false,
  651. X = Pos.X (f2),
  652. Y = Pos.Bottom (f2) + 2,
  653. Width = Dim.Percent (20, true),
  654. Height = Dim.Percent (20, true),
  655. ValidatePosDim = true
  656. };
  657. w.Add (f1, f2, v1, v2, v3, v4, v5, v6);
  658. t.Add (w);
  659. t.Ready += (s, e) => {
  660. Assert.Equal ("Absolute(100)", w.Width.ToString ());
  661. Assert.Equal ("Absolute(100)", w.Height.ToString ());
  662. Assert.Equal (100, w.Frame.Width);
  663. Assert.Equal (100, w.Frame.Height);
  664. Assert.Equal ("Factor(0.5,False)", f1.Width.ToString ());
  665. Assert.Equal ("Absolute(5)", f1.Height.ToString ());
  666. Assert.Equal (49, f1.Frame.Width); // 50-1=49
  667. Assert.Equal (5, f1.Frame.Height);
  668. Assert.Equal ("Fill(0)", f2.Width.ToString ());
  669. Assert.Equal ("Absolute(5)", f2.Height.ToString ());
  670. Assert.Equal (49, f2.Frame.Width); // 50-1=49
  671. Assert.Equal (5, f2.Frame.Height);
  672. Assert.Equal ("Combine(View(Width,FrameView(f1)(0,0,49,5))-Absolute(2))", v1.Width.ToString ());
  673. Assert.Equal ("Combine(Fill(0)-Absolute(2))", v1.Height.ToString ());
  674. Assert.Equal (47, v1.Frame.Width); // 49-2=47
  675. Assert.Equal (89, v1.Frame.Height); // 98-5-2-2=89
  676. Assert.Equal ("Combine(View(Width,FrameView(f2)(49,0,49,5))-Absolute(2))", v2.Width.ToString ());
  677. Assert.Equal ("Combine(Fill(0)-Absolute(2))", v2.Height.ToString ());
  678. Assert.Equal (47, v2.Frame.Width); // 49-2=47
  679. Assert.Equal (89, v2.Frame.Height); // 98-5-2-2=89
  680. Assert.Equal ("Factor(0.1,False)", v3.Width.ToString ());
  681. Assert.Equal ("Factor(0.1,False)", v3.Height.ToString ());
  682. Assert.Equal (9, v3.Frame.Width); // 98*10%=9
  683. Assert.Equal (9, v3.Frame.Height); // 98*10%=9
  684. Assert.Equal ("Absolute(50)", v4.Width.ToString ());
  685. Assert.Equal ("Absolute(50)", v4.Height.ToString ());
  686. Assert.Equal (50, v4.Frame.Width);
  687. Assert.Equal (50, v4.Frame.Height);
  688. Assert.Equal ("Combine(View(Width,Button(v1)(2,7,47,89))-View(Width,Button(v3)(0,0,9,9)))", v5.Width.ToString ());
  689. Assert.Equal ("Combine(View(Height,Button(v1)(2,7,47,89))-View(Height,Button(v3)(0,0,9,9)))", v5.Height.ToString ());
  690. Assert.Equal (38, v5.Frame.Width); // 47-9=38
  691. Assert.Equal (80, v5.Frame.Height); // 89-9=80
  692. Assert.Equal ("Factor(0.2,True)", v6.Width.ToString ());
  693. Assert.Equal ("Factor(0.2,True)", v6.Height.ToString ());
  694. Assert.Equal (9, v6.Frame.Width); // 47*20%=9
  695. Assert.Equal (18, v6.Frame.Height); // 89*20%=18
  696. w.Width = 200;
  697. Assert.True (t.LayoutNeeded);
  698. w.Height = 200;
  699. t.LayoutSubviews ();
  700. Assert.Equal ("Absolute(200)", w.Width.ToString ());
  701. Assert.Equal ("Absolute(200)", w.Height.ToString ());
  702. Assert.Equal (200, w.Frame.Width);
  703. Assert.Equal (200, w.Frame.Height);
  704. f1.Text = "Frame1";
  705. Assert.Equal ("Factor(0.5,False)", f1.Width.ToString ());
  706. Assert.Equal ("Absolute(5)", f1.Height.ToString ());
  707. Assert.Equal (99, f1.Frame.Width); // 100-1=99
  708. Assert.Equal (5, f1.Frame.Height);
  709. f2.Text = "Frame2";
  710. Assert.Equal ("Fill(0)", f2.Width.ToString ());
  711. Assert.Equal ("Absolute(5)", f2.Height.ToString ());
  712. Assert.Equal (99, f2.Frame.Width); // 100-1=99
  713. Assert.Equal (5, f2.Frame.Height);
  714. v1.Text = "Button1";
  715. Assert.Equal ("Combine(View(Width,FrameView(f1)(0,0,99,5))-Absolute(2))", v1.Width.ToString ());
  716. Assert.Equal ("Combine(Fill(0)-Absolute(2))", v1.Height.ToString ());
  717. Assert.Equal (97, v1.Frame.Width); // 99-2=97
  718. Assert.Equal (189, v1.Frame.Height); // 198-2-7=189
  719. v2.Text = "Button2";
  720. Assert.Equal ("Combine(View(Width,FrameView(f2)(99,0,99,5))-Absolute(2))", v2.Width.ToString ());
  721. Assert.Equal ("Combine(Fill(0)-Absolute(2))", v2.Height.ToString ());
  722. Assert.Equal (97, v2.Frame.Width); // 99-2=97
  723. Assert.Equal (189, v2.Frame.Height); // 198-2-7=189
  724. v3.Text = "Button3";
  725. Assert.Equal ("Factor(0.1,False)", v3.Width.ToString ());
  726. Assert.Equal ("Factor(0.1,False)", v3.Height.ToString ());
  727. Assert.Equal (19, v3.Frame.Width); // 198*10%=19 * Percent is related to the super-view if it isn't null otherwise the view width
  728. Assert.Equal (19, v3.Frame.Height); // 199*10%=19
  729. v4.Text = "Button4";
  730. v4.AutoSize = false;
  731. Assert.Equal ("Absolute(50)", v4.Width.ToString ());
  732. Assert.Equal ("Absolute(50)", v4.Height.ToString ());
  733. Assert.Equal (50, v4.Frame.Width);
  734. Assert.Equal (50, v4.Frame.Height);
  735. v4.AutoSize = true;
  736. Assert.Equal ("Absolute(11)", v4.Width.ToString ());
  737. Assert.Equal ("Absolute(1)", v4.Height.ToString ());
  738. Assert.Equal (11, v4.Frame.Width); // 11 is the text length and because is Dim.DimAbsolute
  739. Assert.Equal (1, v4.Frame.Height); // 1 because is Dim.DimAbsolute
  740. v5.Text = "Button5";
  741. Assert.Equal ("Combine(View(Width,Button(v1)(2,7,97,189))-View(Width,Button(v3)(0,0,19,19)))", v5.Width.ToString ());
  742. Assert.Equal ("Combine(View(Height,Button(v1)(2,7,97,189))-View(Height,Button(v3)(0,0,19,19)))", v5.Height.ToString ());
  743. Assert.Equal (78, v5.Frame.Width); // 97-9=78
  744. Assert.Equal (170, v5.Frame.Height); // 189-19=170
  745. v6.Text = "Button6";
  746. Assert.Equal ("Factor(0.2,True)", v6.Width.ToString ());
  747. Assert.Equal ("Factor(0.2,True)", v6.Height.ToString ());
  748. Assert.Equal (19, v6.Frame.Width); // 99*20%=19
  749. Assert.Equal (38, v6.Frame.Height); // 198-7*20=18
  750. };
  751. Application.Iteration += (s, a) => Application.RequestStop ();
  752. Application.Run ();
  753. }
  754. // See #2461
  755. //[Fact]
  756. //public void Dim_Referencing_SuperView_Throws ()
  757. //{
  758. // var super = new View ("super") {
  759. // Width = 10,
  760. // Height = 10
  761. // };
  762. // var view = new View ("view") {
  763. // Width = Dim.Width (super), // this is not allowed
  764. // Height = Dim.Height (super), // this is not allowed
  765. // };
  766. // super.Add (view);
  767. // super.BeginInit ();
  768. // super.EndInit ();
  769. // Assert.Throws<InvalidOperationException> (() => super.LayoutSubviews ());
  770. //}
  771. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  772. // TODO: A new test that calls SetRelativeLayout directly is needed.
  773. /// <summary>
  774. /// This is an intentionally obtuse test. See https://github.com/gui-cs/Terminal.Gui/issues/2461
  775. /// </summary>
  776. [Fact]
  777. [TestRespondersDisposed]
  778. public void DimCombine_ObtuseScenario_Throw_If_SuperView_Refs_SubView ()
  779. {
  780. var t = new View { Width = 80, Height = 25 };
  781. var w = new Window {
  782. Width = Dim.Width (t) - 2, // 78
  783. Height = Dim.Height (t) - 2 // 23
  784. };
  785. var f = new FrameView ();
  786. var v1 = new View {
  787. Width = Dim.Width (w) - 2, // 76
  788. Height = Dim.Height (w) - 2 // 21
  789. };
  790. var v2 = new View {
  791. Width = Dim.Width (v1) - 2, // 74
  792. Height = Dim.Height (v1) - 2 // 19
  793. };
  794. f.Add (v1, v2);
  795. w.Add (f);
  796. t.Add (w);
  797. t.BeginInit ();
  798. t.EndInit ();
  799. f.Width = Dim.Width (t) - Dim.Width (v2); // 80 - 74 = 6
  800. f.Height = Dim.Height (t) - Dim.Height (v2); // 25 - 19 = 6
  801. Assert.Throws<InvalidOperationException> (t.LayoutSubviews);
  802. Assert.Equal (80, t.Frame.Width);
  803. Assert.Equal (25, t.Frame.Height);
  804. Assert.Equal (78, w.Frame.Width);
  805. Assert.Equal (23, w.Frame.Height);
  806. Assert.Equal (6, f.Frame.Width);
  807. Assert.Equal (6, f.Frame.Height);
  808. Assert.Equal (76, v1.Frame.Width);
  809. Assert.Equal (21, v1.Frame.Height);
  810. Assert.Equal (74, v2.Frame.Width);
  811. Assert.Equal (19, v2.Frame.Height);
  812. t.Dispose ();
  813. }
  814. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  815. // TODO: A new test that calls SetRelativeLayout directly is needed.
  816. [Fact]
  817. [TestRespondersDisposed]
  818. public void DimCombine_ObtuseScenario_Does_Not_Throw_If_Two_SubViews_Refs_The_Same_SuperView ()
  819. {
  820. var t = new View ("top") { Width = 80, Height = 25 };
  821. var w = new Window {
  822. Width = Dim.Width (t) - 2, // 78
  823. Height = Dim.Height (t) - 2 // 23
  824. };
  825. var f = new FrameView ();
  826. var v1 = new View {
  827. Width = Dim.Width (w) - 2, // 76
  828. Height = Dim.Height (w) - 2 // 21
  829. };
  830. var v2 = new View {
  831. Width = Dim.Width (v1) - 2, // 74
  832. Height = Dim.Height (v1) - 2 // 19
  833. };
  834. f.Add (v1, v2);
  835. w.Add (f);
  836. t.Add (w);
  837. t.BeginInit ();
  838. t.EndInit ();
  839. f.Width = Dim.Width (t) - Dim.Width (w) + 4; // 80 - 74 = 6
  840. f.Height = Dim.Height (t) - Dim.Height (w) + 4; // 25 - 19 = 6
  841. // BUGBUG: v2 - f references t and w here; t is f's super-superview and w is f's superview. This is supported!
  842. var exception = Record.Exception (t.LayoutSubviews);
  843. Assert.Null (exception);
  844. Assert.Equal (80, t.Frame.Width);
  845. Assert.Equal (25, t.Frame.Height);
  846. Assert.Equal (78, w.Frame.Width);
  847. Assert.Equal (23, w.Frame.Height);
  848. Assert.Equal (6, f.Frame.Width);
  849. Assert.Equal (6, f.Frame.Height);
  850. Assert.Equal (76, v1.Frame.Width);
  851. Assert.Equal (21, v1.Frame.Height);
  852. Assert.Equal (74, v2.Frame.Width);
  853. Assert.Equal (19, v2.Frame.Height);
  854. t.Dispose ();
  855. }
  856. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  857. // TODO: A new test that calls SetRelativeLayout directly is needed.
  858. [Fact]
  859. [TestRespondersDisposed]
  860. public void PosCombine_View_Not_Added_Throws ()
  861. {
  862. var t = new View { Width = 80, Height = 50 };
  863. var super = new View {
  864. Width = Dim.Width (t) - 2,
  865. Height = Dim.Height (t) - 2
  866. };
  867. t.Add (super);
  868. var sub = new View ();
  869. super.Add (sub);
  870. var v1 = new View {
  871. Width = Dim.Width (super) - 2,
  872. Height = Dim.Height (super) - 2
  873. };
  874. var v2 = new View {
  875. Width = Dim.Width (v1) - 2,
  876. Height = Dim.Height (v1) - 2
  877. };
  878. sub.Add (v1);
  879. // v2 not added to sub; should cause exception on Layout since it's referenced by sub.
  880. sub.Width = Dim.Fill () - Dim.Width (v2);
  881. sub.Height = Dim.Fill () - Dim.Height (v2);
  882. t.BeginInit ();
  883. t.EndInit ();
  884. Assert.Throws<InvalidOperationException> (() => t.LayoutSubviews ());
  885. t.Dispose ();
  886. v2.Dispose ();
  887. }
  888. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  889. // A new test that does not depend on Application is needed.
  890. [Fact]
  891. [AutoInitShutdown]
  892. public void Dim_Add_Operator ()
  893. {
  894. var top = Application.Top;
  895. var view = new View { X = 0, Y = 0, Width = 20, Height = 0 };
  896. var field = new TextField { X = 0, Y = Pos.Bottom (view), Width = 20 };
  897. var count = 0;
  898. field.KeyDown += (s, k) => {
  899. if (k.KeyCode == KeyCode.Enter) {
  900. field.Text = $"Label {count}";
  901. var label = new Label (field.Text) { X = 0, Y = view.Bounds.Height, Width = 20 };
  902. view.Add (label);
  903. Assert.Equal ($"Label {count}", label.Text);
  904. Assert.Equal ($"Absolute({count})", label.Y.ToString ());
  905. Assert.Equal ($"Absolute({count})", view.Height.ToString ());
  906. view.Height += 1;
  907. count++;
  908. Assert.Equal ($"Absolute({count})", view.Height.ToString ());
  909. }
  910. };
  911. Application.Iteration += (s, a) => {
  912. while (count < 20) {
  913. field.NewKeyDownEvent (new Key (KeyCode.Enter));
  914. }
  915. Application.RequestStop ();
  916. };
  917. var win = new Window ();
  918. win.Add (view);
  919. win.Add (field);
  920. top.Add (win);
  921. Application.Run (top);
  922. Assert.Equal (20, count);
  923. }
  924. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  925. // TODO: A new test that calls SetRelativeLayout directly is needed.
  926. [Fact]
  927. [AutoInitShutdown]
  928. public void Dim_Subtract_Operator ()
  929. {
  930. var top = Application.Top;
  931. var view = new View { X = 0, Y = 0, Width = 20, Height = 0 };
  932. var field = new TextField { X = 0, Y = Pos.Bottom (view), Width = 20 };
  933. var count = 20;
  934. var listLabels = new List<Label> ();
  935. for (var i = 0; i < count; i++) {
  936. field.Text = $"Label {i}";
  937. var label = new Label (field.Text) { X = 0, Y = view.Bounds.Height, Width = 20 };
  938. view.Add (label);
  939. Assert.Equal ($"Label {i}", label.Text);
  940. Assert.Equal ($"Absolute({i})", label.Y.ToString ());
  941. listLabels.Add (label);
  942. Assert.Equal ($"Absolute({i})", view.Height.ToString ());
  943. view.Height += 1;
  944. Assert.Equal ($"Absolute({i + 1})", view.Height.ToString ());
  945. }
  946. field.KeyDown += (s, k) => {
  947. if (k.KeyCode == KeyCode.Enter) {
  948. Assert.Equal ($"Label {count - 1}", listLabels [count - 1].Text);
  949. view.Remove (listLabels [count - 1]);
  950. listLabels [count - 1].Dispose ();
  951. Assert.Equal ($"Absolute({count})", view.Height.ToString ());
  952. view.Height -= 1;
  953. count--;
  954. Assert.Equal ($"Absolute({count})", view.Height.ToString ());
  955. }
  956. };
  957. Application.Iteration += (s, a) => {
  958. while (count > 0) {
  959. field.NewKeyDownEvent (new Key (KeyCode.Enter));
  960. }
  961. Application.RequestStop ();
  962. };
  963. var win = new Window ();
  964. win.Add (view);
  965. win.Add (field);
  966. top.Add (win);
  967. Application.Run (top);
  968. Assert.Equal (0, count);
  969. }
  970. [Fact]
  971. [TestRespondersDisposed]
  972. public void Internal_Tests ()
  973. {
  974. var dimFactor = new Dim.DimFactor (0.10F);
  975. Assert.Equal (10, dimFactor.Anchor (100));
  976. var dimAbsolute = new Dim.DimAbsolute (10);
  977. Assert.Equal (10, dimAbsolute.Anchor (0));
  978. var dimFill = new Dim.DimFill (1);
  979. Assert.Equal (99, dimFill.Anchor (100));
  980. var dimCombine = new Dim.DimCombine (true, dimFactor, dimAbsolute);
  981. Assert.Equal (dimCombine._left, dimFactor);
  982. Assert.Equal (dimCombine._right, dimAbsolute);
  983. Assert.Equal (20, dimCombine.Anchor (100));
  984. var view = new View (new Rect (20, 10, 20, 1));
  985. var dimViewHeight = new Dim.DimView (view, 0);
  986. Assert.Equal (1, dimViewHeight.Anchor (0));
  987. var dimViewWidth = new Dim.DimView (view, 1);
  988. Assert.Equal (20, dimViewWidth.Anchor (0));
  989. view.Dispose ();
  990. }
  991. [Fact]
  992. public void Function_SetsValue ()
  993. {
  994. var text = "Test";
  995. var dim = Dim.Function (() => text.Length);
  996. Assert.Equal ("DimFunc(4)", dim.ToString ());
  997. text = "New Test";
  998. Assert.Equal ("DimFunc(8)", dim.ToString ());
  999. text = "";
  1000. Assert.Equal ("DimFunc(0)", dim.ToString ());
  1001. }
  1002. [Fact]
  1003. public void Function_Equal ()
  1004. {
  1005. var f1 = () => 0;
  1006. var f2 = () => 0;
  1007. var dim1 = Dim.Function (f1);
  1008. var dim2 = Dim.Function (f2);
  1009. Assert.Equal (dim1, dim2);
  1010. f2 = () => 1;
  1011. dim2 = Dim.Function (f2);
  1012. Assert.NotEqual (dim1, dim2);
  1013. }
  1014. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  1015. // TODO: A new test that calls SetRelativeLayout directly is needed.
  1016. [Theory]
  1017. [AutoInitShutdown]
  1018. [InlineData (0, true)]
  1019. [InlineData (0, false)]
  1020. [InlineData (50, true)]
  1021. [InlineData (50, false)]
  1022. public void DimPercentPlusOne (int startingDistance, bool testHorizontal)
  1023. {
  1024. var container = new View {
  1025. Width = 100,
  1026. Height = 100
  1027. };
  1028. var label = new Label {
  1029. X = testHorizontal ? startingDistance : 0,
  1030. Y = testHorizontal ? 0 : startingDistance,
  1031. Width = testHorizontal ? Dim.Percent (50) + 1 : 1,
  1032. Height = testHorizontal ? 1 : Dim.Percent (50) + 1
  1033. };
  1034. container.Add (label);
  1035. Application.Top.Add (container);
  1036. Application.Top.BeginInit ();
  1037. Application.Top.EndInit ();
  1038. Application.Top.LayoutSubviews ();
  1039. Assert.Equal (100, container.Frame.Width);
  1040. Assert.Equal (100, container.Frame.Height);
  1041. if (testHorizontal) {
  1042. Assert.Equal (51, label.Frame.Width);
  1043. Assert.Equal (1, label.Frame.Height);
  1044. } else {
  1045. Assert.Equal (1, label.Frame.Width);
  1046. Assert.Equal (51, label.Frame.Height);
  1047. }
  1048. }
  1049. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  1050. // TODO: A new test that calls SetRelativeLayout directly is needed.
  1051. [Fact]
  1052. [TestRespondersDisposed]
  1053. public void Dim_Referencing_SuperView_Does_Not_Throw ()
  1054. {
  1055. var super = new View ("super") {
  1056. Width = 10,
  1057. Height = 10
  1058. };
  1059. var view = new View ("view") {
  1060. Width = Dim.Width (super), // this is allowed
  1061. Height = Dim.Height (super) // this is allowed
  1062. };
  1063. super.Add (view);
  1064. super.BeginInit ();
  1065. super.EndInit ();
  1066. var exception = Record.Exception (super.LayoutSubviews);
  1067. Assert.Null (exception);
  1068. super.Dispose ();
  1069. }
  1070. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  1071. // TODO: A new test that calls SetRelativeLayout directly is needed.
  1072. [Fact]
  1073. [TestRespondersDisposed]
  1074. public void Dim_SyperView_Referencing_SubView_Throws ()
  1075. {
  1076. var super = new View ("super") {
  1077. Width = 10,
  1078. Height = 10
  1079. };
  1080. var view2 = new View ("view2") {
  1081. Width = 10,
  1082. Height = 10
  1083. };
  1084. var view = new View ("view") {
  1085. Width = Dim.Width (view2), // this is not allowed
  1086. Height = Dim.Height (view2) // this is not allowed
  1087. };
  1088. view.Add (view2);
  1089. super.Add (view);
  1090. super.BeginInit ();
  1091. super.EndInit ();
  1092. Assert.Throws<InvalidOperationException> (super.LayoutSubviews);
  1093. super.Dispose ();
  1094. }
  1095. }