DimTests.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.IO;
  5. using System.Linq;
  6. using Terminal.Gui;
  7. using Xunit;
  8. // Alais Console to MockConsole so we don't accidentally use Console
  9. using Console = Terminal.Gui.FakeConsole;
  10. namespace Terminal.Gui {
  11. public class DimTests {
  12. [Fact]
  13. public void New_Works ()
  14. {
  15. var dim = new Dim ();
  16. Assert.Equal ("Terminal.Gui.Dim", dim.ToString ());
  17. }
  18. [Fact]
  19. public void Sized_SetsValue ()
  20. {
  21. var dim = Dim.Sized (0);
  22. Assert.Equal ("Dim.Absolute(0)", dim.ToString ());
  23. int testVal = 5;
  24. dim = Dim.Sized (testVal);
  25. Assert.Equal ($"Dim.Absolute({testVal})", dim.ToString ());
  26. testVal = -1;
  27. dim = Dim.Sized (testVal);
  28. Assert.Equal ($"Dim.Absolute({testVal})", dim.ToString ());
  29. }
  30. [Fact]
  31. public void Sized_Equals ()
  32. {
  33. int n1 = 0;
  34. int n2 = 0;
  35. var dim1 = Dim.Sized (n1);
  36. var dim2 = Dim.Sized (n2);
  37. Assert.Equal (dim1, dim2);
  38. n1 = n2 = 1;
  39. dim1 = Dim.Sized (n1);
  40. dim2 = Dim.Sized (n2);
  41. Assert.Equal (dim1, dim2);
  42. n1 = n2 = -1;
  43. dim1 = Dim.Sized (n1);
  44. dim2 = Dim.Sized (n2);
  45. Assert.Equal (dim1, dim2);
  46. n1 = 0;
  47. n2 = 1;
  48. dim1 = Dim.Sized (n1);
  49. dim2 = Dim.Sized (n2);
  50. Assert.NotEqual (dim1, dim2);
  51. }
  52. [Fact]
  53. public void Width_SetsValue ()
  54. {
  55. var dim = Dim.Width (null);
  56. Assert.Throws<NullReferenceException> (() => dim.ToString ());
  57. var testVal = Rect.Empty;
  58. testVal = Rect.Empty;
  59. dim = Dim.Width (new View (testVal));
  60. Assert.Equal ($"DimView(side=Width, target=View()({{X={testVal.X},Y={testVal.Y},Width={testVal.Width},Height={testVal.Height}}}))", dim.ToString ());
  61. testVal = new Rect (1, 2, 3, 4);
  62. dim = Dim.Width (new View (testVal));
  63. Assert.Equal ($"DimView(side=Width, target=View()({{X={testVal.X},Y={testVal.Y},Width={testVal.Width},Height={testVal.Height}}}))", dim.ToString ());
  64. }
  65. [Fact]
  66. public void Width_Equals ()
  67. {
  68. var testRect1 = Rect.Empty;
  69. var view1 = new View (testRect1);
  70. var testRect2 = Rect.Empty;
  71. var view2 = new View (testRect2);
  72. var dim1 = Dim.Width (view1);
  73. var dim2 = Dim.Width (view1);
  74. // FIXED: Dim.Width should support Equals() and this should change to Equal.
  75. Assert.Equal (dim1, dim2);
  76. dim2 = Dim.Width (view2);
  77. Assert.NotEqual (dim1, dim2);
  78. testRect1 = new Rect (0, 1, 2, 3);
  79. view1 = new View (testRect1);
  80. testRect2 = new Rect (0, 1, 2, 3);
  81. dim1 = Dim.Width (view1);
  82. dim2 = Dim.Width (view1);
  83. // FIXED: Dim.Width should support Equals() and this should change to Equal.
  84. Assert.Equal (dim1, dim2);
  85. Assert.Throws<ArgumentException> (() => new Rect (0, -1, -2, -3));
  86. testRect1 = new Rect (0, -1, 2, 3);
  87. view1 = new View (testRect1);
  88. testRect2 = new Rect (0, -1, 2, 3);
  89. dim1 = Dim.Width (view1);
  90. dim2 = Dim.Width (view1);
  91. // FIXED: Dim.Width should support Equals() and this should change to Equal.
  92. Assert.Equal (dim1, dim2);
  93. testRect1 = new Rect (0, -1, 2, 3);
  94. view1 = new View (testRect1);
  95. testRect2 = Rect.Empty;
  96. view2 = new View (testRect2);
  97. dim1 = Dim.Width (view1);
  98. dim2 = Dim.Width (view2);
  99. Assert.NotEqual (dim1, dim2);
  100. }
  101. [Fact]
  102. public void Height_SetsValue ()
  103. {
  104. var dim = Dim.Height (null);
  105. Assert.Throws<NullReferenceException> (() => dim.ToString ());
  106. var testVal = Rect.Empty;
  107. testVal = Rect.Empty;
  108. dim = Dim.Height (new View (testVal));
  109. Assert.Equal ($"DimView(side=Height, target=View()({{X={testVal.X},Y={testVal.Y},Width={testVal.Width},Height={testVal.Height}}}))", dim.ToString ());
  110. testVal = new Rect (1, 2, 3, 4);
  111. dim = Dim.Height (new View (testVal));
  112. Assert.Equal ($"DimView(side=Height, target=View()({{X={testVal.X},Y={testVal.Y},Width={testVal.Width},Height={testVal.Height}}}))", dim.ToString ());
  113. }
  114. // TODO: Other Dim.Height tests (e.g. Equal?)
  115. [Fact]
  116. public void Fill_SetsValue ()
  117. {
  118. var testMargin = 0;
  119. var dim = Dim.Fill ();
  120. Assert.Equal ($"Dim.Fill(margin={testMargin})", dim.ToString ());
  121. testMargin = 0;
  122. dim = Dim.Fill (testMargin);
  123. Assert.Equal ($"Dim.Fill(margin={testMargin})", dim.ToString ());
  124. testMargin = 5;
  125. dim = Dim.Fill (testMargin);
  126. Assert.Equal ($"Dim.Fill(margin={testMargin})", dim.ToString ());
  127. }
  128. [Fact]
  129. public void Fill_Equal ()
  130. {
  131. var margin1 = 0;
  132. var margin2 = 0;
  133. var dim1 = Dim.Fill (margin1);
  134. var dim2 = Dim.Fill (margin2);
  135. Assert.Equal (dim1, dim2);
  136. }
  137. [Fact]
  138. public void Percent_SetsValue ()
  139. {
  140. float f = 0;
  141. var dim = Dim.Percent (f);
  142. Assert.Equal ($"Dim.Factor(factor={f / 100:0.###}, remaining={false})", dim.ToString ());
  143. f = 0.5F;
  144. dim = Dim.Percent (f);
  145. Assert.Equal ($"Dim.Factor(factor={f / 100:0.###}, remaining={false})", dim.ToString ());
  146. f = 100;
  147. dim = Dim.Percent (f);
  148. Assert.Equal ($"Dim.Factor(factor={f / 100:0.###}, remaining={false})", dim.ToString ());
  149. }
  150. [Fact]
  151. public void Percent_Equals ()
  152. {
  153. float n1 = 0;
  154. float n2 = 0;
  155. var dim1 = Dim.Percent (n1);
  156. var dim2 = Dim.Percent (n2);
  157. Assert.Equal (dim1, dim2);
  158. n1 = n2 = 1;
  159. dim1 = Dim.Percent (n1);
  160. dim2 = Dim.Percent (n2);
  161. Assert.Equal (dim1, dim2);
  162. n1 = n2 = 0.5f;
  163. dim1 = Dim.Percent (n1);
  164. dim2 = Dim.Percent (n2);
  165. Assert.Equal (dim1, dim2);
  166. n1 = n2 = 100f;
  167. dim1 = Dim.Percent (n1);
  168. dim2 = Dim.Percent (n2);
  169. Assert.Equal (dim1, dim2);
  170. n1 = n2 = 0.3f;
  171. dim1 = Dim.Percent (n1, true);
  172. dim2 = Dim.Percent (n2, true);
  173. Assert.Equal (dim1, dim2);
  174. n1 = n2 = 0.3f;
  175. dim1 = Dim.Percent (n1);
  176. dim2 = Dim.Percent (n2, true);
  177. Assert.NotEqual (dim1, dim2);
  178. n1 = 0;
  179. n2 = 1;
  180. dim1 = Dim.Percent (n1);
  181. dim2 = Dim.Percent (n2);
  182. Assert.NotEqual (dim1, dim2);
  183. n1 = 0.5f;
  184. n2 = 1.5f;
  185. dim1 = Dim.Percent (n1);
  186. dim2 = Dim.Percent (n2);
  187. Assert.NotEqual (dim1, dim2);
  188. }
  189. [Fact]
  190. public void Percent_ThrowsOnIvalid ()
  191. {
  192. var dim = Dim.Percent (0);
  193. Assert.Throws<ArgumentException> (() => dim = Dim.Percent (-1));
  194. Assert.Throws<ArgumentException> (() => dim = Dim.Percent (101));
  195. Assert.Throws<ArgumentException> (() => dim = Dim.Percent (100.0001F));
  196. Assert.Throws<ArgumentException> (() => dim = Dim.Percent (1000001));
  197. }
  198. [Fact]
  199. public void Dim_Validation_Throws_If_NewValue_Is_DimAbsolute_And_OldValue_Is_Another_Type ()
  200. {
  201. Application.Init (new FakeDriver (), new NetMainLoop (() => FakeConsole.ReadKey (true)));
  202. var t = Application.Top;
  203. var w = new Window ("w") {
  204. Width = Dim.Fill (0),
  205. Height = Dim.Sized (10)
  206. };
  207. var v = new View ("v") {
  208. Width = Dim.Width (w) - 2,
  209. Height = Dim.Percent (10)
  210. };
  211. w.Add (v);
  212. t.Add (w);
  213. t.Ready += () => {
  214. Assert.Equal (2, w.Width = 2);
  215. Assert.Equal (2, w.Height = 2);
  216. Assert.Throws<ArgumentException> (() => v.Width = 2);
  217. Assert.Throws<ArgumentException> (() => v.Height = 2);
  218. };
  219. Application.Iteration += () => Application.RequestStop ();
  220. Application.Run ();
  221. Application.Shutdown ();
  222. }
  223. [Fact]
  224. public void Dim_Validation_Do_Not_Throws_If_NewValue_Is_DimAbsolute_And_OldValue_Is_Null ()
  225. {
  226. Application.Init (new FakeDriver (), new NetMainLoop (() => FakeConsole.ReadKey (true)));
  227. var t = Application.Top;
  228. var w = new Window (new Rect (1, 2, 4, 5), "w");
  229. t.Add (w);
  230. t.Ready += () => {
  231. Assert.Equal (3, w.Width = 3);
  232. Assert.Equal (4, w.Height = 4);
  233. };
  234. Application.Iteration += () => Application.RequestStop ();
  235. Application.Run ();
  236. Application.Shutdown ();
  237. }
  238. [Fact]
  239. public void Dim_Validation_Do_Not_Throws_If_NewValue_Is_DimAbsolute_And_OldValue_Is_Another_Type_After_Sets_To_LayoutStyle_Absolute ()
  240. {
  241. Application.Init (new FakeDriver (), new NetMainLoop (() => FakeConsole.ReadKey (true)));
  242. var t = Application.Top;
  243. var w = new Window ("w") {
  244. Width = Dim.Fill (0),
  245. Height = Dim.Sized (10)
  246. };
  247. var v = new View ("v") {
  248. Width = Dim.Width (w) - 2,
  249. Height = Dim.Percent (10)
  250. };
  251. w.Add (v);
  252. t.Add (w);
  253. t.Ready += () => {
  254. v.LayoutStyle = LayoutStyle.Absolute;
  255. Assert.Equal (2, v.Width = 2);
  256. Assert.Equal (2, v.Height = 2);
  257. };
  258. Application.Iteration += () => Application.RequestStop ();
  259. Application.Run ();
  260. Application.Shutdown ();
  261. }
  262. [Fact]
  263. public void Only_DimAbsolute_And_DimFactor_As_A_Different_Procedure_For_Assigning_Value_To_Width_Or_Height ()
  264. {
  265. // Testing with the Button because it properly handles the Dim class.
  266. Application.Init (new FakeDriver (), new NetMainLoop (() => FakeConsole.ReadKey (true)));
  267. var t = Application.Top;
  268. var w = new Window ("w") {
  269. Width = 100,
  270. Height = 100
  271. };
  272. var f1 = new FrameView ("f1") {
  273. X = 0,
  274. Y = 0,
  275. Width = Dim.Percent (50),
  276. Height = 5
  277. };
  278. var f2 = new FrameView ("f2") {
  279. X = Pos.Right (f1),
  280. Y = 0,
  281. Width = Dim.Fill (),
  282. Height = 5
  283. };
  284. var v1 = new Button ("v1") {
  285. X = Pos.X (f1) + 2,
  286. Y = Pos.Bottom (f1) + 2,
  287. Width = Dim.Width (f1) - 2,
  288. Height = Dim.Fill () - 2
  289. };
  290. var v2 = new Button ("v2") {
  291. X = Pos.X (f2) + 2,
  292. Y = Pos.Bottom (f2) + 2,
  293. Width = Dim.Width (f2) - 2,
  294. Height = Dim.Fill () - 2
  295. };
  296. var v3 = new Button ("v3") {
  297. Width = Dim.Percent (10),
  298. Height = Dim.Percent (10)
  299. };
  300. var v4 = new Button ("v4") {
  301. Width = Dim.Sized (50),
  302. Height = Dim.Sized (50)
  303. };
  304. var v5 = new Button ("v5") {
  305. Width = Dim.Width (v1) - Dim.Width (v3),
  306. Height = Dim.Height (v1) - Dim.Height (v3)
  307. };
  308. var v6 = new Button ("v6") {
  309. X = Pos.X (f2),
  310. Y = Pos.Bottom (f2) + 2,
  311. Width = Dim.Percent (20, true),
  312. Height = Dim.Percent (20, true)
  313. };
  314. w.Add (f1, f2, v1, v2, v3, v4, v5, v6);
  315. t.Add (w);
  316. t.Ready += () => {
  317. Assert.Equal ("Dim.Absolute(100)", w.Width.ToString ());
  318. Assert.Equal ("Dim.Absolute(100)", w.Height.ToString ());
  319. Assert.Equal (100, w.Frame.Width);
  320. Assert.Equal (100, w.Frame.Height);
  321. Assert.Equal ("Dim.Factor(factor=0,5, remaining=False)", f1.Width.ToString ());
  322. Assert.Equal ("Dim.Absolute(5)", f1.Height.ToString ());
  323. Assert.Equal (49, f1.Frame.Width); // 50-1=49
  324. Assert.Equal (5, f1.Frame.Height);
  325. Assert.Equal ("Dim.Fill(margin=0)", f2.Width.ToString ());
  326. Assert.Equal ("Dim.Absolute(5)", f2.Height.ToString ());
  327. Assert.Equal (49, f2.Frame.Width); // 50-1=49
  328. Assert.Equal (5, f2.Frame.Height);
  329. Assert.Equal ("Dim.Combine(DimView(side=Width, target=FrameView()({X=0,Y=0,Width=49,Height=5}))-Dim.Absolute(2))", v1.Width.ToString ());
  330. Assert.Equal ("Dim.Combine(Dim.Fill(margin=0)-Dim.Absolute(2))", v1.Height.ToString ());
  331. Assert.Equal (47, v1.Frame.Width); // 49-2=47
  332. Assert.Equal (89, v1.Frame.Height); // 98-5-2-2=89
  333. Assert.Equal ("Dim.Combine(DimView(side=Width, target=FrameView()({X=49,Y=0,Width=49,Height=5}))-Dim.Absolute(2))", v2.Width.ToString ());
  334. Assert.Equal ("Dim.Combine(Dim.Fill(margin=0)-Dim.Absolute(2))", v2.Height.ToString ());
  335. Assert.Equal (47, v2.Frame.Width); // 49-2=47
  336. Assert.Equal (89, v2.Frame.Height); // 98-5-2-2=89
  337. Assert.Equal ("Dim.Factor(factor=0,1, remaining=False)", v3.Width.ToString ());
  338. Assert.Equal ("Dim.Factor(factor=0,1, remaining=False)", v3.Height.ToString ());
  339. Assert.Equal (9, v3.Frame.Width); // 98*10%=9
  340. Assert.Equal (9, v3.Frame.Height); // 98*10%=9
  341. Assert.Equal ("Dim.Absolute(50)", v4.Width.ToString ());
  342. Assert.Equal ("Dim.Absolute(50)", v4.Height.ToString ());
  343. Assert.Equal (50, v4.Frame.Width);
  344. Assert.Equal (50, v4.Frame.Height);
  345. 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 ());
  346. 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 ());
  347. Assert.Equal (38, v5.Frame.Width); // 47-9=38
  348. Assert.Equal (80, v5.Frame.Height); // 89-9=80
  349. Assert.Equal ("Dim.Factor(factor=0,2, remaining=True)", v6.Width.ToString ());
  350. Assert.Equal ("Dim.Factor(factor=0,2, remaining=True)", v6.Height.ToString ());
  351. Assert.Equal (9, v6.Frame.Width); // 47*20%=9
  352. Assert.Equal (18, v6.Frame.Height); // 89*20%=18
  353. w.Width = 200;
  354. w.Height = 200;
  355. t.LayoutSubviews ();
  356. Assert.Equal ("Dim.Absolute(200)", w.Width.ToString ());
  357. Assert.Equal ("Dim.Absolute(200)", w.Height.ToString ());
  358. Assert.Equal (200, w.Frame.Width);
  359. Assert.Equal (200, w.Frame.Height);
  360. f1.Text = "Frame1";
  361. Assert.Equal ("Dim.Factor(factor=0,5, remaining=False)", f1.Width.ToString ());
  362. Assert.Equal ("Dim.Absolute(5)", f1.Height.ToString ());
  363. Assert.Equal (99, f1.Frame.Width); // 100-1=99
  364. Assert.Equal (5, f1.Frame.Height);
  365. f2.Text = "Frame2";
  366. Assert.Equal ("Dim.Fill(margin=0)", f2.Width.ToString ());
  367. Assert.Equal ("Dim.Absolute(5)", f2.Height.ToString ());
  368. Assert.Equal (99, f2.Frame.Width); // 100-1=99
  369. Assert.Equal (5, f2.Frame.Height);
  370. v1.Text = "Button1";
  371. Assert.Equal ("Dim.Combine(DimView(side=Width, target=FrameView()({X=0,Y=0,Width=99,Height=5}))-Dim.Absolute(2))", v1.Width.ToString ());
  372. Assert.Equal ("Dim.Absolute(1)", v1.Height.ToString ());
  373. Assert.Equal (97, v1.Frame.Width); // 99-2=97
  374. Assert.Equal (1, v1.Frame.Height); // 1 because is Dim.DimAbsolute
  375. v2.Text = "Button2";
  376. Assert.Equal ("Dim.Combine(DimView(side=Width, target=FrameView()({X=99,Y=0,Width=99,Height=5}))-Dim.Absolute(2))", v2.Width.ToString ());
  377. Assert.Equal ("Dim.Absolute(1)", v2.Height.ToString ());
  378. Assert.Equal (97, v2.Frame.Width); // 99-2=97
  379. Assert.Equal (1, v2.Frame.Height); // 1 because is Dim.DimAbsolute
  380. v3.Text = "Button3";
  381. Assert.Equal ("Dim.Factor(factor=0,1, remaining=False)", v3.Width.ToString ());
  382. Assert.Equal ("Dim.Absolute(1)", v3.Height.ToString ());
  383. 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
  384. Assert.Equal (1, v3.Frame.Height); // 1 because is Dim.DimAbsolute
  385. v4.Text = "Button4";
  386. Assert.Equal ("Dim.Absolute(11)", v4.Width.ToString ());
  387. Assert.Equal ("Dim.Absolute(1)", v4.Height.ToString ());
  388. Assert.Equal (11, v4.Frame.Width); // 11 is the text length and because is Dim.DimAbsolute
  389. Assert.Equal (1, v4.Frame.Height); // 1 because is Dim.DimAbsolute
  390. v5.Text = "Button5";
  391. 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 ());
  392. Assert.Equal ("Dim.Absolute(1)", v5.Height.ToString ());
  393. Assert.Equal (78, v5.Frame.Width); // 97-19=78
  394. Assert.Equal (1, v5.Frame.Height); // 1 because is Dim.DimAbsolute
  395. v6.Text = "Button6";
  396. Assert.Equal ("Dim.Factor(factor=0,2, remaining=True)", v6.Width.ToString ());
  397. Assert.Equal ("Dim.Absolute(1)", v6.Height.ToString ());
  398. Assert.Equal (19, v6.Frame.Width); // 99*20%=19
  399. Assert.Equal (1, v6.Frame.Height); // 1 because is Dim.DimAbsolute
  400. };
  401. Application.Iteration += () => Application.RequestStop ();
  402. Application.Run ();
  403. Application.Shutdown ();
  404. }
  405. // TODO: Test operators
  406. }
  407. }