DimTests.cs 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347
  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. [Fact] [TestRespondersDisposed]
  544. public void Dim_Validation_Do_Not_Throws_If_NewValue_Is_DimAbsolute_And_OldValue_Is_Null ()
  545. {
  546. var t = new View ("top") { Width = 80, Height = 25 };
  547. var w = new Window (new Rect (1, 2, 4, 5)) { Title = "w" };
  548. t.Add (w);
  549. t.LayoutSubviews ();
  550. Assert.Equal (3, w.Width = 3);
  551. Assert.Equal (4, w.Height = 4);
  552. t.Dispose ();
  553. }
  554. [Fact] [TestRespondersDisposed]
  555. public void Dim_Validation_Do_Not_Throws_If_NewValue_Is_DimAbsolute_And_OldValue_Is_Another_Type_After_Sets_To_LayoutStyle_Absolute ()
  556. {
  557. var t = new View ("top") { Width = 80, Height = 25 };
  558. var w = new Window {
  559. Width = Dim.Fill (),
  560. Height = Dim.Sized (10)
  561. };
  562. var v = new View ("v") {
  563. Width = Dim.Width (w) - 2,
  564. Height = Dim.Percent (10)
  565. };
  566. w.Add (v);
  567. t.Add (w);
  568. t.LayoutSubviews ();
  569. Assert.Equal (2, v.Width = 2);
  570. Assert.Equal (2, v.Height = 2);
  571. v.LayoutStyle = LayoutStyle.Absolute;
  572. t.LayoutSubviews ();
  573. Assert.Equal (2, v.Width = 2);
  574. Assert.Equal (2, v.Height = 2);
  575. t.Dispose ();
  576. }
  577. [Fact] [AutoInitShutdown]
  578. public void Only_DimAbsolute_And_DimFactor_As_A_Different_Procedure_For_Assigning_Value_To_Width_Or_Height ()
  579. {
  580. // Testing with the Button because it properly handles the Dim class.
  581. var t = Application.Top;
  582. var w = new Window {
  583. Width = 100,
  584. Height = 100
  585. };
  586. var f1 = new FrameView ("f1") {
  587. X = 0,
  588. Y = 0,
  589. Width = Dim.Percent (50),
  590. Height = 5
  591. };
  592. var f2 = new FrameView ("f2") {
  593. X = Pos.Right (f1),
  594. Y = 0,
  595. Width = Dim.Fill (),
  596. Height = 5
  597. };
  598. var v1 = new Button ("v1") {
  599. AutoSize = false,
  600. X = Pos.X (f1) + 2,
  601. Y = Pos.Bottom (f1) + 2,
  602. Width = Dim.Width (f1) - 2,
  603. Height = Dim.Fill () - 2,
  604. ValidatePosDim = true
  605. };
  606. var v2 = new Button ("v2") {
  607. AutoSize = false,
  608. X = Pos.X (f2) + 2,
  609. Y = Pos.Bottom (f2) + 2,
  610. Width = Dim.Width (f2) - 2,
  611. Height = Dim.Fill () - 2,
  612. ValidatePosDim = true
  613. };
  614. var v3 = new Button ("v3") {
  615. AutoSize = false,
  616. Width = Dim.Percent (10),
  617. Height = Dim.Percent (10),
  618. ValidatePosDim = true
  619. };
  620. var v4 = new Button ("v4") {
  621. AutoSize = false,
  622. Width = Dim.Sized (50),
  623. Height = Dim.Sized (50),
  624. ValidatePosDim = true
  625. };
  626. var v5 = new Button ("v5") {
  627. AutoSize = false,
  628. Width = Dim.Width (v1) - Dim.Width (v3),
  629. Height = Dim.Height (v1) - Dim.Height (v3),
  630. ValidatePosDim = true
  631. };
  632. var v6 = new Button ("v6") {
  633. AutoSize = false,
  634. X = Pos.X (f2),
  635. Y = Pos.Bottom (f2) + 2,
  636. Width = Dim.Percent (20, true),
  637. Height = Dim.Percent (20, true),
  638. ValidatePosDim = true
  639. };
  640. w.Add (f1, f2, v1, v2, v3, v4, v5, v6);
  641. t.Add (w);
  642. t.Ready += (s, e) => {
  643. Assert.Equal ("Absolute(100)", w.Width.ToString ());
  644. Assert.Equal ("Absolute(100)", w.Height.ToString ());
  645. Assert.Equal (100, w.Frame.Width);
  646. Assert.Equal (100, w.Frame.Height);
  647. Assert.Equal ("Factor(0.5,False)", f1.Width.ToString ());
  648. Assert.Equal ("Absolute(5)", f1.Height.ToString ());
  649. Assert.Equal (49, f1.Frame.Width); // 50-1=49
  650. Assert.Equal (5, f1.Frame.Height);
  651. Assert.Equal ("Fill(0)", f2.Width.ToString ());
  652. Assert.Equal ("Absolute(5)", f2.Height.ToString ());
  653. Assert.Equal (49, f2.Frame.Width); // 50-1=49
  654. Assert.Equal (5, f2.Frame.Height);
  655. Assert.Equal ("Combine(View(Width,FrameView(f1)(0,0,49,5))-Absolute(2))", v1.Width.ToString ());
  656. Assert.Equal ("Combine(Fill(0)-Absolute(2))", v1.Height.ToString ());
  657. Assert.Equal (47, v1.Frame.Width); // 49-2=47
  658. Assert.Equal (89, v1.Frame.Height); // 98-5-2-2=89
  659. Assert.Equal ("Combine(View(Width,FrameView(f2)(49,0,49,5))-Absolute(2))", v2.Width.ToString ());
  660. Assert.Equal ("Combine(Fill(0)-Absolute(2))", v2.Height.ToString ());
  661. Assert.Equal (47, v2.Frame.Width); // 49-2=47
  662. Assert.Equal (89, v2.Frame.Height); // 98-5-2-2=89
  663. Assert.Equal ("Factor(0.1,False)", v3.Width.ToString ());
  664. Assert.Equal ("Factor(0.1,False)", v3.Height.ToString ());
  665. Assert.Equal (9, v3.Frame.Width); // 98*10%=9
  666. Assert.Equal (9, v3.Frame.Height); // 98*10%=9
  667. Assert.Equal ("Absolute(50)", v4.Width.ToString ());
  668. Assert.Equal ("Absolute(50)", v4.Height.ToString ());
  669. Assert.Equal (50, v4.Frame.Width);
  670. Assert.Equal (50, v4.Frame.Height);
  671. Assert.Equal ("Combine(View(Width,Button(v1)(2,7,47,89))-View(Width,Button(v3)(0,0,9,9)))", v5.Width.ToString ());
  672. Assert.Equal ("Combine(View(Height,Button(v1)(2,7,47,89))-View(Height,Button(v3)(0,0,9,9)))", v5.Height.ToString ());
  673. Assert.Equal (38, v5.Frame.Width); // 47-9=38
  674. Assert.Equal (80, v5.Frame.Height); // 89-9=80
  675. Assert.Equal ("Factor(0.2,True)", v6.Width.ToString ());
  676. Assert.Equal ("Factor(0.2,True)", v6.Height.ToString ());
  677. Assert.Equal (9, v6.Frame.Width); // 47*20%=9
  678. Assert.Equal (18, v6.Frame.Height); // 89*20%=18
  679. w.Width = 200;
  680. Assert.True (t.LayoutNeeded);
  681. w.Height = 200;
  682. t.LayoutSubviews ();
  683. Assert.Equal ("Absolute(200)", w.Width.ToString ());
  684. Assert.Equal ("Absolute(200)", w.Height.ToString ());
  685. Assert.Equal (200, w.Frame.Width);
  686. Assert.Equal (200, w.Frame.Height);
  687. f1.Text = "Frame1";
  688. Assert.Equal ("Factor(0.5,False)", f1.Width.ToString ());
  689. Assert.Equal ("Absolute(5)", f1.Height.ToString ());
  690. Assert.Equal (99, f1.Frame.Width); // 100-1=99
  691. Assert.Equal (5, f1.Frame.Height);
  692. f2.Text = "Frame2";
  693. Assert.Equal ("Fill(0)", f2.Width.ToString ());
  694. Assert.Equal ("Absolute(5)", f2.Height.ToString ());
  695. Assert.Equal (99, f2.Frame.Width); // 100-1=99
  696. Assert.Equal (5, f2.Frame.Height);
  697. v1.Text = "Button1";
  698. Assert.Equal ("Combine(View(Width,FrameView(f1)(0,0,99,5))-Absolute(2))", v1.Width.ToString ());
  699. Assert.Equal ("Combine(Fill(0)-Absolute(2))", v1.Height.ToString ());
  700. Assert.Equal (97, v1.Frame.Width); // 99-2=97
  701. Assert.Equal (189, v1.Frame.Height); // 198-2-7=189
  702. v2.Text = "Button2";
  703. Assert.Equal ("Combine(View(Width,FrameView(f2)(99,0,99,5))-Absolute(2))", v2.Width.ToString ());
  704. Assert.Equal ("Combine(Fill(0)-Absolute(2))", v2.Height.ToString ());
  705. Assert.Equal (97, v2.Frame.Width); // 99-2=97
  706. Assert.Equal (189, v2.Frame.Height); // 198-2-7=189
  707. v3.Text = "Button3";
  708. Assert.Equal ("Factor(0.1,False)", v3.Width.ToString ());
  709. Assert.Equal ("Factor(0.1,False)", v3.Height.ToString ());
  710. 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
  711. Assert.Equal (19, v3.Frame.Height); // 199*10%=19
  712. v4.Text = "Button4";
  713. v4.AutoSize = false;
  714. Assert.Equal ("Absolute(50)", v4.Width.ToString ());
  715. Assert.Equal ("Absolute(50)", v4.Height.ToString ());
  716. Assert.Equal (50, v4.Frame.Width);
  717. Assert.Equal (50, v4.Frame.Height);
  718. v4.AutoSize = true;
  719. Assert.Equal ("Absolute(11)", v4.Width.ToString ());
  720. Assert.Equal ("Absolute(1)", v4.Height.ToString ());
  721. Assert.Equal (11, v4.Frame.Width); // 11 is the text length and because is Dim.DimAbsolute
  722. Assert.Equal (1, v4.Frame.Height); // 1 because is Dim.DimAbsolute
  723. v5.Text = "Button5";
  724. Assert.Equal ("Combine(View(Width,Button(v1)(2,7,97,189))-View(Width,Button(v3)(0,0,19,19)))", v5.Width.ToString ());
  725. Assert.Equal ("Combine(View(Height,Button(v1)(2,7,97,189))-View(Height,Button(v3)(0,0,19,19)))", v5.Height.ToString ());
  726. Assert.Equal (78, v5.Frame.Width); // 97-9=78
  727. Assert.Equal (170, v5.Frame.Height); // 189-19=170
  728. v6.Text = "Button6";
  729. Assert.Equal ("Factor(0.2,True)", v6.Width.ToString ());
  730. Assert.Equal ("Factor(0.2,True)", v6.Height.ToString ());
  731. Assert.Equal (19, v6.Frame.Width); // 99*20%=19
  732. Assert.Equal (38, v6.Frame.Height); // 198-7*20=18
  733. };
  734. Application.Iteration += (s, a) => Application.RequestStop ();
  735. Application.Run ();
  736. }
  737. // See #2461
  738. //[Fact]
  739. //public void Dim_Referencing_SuperView_Throws ()
  740. //{
  741. // var super = new View ("super") {
  742. // Width = 10,
  743. // Height = 10
  744. // };
  745. // var view = new View ("view") {
  746. // Width = Dim.Width (super), // this is not allowed
  747. // Height = Dim.Height (super), // this is not allowed
  748. // };
  749. // super.Add (view);
  750. // super.BeginInit ();
  751. // super.EndInit ();
  752. // Assert.Throws<InvalidOperationException> (() => super.LayoutSubviews ());
  753. //}
  754. /// <summary>
  755. /// This is an intentionally obtuse test. See https://github.com/gui-cs/Terminal.Gui/issues/2461
  756. /// </summary>
  757. [Fact] [TestRespondersDisposed]
  758. public void DimCombine_ObtuseScenario_Throw_If_SuperView_Refs_SubView ()
  759. {
  760. var t = new View { Width = 80, Height = 25 };
  761. var w = new Window {
  762. Width = Dim.Width (t) - 2, // 78
  763. Height = Dim.Height (t) - 2 // 23
  764. };
  765. var f = new FrameView ();
  766. var v1 = new View {
  767. Width = Dim.Width (w) - 2, // 76
  768. Height = Dim.Height (w) - 2 // 21
  769. };
  770. var v2 = new View {
  771. Width = Dim.Width (v1) - 2, // 74
  772. Height = Dim.Height (v1) - 2 // 19
  773. };
  774. f.Add (v1, v2);
  775. w.Add (f);
  776. t.Add (w);
  777. t.BeginInit ();
  778. t.EndInit ();
  779. // BUGBUG: v2 - f references t here; t is f's super-superview. This is supported!
  780. // BUGBUG: v2 - f references v2 here; v2 is f's subview. This is not supported!
  781. f.Width = Dim.Width (t) - Dim.Width (v2); // 80 - 74 = 6
  782. f.Height = Dim.Height (t) - Dim.Height (v2); // 25 - 19 = 6
  783. Assert.Throws<InvalidOperationException> (t.LayoutSubviews);
  784. Assert.Equal (80, t.Frame.Width);
  785. Assert.Equal (25, t.Frame.Height);
  786. Assert.Equal (78, w.Frame.Width);
  787. Assert.Equal (23, w.Frame.Height);
  788. // BUGBUG: v2 - this no longer works - see above
  789. //Assert.Equal (6, f.Frame.Width);
  790. //Assert.Equal (6, f.Frame.Height);
  791. //Assert.Equal (76, v1.Frame.Width);
  792. //Assert.Equal (21, v1.Frame.Height);
  793. //Assert.Equal (74, v2.Frame.Width);
  794. //Assert.Equal (19, v2.Frame.Height);
  795. t.Dispose ();
  796. }
  797. [Fact] [TestRespondersDisposed]
  798. public void DimCombine_ObtuseScenario_Does_Not_Throw_If_Two_SubViews_Refs_The_Same_SuperView ()
  799. {
  800. var t = new View ("top") { Width = 80, Height = 25 };
  801. var w = new Window {
  802. Width = Dim.Width (t) - 2, // 78
  803. Height = Dim.Height (t) - 2 // 23
  804. };
  805. var f = new FrameView ();
  806. var v1 = new View {
  807. Width = Dim.Width (w) - 2, // 76
  808. Height = Dim.Height (w) - 2 // 21
  809. };
  810. var v2 = new View {
  811. Width = Dim.Width (v1) - 2, // 74
  812. Height = Dim.Height (v1) - 2 // 19
  813. };
  814. f.Add (v1, v2);
  815. w.Add (f);
  816. t.Add (w);
  817. t.BeginInit ();
  818. t.EndInit ();
  819. f.Width = Dim.Width (t) - Dim.Width (w) + 4; // 80 - 74 = 6
  820. f.Height = Dim.Height (t) - Dim.Height (w) + 4; // 25 - 19 = 6
  821. // BUGBUG: v2 - f references t and w here; t is f's super-superview and w is f's superview. This is supported!
  822. var exception = Record.Exception (t.LayoutSubviews);
  823. Assert.Null (exception);
  824. Assert.Equal (80, t.Frame.Width);
  825. Assert.Equal (25, t.Frame.Height);
  826. Assert.Equal (78, w.Frame.Width);
  827. Assert.Equal (23, w.Frame.Height);
  828. Assert.Equal (6, f.Frame.Width);
  829. Assert.Equal (6, f.Frame.Height);
  830. Assert.Equal (76, v1.Frame.Width);
  831. Assert.Equal (21, v1.Frame.Height);
  832. Assert.Equal (74, v2.Frame.Width);
  833. Assert.Equal (19, v2.Frame.Height);
  834. t.Dispose ();
  835. }
  836. [Fact] [TestRespondersDisposed]
  837. public void PosCombine_View_Not_Added_Throws ()
  838. {
  839. var t = new View { Width = 80, Height = 50 };
  840. // BUGBUG: v2 - super should not reference it's superview (t)
  841. var super = new View {
  842. Width = Dim.Width (t) - 2,
  843. Height = Dim.Height (t) - 2
  844. };
  845. t.Add (super);
  846. var sub = new View ();
  847. super.Add (sub);
  848. var v1 = new View {
  849. Width = Dim.Width (super) - 2,
  850. Height = Dim.Height (super) - 2
  851. };
  852. var v2 = new View {
  853. Width = Dim.Width (v1) - 2,
  854. Height = Dim.Height (v1) - 2
  855. };
  856. sub.Add (v1);
  857. // v2 not added to sub; should cause exception on Layout since it's referenced by sub.
  858. sub.Width = Dim.Fill () - Dim.Width (v2);
  859. sub.Height = Dim.Fill () - Dim.Height (v2);
  860. t.BeginInit ();
  861. t.EndInit ();
  862. Assert.Throws<InvalidOperationException> (() => t.LayoutSubviews ());
  863. t.Dispose ();
  864. v2.Dispose ();
  865. }
  866. [Fact] [AutoInitShutdown]
  867. public void Dim_Add_Operator ()
  868. {
  869. var top = Application.Top;
  870. var view = new View { X = 0, Y = 0, Width = 20, Height = 0 };
  871. var field = new TextField { X = 0, Y = Pos.Bottom (view), Width = 20 };
  872. var count = 0;
  873. field.KeyDown += (s, k) => {
  874. if (k.KeyCode == KeyCode.Enter) {
  875. field.Text = $"Label {count}";
  876. var label = new Label (field.Text) { X = 0, Y = view.Bounds.Height, Width = 20 };
  877. view.Add (label);
  878. Assert.Equal ($"Label {count}", label.Text);
  879. Assert.Equal ($"Absolute({count})", label.Y.ToString ());
  880. Assert.Equal ($"Absolute({count})", view.Height.ToString ());
  881. view.Height += 1;
  882. count++;
  883. Assert.Equal ($"Absolute({count})", view.Height.ToString ());
  884. }
  885. };
  886. Application.Iteration += (s, a) => {
  887. while (count < 20) {
  888. field.NewKeyDownEvent (new Key (KeyCode.Enter));
  889. }
  890. Application.RequestStop ();
  891. };
  892. var win = new Window ();
  893. win.Add (view);
  894. win.Add (field);
  895. top.Add (win);
  896. Application.Run (top);
  897. Assert.Equal (20, count);
  898. }
  899. [Fact] [AutoInitShutdown]
  900. public void Dim_Add_Operator_With_Text ()
  901. {
  902. var top = Application.Top;
  903. var view = new View ("View with long text") { X = 0, Y = 0, Width = 20, Height = 1 };
  904. var field = new TextField { X = 0, Y = Pos.Bottom (view), Width = 20 };
  905. var count = 0;
  906. var listLabels = new List<Label> ();
  907. field.KeyDown += (s, k) => {
  908. if (k.KeyCode == KeyCode.Enter) {
  909. ((FakeDriver)Application.Driver).SetBufferSize (22, count + 4);
  910. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expecteds [count], _output);
  911. Assert.Equal (new Rect (0, 0, 22, count + 4), pos);
  912. if (count < 20) {
  913. field.Text = $"Label {count}";
  914. var label = new Label (field.Text) { X = 0, Y = view.Bounds.Height, Width = 10 };
  915. view.Add (label);
  916. Assert.Equal ($"Label {count}", label.Text);
  917. Assert.Equal ($"Absolute({count + 1})", label.Y.ToString ());
  918. listLabels.Add (label);
  919. //if (count == 0) {
  920. // Assert.Equal ($"Absolute({count})", view.Height.ToString ());
  921. // view.Height += 2;
  922. //} else {
  923. Assert.Equal ($"Absolute({count + 1})", view.Height.ToString ());
  924. view.Height += 1;
  925. //}
  926. count++;
  927. }
  928. Assert.Equal ($"Absolute({count + 1})", view.Height.ToString ());
  929. }
  930. };
  931. Application.Iteration += (s, a) => {
  932. while (count < 21) {
  933. field.NewKeyDownEvent (new Key (KeyCode.Enter));
  934. if (count == 20) {
  935. field.NewKeyDownEvent (new Key (KeyCode.Enter));
  936. break;
  937. }
  938. }
  939. Application.RequestStop ();
  940. };
  941. var win = new Window ();
  942. win.Add (view);
  943. win.Add (field);
  944. top.Add (win);
  945. Application.Run (top);
  946. Assert.Equal (20, count);
  947. Assert.Equal (count, listLabels.Count);
  948. }
  949. [Fact] [AutoInitShutdown]
  950. public void Dim_Subtract_Operator ()
  951. {
  952. var top = Application.Top;
  953. var view = new View { X = 0, Y = 0, Width = 20, Height = 0 };
  954. var field = new TextField { X = 0, Y = Pos.Bottom (view), Width = 20 };
  955. var count = 20;
  956. var listLabels = new List<Label> ();
  957. for (var i = 0; i < count; i++) {
  958. field.Text = $"Label {i}";
  959. var label = new Label (field.Text) { X = 0, Y = view.Bounds.Height, Width = 20 };
  960. view.Add (label);
  961. Assert.Equal ($"Label {i}", label.Text);
  962. // BUGBUG: Bogus test; views have not been initialized yet
  963. //Assert.Equal ($"Absolute({i})", label.Y.ToString ());
  964. listLabels.Add (label);
  965. // BUGBUG: Bogus test; views have not been initialized yet
  966. //Assert.Equal ($"Absolute({i})", view.Height.ToString ());
  967. view.Height += 1;
  968. //Assert.Equal ($"Absolute({i + 1})", view.Height.ToString ());
  969. }
  970. field.KeyDown += (s, k) => {
  971. if (k.KeyCode == KeyCode.Enter) {
  972. Assert.Equal ($"Label {count - 1}", listLabels [count - 1].Text);
  973. view.Remove (listLabels [count - 1]);
  974. listLabels [count - 1].Dispose ();
  975. Assert.Equal ($"Absolute({count})", view.Height.ToString ());
  976. view.Height -= 1;
  977. count--;
  978. Assert.Equal ($"Absolute({count})", view.Height.ToString ());
  979. }
  980. };
  981. Application.Iteration += (s, a) => {
  982. while (count > 0) {
  983. field.NewKeyDownEvent (new Key (KeyCode.Enter));
  984. }
  985. Application.RequestStop ();
  986. };
  987. var win = new Window ();
  988. win.Add (view);
  989. win.Add (field);
  990. top.Add (win);
  991. Application.Run (top);
  992. Assert.Equal (0, count);
  993. }
  994. [Fact] [AutoInitShutdown]
  995. public void Dim_Subtract_Operator_With_Text ()
  996. {
  997. var top = Application.Top;
  998. // BUGBUG: v2 - If a View's height is zero, it should not be drawn.
  999. //// Although view height is zero the text it's draw due the SetMinWidthHeight method
  1000. var view = new View ("View with long text") { X = 0, Y = 0, Width = 20, Height = 1 };
  1001. var field = new TextField { X = 0, Y = Pos.Bottom (view), Width = 20 };
  1002. var count = 20;
  1003. var listLabels = new List<Label> ();
  1004. for (var i = 0; i < count; i++) {
  1005. field.Text = $"Label {i}";
  1006. // BUGBUG: v2 - view has not been initialied yet; view.Bounds is indeterminate
  1007. var label = new Label (field.Text) { X = 0, Y = i + 1, Width = 10 };
  1008. view.Add (label);
  1009. Assert.Equal ($"Label {i}", label.Text);
  1010. // BUGBUG: Bogus test; views have not been initialized yet
  1011. //Assert.Equal ($"Absolute({i + 1})", label.Y.ToString ());
  1012. listLabels.Add (label);
  1013. //if (i == 0) {
  1014. // BUGBUG: Bogus test; views have not been initialized yet
  1015. //Assert.Equal ($"Absolute({i})", view.Height.ToString ());
  1016. //view.Height += 2;
  1017. // BUGBUG: Bogus test; views have not been initialized yet
  1018. //Assert.Equal ($"Absolute({i + 2})", view.Height.ToString ());
  1019. //} else {
  1020. // BUGBUG: Bogus test; views have not been initialized yet
  1021. //Assert.Equal ($"Absolute({i + 1})", view.Height.ToString ());
  1022. view.Height += 1;
  1023. // BUGBUG: Bogus test; views have not been initialized yet
  1024. //Assert.Equal ($"Absolute({i + 2})", view.Height.ToString ());
  1025. //}
  1026. }
  1027. field.KeyDown += (s, k) => {
  1028. if (k.KeyCode == KeyCode.Enter) {
  1029. ((FakeDriver)Application.Driver).SetBufferSize (22, count + 4);
  1030. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expecteds [count], _output);
  1031. Assert.Equal (new Rect (0, 0, 22, count + 4), pos);
  1032. if (count > 0) {
  1033. Assert.Equal ($"Label {count - 1}", listLabels [count - 1].Text);
  1034. view.Remove (listLabels [count - 1]);
  1035. listLabels [count - 1].Dispose ();
  1036. listLabels.RemoveAt (count - 1);
  1037. Assert.Equal ($"Absolute({count + 1})", view.Height.ToString ());
  1038. view.Height -= 1;
  1039. count--;
  1040. if (listLabels.Count > 0) {
  1041. field.Text = listLabels [count - 1].Text;
  1042. } else {
  1043. field.Text = string.Empty;
  1044. }
  1045. }
  1046. Assert.Equal ($"Absolute({count + 1})", view.Height.ToString ());
  1047. }
  1048. };
  1049. Application.Iteration += (s, a) => {
  1050. while (count > -1) {
  1051. field.NewKeyDownEvent (new Key (KeyCode.Enter));
  1052. if (count == 0) {
  1053. field.NewKeyDownEvent (new Key (KeyCode.Enter));
  1054. break;
  1055. }
  1056. }
  1057. Application.RequestStop ();
  1058. };
  1059. var win = new Window ();
  1060. win.Add (view);
  1061. win.Add (field);
  1062. top.Add (win);
  1063. Application.Run (top);
  1064. Assert.Equal (0, count);
  1065. Assert.Equal (count, listLabels.Count);
  1066. }
  1067. [Fact] [TestRespondersDisposed]
  1068. public void Internal_Tests ()
  1069. {
  1070. var dimFactor = new Dim.DimFactor (0.10F);
  1071. Assert.Equal (10, dimFactor.Anchor (100));
  1072. var dimAbsolute = new Dim.DimAbsolute (10);
  1073. Assert.Equal (10, dimAbsolute.Anchor (0));
  1074. var dimFill = new Dim.DimFill (1);
  1075. Assert.Equal (99, dimFill.Anchor (100));
  1076. var dimCombine = new Dim.DimCombine (true, dimFactor, dimAbsolute);
  1077. Assert.Equal (dimCombine._left, dimFactor);
  1078. Assert.Equal (dimCombine._right, dimAbsolute);
  1079. Assert.Equal (20, dimCombine.Anchor (100));
  1080. var view = new View (new Rect (20, 10, 20, 1));
  1081. var dimViewHeight = new Dim.DimView (view, 0);
  1082. Assert.Equal (1, dimViewHeight.Anchor (0));
  1083. var dimViewWidth = new Dim.DimView (view, 1);
  1084. Assert.Equal (20, dimViewWidth.Anchor (0));
  1085. view.Dispose ();
  1086. }
  1087. [Fact]
  1088. public void Function_SetsValue ()
  1089. {
  1090. var text = "Test";
  1091. var dim = Dim.Function (() => text.Length);
  1092. Assert.Equal ("DimFunc(4)", dim.ToString ());
  1093. text = "New Test";
  1094. Assert.Equal ("DimFunc(8)", dim.ToString ());
  1095. text = "";
  1096. Assert.Equal ("DimFunc(0)", dim.ToString ());
  1097. }
  1098. [Fact]
  1099. public void Function_Equal ()
  1100. {
  1101. var f1 = () => 0;
  1102. var f2 = () => 0;
  1103. var dim1 = Dim.Function (f1);
  1104. var dim2 = Dim.Function (f2);
  1105. Assert.Equal (dim1, dim2);
  1106. f2 = () => 1;
  1107. dim2 = Dim.Function (f2);
  1108. Assert.NotEqual (dim1, dim2);
  1109. }
  1110. [Theory] [AutoInitShutdown]
  1111. [InlineData (0, true)]
  1112. [InlineData (0, false)]
  1113. [InlineData (50, true)]
  1114. [InlineData (50, false)]
  1115. public void DimPercentPlusOne (int startingDistance, bool testHorizontal)
  1116. {
  1117. var container = new View {
  1118. Width = 100,
  1119. Height = 100
  1120. };
  1121. var label = new Label {
  1122. X = testHorizontal ? startingDistance : 0,
  1123. Y = testHorizontal ? 0 : startingDistance,
  1124. Width = testHorizontal ? Dim.Percent (50) + 1 : 1,
  1125. Height = testHorizontal ? 1 : Dim.Percent (50) + 1
  1126. };
  1127. container.Add (label);
  1128. Application.Top.Add (container);
  1129. Application.Top.BeginInit ();
  1130. Application.Top.EndInit ();
  1131. Application.Top.LayoutSubviews ();
  1132. Assert.Equal (100, container.Frame.Width);
  1133. Assert.Equal (100, container.Frame.Height);
  1134. if (testHorizontal) {
  1135. Assert.Equal (51, label.Frame.Width);
  1136. Assert.Equal (1, label.Frame.Height);
  1137. } else {
  1138. Assert.Equal (1, label.Frame.Width);
  1139. Assert.Equal (51, label.Frame.Height);
  1140. }
  1141. }
  1142. [Fact] [TestRespondersDisposed]
  1143. public void Dim_Referencing_SuperView_Does_Not_Throw ()
  1144. {
  1145. var super = new View ("super") {
  1146. Width = 10,
  1147. Height = 10
  1148. };
  1149. var view = new View ("view") {
  1150. Width = Dim.Width (super), // this is allowed
  1151. Height = Dim.Height (super) // this is allowed
  1152. };
  1153. super.Add (view);
  1154. super.BeginInit ();
  1155. super.EndInit ();
  1156. var exception = Record.Exception (super.LayoutSubviews);
  1157. Assert.Null (exception);
  1158. super.Dispose ();
  1159. }
  1160. [Fact] [TestRespondersDisposed]
  1161. public void Dim_SyperView_Referencing_SubView_Throws ()
  1162. {
  1163. var super = new View ("super") {
  1164. Width = 10,
  1165. Height = 10
  1166. };
  1167. var view2 = new View ("view2") {
  1168. Width = 10,
  1169. Height = 10
  1170. };
  1171. var view = new View ("view") {
  1172. Width = Dim.Width (view2), // this is not allowed
  1173. Height = Dim.Height (view2) // this is not allowed
  1174. };
  1175. view.Add (view2);
  1176. super.Add (view);
  1177. super.BeginInit ();
  1178. super.EndInit ();
  1179. Assert.Throws<InvalidOperationException> (super.LayoutSubviews);
  1180. super.Dispose ();
  1181. }
  1182. }