DimTests.cs 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282
  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. v4.Text = "Button4";
  420. v4.AutoSize = false;
  421. Assert.Equal ("Absolute(50)", v4.Width.ToString ());
  422. Assert.Equal ("Absolute(50)", v4.Height.ToString ());
  423. Assert.Equal (50, v4.Frame.Width);
  424. Assert.Equal (50, v4.Frame.Height);
  425. v4.AutoSize = true;
  426. Assert.Equal ("Absolute(11)", v4.Width.ToString ());
  427. Assert.Equal ("Absolute(1)", v4.Height.ToString ());
  428. Assert.Equal (11, v4.Frame.Width); // 11 is the text length and because is Dim.DimAbsolute
  429. Assert.Equal (1, v4.Frame.Height); // 1 because is Dim.DimAbsolute
  430. v5.Text = "Button5";
  431. Assert.Equal ("Combine(View(Width,Button()({X=2,Y=7,Width=97,Height=189}))-View(Width,Button()({X=0,Y=0,Width=19,Height=19})))", v5.Width.ToString ());
  432. Assert.Equal ("Combine(View(Height,Button()({X=2,Y=7,Width=97,Height=189}))-View(Height,Button()({X=0,Y=0,Width=19,Height=19})))", v5.Height.ToString ());
  433. Assert.Equal (78, v5.Frame.Width); // 97-9=78
  434. Assert.Equal (170, v5.Frame.Height); // 189-19=170
  435. v6.Text = "Button6";
  436. Assert.Equal ("Factor(0.2,True)", v6.Width.ToString ());
  437. Assert.Equal ("Factor(0.2,True)", v6.Height.ToString ());
  438. Assert.Equal (19, v6.Frame.Width); // 99*20%=19
  439. Assert.Equal (38, v6.Frame.Height); // 198-7*20=18
  440. };
  441. Application.Iteration += () => Application.RequestStop ();
  442. Application.Run ();
  443. Application.Shutdown ();
  444. }
  445. // DONE: Test operators
  446. [Fact]
  447. public void DimCombine_Do_Not_Throws ()
  448. {
  449. Application.Init (new FakeDriver ());
  450. var t = Application.Top;
  451. var w = new Window ("w") {
  452. Width = Dim.Width (t) - 2,
  453. Height = Dim.Height (t) - 2
  454. };
  455. var f = new FrameView ("f");
  456. var v1 = new View ("v1") {
  457. Width = Dim.Width (w) - 2,
  458. Height = Dim.Height (w) - 2
  459. };
  460. var v2 = new View ("v2") {
  461. Width = Dim.Width (v1) - 2,
  462. Height = Dim.Height (v1) - 2
  463. };
  464. f.Add (v1, v2);
  465. w.Add (f);
  466. t.Add (w);
  467. f.Width = Dim.Width (t) - Dim.Width (v2);
  468. f.Height = Dim.Height (t) - Dim.Height (v2);
  469. t.Ready += () => {
  470. Assert.Equal (80, t.Frame.Width);
  471. Assert.Equal (25, t.Frame.Height);
  472. Assert.Equal (78, w.Frame.Width);
  473. Assert.Equal (23, w.Frame.Height);
  474. Assert.Equal (6, f.Frame.Width);
  475. Assert.Equal (6, f.Frame.Height);
  476. Assert.Equal (76, v1.Frame.Width);
  477. Assert.Equal (21, v1.Frame.Height);
  478. Assert.Equal (74, v2.Frame.Width);
  479. Assert.Equal (19, v2.Frame.Height);
  480. };
  481. Application.Iteration += () => Application.RequestStop ();
  482. Application.Run ();
  483. Application.Shutdown ();
  484. }
  485. [Fact]
  486. public void PosCombine_Will_Throws ()
  487. {
  488. Application.Init (new FakeDriver ());
  489. var t = Application.Top;
  490. var w = new Window ("w") {
  491. Width = Dim.Width (t) - 2,
  492. Height = Dim.Height (t) - 2
  493. };
  494. var f = new FrameView ("f");
  495. var v1 = new View ("v1") {
  496. Width = Dim.Width (w) - 2,
  497. Height = Dim.Height (w) - 2
  498. };
  499. var v2 = new View ("v2") {
  500. Width = Dim.Width (v1) - 2,
  501. Height = Dim.Height (v1) - 2
  502. };
  503. f.Add (v1); // v2 not added
  504. w.Add (f);
  505. t.Add (w);
  506. f.Width = Dim.Width (t) - Dim.Width (v2);
  507. f.Height = Dim.Height (t) - Dim.Height (v2);
  508. Assert.Throws<InvalidOperationException> (() => Application.Run ());
  509. Application.Shutdown ();
  510. }
  511. [Fact]
  512. public void Dim_Add_Operator ()
  513. {
  514. Application.Init (new FakeDriver ());
  515. var top = Application.Top;
  516. var view = new View () { X = 0, Y = 0, Width = 20, Height = 0 };
  517. var field = new TextField () { X = 0, Y = Pos.Bottom (view), Width = 20 };
  518. var count = 0;
  519. field.KeyDown += (k) => {
  520. if (k.KeyEvent.Key == Key.Enter) {
  521. field.Text = $"Label {count}";
  522. var label = new Label (field.Text) { X = 0, Y = view.Bounds.Height, Width = 20 };
  523. view.Add (label);
  524. Assert.Equal ($"Label {count}", label.Text);
  525. Assert.Equal ($"Absolute({count})", label.Y.ToString ());
  526. Assert.Equal ($"Absolute({count})", view.Height.ToString ());
  527. view.Height += 1;
  528. count++;
  529. Assert.Equal ($"Absolute({count})", view.Height.ToString ());
  530. }
  531. };
  532. Application.Iteration += () => {
  533. while (count < 20) field.OnKeyDown (new KeyEvent (Key.Enter, new KeyModifiers ()));
  534. Application.RequestStop ();
  535. };
  536. var win = new Window ();
  537. win.Add (view);
  538. win.Add (field);
  539. top.Add (win);
  540. Application.Run (top);
  541. Assert.Equal (20, count);
  542. // Shutdown must be called to safely clean up Application if Init has been called
  543. Application.Shutdown ();
  544. }
  545. private string [] expecteds = new string [21] {
  546. @"
  547. ┌────────────────────┐
  548. │View with long text │
  549. │ │
  550. └────────────────────┘",
  551. @"
  552. ┌────────────────────┐
  553. │View with long text │
  554. │Label 0 │
  555. │Label 0 │
  556. └────────────────────┘",
  557. @"
  558. ┌────────────────────┐
  559. │View with long text │
  560. │Label 0 │
  561. │Label 1 │
  562. │Label 1 │
  563. └────────────────────┘",
  564. @"
  565. ┌────────────────────┐
  566. │View with long text │
  567. │Label 0 │
  568. │Label 1 │
  569. │Label 2 │
  570. │Label 2 │
  571. └────────────────────┘",
  572. @"
  573. ┌────────────────────┐
  574. │View with long text │
  575. │Label 0 │
  576. │Label 1 │
  577. │Label 2 │
  578. │Label 3 │
  579. │Label 3 │
  580. └────────────────────┘",
  581. @"
  582. ┌────────────────────┐
  583. │View with long text │
  584. │Label 0 │
  585. │Label 1 │
  586. │Label 2 │
  587. │Label 3 │
  588. │Label 4 │
  589. │Label 4 │
  590. └────────────────────┘",
  591. @"
  592. ┌────────────────────┐
  593. │View with long text │
  594. │Label 0 │
  595. │Label 1 │
  596. │Label 2 │
  597. │Label 3 │
  598. │Label 4 │
  599. │Label 5 │
  600. │Label 5 │
  601. └────────────────────┘",
  602. @"
  603. ┌────────────────────┐
  604. │View with long text │
  605. │Label 0 │
  606. │Label 1 │
  607. │Label 2 │
  608. │Label 3 │
  609. │Label 4 │
  610. │Label 5 │
  611. │Label 6 │
  612. │Label 6 │
  613. └────────────────────┘",
  614. @"
  615. ┌────────────────────┐
  616. │View with long text │
  617. │Label 0 │
  618. │Label 1 │
  619. │Label 2 │
  620. │Label 3 │
  621. │Label 4 │
  622. │Label 5 │
  623. │Label 6 │
  624. │Label 7 │
  625. │Label 7 │
  626. └────────────────────┘",
  627. @"
  628. ┌────────────────────┐
  629. │View with long text │
  630. │Label 0 │
  631. │Label 1 │
  632. │Label 2 │
  633. │Label 3 │
  634. │Label 4 │
  635. │Label 5 │
  636. │Label 6 │
  637. │Label 7 │
  638. │Label 8 │
  639. │Label 8 │
  640. └────────────────────┘",
  641. @"
  642. ┌────────────────────┐
  643. │View with long text │
  644. │Label 0 │
  645. │Label 1 │
  646. │Label 2 │
  647. │Label 3 │
  648. │Label 4 │
  649. │Label 5 │
  650. │Label 6 │
  651. │Label 7 │
  652. │Label 8 │
  653. │Label 9 │
  654. │Label 9 │
  655. └────────────────────┘",
  656. @"
  657. ┌────────────────────┐
  658. │View with long text │
  659. │Label 0 │
  660. │Label 1 │
  661. │Label 2 │
  662. │Label 3 │
  663. │Label 4 │
  664. │Label 5 │
  665. │Label 6 │
  666. │Label 7 │
  667. │Label 8 │
  668. │Label 9 │
  669. │Label 10 │
  670. │Label 10 │
  671. └────────────────────┘",
  672. @"
  673. ┌────────────────────┐
  674. │View with long text │
  675. │Label 0 │
  676. │Label 1 │
  677. │Label 2 │
  678. │Label 3 │
  679. │Label 4 │
  680. │Label 5 │
  681. │Label 6 │
  682. │Label 7 │
  683. │Label 8 │
  684. │Label 9 │
  685. │Label 10 │
  686. │Label 11 │
  687. │Label 11 │
  688. └────────────────────┘",
  689. @"
  690. ┌────────────────────┐
  691. │View with long text │
  692. │Label 0 │
  693. │Label 1 │
  694. │Label 2 │
  695. │Label 3 │
  696. │Label 4 │
  697. │Label 5 │
  698. │Label 6 │
  699. │Label 7 │
  700. │Label 8 │
  701. │Label 9 │
  702. │Label 10 │
  703. │Label 11 │
  704. │Label 12 │
  705. │Label 12 │
  706. └────────────────────┘",
  707. @"
  708. ┌────────────────────┐
  709. │View with long text │
  710. │Label 0 │
  711. │Label 1 │
  712. │Label 2 │
  713. │Label 3 │
  714. │Label 4 │
  715. │Label 5 │
  716. │Label 6 │
  717. │Label 7 │
  718. │Label 8 │
  719. │Label 9 │
  720. │Label 10 │
  721. │Label 11 │
  722. │Label 12 │
  723. │Label 13 │
  724. │Label 13 │
  725. └────────────────────┘",
  726. @"
  727. ┌────────────────────┐
  728. │View with long text │
  729. │Label 0 │
  730. │Label 1 │
  731. │Label 2 │
  732. │Label 3 │
  733. │Label 4 │
  734. │Label 5 │
  735. │Label 6 │
  736. │Label 7 │
  737. │Label 8 │
  738. │Label 9 │
  739. │Label 10 │
  740. │Label 11 │
  741. │Label 12 │
  742. │Label 13 │
  743. │Label 14 │
  744. │Label 14 │
  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 15 │
  765. │Label 15 │
  766. └────────────────────┘",
  767. @"
  768. ┌────────────────────┐
  769. │View with long text │
  770. │Label 0 │
  771. │Label 1 │
  772. │Label 2 │
  773. │Label 3 │
  774. │Label 4 │
  775. │Label 5 │
  776. │Label 6 │
  777. │Label 7 │
  778. │Label 8 │
  779. │Label 9 │
  780. │Label 10 │
  781. │Label 11 │
  782. │Label 12 │
  783. │Label 13 │
  784. │Label 14 │
  785. │Label 15 │
  786. │Label 16 │
  787. │Label 16 │
  788. └────────────────────┘",
  789. @"
  790. ┌────────────────────┐
  791. │View with long text │
  792. │Label 0 │
  793. │Label 1 │
  794. │Label 2 │
  795. │Label 3 │
  796. │Label 4 │
  797. │Label 5 │
  798. │Label 6 │
  799. │Label 7 │
  800. │Label 8 │
  801. │Label 9 │
  802. │Label 10 │
  803. │Label 11 │
  804. │Label 12 │
  805. │Label 13 │
  806. │Label 14 │
  807. │Label 15 │
  808. │Label 16 │
  809. │Label 17 │
  810. │Label 17 │
  811. └────────────────────┘",
  812. @"
  813. ┌────────────────────┐
  814. │View with long text │
  815. │Label 0 │
  816. │Label 1 │
  817. │Label 2 │
  818. │Label 3 │
  819. │Label 4 │
  820. │Label 5 │
  821. │Label 6 │
  822. │Label 7 │
  823. │Label 8 │
  824. │Label 9 │
  825. │Label 10 │
  826. │Label 11 │
  827. │Label 12 │
  828. │Label 13 │
  829. │Label 14 │
  830. │Label 15 │
  831. │Label 16 │
  832. │Label 17 │
  833. │Label 18 │
  834. │Label 18 │
  835. └────────────────────┘",
  836. @"
  837. ┌────────────────────┐
  838. │View with long text │
  839. │Label 0 │
  840. │Label 1 │
  841. │Label 2 │
  842. │Label 3 │
  843. │Label 4 │
  844. │Label 5 │
  845. │Label 6 │
  846. │Label 7 │
  847. │Label 8 │
  848. │Label 9 │
  849. │Label 10 │
  850. │Label 11 │
  851. │Label 12 │
  852. │Label 13 │
  853. │Label 14 │
  854. │Label 15 │
  855. │Label 16 │
  856. │Label 17 │
  857. │Label 18 │
  858. │Label 19 │
  859. │Label 19 │
  860. └────────────────────┘",
  861. };
  862. [Fact]
  863. public void Dim_Add_Operator_With_Text ()
  864. {
  865. Application.Init (new FakeDriver ());
  866. var top = Application.Top;
  867. // Although view height is zero the text it's draw due the SetMinWidthHeight method
  868. var view = new View ("View with long text") { X = 0, Y = 0, Width = 20, Height = 0 };
  869. var field = new TextField () { X = 0, Y = Pos.Bottom (view), Width = 20 };
  870. var count = 0;
  871. var listLabels = new List<Label> ();
  872. field.KeyDown += (k) => {
  873. if (k.KeyEvent.Key == Key.Enter) {
  874. ((FakeDriver)Application.Driver).SetBufferSize (22, count + 4);
  875. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expecteds [count], output);
  876. Assert.Equal (new Rect (0, 0, 22, count + 4), pos);
  877. if (count < 20) {
  878. field.Text = $"Label {count}";
  879. var label = new Label (field.Text) { X = 0, Y = view.Bounds.Height, Width = 10 };
  880. view.Add (label);
  881. Assert.Equal ($"Label {count}", label.Text);
  882. Assert.Equal ($"Absolute({count + 1})", label.Y.ToString ());
  883. listLabels.Add (label);
  884. if (count == 0) {
  885. Assert.Equal ($"Absolute({count})", view.Height.ToString ());
  886. view.Height += 2;
  887. } else {
  888. Assert.Equal ($"Absolute({count + 1})", view.Height.ToString ());
  889. view.Height += 1;
  890. }
  891. count++;
  892. }
  893. Assert.Equal ($"Absolute({count + 1})", view.Height.ToString ());
  894. }
  895. };
  896. Application.Iteration += () => {
  897. while (count < 21) {
  898. field.OnKeyDown (new KeyEvent (Key.Enter, new KeyModifiers ()));
  899. if (count == 20) {
  900. field.OnKeyDown (new KeyEvent (Key.Enter, new KeyModifiers ()));
  901. break;
  902. }
  903. }
  904. Application.RequestStop ();
  905. };
  906. var win = new Window ();
  907. win.Add (view);
  908. win.Add (field);
  909. top.Add (win);
  910. Application.Run (top);
  911. Assert.Equal (20, count);
  912. Assert.Equal (count, listLabels.Count);
  913. // Shutdown must be called to safely clean up Application if Init has been called
  914. Application.Shutdown ();
  915. }
  916. [Fact]
  917. public void Dim_Subtract_Operator ()
  918. {
  919. Application.Init (new FakeDriver ());
  920. var top = Application.Top;
  921. var view = new View () { X = 0, Y = 0, Width = 20, Height = 0 };
  922. var field = new TextField () { X = 0, Y = Pos.Bottom (view), Width = 20 };
  923. var count = 20;
  924. var listLabels = new List<Label> ();
  925. for (int i = 0; i < count; i++) {
  926. field.Text = $"Label {i}";
  927. var label = new Label (field.Text) { X = 0, Y = view.Bounds.Height, Width = 20 };
  928. view.Add (label);
  929. Assert.Equal ($"Label {i}", label.Text);
  930. Assert.Equal ($"Absolute({i})", label.Y.ToString ());
  931. listLabels.Add (label);
  932. Assert.Equal ($"Absolute({i})", view.Height.ToString ());
  933. view.Height += 1;
  934. Assert.Equal ($"Absolute({i + 1})", view.Height.ToString ());
  935. }
  936. field.KeyDown += (k) => {
  937. if (k.KeyEvent.Key == Key.Enter) {
  938. Assert.Equal ($"Label {count - 1}", listLabels [count - 1].Text);
  939. view.Remove (listLabels [count - 1]);
  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 += () => {
  947. while (count > 0) field.OnKeyDown (new KeyEvent (Key.Enter, new KeyModifiers ()));
  948. Application.RequestStop ();
  949. };
  950. var win = new Window ();
  951. win.Add (view);
  952. win.Add (field);
  953. top.Add (win);
  954. Application.Run (top);
  955. Assert.Equal (0, count);
  956. // Shutdown must be called to safely clean up Application if Init has been called
  957. Application.Shutdown ();
  958. }
  959. [Fact]
  960. public void Dim_Subtract_Operator_With_Text ()
  961. {
  962. Application.Init (new FakeDriver ());
  963. var top = Application.Top;
  964. // Although view height is zero the text it's draw due the SetMinWidthHeight method
  965. var view = new View ("View with long text") { X = 0, Y = 0, Width = 20, Height = 0 };
  966. var field = new TextField () { X = 0, Y = Pos.Bottom (view), Width = 20 };
  967. var count = 20;
  968. var listLabels = new List<Label> ();
  969. for (int i = 0; i < count; i++) {
  970. field.Text = $"Label {i}";
  971. var label = new Label (field.Text) { X = 0, Y = view.Bounds.Height, Width = 10 };
  972. view.Add (label);
  973. Assert.Equal ($"Label {i}", label.Text);
  974. Assert.Equal ($"Absolute({i + 1})", label.Y.ToString ());
  975. listLabels.Add (label);
  976. if (i == 0) {
  977. Assert.Equal ($"Absolute({i})", view.Height.ToString ());
  978. view.Height += 2;
  979. Assert.Equal ($"Absolute({i + 2})", view.Height.ToString ());
  980. } else {
  981. Assert.Equal ($"Absolute({i + 1})", view.Height.ToString ());
  982. view.Height += 1;
  983. Assert.Equal ($"Absolute({i + 2})", view.Height.ToString ());
  984. }
  985. }
  986. field.KeyDown += (k) => {
  987. if (k.KeyEvent.Key == Key.Enter) {
  988. ((FakeDriver)Application.Driver).SetBufferSize (22, count + 4);
  989. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expecteds [count], output);
  990. Assert.Equal (new Rect (0, 0, 22, count + 4), pos);
  991. if (count > 0) {
  992. Assert.Equal ($"Label {count - 1}", listLabels [count - 1].Text);
  993. view.Remove (listLabels [count - 1]);
  994. listLabels.RemoveAt (count - 1);
  995. Assert.Equal ($"Absolute({count + 1})", view.Height.ToString ());
  996. view.Height -= 1;
  997. count--;
  998. if (listLabels.Count > 0)
  999. field.Text = listLabels [count - 1].Text;
  1000. else
  1001. field.Text = NStack.ustring.Empty;
  1002. }
  1003. Assert.Equal ($"Absolute({count + 1})", view.Height.ToString ());
  1004. }
  1005. };
  1006. Application.Iteration += () => {
  1007. while (count > -1) {
  1008. field.OnKeyDown (new KeyEvent (Key.Enter, new KeyModifiers ()));
  1009. if (count == 0) {
  1010. field.OnKeyDown (new KeyEvent (Key.Enter, new KeyModifiers ()));
  1011. break;
  1012. }
  1013. }
  1014. Application.RequestStop ();
  1015. };
  1016. var win = new Window ();
  1017. win.Add (view);
  1018. win.Add (field);
  1019. top.Add (win);
  1020. Application.Run (top);
  1021. Assert.Equal (0, count);
  1022. Assert.Equal (count, listLabels.Count);
  1023. // Shutdown must be called to safely clean up Application if Init has been called
  1024. Application.Shutdown ();
  1025. }
  1026. [Fact]
  1027. public void Internal_Tests ()
  1028. {
  1029. var dimFactor = new Dim.DimFactor (0.10F);
  1030. Assert.Equal (10, dimFactor.Anchor (100));
  1031. var dimAbsolute = new Dim.DimAbsolute (10);
  1032. Assert.Equal (10, dimAbsolute.Anchor (0));
  1033. var dimFill = new Dim.DimFill (1);
  1034. Assert.Equal (99, dimFill.Anchor (100));
  1035. var dimCombine = new Dim.DimCombine (true, dimFactor, dimAbsolute);
  1036. Assert.Equal (dimCombine.left, dimFactor);
  1037. Assert.Equal (dimCombine.right, dimAbsolute);
  1038. Assert.Equal (20, dimCombine.Anchor (100));
  1039. var view = new View (new Rect (20, 10, 20, 1));
  1040. var dimViewHeight = new Dim.DimView (view, 0);
  1041. Assert.Equal (1, dimViewHeight.Anchor (0));
  1042. var dimViewWidth = new Dim.DimView (view, 1);
  1043. Assert.Equal (20, dimViewWidth.Anchor (0));
  1044. }
  1045. [Fact]
  1046. public void Function_SetsValue ()
  1047. {
  1048. var text = "Test";
  1049. var dim = Dim.Function (() => text.Length);
  1050. Assert.Equal ("DimFunc(4)", dim.ToString ());
  1051. text = "New Test";
  1052. Assert.Equal ("DimFunc(8)", dim.ToString ());
  1053. text = "";
  1054. Assert.Equal ("DimFunc(0)", dim.ToString ());
  1055. }
  1056. [Fact]
  1057. public void Function_Equal ()
  1058. {
  1059. var f1 = () => 0;
  1060. var f2 = () => 0;
  1061. var dim1 = Dim.Function (f1);
  1062. var dim2 = Dim.Function (f2);
  1063. Assert.Equal (dim1, dim2);
  1064. f2 = () => 1;
  1065. dim2 = Dim.Function (f2);
  1066. Assert.NotEqual (dim1, dim2);
  1067. }
  1068. [Theory, AutoInitShutdown]
  1069. [InlineData (0, true)]
  1070. [InlineData (0, false)]
  1071. [InlineData (50, true)]
  1072. [InlineData (50, false)]
  1073. public void DimPercentPlusOne (int startingDistance, bool testHorizontal)
  1074. {
  1075. var container = new View {
  1076. Width = 100,
  1077. Height = 100,
  1078. };
  1079. var label = new Label {
  1080. X = testHorizontal ? startingDistance : 0,
  1081. Y = testHorizontal ? 0 : startingDistance,
  1082. Width = testHorizontal ? Dim.Percent (50) + 1 : 1,
  1083. Height = testHorizontal ? 1 : Dim.Percent (50) + 1,
  1084. };
  1085. container.Add (label);
  1086. Application.Top.Add (container);
  1087. Application.Top.LayoutSubviews ();
  1088. Assert.Equal (100, container.Frame.Width);
  1089. Assert.Equal (100, container.Frame.Height);
  1090. if (testHorizontal) {
  1091. Assert.Equal (51, label.Frame.Width);
  1092. Assert.Equal (1, label.Frame.Height);
  1093. } else {
  1094. Assert.Equal (1, label.Frame.Width);
  1095. Assert.Equal (51, label.Frame.Height);
  1096. }
  1097. }
  1098. }
  1099. }