DimTests.cs 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355
  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.ViewTests {
  14. public class LayoutTests_DimTests {
  15. readonly ITestOutputHelper output;
  16. public LayoutTests_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_Set_To_Null_Throws ()
  67. {
  68. var dim = Dim.Width (null);
  69. Assert.Throws<NullReferenceException> (() => dim.ToString ());
  70. }
  71. [Fact]
  72. public void SetsValue ()
  73. {
  74. var testVal = Rect.Empty;
  75. var dim = Dim.Width (new View (testVal));
  76. Assert.Equal ($"View(Width,View()({{X={testVal.X},Y={testVal.Y},Width={testVal.Width},Height={testVal.Height}}}))", dim.ToString ());
  77. testVal = new Rect (1, 2, 3, 4);
  78. dim = Dim.Width (new View (testVal));
  79. Assert.Equal ($"View(Width,View()({{X={testVal.X},Y={testVal.Y},Width={testVal.Width},Height={testVal.Height}}}))", dim.ToString ());
  80. }
  81. [Fact]
  82. public void Width_Equals ()
  83. {
  84. var testRect1 = Rect.Empty;
  85. var view1 = new View (testRect1);
  86. var testRect2 = Rect.Empty;
  87. var view2 = new View (testRect2);
  88. var dim1 = Dim.Width (view1);
  89. var dim2 = Dim.Width (view1);
  90. // FIXED: Dim.Width should support Equals() and this should change to Equal.
  91. Assert.Equal (dim1, dim2);
  92. dim2 = Dim.Width (view2);
  93. Assert.NotEqual (dim1, dim2);
  94. testRect1 = new Rect (0, 1, 2, 3);
  95. view1 = new View (testRect1);
  96. testRect2 = new Rect (0, 1, 2, 3);
  97. dim1 = Dim.Width (view1);
  98. dim2 = Dim.Width (view1);
  99. // FIXED: Dim.Width should support Equals() and this should change to Equal.
  100. Assert.Equal (dim1, dim2);
  101. testRect1 = new Rect (0, -1, 2, 3);
  102. view1 = new View (testRect1);
  103. testRect2 = new Rect (0, -1, 2, 3);
  104. dim1 = Dim.Width (view1);
  105. dim2 = Dim.Width (view1);
  106. // FIXED: Dim.Width should support Equals() and this should change to Equal.
  107. Assert.Equal (dim1, dim2);
  108. testRect1 = new Rect (0, -1, 2, 3);
  109. view1 = new View (testRect1);
  110. testRect2 = Rect.Empty;
  111. view2 = new View (testRect2);
  112. dim1 = Dim.Width (view1);
  113. dim2 = Dim.Width (view2);
  114. Assert.NotEqual (dim1, dim2);
  115. }
  116. [Fact]
  117. public void Height_Set_To_Null_Throws ()
  118. {
  119. var dim = Dim.Height (null);
  120. Assert.Throws<NullReferenceException> (() => dim.ToString ());
  121. }
  122. [Fact]
  123. public void Height_SetsValue ()
  124. {
  125. var testVal = Rect.Empty;
  126. var dim = Dim.Height (new View (testVal));
  127. Assert.Equal ($"View(Height,View()({{X={testVal.X},Y={testVal.Y},Width={testVal.Width},Height={testVal.Height}}}))", dim.ToString ());
  128. testVal = new Rect (1, 2, 3, 4);
  129. dim = Dim.Height (new View (testVal));
  130. Assert.Equal ($"View(Height,View()({{X={testVal.X},Y={testVal.Y},Width={testVal.Width},Height={testVal.Height}}}))", dim.ToString ());
  131. }
  132. // TODO: Other Dim.Height tests (e.g. Equal?)
  133. [Fact]
  134. public void Fill_SetsValue ()
  135. {
  136. var testMargin = 0;
  137. var dim = Dim.Fill ();
  138. Assert.Equal ($"Fill({testMargin})", dim.ToString ());
  139. testMargin = 0;
  140. dim = Dim.Fill (testMargin);
  141. Assert.Equal ($"Fill({testMargin})", dim.ToString ());
  142. testMargin = 5;
  143. dim = Dim.Fill (testMargin);
  144. Assert.Equal ($"Fill({testMargin})", dim.ToString ());
  145. }
  146. [Fact]
  147. public void Fill_Equal ()
  148. {
  149. var margin1 = 0;
  150. var margin2 = 0;
  151. var dim1 = Dim.Fill (margin1);
  152. var dim2 = Dim.Fill (margin2);
  153. Assert.Equal (dim1, dim2);
  154. }
  155. [Fact]
  156. public void Percent_SetsValue ()
  157. {
  158. float f = 0;
  159. var dim = Dim.Percent (f);
  160. Assert.Equal ($"Factor({f / 100:0.###},{false})", dim.ToString ());
  161. f = 0.5F;
  162. dim = Dim.Percent (f);
  163. Assert.Equal ($"Factor({f / 100:0.###},{false})", dim.ToString ());
  164. f = 100;
  165. dim = Dim.Percent (f);
  166. Assert.Equal ($"Factor({f / 100:0.###},{false})", dim.ToString ());
  167. }
  168. [Fact]
  169. public void Percent_Equals ()
  170. {
  171. float n1 = 0;
  172. float n2 = 0;
  173. var dim1 = Dim.Percent (n1);
  174. var dim2 = Dim.Percent (n2);
  175. Assert.Equal (dim1, dim2);
  176. n1 = n2 = 1;
  177. dim1 = Dim.Percent (n1);
  178. dim2 = Dim.Percent (n2);
  179. Assert.Equal (dim1, dim2);
  180. n1 = n2 = 0.5f;
  181. dim1 = Dim.Percent (n1);
  182. dim2 = Dim.Percent (n2);
  183. Assert.Equal (dim1, dim2);
  184. n1 = n2 = 100f;
  185. dim1 = Dim.Percent (n1);
  186. dim2 = Dim.Percent (n2);
  187. Assert.Equal (dim1, dim2);
  188. n1 = n2 = 0.3f;
  189. dim1 = Dim.Percent (n1, true);
  190. dim2 = Dim.Percent (n2, true);
  191. Assert.Equal (dim1, dim2);
  192. n1 = n2 = 0.3f;
  193. dim1 = Dim.Percent (n1);
  194. dim2 = Dim.Percent (n2, true);
  195. Assert.NotEqual (dim1, dim2);
  196. n1 = 0;
  197. n2 = 1;
  198. dim1 = Dim.Percent (n1);
  199. dim2 = Dim.Percent (n2);
  200. Assert.NotEqual (dim1, dim2);
  201. n1 = 0.5f;
  202. n2 = 1.5f;
  203. dim1 = Dim.Percent (n1);
  204. dim2 = Dim.Percent (n2);
  205. Assert.NotEqual (dim1, dim2);
  206. }
  207. [Fact]
  208. public void Percent_Invalid_Throws ()
  209. {
  210. var dim = Dim.Percent (0);
  211. Assert.Throws<ArgumentException> (() => dim = Dim.Percent (-1));
  212. Assert.Throws<ArgumentException> (() => dim = Dim.Percent (101));
  213. Assert.Throws<ArgumentException> (() => dim = Dim.Percent (100.0001F));
  214. Assert.Throws<ArgumentException> (() => dim = Dim.Percent (1000001));
  215. }
  216. [Fact, AutoInitShutdown]
  217. public void ForceValidatePosDim_True_Dim_Validation_If_NewValue_Is_DimAbsolute_And_OldValue_Is_Another_Type_Throws ()
  218. {
  219. var t = Application.Top;
  220. var w = new Window ("w") {
  221. Width = Dim.Fill (0),
  222. Height = Dim.Sized (10)
  223. };
  224. var v = new View ("v") {
  225. Width = Dim.Width (w) - 2,
  226. Height = Dim.Percent (10),
  227. ForceValidatePosDim = true
  228. };
  229. w.Add (v);
  230. t.Add (w);
  231. t.Ready += (s, e) => {
  232. Assert.Equal (2, w.Width = 2);
  233. Assert.Equal (2, w.Height = 2);
  234. Assert.Throws<ArgumentException> (() => v.Width = 2);
  235. Assert.Throws<ArgumentException> (() => v.Height = 2);
  236. v.ForceValidatePosDim = false;
  237. var exception = Record.Exception (() => v.Width = 2);
  238. Assert.Null (exception);
  239. Assert.Equal (2, v.Width);
  240. exception = Record.Exception (() => v.Height = 2);
  241. Assert.Null (exception);
  242. Assert.Equal (2, v.Height);
  243. };
  244. Application.Iteration += () => Application.RequestStop ();
  245. Application.Run ();
  246. }
  247. [Fact]
  248. public void Dim_Validation_Do_Not_Throws_If_NewValue_Is_DimAbsolute_And_OldValue_Is_Null ()
  249. {
  250. var t = new View ("top") { Width = 80, Height = 25 };
  251. var w = new Window (new Rect (1, 2, 4, 5), "w");
  252. t.Add (w);
  253. t.LayoutSubviews ();
  254. Assert.Equal (3, w.Width = 3);
  255. Assert.Equal (4, w.Height = 4);
  256. }
  257. [Fact]
  258. public void Dim_Validation_Do_Not_Throws_If_NewValue_Is_DimAbsolute_And_OldValue_Is_Another_Type_After_Sets_To_LayoutStyle_Absolute ()
  259. {
  260. var t = new View ("top") { Width = 80, Height = 25 };
  261. var w = new Window ("w") {
  262. Width = Dim.Fill (0),
  263. Height = Dim.Sized (10)
  264. };
  265. var v = new View ("v") {
  266. Width = Dim.Width (w) - 2,
  267. Height = Dim.Percent (10)
  268. };
  269. w.Add (v);
  270. t.Add (w);
  271. t.LayoutSubviews ();
  272. Assert.Equal (2, v.Width = 2);
  273. Assert.Equal (2, v.Height = 2);
  274. v.LayoutStyle = LayoutStyle.Absolute;
  275. t.LayoutSubviews ();
  276. Assert.Equal (2, v.Width = 2);
  277. Assert.Equal (2, v.Height = 2);
  278. }
  279. [Fact, AutoInitShutdown]
  280. public void Only_DimAbsolute_And_DimFactor_As_A_Different_Procedure_For_Assigning_Value_To_Width_Or_Height ()
  281. {
  282. // Testing with the Button because it properly handles the Dim class.
  283. var t = Application.Top;
  284. var w = new Window ("w") {
  285. Width = 100,
  286. Height = 100
  287. };
  288. var f1 = new FrameView ("f1") {
  289. X = 0,
  290. Y = 0,
  291. Width = Dim.Percent (50),
  292. Height = 5
  293. };
  294. var f2 = new FrameView ("f2") {
  295. X = Pos.Right (f1),
  296. Y = 0,
  297. Width = Dim.Fill (),
  298. Height = 5
  299. };
  300. var v1 = new Button ("v1") {
  301. AutoSize = false,
  302. X = Pos.X (f1) + 2,
  303. Y = Pos.Bottom (f1) + 2,
  304. Width = Dim.Width (f1) - 2,
  305. Height = Dim.Fill () - 2,
  306. ForceValidatePosDim = true
  307. };
  308. var v2 = new Button ("v2") {
  309. AutoSize = false,
  310. X = Pos.X (f2) + 2,
  311. Y = Pos.Bottom (f2) + 2,
  312. Width = Dim.Width (f2) - 2,
  313. Height = Dim.Fill () - 2,
  314. ForceValidatePosDim = true
  315. };
  316. var v3 = new Button ("v3") {
  317. AutoSize = false,
  318. Width = Dim.Percent (10),
  319. Height = Dim.Percent (10),
  320. ForceValidatePosDim = true
  321. };
  322. var v4 = new Button ("v4") {
  323. AutoSize = false,
  324. Width = Dim.Sized (50),
  325. Height = Dim.Sized (50),
  326. ForceValidatePosDim = true
  327. };
  328. var v5 = new Button ("v5") {
  329. AutoSize = false,
  330. Width = Dim.Width (v1) - Dim.Width (v3),
  331. Height = Dim.Height (v1) - Dim.Height (v3),
  332. ForceValidatePosDim = true
  333. };
  334. var v6 = new Button ("v6") {
  335. AutoSize = false,
  336. X = Pos.X (f2),
  337. Y = Pos.Bottom (f2) + 2,
  338. Width = Dim.Percent (20, true),
  339. Height = Dim.Percent (20, true),
  340. ForceValidatePosDim = true
  341. };
  342. w.Add (f1, f2, v1, v2, v3, v4, v5, v6);
  343. t.Add (w);
  344. t.Ready += (s, e) => {
  345. Assert.Equal ("Absolute(100)", w.Width.ToString ());
  346. Assert.Equal ("Absolute(100)", w.Height.ToString ());
  347. Assert.Equal (100, w.Frame.Width);
  348. Assert.Equal (100, w.Frame.Height);
  349. Assert.Equal ("Factor(0.5,False)", f1.Width.ToString ());
  350. Assert.Equal ("Absolute(5)", f1.Height.ToString ());
  351. Assert.Equal (49, f1.Frame.Width); // 50-1=49
  352. Assert.Equal (5, f1.Frame.Height);
  353. Assert.Equal ("Fill(0)", f2.Width.ToString ());
  354. Assert.Equal ("Absolute(5)", f2.Height.ToString ());
  355. Assert.Equal (49, f2.Frame.Width); // 50-1=49
  356. Assert.Equal (5, f2.Frame.Height);
  357. Assert.Equal ("Combine(View(Width,FrameView(f1)({X=0,Y=0,Width=49,Height=5}))-Absolute(2))", v1.Width.ToString ());
  358. Assert.Equal ("Combine(Fill(0)-Absolute(2))", v1.Height.ToString ());
  359. Assert.Equal (47, v1.Frame.Width); // 49-2=47
  360. Assert.Equal (89, v1.Frame.Height); // 98-5-2-2=89
  361. Assert.Equal ("Combine(View(Width,FrameView(f2)({X=49,Y=0,Width=49,Height=5}))-Absolute(2))", v2.Width.ToString ());
  362. Assert.Equal ("Combine(Fill(0)-Absolute(2))", v2.Height.ToString ());
  363. Assert.Equal (47, v2.Frame.Width); // 49-2=47
  364. Assert.Equal (89, v2.Frame.Height); // 98-5-2-2=89
  365. Assert.Equal ("Factor(0.1,False)", v3.Width.ToString ());
  366. Assert.Equal ("Factor(0.1,False)", v3.Height.ToString ());
  367. Assert.Equal (9, v3.Frame.Width); // 98*10%=9
  368. Assert.Equal (9, v3.Frame.Height); // 98*10%=9
  369. Assert.Equal ("Absolute(50)", v4.Width.ToString ());
  370. Assert.Equal ("Absolute(50)", v4.Height.ToString ());
  371. Assert.Equal (50, v4.Frame.Width);
  372. Assert.Equal (50, v4.Frame.Height);
  373. Assert.Equal ("Combine(View(Width,Button(v1)({X=2,Y=7,Width=47,Height=89}))-View(Width,Button(v3)({X=0,Y=0,Width=9,Height=9})))", v5.Width.ToString ());
  374. Assert.Equal ("Combine(View(Height,Button(v1)({X=2,Y=7,Width=47,Height=89}))-View(Height,Button(v3)({X=0,Y=0,Width=9,Height=9})))", v5.Height.ToString ());
  375. Assert.Equal (38, v5.Frame.Width); // 47-9=38
  376. Assert.Equal (80, v5.Frame.Height); // 89-9=80
  377. Assert.Equal ("Factor(0.2,True)", v6.Width.ToString ());
  378. Assert.Equal ("Factor(0.2,True)", v6.Height.ToString ());
  379. Assert.Equal (9, v6.Frame.Width); // 47*20%=9
  380. Assert.Equal (18, v6.Frame.Height); // 89*20%=18
  381. w.Width = 200;
  382. Assert.True (t.LayoutNeeded);
  383. w.Height = 200;
  384. t.LayoutSubviews ();
  385. Assert.Equal ("Absolute(200)", w.Width.ToString ());
  386. Assert.Equal ("Absolute(200)", w.Height.ToString ());
  387. Assert.Equal (200, w.Frame.Width);
  388. Assert.Equal (200, w.Frame.Height);
  389. f1.Text = "Frame1";
  390. Assert.Equal ("Factor(0.5,False)", f1.Width.ToString ());
  391. Assert.Equal ("Absolute(5)", f1.Height.ToString ());
  392. Assert.Equal (99, f1.Frame.Width); // 100-1=99
  393. Assert.Equal (5, f1.Frame.Height);
  394. f2.Text = "Frame2";
  395. Assert.Equal ("Fill(0)", f2.Width.ToString ());
  396. Assert.Equal ("Absolute(5)", f2.Height.ToString ());
  397. Assert.Equal (99, f2.Frame.Width); // 100-1=99
  398. Assert.Equal (5, f2.Frame.Height);
  399. v1.Text = "Button1";
  400. Assert.Equal ("Combine(View(Width,FrameView(f1)({X=0,Y=0,Width=99,Height=5}))-Absolute(2))", v1.Width.ToString ());
  401. Assert.Equal ("Combine(Fill(0)-Absolute(2))", v1.Height.ToString ());
  402. Assert.Equal (97, v1.Frame.Width); // 99-2=97
  403. Assert.Equal (189, v1.Frame.Height); // 198-2-7=189
  404. v2.Text = "Button2";
  405. Assert.Equal ("Combine(View(Width,FrameView(f2)({X=99,Y=0,Width=99,Height=5}))-Absolute(2))", v2.Width.ToString ());
  406. Assert.Equal ("Combine(Fill(0)-Absolute(2))", v2.Height.ToString ());
  407. Assert.Equal (97, v2.Frame.Width); // 99-2=97
  408. Assert.Equal (189, v2.Frame.Height); // 198-2-7=189
  409. v3.Text = "Button3";
  410. Assert.Equal ("Factor(0.1,False)", v3.Width.ToString ());
  411. Assert.Equal ("Factor(0.1,False)", v3.Height.ToString ());
  412. 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
  413. Assert.Equal (19, v3.Frame.Height); // 199*10%=19
  414. v4.Text = "Button4";
  415. v4.AutoSize = false;
  416. Assert.Equal ("Absolute(50)", v4.Width.ToString ());
  417. Assert.Equal ("Absolute(50)", v4.Height.ToString ());
  418. Assert.Equal (50, v4.Frame.Width);
  419. Assert.Equal (50, v4.Frame.Height);
  420. v4.AutoSize = true;
  421. Assert.Equal ("Absolute(11)", v4.Width.ToString ());
  422. Assert.Equal ("Absolute(1)", v4.Height.ToString ());
  423. Assert.Equal (11, v4.Frame.Width); // 11 is the text length and because is Dim.DimAbsolute
  424. Assert.Equal (1, v4.Frame.Height); // 1 because is Dim.DimAbsolute
  425. v5.Text = "Button5";
  426. Assert.Equal ("Combine(View(Width,Button(v1)({X=2,Y=7,Width=97,Height=189}))-View(Width,Button(v3)({X=0,Y=0,Width=19,Height=19})))", v5.Width.ToString ());
  427. Assert.Equal ("Combine(View(Height,Button(v1)({X=2,Y=7,Width=97,Height=189}))-View(Height,Button(v3)({X=0,Y=0,Width=19,Height=19})))", v5.Height.ToString ());
  428. Assert.Equal (78, v5.Frame.Width); // 97-9=78
  429. Assert.Equal (170, v5.Frame.Height); // 189-19=170
  430. v6.Text = "Button6";
  431. Assert.Equal ("Factor(0.2,True)", v6.Width.ToString ());
  432. Assert.Equal ("Factor(0.2,True)", v6.Height.ToString ());
  433. Assert.Equal (19, v6.Frame.Width); // 99*20%=19
  434. Assert.Equal (38, v6.Frame.Height); // 198-7*20=18
  435. };
  436. Application.Iteration += () => Application.RequestStop ();
  437. Application.Run ();
  438. }
  439. // See #2461
  440. //[Fact]
  441. //public void Dim_Referencing_SuperView_Throws ()
  442. //{
  443. // var super = new View ("super") {
  444. // Width = 10,
  445. // Height = 10
  446. // };
  447. // var view = new View ("view") {
  448. // Width = Dim.Width (super), // this is not allowed
  449. // Height = Dim.Height (super), // this is not allowed
  450. // };
  451. // super.Add (view);
  452. // super.BeginInit ();
  453. // super.EndInit ();
  454. // Assert.Throws<InvalidOperationException> (() => super.LayoutSubviews ());
  455. //}
  456. /// <summary>
  457. /// This is an intentionally obtuse test. See https://github.com/gui-cs/Terminal.Gui/issues/2461
  458. /// </summary>
  459. [Fact]
  460. public void DimCombine_ObtuseScenario_Throw_If_SuperView_Refs_SubView ()
  461. {
  462. var t = new View ("top") { Width = 80, Height = 25 };
  463. var w = new Window ("w") {
  464. Width = Dim.Width (t) - 2, // 78
  465. Height = Dim.Height (t) - 2 // 23
  466. };
  467. var f = new FrameView ("f");
  468. var v1 = new View ("v1") {
  469. Width = Dim.Width (w) - 2, // 76
  470. Height = Dim.Height (w) - 2 // 21
  471. };
  472. var v2 = new View ("v2") {
  473. Width = Dim.Width (v1) - 2, // 74
  474. Height = Dim.Height (v1) - 2 // 19
  475. };
  476. f.Add (v1, v2);
  477. w.Add (f);
  478. t.Add (w);
  479. // BUGBUG: v2 - f references t here; t is f's super-superview. This is supported!
  480. // BUGBUG: v2 - f references v2 here; v2 is f's subview. This is not supported!
  481. f.Width = Dim.Width (t) - Dim.Width (v2); // 80 - 74 = 6
  482. f.Height = Dim.Height (t) - Dim.Height (v2); // 25 - 19 = 6
  483. Assert.Throws<InvalidOperationException> (t.LayoutSubviews);
  484. Assert.Equal (80, t.Frame.Width);
  485. Assert.Equal (25, t.Frame.Height);
  486. Assert.Equal (78, w.Frame.Width);
  487. Assert.Equal (23, w.Frame.Height);
  488. // BUGBUG: v2 - this no longer works - see above
  489. //Assert.Equal (6, f.Frame.Width);
  490. //Assert.Equal (6, f.Frame.Height);
  491. //Assert.Equal (76, v1.Frame.Width);
  492. //Assert.Equal (21, v1.Frame.Height);
  493. //Assert.Equal (74, v2.Frame.Width);
  494. //Assert.Equal (19, v2.Frame.Height);
  495. }
  496. [Fact]
  497. public void DimCombine_ObtuseScenario_Does_Not_Throw_If_Two_SubViews_Refs_The_Same_SuperView ()
  498. {
  499. var t = new View ("top") { Width = 80, Height = 25 };
  500. var w = new Window ("w") {
  501. Width = Dim.Width (t) - 2, // 78
  502. Height = Dim.Height (t) - 2 // 23
  503. };
  504. var f = new FrameView ("f");
  505. var v1 = new View ("v1") {
  506. Width = Dim.Width (w) - 2, // 76
  507. Height = Dim.Height (w) - 2 // 21
  508. };
  509. var v2 = new View ("v2") {
  510. Width = Dim.Width (v1) - 2, // 74
  511. Height = Dim.Height (v1) - 2 // 19
  512. };
  513. f.Add (v1, v2);
  514. w.Add (f);
  515. t.Add (w);
  516. f.Width = Dim.Width (t) - Dim.Width (w) + 4; // 80 - 74 = 6
  517. f.Height = Dim.Height (t) - Dim.Height (w) + 4; // 25 - 19 = 6
  518. // BUGBUG: v2 - f references t and w here; t is f's super-superview and w is f's superview. This is supported!
  519. var exception = Record.Exception (t.LayoutSubviews);
  520. Assert.Null (exception);
  521. Assert.Equal (80, t.Frame.Width);
  522. Assert.Equal (25, t.Frame.Height);
  523. Assert.Equal (78, w.Frame.Width);
  524. Assert.Equal (23, w.Frame.Height);
  525. Assert.Equal (6, f.Frame.Width);
  526. Assert.Equal (6, f.Frame.Height);
  527. Assert.Equal (76, v1.Frame.Width);
  528. Assert.Equal (21, v1.Frame.Height);
  529. Assert.Equal (74, v2.Frame.Width);
  530. Assert.Equal (19, v2.Frame.Height);
  531. }
  532. [Fact]
  533. public void PosCombine_View_Not_Added_Throws ()
  534. {
  535. var t = new View ("t") { Width = 80, Height = 50 };
  536. // BUGBUG: v2 - super should not reference it's superview (t)
  537. var super = new View ("super") {
  538. Width = Dim.Width (t) - 2,
  539. Height = Dim.Height (t) - 2
  540. };
  541. t.Add (super);
  542. var sub = new View ("sub");
  543. super.Add (sub);
  544. var v1 = new View ("v1") {
  545. Width = Dim.Width (super) - 2,
  546. Height = Dim.Height (super) - 2
  547. };
  548. var v2 = new View ("v2") {
  549. Width = Dim.Width (v1) - 2,
  550. Height = Dim.Height (v1) - 2
  551. };
  552. sub.Add (v1);
  553. // v2 not added to sub; should cause exception on Layout since it's referenced by sub.
  554. sub.Width = Dim.Fill () - Dim.Width (v2);
  555. sub.Height = Dim.Fill () - Dim.Height (v2);
  556. Assert.Throws<InvalidOperationException> (() => t.LayoutSubviews ());
  557. }
  558. [Fact, AutoInitShutdown]
  559. public void Dim_Add_Operator ()
  560. {
  561. var top = Application.Top;
  562. var view = new View () { X = 0, Y = 0, Width = 20, Height = 0 };
  563. var field = new TextField () { X = 0, Y = Pos.Bottom (view), Width = 20 };
  564. var count = 0;
  565. field.KeyDown += (s, k) => {
  566. if (k.KeyEvent.Key == Key.Enter) {
  567. field.Text = $"Label {count}";
  568. var label = new Label (field.Text) { X = 0, Y = view.Bounds.Height, Width = 20 };
  569. view.Add (label);
  570. Assert.Equal ($"Label {count}", label.Text);
  571. Assert.Equal ($"Absolute({count})", label.Y.ToString ());
  572. Assert.Equal ($"Absolute({count})", view.Height.ToString ());
  573. view.Height += 1;
  574. count++;
  575. Assert.Equal ($"Absolute({count})", view.Height.ToString ());
  576. }
  577. };
  578. Application.Iteration += () => {
  579. while (count < 20) field.OnKeyDown (new KeyEvent (Key.Enter, new KeyModifiers ()));
  580. Application.RequestStop ();
  581. };
  582. var win = new Window ();
  583. win.Add (view);
  584. win.Add (field);
  585. top.Add (win);
  586. Application.Run (top);
  587. Assert.Equal (20, count);
  588. }
  589. private string [] expecteds = new string [21] {
  590. @"
  591. ┌────────────────────┐
  592. │View with long text │
  593. │ │
  594. └────────────────────┘",
  595. @"
  596. ┌────────────────────┐
  597. │View with long text │
  598. │Label 0 │
  599. │Label 0 │
  600. └────────────────────┘",
  601. @"
  602. ┌────────────────────┐
  603. │View with long text │
  604. │Label 0 │
  605. │Label 1 │
  606. │Label 1 │
  607. └────────────────────┘",
  608. @"
  609. ┌────────────────────┐
  610. │View with long text │
  611. │Label 0 │
  612. │Label 1 │
  613. │Label 2 │
  614. │Label 2 │
  615. └────────────────────┘",
  616. @"
  617. ┌────────────────────┐
  618. │View with long text │
  619. │Label 0 │
  620. │Label 1 │
  621. │Label 2 │
  622. │Label 3 │
  623. │Label 3 │
  624. └────────────────────┘",
  625. @"
  626. ┌────────────────────┐
  627. │View with long text │
  628. │Label 0 │
  629. │Label 1 │
  630. │Label 2 │
  631. │Label 3 │
  632. │Label 4 │
  633. │Label 4 │
  634. └────────────────────┘",
  635. @"
  636. ┌────────────────────┐
  637. │View with long text │
  638. │Label 0 │
  639. │Label 1 │
  640. │Label 2 │
  641. │Label 3 │
  642. │Label 4 │
  643. │Label 5 │
  644. │Label 5 │
  645. └────────────────────┘",
  646. @"
  647. ┌────────────────────┐
  648. │View with long text │
  649. │Label 0 │
  650. │Label 1 │
  651. │Label 2 │
  652. │Label 3 │
  653. │Label 4 │
  654. │Label 5 │
  655. │Label 6 │
  656. │Label 6 │
  657. └────────────────────┘",
  658. @"
  659. ┌────────────────────┐
  660. │View with long text │
  661. │Label 0 │
  662. │Label 1 │
  663. │Label 2 │
  664. │Label 3 │
  665. │Label 4 │
  666. │Label 5 │
  667. │Label 6 │
  668. │Label 7 │
  669. │Label 7 │
  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 8 │
  684. └────────────────────┘",
  685. @"
  686. ┌────────────────────┐
  687. │View with long text │
  688. │Label 0 │
  689. │Label 1 │
  690. │Label 2 │
  691. │Label 3 │
  692. │Label 4 │
  693. │Label 5 │
  694. │Label 6 │
  695. │Label 7 │
  696. │Label 8 │
  697. │Label 9 │
  698. │Label 9 │
  699. └────────────────────┘",
  700. @"
  701. ┌────────────────────┐
  702. │View with long text │
  703. │Label 0 │
  704. │Label 1 │
  705. │Label 2 │
  706. │Label 3 │
  707. │Label 4 │
  708. │Label 5 │
  709. │Label 6 │
  710. │Label 7 │
  711. │Label 8 │
  712. │Label 9 │
  713. │Label 10 │
  714. │Label 10 │
  715. └────────────────────┘",
  716. @"
  717. ┌────────────────────┐
  718. │View with long text │
  719. │Label 0 │
  720. │Label 1 │
  721. │Label 2 │
  722. │Label 3 │
  723. │Label 4 │
  724. │Label 5 │
  725. │Label 6 │
  726. │Label 7 │
  727. │Label 8 │
  728. │Label 9 │
  729. │Label 10 │
  730. │Label 11 │
  731. │Label 11 │
  732. └────────────────────┘",
  733. @"
  734. ┌────────────────────┐
  735. │View with long text │
  736. │Label 0 │
  737. │Label 1 │
  738. │Label 2 │
  739. │Label 3 │
  740. │Label 4 │
  741. │Label 5 │
  742. │Label 6 │
  743. │Label 7 │
  744. │Label 8 │
  745. │Label 9 │
  746. │Label 10 │
  747. │Label 11 │
  748. │Label 12 │
  749. │Label 12 │
  750. └────────────────────┘",
  751. @"
  752. ┌────────────────────┐
  753. │View with long text │
  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 13 │
  769. └────────────────────┘",
  770. @"
  771. ┌────────────────────┐
  772. │View with long text │
  773. │Label 0 │
  774. │Label 1 │
  775. │Label 2 │
  776. │Label 3 │
  777. │Label 4 │
  778. │Label 5 │
  779. │Label 6 │
  780. │Label 7 │
  781. │Label 8 │
  782. │Label 9 │
  783. │Label 10 │
  784. │Label 11 │
  785. │Label 12 │
  786. │Label 13 │
  787. │Label 14 │
  788. │Label 14 │
  789. └────────────────────┘",
  790. @"
  791. ┌────────────────────┐
  792. │View with long text │
  793. │Label 0 │
  794. │Label 1 │
  795. │Label 2 │
  796. │Label 3 │
  797. │Label 4 │
  798. │Label 5 │
  799. │Label 6 │
  800. │Label 7 │
  801. │Label 8 │
  802. │Label 9 │
  803. │Label 10 │
  804. │Label 11 │
  805. │Label 12 │
  806. │Label 13 │
  807. │Label 14 │
  808. │Label 15 │
  809. │Label 15 │
  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 16 │
  832. └────────────────────┘",
  833. @"
  834. ┌────────────────────┐
  835. │View with long text │
  836. │Label 0 │
  837. │Label 1 │
  838. │Label 2 │
  839. │Label 3 │
  840. │Label 4 │
  841. │Label 5 │
  842. │Label 6 │
  843. │Label 7 │
  844. │Label 8 │
  845. │Label 9 │
  846. │Label 10 │
  847. │Label 11 │
  848. │Label 12 │
  849. │Label 13 │
  850. │Label 14 │
  851. │Label 15 │
  852. │Label 16 │
  853. │Label 17 │
  854. │Label 17 │
  855. └────────────────────┘",
  856. @"
  857. ┌────────────────────┐
  858. │View with long text │
  859. │Label 0 │
  860. │Label 1 │
  861. │Label 2 │
  862. │Label 3 │
  863. │Label 4 │
  864. │Label 5 │
  865. │Label 6 │
  866. │Label 7 │
  867. │Label 8 │
  868. │Label 9 │
  869. │Label 10 │
  870. │Label 11 │
  871. │Label 12 │
  872. │Label 13 │
  873. │Label 14 │
  874. │Label 15 │
  875. │Label 16 │
  876. │Label 17 │
  877. │Label 18 │
  878. │Label 18 │
  879. └────────────────────┘",
  880. @"
  881. ┌────────────────────┐
  882. │View with long text │
  883. │Label 0 │
  884. │Label 1 │
  885. │Label 2 │
  886. │Label 3 │
  887. │Label 4 │
  888. │Label 5 │
  889. │Label 6 │
  890. │Label 7 │
  891. │Label 8 │
  892. │Label 9 │
  893. │Label 10 │
  894. │Label 11 │
  895. │Label 12 │
  896. │Label 13 │
  897. │Label 14 │
  898. │Label 15 │
  899. │Label 16 │
  900. │Label 17 │
  901. │Label 18 │
  902. │Label 19 │
  903. │Label 19 │
  904. └────────────────────┘",
  905. };
  906. [Fact, AutoInitShutdown]
  907. public void Dim_Add_Operator_With_Text ()
  908. {
  909. var top = Application.Top;
  910. // BUGBUG: v2 - If a View's height is zero, it should not be drawn.
  911. //// Although view height is zero the text it's draw due the SetMinWidthHeight method
  912. var view = new View ("View with long text") { X = 0, Y = 0, Width = 20, Height = 1 };
  913. var field = new TextField () { X = 0, Y = Pos.Bottom (view), Width = 20 };
  914. var count = 0;
  915. var listLabels = new List<Label> ();
  916. field.KeyDown += (s, k) => {
  917. if (k.KeyEvent.Key == Key.Enter) {
  918. ((FakeDriver)Application.Driver).SetBufferSize (22, count + 4);
  919. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expecteds [count], output);
  920. Assert.Equal (new Rect (0, 0, 22, count + 4), pos);
  921. if (count < 20) {
  922. field.Text = $"Label {count}";
  923. var label = new Label (field.Text) { X = 0, Y = view.Bounds.Height, Width = 10 };
  924. view.Add (label);
  925. Assert.Equal ($"Label {count}", label.Text);
  926. Assert.Equal ($"Absolute({count + 1})", label.Y.ToString ());
  927. listLabels.Add (label);
  928. //if (count == 0) {
  929. // Assert.Equal ($"Absolute({count})", view.Height.ToString ());
  930. // view.Height += 2;
  931. //} else {
  932. Assert.Equal ($"Absolute({count + 1})", view.Height.ToString ());
  933. view.Height += 1;
  934. //}
  935. count++;
  936. }
  937. Assert.Equal ($"Absolute({count + 1})", view.Height.ToString ());
  938. }
  939. };
  940. Application.Iteration += () => {
  941. while (count < 21) {
  942. field.OnKeyDown (new KeyEvent (Key.Enter, new KeyModifiers ()));
  943. if (count == 20) {
  944. field.OnKeyDown (new KeyEvent (Key.Enter, new KeyModifiers ()));
  945. break;
  946. }
  947. }
  948. Application.RequestStop ();
  949. };
  950. var win = new Window ();
  951. win.Add (view);
  952. win.Add (field);
  953. top.Add (win);
  954. Application.Run (top);
  955. Assert.Equal (20, count);
  956. Assert.Equal (count, listLabels.Count);
  957. }
  958. [Fact, AutoInitShutdown]
  959. public void Dim_Subtract_Operator ()
  960. {
  961. var top = Application.Top;
  962. var view = new View () { X = 0, Y = 0, Width = 20, Height = 0 };
  963. var field = new TextField () { X = 0, Y = Pos.Bottom (view), Width = 20 };
  964. var count = 20;
  965. var listLabels = new List<Label> ();
  966. for (int i = 0; i < count; i++) {
  967. field.Text = $"Label {i}";
  968. var label = new Label (field.Text) { X = 0, Y = view.Bounds.Height, Width = 20 };
  969. view.Add (label);
  970. Assert.Equal ($"Label {i}", label.Text);
  971. // BUGBUG: Bogus test; views have not been initialized yet
  972. //Assert.Equal ($"Absolute({i})", label.Y.ToString ());
  973. listLabels.Add (label);
  974. // BUGBUG: Bogus test; views have not been initialized yet
  975. //Assert.Equal ($"Absolute({i})", view.Height.ToString ());
  976. view.Height += 1;
  977. //Assert.Equal ($"Absolute({i + 1})", view.Height.ToString ());
  978. }
  979. field.KeyDown += (s, k) => {
  980. if (k.KeyEvent.Key == Key.Enter) {
  981. Assert.Equal ($"Label {count - 1}", listLabels [count - 1].Text);
  982. view.Remove (listLabels [count - 1]);
  983. Assert.Equal ($"Absolute({count})", view.Height.ToString ());
  984. view.Height -= 1;
  985. count--;
  986. Assert.Equal ($"Absolute({count})", view.Height.ToString ());
  987. }
  988. };
  989. Application.Iteration += () => {
  990. while (count > 0) field.OnKeyDown (new KeyEvent (Key.Enter, new KeyModifiers ()));
  991. Application.RequestStop ();
  992. };
  993. var win = new Window ();
  994. win.Add (view);
  995. win.Add (field);
  996. top.Add (win);
  997. Application.Run (top);
  998. Assert.Equal (0, count);
  999. }
  1000. [Fact, AutoInitShutdown]
  1001. public void Dim_Subtract_Operator_With_Text ()
  1002. {
  1003. var top = Application.Top;
  1004. // BUGBUG: v2 - If a View's height is zero, it should not be drawn.
  1005. //// Although view height is zero the text it's draw due the SetMinWidthHeight method
  1006. var view = new View ("View with long text") { X = 0, Y = 0, Width = 20, Height = 1 };
  1007. var field = new TextField () { X = 0, Y = Pos.Bottom (view), Width = 20 };
  1008. var count = 20;
  1009. var listLabels = new List<Label> ();
  1010. for (int i = 0; i < count; i++) {
  1011. field.Text = $"Label {i}";
  1012. // BUGBUG: v2 - view has not been initialied yet; view.Bounds is indeterminate
  1013. var label = new Label (field.Text) { X = 0, Y = i + 1, Width = 10 };
  1014. view.Add (label);
  1015. Assert.Equal ($"Label {i}", label.Text);
  1016. // BUGBUG: Bogus test; views have not been initialized yet
  1017. //Assert.Equal ($"Absolute({i + 1})", label.Y.ToString ());
  1018. listLabels.Add (label);
  1019. //if (i == 0) {
  1020. // BUGBUG: Bogus test; views have not been initialized yet
  1021. //Assert.Equal ($"Absolute({i})", view.Height.ToString ());
  1022. //view.Height += 2;
  1023. // BUGBUG: Bogus test; views have not been initialized yet
  1024. //Assert.Equal ($"Absolute({i + 2})", view.Height.ToString ());
  1025. //} else {
  1026. // BUGBUG: Bogus test; views have not been initialized yet
  1027. //Assert.Equal ($"Absolute({i + 1})", view.Height.ToString ());
  1028. view.Height += 1;
  1029. // BUGBUG: Bogus test; views have not been initialized yet
  1030. //Assert.Equal ($"Absolute({i + 2})", view.Height.ToString ());
  1031. //}
  1032. }
  1033. field.KeyDown += (s, k) => {
  1034. if (k.KeyEvent.Key == Key.Enter) {
  1035. ((FakeDriver)Application.Driver).SetBufferSize (22, count + 4);
  1036. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expecteds [count], output);
  1037. Assert.Equal (new Rect (0, 0, 22, count + 4), pos);
  1038. if (count > 0) {
  1039. Assert.Equal ($"Label {count - 1}", listLabels [count - 1].Text);
  1040. view.Remove (listLabels [count - 1]);
  1041. listLabels.RemoveAt (count - 1);
  1042. Assert.Equal ($"Absolute({count + 1})", view.Height.ToString ());
  1043. view.Height -= 1;
  1044. count--;
  1045. if (listLabels.Count > 0)
  1046. field.Text = listLabels [count - 1].Text;
  1047. else
  1048. field.Text = NStack.ustring.Empty;
  1049. }
  1050. Assert.Equal ($"Absolute({count + 1})", view.Height.ToString ());
  1051. }
  1052. };
  1053. Application.Iteration += () => {
  1054. while (count > -1) {
  1055. field.OnKeyDown (new KeyEvent (Key.Enter, new KeyModifiers ()));
  1056. if (count == 0) {
  1057. field.OnKeyDown (new KeyEvent (Key.Enter, new KeyModifiers ()));
  1058. break;
  1059. }
  1060. }
  1061. Application.RequestStop ();
  1062. };
  1063. var win = new Window ();
  1064. win.Add (view);
  1065. win.Add (field);
  1066. top.Add (win);
  1067. Application.Run (top);
  1068. Assert.Equal (0, count);
  1069. Assert.Equal (count, listLabels.Count);
  1070. }
  1071. [Fact]
  1072. public void Internal_Tests ()
  1073. {
  1074. var dimFactor = new Dim.DimFactor (0.10F);
  1075. Assert.Equal (10, dimFactor.Anchor (100));
  1076. var dimAbsolute = new Dim.DimAbsolute (10);
  1077. Assert.Equal (10, dimAbsolute.Anchor (0));
  1078. var dimFill = new Dim.DimFill (1);
  1079. Assert.Equal (99, dimFill.Anchor (100));
  1080. var dimCombine = new Dim.DimCombine (true, dimFactor, dimAbsolute);
  1081. Assert.Equal (dimCombine.left, dimFactor);
  1082. Assert.Equal (dimCombine.right, dimAbsolute);
  1083. Assert.Equal (20, dimCombine.Anchor (100));
  1084. var view = new View (new Rect (20, 10, 20, 1));
  1085. var dimViewHeight = new Dim.DimView (view, 0);
  1086. Assert.Equal (1, dimViewHeight.Anchor (0));
  1087. var dimViewWidth = new Dim.DimView (view, 1);
  1088. Assert.Equal (20, dimViewWidth.Anchor (0));
  1089. }
  1090. [Fact]
  1091. public void Function_SetsValue ()
  1092. {
  1093. var text = "Test";
  1094. var dim = Dim.Function (() => text.Length);
  1095. Assert.Equal ("DimFunc(4)", dim.ToString ());
  1096. text = "New Test";
  1097. Assert.Equal ("DimFunc(8)", dim.ToString ());
  1098. text = "";
  1099. Assert.Equal ("DimFunc(0)", dim.ToString ());
  1100. }
  1101. [Fact]
  1102. public void Function_Equal ()
  1103. {
  1104. var f1 = () => 0;
  1105. var f2 = () => 0;
  1106. var dim1 = Dim.Function (f1);
  1107. var dim2 = Dim.Function (f2);
  1108. Assert.Equal (dim1, dim2);
  1109. f2 = () => 1;
  1110. dim2 = Dim.Function (f2);
  1111. Assert.NotEqual (dim1, dim2);
  1112. }
  1113. [Theory, AutoInitShutdown]
  1114. [InlineData (0, true)]
  1115. [InlineData (0, false)]
  1116. [InlineData (50, true)]
  1117. [InlineData (50, false)]
  1118. public void DimPercentPlusOne (int startingDistance, bool testHorizontal)
  1119. {
  1120. var container = new View {
  1121. Width = 100,
  1122. Height = 100,
  1123. };
  1124. var label = new Label {
  1125. X = testHorizontal ? startingDistance : 0,
  1126. Y = testHorizontal ? 0 : startingDistance,
  1127. Width = testHorizontal ? Dim.Percent (50) + 1 : 1,
  1128. Height = testHorizontal ? 1 : Dim.Percent (50) + 1,
  1129. };
  1130. container.Add (label);
  1131. Application.Top.Add (container);
  1132. Application.Top.LayoutSubviews ();
  1133. Assert.Equal (100, container.Frame.Width);
  1134. Assert.Equal (100, container.Frame.Height);
  1135. if (testHorizontal) {
  1136. Assert.Equal (51, label.Frame.Width);
  1137. Assert.Equal (1, label.Frame.Height);
  1138. } else {
  1139. Assert.Equal (1, label.Frame.Width);
  1140. Assert.Equal (51, label.Frame.Height);
  1141. }
  1142. }
  1143. [Fact]
  1144. public void Dim_Referencing_SuperView_Does_Not_Throw ()
  1145. {
  1146. var super = new View ("super") {
  1147. Width = 10,
  1148. Height = 10
  1149. };
  1150. var view = new View ("view") {
  1151. Width = Dim.Width (super), // this is allowed
  1152. Height = Dim.Height (super), // this is allowed
  1153. };
  1154. super.Add (view);
  1155. super.BeginInit ();
  1156. super.EndInit ();
  1157. var exception = Record.Exception (super.LayoutSubviews);
  1158. Assert.Null (exception);
  1159. }
  1160. [Fact]
  1161. public void Dim_SyperView_Referencing_SubView_Throws ()
  1162. {
  1163. var super = new View ("super") {
  1164. Width = 10,
  1165. Height = 10
  1166. };
  1167. var view2 = new View ("view2") {
  1168. Width = 10,
  1169. Height = 10,
  1170. };
  1171. var view = new View ("view") {
  1172. Width = Dim.Width (view2), // this is not allowed
  1173. Height = Dim.Height (view2), // this is not allowed
  1174. };
  1175. view.Add (view2);
  1176. super.Add (view);
  1177. super.BeginInit ();
  1178. super.EndInit ();
  1179. Assert.Throws<InvalidOperationException> (super.LayoutSubviews);
  1180. }
  1181. }
  1182. }