DimTests.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705
  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. // Alais Console to MockConsole so we don't accidentally use Console
  11. using Console = Terminal.Gui.FakeConsole;
  12. namespace Terminal.Gui.Core {
  13. public class DimTests {
  14. public DimTests ()
  15. {
  16. Console.OutputEncoding = System.Text.Encoding.Default;
  17. // Change current culture
  18. CultureInfo culture = CultureInfo.CreateSpecificCulture ("en-US");
  19. Thread.CurrentThread.CurrentCulture = culture;
  20. Thread.CurrentThread.CurrentUICulture = culture;
  21. }
  22. [Fact]
  23. public void New_Works ()
  24. {
  25. var dim = new Dim ();
  26. Assert.Equal ("Terminal.Gui.Dim", dim.ToString ());
  27. }
  28. [Fact]
  29. public void Sized_SetsValue ()
  30. {
  31. var dim = Dim.Sized (0);
  32. Assert.Equal ("Dim.Absolute(0)", dim.ToString ());
  33. int testVal = 5;
  34. dim = Dim.Sized (testVal);
  35. Assert.Equal ($"Dim.Absolute({testVal})", dim.ToString ());
  36. testVal = -1;
  37. dim = Dim.Sized (testVal);
  38. Assert.Equal ($"Dim.Absolute({testVal})", dim.ToString ());
  39. }
  40. [Fact]
  41. public void Sized_Equals ()
  42. {
  43. int n1 = 0;
  44. int n2 = 0;
  45. var dim1 = Dim.Sized (n1);
  46. var dim2 = Dim.Sized (n2);
  47. Assert.Equal (dim1, dim2);
  48. n1 = n2 = 1;
  49. dim1 = Dim.Sized (n1);
  50. 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 = 0;
  57. n2 = 1;
  58. dim1 = Dim.Sized (n1);
  59. dim2 = Dim.Sized (n2);
  60. Assert.NotEqual (dim1, dim2);
  61. }
  62. [Fact]
  63. public void Width_SetsValue ()
  64. {
  65. var dim = Dim.Width (null);
  66. Assert.Throws<NullReferenceException> (() => dim.ToString ());
  67. var testVal = Rect.Empty;
  68. testVal = Rect.Empty;
  69. dim = Dim.Width (new View (testVal));
  70. Assert.Equal ($"DimView(side=Width, target=View()({{X={testVal.X},Y={testVal.Y},Width={testVal.Width},Height={testVal.Height}}}))", dim.ToString ());
  71. testVal = new Rect (1, 2, 3, 4);
  72. dim = Dim.Width (new View (testVal));
  73. Assert.Equal ($"DimView(side=Width, target=View()({{X={testVal.X},Y={testVal.Y},Width={testVal.Width},Height={testVal.Height}}}))", dim.ToString ());
  74. }
  75. [Fact]
  76. public void Width_Equals ()
  77. {
  78. var testRect1 = Rect.Empty;
  79. var view1 = new View (testRect1);
  80. var testRect2 = Rect.Empty;
  81. var view2 = new View (testRect2);
  82. var dim1 = Dim.Width (view1);
  83. var dim2 = Dim.Width (view1);
  84. // FIXED: Dim.Width should support Equals() and this should change to Equal.
  85. Assert.Equal (dim1, dim2);
  86. dim2 = Dim.Width (view2);
  87. Assert.NotEqual (dim1, dim2);
  88. testRect1 = new Rect (0, 1, 2, 3);
  89. view1 = new View (testRect1);
  90. testRect2 = new Rect (0, 1, 2, 3);
  91. dim1 = Dim.Width (view1);
  92. dim2 = Dim.Width (view1);
  93. // FIXED: Dim.Width should support Equals() and this should change to Equal.
  94. Assert.Equal (dim1, dim2);
  95. Assert.Throws<ArgumentException> (() => new Rect (0, -1, -2, -3));
  96. testRect1 = new Rect (0, -1, 2, 3);
  97. view1 = new View (testRect1);
  98. testRect2 = new Rect (0, -1, 2, 3);
  99. dim1 = Dim.Width (view1);
  100. dim2 = Dim.Width (view1);
  101. // FIXED: Dim.Width should support Equals() and this should change to Equal.
  102. Assert.Equal (dim1, dim2);
  103. testRect1 = new Rect (0, -1, 2, 3);
  104. view1 = new View (testRect1);
  105. testRect2 = Rect.Empty;
  106. view2 = new View (testRect2);
  107. dim1 = Dim.Width (view1);
  108. dim2 = Dim.Width (view2);
  109. Assert.NotEqual (dim1, dim2);
  110. }
  111. [Fact]
  112. public void Height_SetsValue ()
  113. {
  114. var dim = Dim.Height (null);
  115. Assert.Throws<NullReferenceException> (() => dim.ToString ());
  116. var testVal = Rect.Empty;
  117. testVal = Rect.Empty;
  118. dim = Dim.Height (new View (testVal));
  119. Assert.Equal ($"DimView(side=Height, target=View()({{X={testVal.X},Y={testVal.Y},Width={testVal.Width},Height={testVal.Height}}}))", dim.ToString ());
  120. testVal = new Rect (1, 2, 3, 4);
  121. dim = Dim.Height (new View (testVal));
  122. Assert.Equal ($"DimView(side=Height, target=View()({{X={testVal.X},Y={testVal.Y},Width={testVal.Width},Height={testVal.Height}}}))", dim.ToString ());
  123. }
  124. // TODO: Other Dim.Height tests (e.g. Equal?)
  125. [Fact]
  126. public void Fill_SetsValue ()
  127. {
  128. var testMargin = 0;
  129. var dim = Dim.Fill ();
  130. Assert.Equal ($"Dim.Fill(margin={testMargin})", dim.ToString ());
  131. testMargin = 0;
  132. dim = Dim.Fill (testMargin);
  133. Assert.Equal ($"Dim.Fill(margin={testMargin})", dim.ToString ());
  134. testMargin = 5;
  135. dim = Dim.Fill (testMargin);
  136. Assert.Equal ($"Dim.Fill(margin={testMargin})", dim.ToString ());
  137. }
  138. [Fact]
  139. public void Fill_Equal ()
  140. {
  141. var margin1 = 0;
  142. var margin2 = 0;
  143. var dim1 = Dim.Fill (margin1);
  144. var dim2 = Dim.Fill (margin2);
  145. Assert.Equal (dim1, dim2);
  146. }
  147. [Fact]
  148. public void Percent_SetsValue ()
  149. {
  150. float f = 0;
  151. var dim = Dim.Percent (f);
  152. Assert.Equal ($"Dim.Factor(factor={f / 100:0.###}, remaining={false})", dim.ToString ());
  153. f = 0.5F;
  154. dim = Dim.Percent (f);
  155. Assert.Equal ($"Dim.Factor(factor={f / 100:0.###}, remaining={false})", dim.ToString ());
  156. f = 100;
  157. dim = Dim.Percent (f);
  158. Assert.Equal ($"Dim.Factor(factor={f / 100:0.###}, remaining={false})", dim.ToString ());
  159. }
  160. [Fact]
  161. public void Percent_Equals ()
  162. {
  163. float n1 = 0;
  164. float n2 = 0;
  165. var dim1 = Dim.Percent (n1);
  166. var dim2 = Dim.Percent (n2);
  167. Assert.Equal (dim1, dim2);
  168. n1 = n2 = 1;
  169. dim1 = Dim.Percent (n1);
  170. dim2 = Dim.Percent (n2);
  171. Assert.Equal (dim1, dim2);
  172. n1 = n2 = 0.5f;
  173. dim1 = Dim.Percent (n1);
  174. dim2 = Dim.Percent (n2);
  175. Assert.Equal (dim1, dim2);
  176. n1 = n2 = 100f;
  177. dim1 = Dim.Percent (n1);
  178. dim2 = Dim.Percent (n2);
  179. Assert.Equal (dim1, dim2);
  180. n1 = n2 = 0.3f;
  181. dim1 = Dim.Percent (n1, true);
  182. dim2 = Dim.Percent (n2, true);
  183. Assert.Equal (dim1, dim2);
  184. n1 = n2 = 0.3f;
  185. dim1 = Dim.Percent (n1);
  186. dim2 = Dim.Percent (n2, true);
  187. Assert.NotEqual (dim1, dim2);
  188. n1 = 0;
  189. n2 = 1;
  190. dim1 = Dim.Percent (n1);
  191. dim2 = Dim.Percent (n2);
  192. Assert.NotEqual (dim1, dim2);
  193. n1 = 0.5f;
  194. n2 = 1.5f;
  195. dim1 = Dim.Percent (n1);
  196. dim2 = Dim.Percent (n2);
  197. Assert.NotEqual (dim1, dim2);
  198. }
  199. [Fact]
  200. public void Percent_ThrowsOnIvalid ()
  201. {
  202. var dim = Dim.Percent (0);
  203. Assert.Throws<ArgumentException> (() => dim = Dim.Percent (-1));
  204. Assert.Throws<ArgumentException> (() => dim = Dim.Percent (101));
  205. Assert.Throws<ArgumentException> (() => dim = Dim.Percent (100.0001F));
  206. Assert.Throws<ArgumentException> (() => dim = Dim.Percent (1000001));
  207. }
  208. [Fact]
  209. public void Dim_Validation_Throws_If_NewValue_Is_DimAbsolute_And_OldValue_Is_Another_Type ()
  210. {
  211. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  212. var t = Application.Top;
  213. var w = new Window ("w") {
  214. Width = Dim.Fill (0),
  215. Height = Dim.Sized (10)
  216. };
  217. var v = new View ("v") {
  218. Width = Dim.Width (w) - 2,
  219. Height = Dim.Percent (10)
  220. };
  221. w.Add (v);
  222. t.Add (w);
  223. t.Ready += () => {
  224. Assert.Equal (2, w.Width = 2);
  225. Assert.Equal (2, w.Height = 2);
  226. Assert.Throws<ArgumentException> (() => v.Width = 2);
  227. Assert.Throws<ArgumentException> (() => v.Height = 2);
  228. };
  229. Application.Iteration += () => Application.RequestStop ();
  230. Application.Run ();
  231. Application.Shutdown ();
  232. }
  233. [Fact]
  234. public void Dim_Validation_Do_Not_Throws_If_NewValue_Is_DimAbsolute_And_OldValue_Is_Null ()
  235. {
  236. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  237. var t = Application.Top;
  238. var w = new Window (new Rect (1, 2, 4, 5), "w");
  239. t.Add (w);
  240. t.Ready += () => {
  241. Assert.Equal (3, w.Width = 3);
  242. Assert.Equal (4, w.Height = 4);
  243. };
  244. Application.Iteration += () => Application.RequestStop ();
  245. Application.Run ();
  246. Application.Shutdown ();
  247. }
  248. [Fact]
  249. public void Dim_Validation_Do_Not_Throws_If_NewValue_Is_DimAbsolute_And_OldValue_Is_Another_Type_After_Sets_To_LayoutStyle_Absolute ()
  250. {
  251. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  252. var t = Application.Top;
  253. var w = new Window ("w") {
  254. Width = Dim.Fill (0),
  255. Height = Dim.Sized (10)
  256. };
  257. var v = new View ("v") {
  258. Width = Dim.Width (w) - 2,
  259. Height = Dim.Percent (10)
  260. };
  261. w.Add (v);
  262. t.Add (w);
  263. t.Ready += () => {
  264. v.LayoutStyle = LayoutStyle.Absolute;
  265. Assert.Equal (2, v.Width = 2);
  266. Assert.Equal (2, v.Height = 2);
  267. };
  268. Application.Iteration += () => Application.RequestStop ();
  269. Application.Run ();
  270. Application.Shutdown ();
  271. }
  272. [Fact]
  273. public void Only_DimAbsolute_And_DimFactor_As_A_Different_Procedure_For_Assigning_Value_To_Width_Or_Height ()
  274. {
  275. // Testing with the Button because it properly handles the Dim class.
  276. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  277. var t = Application.Top;
  278. var w = new Window ("w") {
  279. Width = 100,
  280. Height = 100
  281. };
  282. var f1 = new FrameView ("f1") {
  283. X = 0,
  284. Y = 0,
  285. Width = Dim.Percent (50),
  286. Height = 5
  287. };
  288. var f2 = new FrameView ("f2") {
  289. X = Pos.Right (f1),
  290. Y = 0,
  291. Width = Dim.Fill (),
  292. Height = 5
  293. };
  294. var v1 = new Button ("v1") {
  295. X = Pos.X (f1) + 2,
  296. Y = Pos.Bottom (f1) + 2,
  297. Width = Dim.Width (f1) - 2,
  298. Height = Dim.Fill () - 2
  299. };
  300. var v2 = new Button ("v2") {
  301. X = Pos.X (f2) + 2,
  302. Y = Pos.Bottom (f2) + 2,
  303. Width = Dim.Width (f2) - 2,
  304. Height = Dim.Fill () - 2
  305. };
  306. var v3 = new Button ("v3") {
  307. Width = Dim.Percent (10),
  308. Height = Dim.Percent (10)
  309. };
  310. var v4 = new Button ("v4") {
  311. Width = Dim.Sized (50),
  312. Height = Dim.Sized (50)
  313. };
  314. var v5 = new Button ("v5") {
  315. Width = Dim.Width (v1) - Dim.Width (v3),
  316. Height = Dim.Height (v1) - Dim.Height (v3)
  317. };
  318. var v6 = new Button ("v6") {
  319. X = Pos.X (f2),
  320. Y = Pos.Bottom (f2) + 2,
  321. Width = Dim.Percent (20, true),
  322. Height = Dim.Percent (20, true)
  323. };
  324. w.Add (f1, f2, v1, v2, v3, v4, v5, v6);
  325. t.Add (w);
  326. t.Ready += () => {
  327. Assert.Equal ("Dim.Absolute(100)", w.Width.ToString ());
  328. Assert.Equal ("Dim.Absolute(100)", w.Height.ToString ());
  329. Assert.Equal (100, w.Frame.Width);
  330. Assert.Equal (100, w.Frame.Height);
  331. Assert.Equal ("Dim.Factor(factor=0.5, remaining=False)", f1.Width.ToString ());
  332. Assert.Equal ("Dim.Absolute(5)", f1.Height.ToString ());
  333. Assert.Equal (49, f1.Frame.Width); // 50-1=49
  334. Assert.Equal (5, f1.Frame.Height);
  335. Assert.Equal ("Dim.Fill(margin=0)", f2.Width.ToString ());
  336. Assert.Equal ("Dim.Absolute(5)", f2.Height.ToString ());
  337. Assert.Equal (49, f2.Frame.Width); // 50-1=49
  338. Assert.Equal (5, f2.Frame.Height);
  339. Assert.Equal ("Dim.Combine(DimView(side=Width, target=FrameView()({X=0,Y=0,Width=49,Height=5}))-Dim.Absolute(2))", v1.Width.ToString ());
  340. Assert.Equal ("Dim.Combine(Dim.Fill(margin=0)-Dim.Absolute(2))", v1.Height.ToString ());
  341. Assert.Equal (47, v1.Frame.Width); // 49-2=47
  342. Assert.Equal (89, v1.Frame.Height); // 98-5-2-2=89
  343. Assert.Equal ("Dim.Combine(DimView(side=Width, target=FrameView()({X=49,Y=0,Width=49,Height=5}))-Dim.Absolute(2))", v2.Width.ToString ());
  344. Assert.Equal ("Dim.Combine(Dim.Fill(margin=0)-Dim.Absolute(2))", v2.Height.ToString ());
  345. Assert.Equal (47, v2.Frame.Width); // 49-2=47
  346. Assert.Equal (89, v2.Frame.Height); // 98-5-2-2=89
  347. Assert.Equal ("Dim.Factor(factor=0.1, remaining=False)", v3.Width.ToString ());
  348. Assert.Equal ("Dim.Factor(factor=0.1, remaining=False)", v3.Height.ToString ());
  349. Assert.Equal (9, v3.Frame.Width); // 98*10%=9
  350. Assert.Equal (9, v3.Frame.Height); // 98*10%=9
  351. Assert.Equal ("Dim.Absolute(50)", v4.Width.ToString ());
  352. Assert.Equal ("Dim.Absolute(50)", v4.Height.ToString ());
  353. Assert.Equal (50, v4.Frame.Width);
  354. Assert.Equal (50, v4.Frame.Height);
  355. 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 ());
  356. 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 ());
  357. Assert.Equal (38, v5.Frame.Width); // 47-9=38
  358. Assert.Equal (80, v5.Frame.Height); // 89-9=80
  359. Assert.Equal ("Dim.Factor(factor=0.2, remaining=True)", v6.Width.ToString ());
  360. Assert.Equal ("Dim.Factor(factor=0.2, remaining=True)", v6.Height.ToString ());
  361. Assert.Equal (9, v6.Frame.Width); // 47*20%=9
  362. Assert.Equal (18, v6.Frame.Height); // 89*20%=18
  363. w.Width = 200;
  364. w.Height = 200;
  365. t.LayoutSubviews ();
  366. Assert.Equal ("Dim.Absolute(200)", w.Width.ToString ());
  367. Assert.Equal ("Dim.Absolute(200)", w.Height.ToString ());
  368. Assert.Equal (200, w.Frame.Width);
  369. Assert.Equal (200, w.Frame.Height);
  370. f1.Text = "Frame1";
  371. Assert.Equal ("Dim.Factor(factor=0.5, remaining=False)", f1.Width.ToString ());
  372. Assert.Equal ("Dim.Absolute(5)", f1.Height.ToString ());
  373. Assert.Equal (99, f1.Frame.Width); // 100-1=99
  374. Assert.Equal (5, f1.Frame.Height);
  375. f2.Text = "Frame2";
  376. Assert.Equal ("Dim.Fill(margin=0)", f2.Width.ToString ());
  377. Assert.Equal ("Dim.Absolute(5)", f2.Height.ToString ());
  378. Assert.Equal (99, f2.Frame.Width); // 100-1=99
  379. Assert.Equal (5, f2.Frame.Height);
  380. v1.Text = "Button1";
  381. Assert.Equal ("Dim.Combine(DimView(side=Width, target=FrameView()({X=0,Y=0,Width=99,Height=5}))-Dim.Absolute(2))", v1.Width.ToString ());
  382. Assert.Equal ("Dim.Absolute(1)", v1.Height.ToString ());
  383. Assert.Equal (97, v1.Frame.Width); // 99-2=97
  384. Assert.Equal (1, v1.Frame.Height); // 1 because is Dim.DimAbsolute
  385. v2.Text = "Button2";
  386. Assert.Equal ("Dim.Combine(DimView(side=Width, target=FrameView()({X=99,Y=0,Width=99,Height=5}))-Dim.Absolute(2))", v2.Width.ToString ());
  387. Assert.Equal ("Dim.Absolute(1)", v2.Height.ToString ());
  388. Assert.Equal (97, v2.Frame.Width); // 99-2=97
  389. Assert.Equal (1, v2.Frame.Height); // 1 because is Dim.DimAbsolute
  390. v3.Text = "Button3";
  391. Assert.Equal ("Dim.Factor(factor=0.1, remaining=False)", v3.Width.ToString ());
  392. Assert.Equal ("Dim.Absolute(1)", v3.Height.ToString ());
  393. 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
  394. Assert.Equal (1, v3.Frame.Height); // 1 because is Dim.DimAbsolute
  395. v4.Text = "Button4";
  396. v4.AutoSize = false;
  397. Assert.Equal ("Dim.Absolute(50)", v4.Width.ToString ());
  398. Assert.Equal ("Dim.Absolute(1)", v4.Height.ToString ());
  399. v4.AutoSize = true;
  400. Assert.Equal ("Dim.Absolute(11)", v4.Width.ToString ());
  401. Assert.Equal ("Dim.Absolute(1)", v4.Height.ToString ());
  402. Assert.Equal (11, v4.Frame.Width); // 11 is the text length and because is Dim.DimAbsolute
  403. Assert.Equal (1, v4.Frame.Height); // 1 because is Dim.DimAbsolute
  404. v5.Text = "Button5";
  405. 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 ());
  406. Assert.Equal ("Dim.Absolute(1)", v5.Height.ToString ());
  407. Assert.Equal (78, v5.Frame.Width); // 97-19=78
  408. Assert.Equal (1, v5.Frame.Height); // 1 because is Dim.DimAbsolute
  409. v6.Text = "Button6";
  410. Assert.Equal ("Dim.Factor(factor=0.2, remaining=True)", v6.Width.ToString ());
  411. Assert.Equal ("Dim.Absolute(1)", v6.Height.ToString ());
  412. Assert.Equal (19, v6.Frame.Width); // 99*20%=19
  413. Assert.Equal (1, v6.Frame.Height); // 1 because is Dim.DimAbsolute
  414. };
  415. Application.Iteration += () => Application.RequestStop ();
  416. Application.Run ();
  417. Application.Shutdown ();
  418. }
  419. // DONE: Test operators
  420. [Fact]
  421. public void DimCombine_Do_Not_Throws ()
  422. {
  423. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  424. var t = Application.Top;
  425. var w = new Window ("w") {
  426. Width = Dim.Width (t) - 2,
  427. Height = Dim.Height (t) - 2
  428. };
  429. var f = new FrameView ("f");
  430. var v1 = new View ("v1") {
  431. Width = Dim.Width (w) - 2,
  432. Height = Dim.Height (w) - 2
  433. };
  434. var v2 = new View ("v2") {
  435. Width = Dim.Width (v1) - 2,
  436. Height = Dim.Height (v1) - 2
  437. };
  438. f.Add (v1, v2);
  439. w.Add (f);
  440. t.Add (w);
  441. f.Width = Dim.Width (t) - Dim.Width (v2);
  442. f.Height = Dim.Height (t) - Dim.Height (v2);
  443. t.Ready += () => {
  444. Assert.Equal (80, t.Frame.Width);
  445. Assert.Equal (25, t.Frame.Height);
  446. Assert.Equal (78, w.Frame.Width);
  447. Assert.Equal (23, w.Frame.Height);
  448. Assert.Equal (6, f.Frame.Width);
  449. Assert.Equal (6, f.Frame.Height);
  450. Assert.Equal (76, v1.Frame.Width);
  451. Assert.Equal (21, v1.Frame.Height);
  452. Assert.Equal (74, v2.Frame.Width);
  453. Assert.Equal (19, v2.Frame.Height);
  454. };
  455. Application.Iteration += () => Application.RequestStop ();
  456. Application.Run ();
  457. Application.Shutdown ();
  458. }
  459. [Fact]
  460. public void PosCombine_Will_Throws ()
  461. {
  462. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  463. var t = Application.Top;
  464. var w = new Window ("w") {
  465. Width = Dim.Width (t) - 2,
  466. Height = Dim.Height (t) - 2
  467. };
  468. var f = new FrameView ("f");
  469. var v1 = new View ("v1") {
  470. Width = Dim.Width (w) - 2,
  471. Height = Dim.Height (w) - 2
  472. };
  473. var v2 = new View ("v2") {
  474. Width = Dim.Width (v1) - 2,
  475. Height = Dim.Height (v1) - 2
  476. };
  477. f.Add (v1); // v2 not added
  478. w.Add (f);
  479. t.Add (w);
  480. f.Width = Dim.Width (t) - Dim.Width (v2);
  481. f.Height = Dim.Height (t) - Dim.Height (v2);
  482. Assert.Throws<InvalidOperationException> (() => Application.Run ());
  483. Application.Shutdown ();
  484. }
  485. [Fact]
  486. public void Dim_Add_Operator ()
  487. {
  488. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  489. var top = Application.Top;
  490. var view = new View () { X = 0, Y = 0, Width = 20, Height = 0 };
  491. var field = new TextField () { X = 0, Y = Pos.Bottom (view), Width = 20 };
  492. var count = 0;
  493. field.KeyDown += (k) => {
  494. if (k.KeyEvent.Key == Key.Enter) {
  495. field.Text = $"Label {count}";
  496. var label = new Label (field.Text) { X = 0, Y = view.Bounds.Height, Width = 20 };
  497. view.Add (label);
  498. Assert.Equal ($"Label {count}", label.Text);
  499. Assert.Equal ($"Pos.Absolute({count})", label.Y.ToString ());
  500. Assert.Equal ($"Dim.Absolute({count})", view.Height.ToString ());
  501. view.Height += 1;
  502. count++;
  503. Assert.Equal ($"Dim.Absolute({count})", view.Height.ToString ());
  504. }
  505. };
  506. Application.Iteration += () => {
  507. while (count < 20) {
  508. field.OnKeyDown (new KeyEvent (Key.Enter, new KeyModifiers ()));
  509. }
  510. Application.RequestStop ();
  511. };
  512. var win = new Window ();
  513. win.Add (view);
  514. win.Add (field);
  515. top.Add (win);
  516. Application.Run (top);
  517. Assert.Equal (20, count);
  518. // Shutdown must be called to safely clean up Application if Init has been called
  519. Application.Shutdown ();
  520. }
  521. [Fact]
  522. public void Dim_Subtract_Operator ()
  523. {
  524. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  525. var top = Application.Top;
  526. var view = new View () { X = 0, Y = 0, Width = 20, Height = 0 };
  527. var field = new TextField () { X = 0, Y = Pos.Bottom (view), Width = 20 };
  528. var count = 20;
  529. var listLabels = new List<Label> ();
  530. for (int i = 0; i < count; i++) {
  531. field.Text = $"Label {i}";
  532. var label = new Label (field.Text) { X = 0, Y = view.Bounds.Height, Width = 20 };
  533. view.Add (label);
  534. Assert.Equal ($"Label {i}", label.Text);
  535. Assert.Equal ($"Pos.Absolute({i})", label.Y.ToString ());
  536. listLabels.Add (label);
  537. Assert.Equal ($"Dim.Absolute({i})", view.Height.ToString ());
  538. view.Height += 1;
  539. Assert.Equal ($"Dim.Absolute({i + 1})", view.Height.ToString ());
  540. }
  541. field.KeyDown += (k) => {
  542. if (k.KeyEvent.Key == Key.Enter) {
  543. Assert.Equal ($"Label {count - 1}", listLabels [count - 1].Text);
  544. view.Remove (listLabels [count - 1]);
  545. Assert.Equal ($"Dim.Absolute({count})", view.Height.ToString ());
  546. view.Height -= 1;
  547. count--;
  548. Assert.Equal ($"Dim.Absolute({count})", view.Height.ToString ());
  549. }
  550. };
  551. Application.Iteration += () => {
  552. while (count > 0) {
  553. field.OnKeyDown (new KeyEvent (Key.Enter, new KeyModifiers ()));
  554. }
  555. Application.RequestStop ();
  556. };
  557. var win = new Window ();
  558. win.Add (view);
  559. win.Add (field);
  560. top.Add (win);
  561. Application.Run (top);
  562. Assert.Equal (0, count);
  563. // Shutdown must be called to safely clean up Application if Init has been called
  564. Application.Shutdown ();
  565. }
  566. }
  567. }