DimTests.cs 37 KB

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