DimTests.cs 36 KB

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