DimTests.cs 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225
  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] [TestRespondersDisposed]
  386. public void SetsValue ()
  387. {
  388. var testVal = Rect.Empty;
  389. var testValView = new View (testVal);
  390. var dim = Dim.Width (testValView);
  391. Assert.Equal ($"View(Width,View(){testVal})", dim.ToString ());
  392. testValView.Dispose ();
  393. testVal = new Rect (1, 2, 3, 4);
  394. testValView = new View (testVal);
  395. dim = Dim.Width (testValView);
  396. Assert.Equal ($"View(Width,View(){testVal})", dim.ToString ());
  397. testValView.Dispose ();
  398. }
  399. [Fact] [TestRespondersDisposed]
  400. public void Width_Equals ()
  401. {
  402. var testRect1 = Rect.Empty;
  403. var view1 = new View (testRect1);
  404. var testRect2 = Rect.Empty;
  405. var view2 = new View (testRect2);
  406. var dim1 = Dim.Width (view1);
  407. var dim2 = Dim.Width (view1);
  408. // FIXED: Dim.Width should support Equals() and this should change to Equal.
  409. Assert.Equal (dim1, dim2);
  410. dim2 = Dim.Width (view2);
  411. Assert.NotEqual (dim1, dim2);
  412. testRect1 = new Rect (0, 1, 2, 3);
  413. view1 = new View (testRect1);
  414. testRect2 = new Rect (0, 1, 2, 3);
  415. dim1 = Dim.Width (view1);
  416. dim2 = Dim.Width (view1);
  417. // FIXED: Dim.Width should support Equals() and this should change to Equal.
  418. Assert.Equal (dim1, dim2);
  419. testRect1 = new Rect (0, -1, 2, 3);
  420. view1 = new View (testRect1);
  421. testRect2 = new Rect (0, -1, 2, 3);
  422. dim1 = Dim.Width (view1);
  423. dim2 = Dim.Width (view1);
  424. // FIXED: Dim.Width should support Equals() and this should change to Equal.
  425. Assert.Equal (dim1, dim2);
  426. testRect1 = new Rect (0, -1, 2, 3);
  427. view1 = new View (testRect1);
  428. testRect2 = Rect.Empty;
  429. view2 = new View (testRect2);
  430. dim1 = Dim.Width (view1);
  431. dim2 = Dim.Width (view2);
  432. Assert.NotEqual (dim1, dim2);
  433. #if DEBUG_IDISPOSABLE
  434. // HACK: Force clean up of Responders to avoid having to Dispose all the Views created above.
  435. Responder.Instances.Clear ();
  436. Assert.Empty (Responder.Instances);
  437. #endif
  438. }
  439. [Fact]
  440. public void Height_Set_To_Null_Throws ()
  441. {
  442. var dim = Dim.Height (null);
  443. Assert.Throws<NullReferenceException> (() => dim.ToString ());
  444. }
  445. [Fact] [TestRespondersDisposed]
  446. public void Height_SetsValue ()
  447. {
  448. var testVal = Rect.Empty;
  449. var testValview = new View (testVal);
  450. var dim = Dim.Height (testValview);
  451. Assert.Equal ($"View(Height,View(){testVal})", dim.ToString ());
  452. testValview.Dispose ();
  453. testVal = new Rect (1, 2, 3, 4);
  454. testValview = new View (testVal);
  455. dim = Dim.Height (testValview);
  456. Assert.Equal ($"View(Height,View(){testVal})", dim.ToString ());
  457. testValview.Dispose ();
  458. }
  459. // TODO: Other Dim.Height tests (e.g. Equal?)
  460. [Fact]
  461. public void Fill_SetsValue ()
  462. {
  463. var testMargin = 0;
  464. var dim = Dim.Fill ();
  465. Assert.Equal ($"Fill({testMargin})", dim.ToString ());
  466. testMargin = 0;
  467. dim = Dim.Fill (testMargin);
  468. Assert.Equal ($"Fill({testMargin})", dim.ToString ());
  469. testMargin = 5;
  470. dim = Dim.Fill (testMargin);
  471. Assert.Equal ($"Fill({testMargin})", dim.ToString ());
  472. }
  473. [Fact]
  474. public void Fill_Equal ()
  475. {
  476. var margin1 = 0;
  477. var margin2 = 0;
  478. var dim1 = Dim.Fill (margin1);
  479. var dim2 = Dim.Fill (margin2);
  480. Assert.Equal (dim1, dim2);
  481. }
  482. [Fact]
  483. public void Percent_SetsValue ()
  484. {
  485. float f = 0;
  486. var dim = Dim.Percent (f);
  487. Assert.Equal ($"Factor({f / 100:0.###},{false})", dim.ToString ());
  488. f = 0.5F;
  489. dim = Dim.Percent (f);
  490. Assert.Equal ($"Factor({f / 100:0.###},{false})", dim.ToString ());
  491. f = 100;
  492. dim = Dim.Percent (f);
  493. Assert.Equal ($"Factor({f / 100:0.###},{false})", dim.ToString ());
  494. }
  495. [Fact]
  496. public void Percent_Equals ()
  497. {
  498. float n1 = 0;
  499. float n2 = 0;
  500. var dim1 = Dim.Percent (n1);
  501. var dim2 = Dim.Percent (n2);
  502. Assert.Equal (dim1, dim2);
  503. n1 = n2 = 1;
  504. dim1 = Dim.Percent (n1);
  505. dim2 = Dim.Percent (n2);
  506. Assert.Equal (dim1, dim2);
  507. n1 = n2 = 0.5f;
  508. dim1 = Dim.Percent (n1);
  509. dim2 = Dim.Percent (n2);
  510. Assert.Equal (dim1, dim2);
  511. n1 = n2 = 100f;
  512. dim1 = Dim.Percent (n1);
  513. dim2 = Dim.Percent (n2);
  514. Assert.Equal (dim1, dim2);
  515. n1 = n2 = 0.3f;
  516. dim1 = Dim.Percent (n1, true);
  517. dim2 = Dim.Percent (n2, true);
  518. Assert.Equal (dim1, dim2);
  519. n1 = n2 = 0.3f;
  520. dim1 = Dim.Percent (n1);
  521. dim2 = Dim.Percent (n2, true);
  522. Assert.NotEqual (dim1, dim2);
  523. n1 = 0;
  524. n2 = 1;
  525. dim1 = Dim.Percent (n1);
  526. dim2 = Dim.Percent (n2);
  527. Assert.NotEqual (dim1, dim2);
  528. n1 = 0.5f;
  529. n2 = 1.5f;
  530. dim1 = Dim.Percent (n1);
  531. dim2 = Dim.Percent (n2);
  532. Assert.NotEqual (dim1, dim2);
  533. }
  534. [Fact]
  535. public void Percent_Invalid_Throws ()
  536. {
  537. var dim = Dim.Percent (0);
  538. Assert.Throws<ArgumentException> (() => dim = Dim.Percent (-1));
  539. Assert.Throws<ArgumentException> (() => dim = Dim.Percent (101));
  540. Assert.Throws<ArgumentException> (() => dim = Dim.Percent (100.0001F));
  541. Assert.Throws<ArgumentException> (() => dim = Dim.Percent (1000001));
  542. }
  543. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  544. // TODO: A new test that calls SetRelativeLayout directly is needed.
  545. [Fact] [TestRespondersDisposed]
  546. public void Dim_Validation_Do_Not_Throws_If_NewValue_Is_DimAbsolute_And_OldValue_Is_Null ()
  547. {
  548. var t = new View ("top") { Width = 80, Height = 25 };
  549. var w = new Window (new Rect (1, 2, 4, 5)) { Title = "w" };
  550. t.Add (w);
  551. t.LayoutSubviews ();
  552. Assert.Equal (3, w.Width = 3);
  553. Assert.Equal (4, w.Height = 4);
  554. t.Dispose ();
  555. }
  556. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  557. // TODO: A new test that calls SetRelativeLayout directly is needed.
  558. [Fact] [TestRespondersDisposed]
  559. public void Dim_Validation_Do_Not_Throws_If_NewValue_Is_DimAbsolute_And_OldValue_Is_Another_Type_After_Sets_To_LayoutStyle_Absolute ()
  560. {
  561. var t = new View ("top") { Width = 80, Height = 25 };
  562. var w = new Window {
  563. Width = Dim.Fill (),
  564. Height = Dim.Sized (10)
  565. };
  566. var v = new View ("v") {
  567. Width = Dim.Width (w) - 2,
  568. Height = Dim.Percent (10)
  569. };
  570. w.Add (v);
  571. t.Add (w);
  572. Assert.Equal (LayoutStyle.Absolute, t.LayoutStyle);
  573. Assert.Equal (LayoutStyle.Computed, w.LayoutStyle);
  574. Assert.Equal (LayoutStyle.Computed, v.LayoutStyle);
  575. t.LayoutSubviews ();
  576. Assert.Equal (2, v.Width = 2);
  577. Assert.Equal (2, v.Height = 2);
  578. // Force v to be LayoutStyle.Absolute;
  579. v.Frame = new Rect (0, 1, 3, 4);
  580. Assert.Equal (LayoutStyle.Absolute, v.LayoutStyle);
  581. t.LayoutSubviews ();
  582. Assert.Equal (2, v.Width = 2);
  583. Assert.Equal (2, v.Height = 2);
  584. t.Dispose ();
  585. }
  586. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  587. // TODO: A new test that calls SetRelativeLayout directly is needed.
  588. [Fact] [AutoInitShutdown]
  589. public void Only_DimAbsolute_And_DimFactor_As_A_Different_Procedure_For_Assigning_Value_To_Width_Or_Height ()
  590. {
  591. // Testing with the Button because it properly handles the Dim class.
  592. var t = Application.Top;
  593. var w = new Window {
  594. Width = 100,
  595. Height = 100
  596. };
  597. var f1 = new FrameView ("f1") {
  598. X = 0,
  599. Y = 0,
  600. Width = Dim.Percent (50),
  601. Height = 5
  602. };
  603. var f2 = new FrameView ("f2") {
  604. X = Pos.Right (f1),
  605. Y = 0,
  606. Width = Dim.Fill (),
  607. Height = 5
  608. };
  609. var v1 = new Button ("v1") {
  610. AutoSize = false,
  611. X = Pos.X (f1) + 2,
  612. Y = Pos.Bottom (f1) + 2,
  613. Width = Dim.Width (f1) - 2,
  614. Height = Dim.Fill () - 2,
  615. ValidatePosDim = true
  616. };
  617. var v2 = new Button ("v2") {
  618. AutoSize = false,
  619. X = Pos.X (f2) + 2,
  620. Y = Pos.Bottom (f2) + 2,
  621. Width = Dim.Width (f2) - 2,
  622. Height = Dim.Fill () - 2,
  623. ValidatePosDim = true
  624. };
  625. var v3 = new Button ("v3") {
  626. AutoSize = false,
  627. Width = Dim.Percent (10),
  628. Height = Dim.Percent (10),
  629. ValidatePosDim = true
  630. };
  631. var v4 = new Button ("v4") {
  632. AutoSize = false,
  633. Width = Dim.Sized (50),
  634. Height = Dim.Sized (50),
  635. ValidatePosDim = true
  636. };
  637. var v5 = new Button ("v5") {
  638. AutoSize = false,
  639. Width = Dim.Width (v1) - Dim.Width (v3),
  640. Height = Dim.Height (v1) - Dim.Height (v3),
  641. ValidatePosDim = true
  642. };
  643. var v6 = new Button ("v6") {
  644. AutoSize = false,
  645. X = Pos.X (f2),
  646. Y = Pos.Bottom (f2) + 2,
  647. Width = Dim.Percent (20, true),
  648. Height = Dim.Percent (20, true),
  649. ValidatePosDim = true
  650. };
  651. w.Add (f1, f2, v1, v2, v3, v4, v5, v6);
  652. t.Add (w);
  653. t.Ready += (s, e) => {
  654. Assert.Equal ("Absolute(100)", w.Width.ToString ());
  655. Assert.Equal ("Absolute(100)", w.Height.ToString ());
  656. Assert.Equal (100, w.Frame.Width);
  657. Assert.Equal (100, w.Frame.Height);
  658. Assert.Equal ("Factor(0.5,False)", f1.Width.ToString ());
  659. Assert.Equal ("Absolute(5)", f1.Height.ToString ());
  660. Assert.Equal (49, f1.Frame.Width); // 50-1=49
  661. Assert.Equal (5, f1.Frame.Height);
  662. Assert.Equal ("Fill(0)", f2.Width.ToString ());
  663. Assert.Equal ("Absolute(5)", f2.Height.ToString ());
  664. Assert.Equal (49, f2.Frame.Width); // 50-1=49
  665. Assert.Equal (5, f2.Frame.Height);
  666. Assert.Equal ("Combine(View(Width,FrameView(f1)(0,0,49,5))-Absolute(2))", v1.Width.ToString ());
  667. Assert.Equal ("Combine(Fill(0)-Absolute(2))", v1.Height.ToString ());
  668. Assert.Equal (47, v1.Frame.Width); // 49-2=47
  669. Assert.Equal (89, v1.Frame.Height); // 98-5-2-2=89
  670. Assert.Equal ("Combine(View(Width,FrameView(f2)(49,0,49,5))-Absolute(2))", v2.Width.ToString ());
  671. Assert.Equal ("Combine(Fill(0)-Absolute(2))", v2.Height.ToString ());
  672. Assert.Equal (47, v2.Frame.Width); // 49-2=47
  673. Assert.Equal (89, v2.Frame.Height); // 98-5-2-2=89
  674. Assert.Equal ("Factor(0.1,False)", v3.Width.ToString ());
  675. Assert.Equal ("Factor(0.1,False)", v3.Height.ToString ());
  676. Assert.Equal (9, v3.Frame.Width); // 98*10%=9
  677. Assert.Equal (9, v3.Frame.Height); // 98*10%=9
  678. Assert.Equal ("Absolute(50)", v4.Width.ToString ());
  679. Assert.Equal ("Absolute(50)", v4.Height.ToString ());
  680. Assert.Equal (50, v4.Frame.Width);
  681. Assert.Equal (50, v4.Frame.Height);
  682. Assert.Equal ("Combine(View(Width,Button(v1)(2,7,47,89))-View(Width,Button(v3)(0,0,9,9)))", v5.Width.ToString ());
  683. Assert.Equal ("Combine(View(Height,Button(v1)(2,7,47,89))-View(Height,Button(v3)(0,0,9,9)))", v5.Height.ToString ());
  684. Assert.Equal (38, v5.Frame.Width); // 47-9=38
  685. Assert.Equal (80, v5.Frame.Height); // 89-9=80
  686. Assert.Equal ("Factor(0.2,True)", v6.Width.ToString ());
  687. Assert.Equal ("Factor(0.2,True)", v6.Height.ToString ());
  688. Assert.Equal (9, v6.Frame.Width); // 47*20%=9
  689. Assert.Equal (18, v6.Frame.Height); // 89*20%=18
  690. w.Width = 200;
  691. Assert.True (t.LayoutNeeded);
  692. w.Height = 200;
  693. t.LayoutSubviews ();
  694. Assert.Equal ("Absolute(200)", w.Width.ToString ());
  695. Assert.Equal ("Absolute(200)", w.Height.ToString ());
  696. Assert.Equal (200, w.Frame.Width);
  697. Assert.Equal (200, w.Frame.Height);
  698. f1.Text = "Frame1";
  699. Assert.Equal ("Factor(0.5,False)", f1.Width.ToString ());
  700. Assert.Equal ("Absolute(5)", f1.Height.ToString ());
  701. Assert.Equal (99, f1.Frame.Width); // 100-1=99
  702. Assert.Equal (5, f1.Frame.Height);
  703. f2.Text = "Frame2";
  704. Assert.Equal ("Fill(0)", f2.Width.ToString ());
  705. Assert.Equal ("Absolute(5)", f2.Height.ToString ());
  706. Assert.Equal (99, f2.Frame.Width); // 100-1=99
  707. Assert.Equal (5, f2.Frame.Height);
  708. v1.Text = "Button1";
  709. Assert.Equal ("Combine(View(Width,FrameView(f1)(0,0,99,5))-Absolute(2))", v1.Width.ToString ());
  710. Assert.Equal ("Combine(Fill(0)-Absolute(2))", v1.Height.ToString ());
  711. Assert.Equal (97, v1.Frame.Width); // 99-2=97
  712. Assert.Equal (189, v1.Frame.Height); // 198-2-7=189
  713. v2.Text = "Button2";
  714. Assert.Equal ("Combine(View(Width,FrameView(f2)(99,0,99,5))-Absolute(2))", v2.Width.ToString ());
  715. Assert.Equal ("Combine(Fill(0)-Absolute(2))", v2.Height.ToString ());
  716. Assert.Equal (97, v2.Frame.Width); // 99-2=97
  717. Assert.Equal (189, v2.Frame.Height); // 198-2-7=189
  718. v3.Text = "Button3";
  719. Assert.Equal ("Factor(0.1,False)", v3.Width.ToString ());
  720. Assert.Equal ("Factor(0.1,False)", v3.Height.ToString ());
  721. 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
  722. Assert.Equal (19, v3.Frame.Height); // 199*10%=19
  723. v4.Text = "Button4";
  724. v4.AutoSize = false;
  725. Assert.Equal ("Absolute(50)", v4.Width.ToString ());
  726. Assert.Equal ("Absolute(50)", v4.Height.ToString ());
  727. Assert.Equal (50, v4.Frame.Width);
  728. Assert.Equal (50, v4.Frame.Height);
  729. v4.AutoSize = true;
  730. Assert.Equal ("Absolute(11)", v4.Width.ToString ());
  731. Assert.Equal ("Absolute(1)", v4.Height.ToString ());
  732. Assert.Equal (11, v4.Frame.Width); // 11 is the text length and because is Dim.DimAbsolute
  733. Assert.Equal (1, v4.Frame.Height); // 1 because is Dim.DimAbsolute
  734. v5.Text = "Button5";
  735. Assert.Equal ("Combine(View(Width,Button(v1)(2,7,97,189))-View(Width,Button(v3)(0,0,19,19)))", v5.Width.ToString ());
  736. Assert.Equal ("Combine(View(Height,Button(v1)(2,7,97,189))-View(Height,Button(v3)(0,0,19,19)))", v5.Height.ToString ());
  737. Assert.Equal (78, v5.Frame.Width); // 97-9=78
  738. Assert.Equal (170, v5.Frame.Height); // 189-19=170
  739. v6.Text = "Button6";
  740. Assert.Equal ("Factor(0.2,True)", v6.Width.ToString ());
  741. Assert.Equal ("Factor(0.2,True)", v6.Height.ToString ());
  742. Assert.Equal (19, v6.Frame.Width); // 99*20%=19
  743. Assert.Equal (38, v6.Frame.Height); // 198-7*20=18
  744. };
  745. Application.Iteration += (s, a) => Application.RequestStop ();
  746. Application.Run ();
  747. }
  748. // See #2461
  749. //[Fact]
  750. //public void Dim_Referencing_SuperView_Throws ()
  751. //{
  752. // var super = new View ("super") {
  753. // Width = 10,
  754. // Height = 10
  755. // };
  756. // var view = new View ("view") {
  757. // Width = Dim.Width (super), // this is not allowed
  758. // Height = Dim.Height (super), // this is not allowed
  759. // };
  760. // super.Add (view);
  761. // super.BeginInit ();
  762. // super.EndInit ();
  763. // Assert.Throws<InvalidOperationException> (() => super.LayoutSubviews ());
  764. //}
  765. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  766. // TODO: A new test that calls SetRelativeLayout directly is needed.
  767. /// <summary>
  768. /// This is an intentionally obtuse test. See https://github.com/gui-cs/Terminal.Gui/issues/2461
  769. /// </summary>
  770. [Fact] [TestRespondersDisposed]
  771. public void DimCombine_ObtuseScenario_Throw_If_SuperView_Refs_SubView ()
  772. {
  773. var t = new View { Width = 80, Height = 25 };
  774. var w = new Window {
  775. Width = Dim.Width (t) - 2, // 78
  776. Height = Dim.Height (t) - 2 // 23
  777. };
  778. var f = new FrameView ();
  779. var v1 = new View {
  780. Width = Dim.Width (w) - 2, // 76
  781. Height = Dim.Height (w) - 2 // 21
  782. };
  783. var v2 = new View {
  784. Width = Dim.Width (v1) - 2, // 74
  785. Height = Dim.Height (v1) - 2 // 19
  786. };
  787. f.Add (v1, v2);
  788. w.Add (f);
  789. t.Add (w);
  790. t.BeginInit ();
  791. t.EndInit ();
  792. f.Width = Dim.Width (t) - Dim.Width (v2); // 80 - 74 = 6
  793. f.Height = Dim.Height (t) - Dim.Height (v2); // 25 - 19 = 6
  794. Assert.Throws<InvalidOperationException> (t.LayoutSubviews);
  795. Assert.Equal (80, t.Frame.Width);
  796. Assert.Equal (25, t.Frame.Height);
  797. Assert.Equal (78, w.Frame.Width);
  798. Assert.Equal (23, w.Frame.Height);
  799. Assert.Equal (6, f.Frame.Width);
  800. Assert.Equal (6, f.Frame.Height);
  801. Assert.Equal (76, v1.Frame.Width);
  802. Assert.Equal (21, v1.Frame.Height);
  803. Assert.Equal (74, v2.Frame.Width);
  804. Assert.Equal (19, v2.Frame.Height);
  805. t.Dispose ();
  806. }
  807. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  808. // TODO: A new test that calls SetRelativeLayout directly is needed.
  809. [Fact] [TestRespondersDisposed]
  810. public void DimCombine_ObtuseScenario_Does_Not_Throw_If_Two_SubViews_Refs_The_Same_SuperView ()
  811. {
  812. var t = new View ("top") { Width = 80, Height = 25 };
  813. var w = new Window {
  814. Width = Dim.Width (t) - 2, // 78
  815. Height = Dim.Height (t) - 2 // 23
  816. };
  817. var f = new FrameView ();
  818. var v1 = new View {
  819. Width = Dim.Width (w) - 2, // 76
  820. Height = Dim.Height (w) - 2 // 21
  821. };
  822. var v2 = new View {
  823. Width = Dim.Width (v1) - 2, // 74
  824. Height = Dim.Height (v1) - 2 // 19
  825. };
  826. f.Add (v1, v2);
  827. w.Add (f);
  828. t.Add (w);
  829. t.BeginInit ();
  830. t.EndInit ();
  831. f.Width = Dim.Width (t) - Dim.Width (w) + 4; // 80 - 74 = 6
  832. f.Height = Dim.Height (t) - Dim.Height (w) + 4; // 25 - 19 = 6
  833. // BUGBUG: v2 - f references t and w here; t is f's super-superview and w is f's superview. This is supported!
  834. var exception = Record.Exception (t.LayoutSubviews);
  835. Assert.Null (exception);
  836. Assert.Equal (80, t.Frame.Width);
  837. Assert.Equal (25, t.Frame.Height);
  838. Assert.Equal (78, w.Frame.Width);
  839. Assert.Equal (23, w.Frame.Height);
  840. Assert.Equal (6, f.Frame.Width);
  841. Assert.Equal (6, f.Frame.Height);
  842. Assert.Equal (76, v1.Frame.Width);
  843. Assert.Equal (21, v1.Frame.Height);
  844. Assert.Equal (74, v2.Frame.Width);
  845. Assert.Equal (19, v2.Frame.Height);
  846. t.Dispose ();
  847. }
  848. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  849. // TODO: A new test that calls SetRelativeLayout directly is needed.
  850. [Fact] [TestRespondersDisposed]
  851. public void PosCombine_View_Not_Added_Throws ()
  852. {
  853. var t = new View { Width = 80, Height = 50 };
  854. var super = new View {
  855. Width = Dim.Width (t) - 2,
  856. Height = Dim.Height (t) - 2
  857. };
  858. t.Add (super);
  859. var sub = new View ();
  860. super.Add (sub);
  861. var v1 = new View {
  862. Width = Dim.Width (super) - 2,
  863. Height = Dim.Height (super) - 2
  864. };
  865. var v2 = new View {
  866. Width = Dim.Width (v1) - 2,
  867. Height = Dim.Height (v1) - 2
  868. };
  869. sub.Add (v1);
  870. // v2 not added to sub; should cause exception on Layout since it's referenced by sub.
  871. sub.Width = Dim.Fill () - Dim.Width (v2);
  872. sub.Height = Dim.Fill () - Dim.Height (v2);
  873. t.BeginInit ();
  874. t.EndInit ();
  875. Assert.Throws<InvalidOperationException> (() => t.LayoutSubviews ());
  876. t.Dispose ();
  877. v2.Dispose ();
  878. }
  879. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  880. // A new test that does not depend on Application is needed.
  881. [Fact] [AutoInitShutdown]
  882. public void Dim_Add_Operator ()
  883. {
  884. var top = Application.Top;
  885. var view = new View { X = 0, Y = 0, Width = 20, Height = 0 };
  886. var field = new TextField { X = 0, Y = Pos.Bottom (view), Width = 20 };
  887. var count = 0;
  888. field.KeyDown += (s, k) => {
  889. if (k.KeyCode == KeyCode.Enter) {
  890. field.Text = $"Label {count}";
  891. var label = new Label (field.Text) { X = 0, Y = view.Bounds.Height, Width = 20 };
  892. view.Add (label);
  893. Assert.Equal ($"Label {count}", label.Text);
  894. Assert.Equal ($"Absolute({count})", label.Y.ToString ());
  895. Assert.Equal ($"Absolute({count})", view.Height.ToString ());
  896. view.Height += 1;
  897. count++;
  898. Assert.Equal ($"Absolute({count})", view.Height.ToString ());
  899. }
  900. };
  901. Application.Iteration += (s, a) => {
  902. while (count < 20) {
  903. field.NewKeyDownEvent (new Key (KeyCode.Enter));
  904. }
  905. Application.RequestStop ();
  906. };
  907. var win = new Window ();
  908. win.Add (view);
  909. win.Add (field);
  910. top.Add (win);
  911. Application.Run (top);
  912. Assert.Equal (20, count);
  913. }
  914. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  915. // TODO: A new test that calls SetRelativeLayout directly is needed.
  916. [Fact] [AutoInitShutdown]
  917. public void Dim_Subtract_Operator ()
  918. {
  919. var top = Application.Top;
  920. var view = new View { X = 0, Y = 0, Width = 20, Height = 0 };
  921. var field = new TextField { X = 0, Y = Pos.Bottom (view), Width = 20 };
  922. var count = 20;
  923. var listLabels = new List<Label> ();
  924. for (var i = 0; i < count; i++) {
  925. field.Text = $"Label {i}";
  926. var label = new Label (field.Text) { X = 0, Y = view.Bounds.Height, Width = 20 };
  927. view.Add (label);
  928. Assert.Equal ($"Label {i}", label.Text);
  929. Assert.Equal ($"Absolute({i})", label.Y.ToString ());
  930. listLabels.Add (label);
  931. Assert.Equal ($"Absolute({i})", view.Height.ToString ());
  932. view.Height += 1;
  933. Assert.Equal ($"Absolute({i + 1})", view.Height.ToString ());
  934. }
  935. field.KeyDown += (s, k) => {
  936. if (k.KeyCode == KeyCode.Enter) {
  937. Assert.Equal ($"Label {count - 1}", listLabels [count - 1].Text);
  938. view.Remove (listLabels [count - 1]);
  939. listLabels [count - 1].Dispose ();
  940. Assert.Equal ($"Absolute({count})", view.Height.ToString ());
  941. view.Height -= 1;
  942. count--;
  943. Assert.Equal ($"Absolute({count})", view.Height.ToString ());
  944. }
  945. };
  946. Application.Iteration += (s, a) => {
  947. while (count > 0) {
  948. field.NewKeyDownEvent (new Key (KeyCode.Enter));
  949. }
  950. Application.RequestStop ();
  951. };
  952. var win = new Window ();
  953. win.Add (view);
  954. win.Add (field);
  955. top.Add (win);
  956. Application.Run (top);
  957. Assert.Equal (0, count);
  958. }
  959. [Fact] [TestRespondersDisposed]
  960. public void Internal_Tests ()
  961. {
  962. var dimFactor = new Dim.DimFactor (0.10F);
  963. Assert.Equal (10, dimFactor.Anchor (100));
  964. var dimAbsolute = new Dim.DimAbsolute (10);
  965. Assert.Equal (10, dimAbsolute.Anchor (0));
  966. var dimFill = new Dim.DimFill (1);
  967. Assert.Equal (99, dimFill.Anchor (100));
  968. var dimCombine = new Dim.DimCombine (true, dimFactor, dimAbsolute);
  969. Assert.Equal (dimCombine._left, dimFactor);
  970. Assert.Equal (dimCombine._right, dimAbsolute);
  971. Assert.Equal (20, dimCombine.Anchor (100));
  972. var view = new View (new Rect (20, 10, 20, 1));
  973. var dimViewHeight = new Dim.DimView (view, 0);
  974. Assert.Equal (1, dimViewHeight.Anchor (0));
  975. var dimViewWidth = new Dim.DimView (view, 1);
  976. Assert.Equal (20, dimViewWidth.Anchor (0));
  977. view.Dispose ();
  978. }
  979. [Fact]
  980. public void Function_SetsValue ()
  981. {
  982. var text = "Test";
  983. var dim = Dim.Function (() => text.Length);
  984. Assert.Equal ("DimFunc(4)", dim.ToString ());
  985. text = "New Test";
  986. Assert.Equal ("DimFunc(8)", dim.ToString ());
  987. text = "";
  988. Assert.Equal ("DimFunc(0)", dim.ToString ());
  989. }
  990. [Fact]
  991. public void Function_Equal ()
  992. {
  993. var f1 = () => 0;
  994. var f2 = () => 0;
  995. var dim1 = Dim.Function (f1);
  996. var dim2 = Dim.Function (f2);
  997. Assert.Equal (dim1, dim2);
  998. f2 = () => 1;
  999. dim2 = Dim.Function (f2);
  1000. Assert.NotEqual (dim1, dim2);
  1001. }
  1002. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  1003. // TODO: A new test that calls SetRelativeLayout directly is needed.
  1004. [Theory] [AutoInitShutdown]
  1005. [InlineData (0, true)]
  1006. [InlineData (0, false)]
  1007. [InlineData (50, true)]
  1008. [InlineData (50, false)]
  1009. public void DimPercentPlusOne (int startingDistance, bool testHorizontal)
  1010. {
  1011. var container = new View {
  1012. Width = 100,
  1013. Height = 100
  1014. };
  1015. var label = new Label {
  1016. X = testHorizontal ? startingDistance : 0,
  1017. Y = testHorizontal ? 0 : startingDistance,
  1018. Width = testHorizontal ? Dim.Percent (50) + 1 : 1,
  1019. Height = testHorizontal ? 1 : Dim.Percent (50) + 1
  1020. };
  1021. container.Add (label);
  1022. Application.Top.Add (container);
  1023. Application.Top.BeginInit ();
  1024. Application.Top.EndInit ();
  1025. Application.Top.LayoutSubviews ();
  1026. Assert.Equal (100, container.Frame.Width);
  1027. Assert.Equal (100, container.Frame.Height);
  1028. if (testHorizontal) {
  1029. Assert.Equal (51, label.Frame.Width);
  1030. Assert.Equal (1, label.Frame.Height);
  1031. } else {
  1032. Assert.Equal (1, label.Frame.Width);
  1033. Assert.Equal (51, label.Frame.Height);
  1034. }
  1035. }
  1036. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  1037. // TODO: A new test that calls SetRelativeLayout directly is needed.
  1038. [Fact] [TestRespondersDisposed]
  1039. public void Dim_Referencing_SuperView_Does_Not_Throw ()
  1040. {
  1041. var super = new View ("super") {
  1042. Width = 10,
  1043. Height = 10
  1044. };
  1045. var view = new View ("view") {
  1046. Width = Dim.Width (super), // this is allowed
  1047. Height = Dim.Height (super) // this is allowed
  1048. };
  1049. super.Add (view);
  1050. super.BeginInit ();
  1051. super.EndInit ();
  1052. var exception = Record.Exception (super.LayoutSubviews);
  1053. Assert.Null (exception);
  1054. super.Dispose ();
  1055. }
  1056. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  1057. // TODO: A new test that calls SetRelativeLayout directly is needed.
  1058. [Fact] [TestRespondersDisposed]
  1059. public void Dim_SyperView_Referencing_SubView_Throws ()
  1060. {
  1061. var super = new View ("super") {
  1062. Width = 10,
  1063. Height = 10
  1064. };
  1065. var view2 = new View ("view2") {
  1066. Width = 10,
  1067. Height = 10
  1068. };
  1069. var view = new View ("view") {
  1070. Width = Dim.Width (view2), // this is not allowed
  1071. Height = Dim.Height (view2) // this is not allowed
  1072. };
  1073. view.Add (view2);
  1074. super.Add (view);
  1075. super.BeginInit ();
  1076. super.EndInit ();
  1077. Assert.Throws<InvalidOperationException> (super.LayoutSubviews);
  1078. super.Dispose ();
  1079. }
  1080. }