DimTests.cs 35 KB

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