Dim.AutoTests.cs 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499
  1. using System.Globalization;
  2. using System.Text;
  3. using Xunit.Abstractions;
  4. using static Terminal.Gui.Dim;
  5. namespace Terminal.Gui.LayoutTests;
  6. public class DimAutoTests (ITestOutputHelper output)
  7. {
  8. private readonly ITestOutputHelper _output = output;
  9. private class DimAutoTestView : View
  10. {
  11. public DimAutoTestView ()
  12. {
  13. ValidatePosDim = true;
  14. Width = Dim.Auto ();
  15. Height = Dim.Auto ();
  16. }
  17. public DimAutoTestView (Dim width, Dim height)
  18. {
  19. ValidatePosDim = true;
  20. Width = width;
  21. Height = height;
  22. }
  23. public DimAutoTestView (string text, Dim width, Dim height)
  24. {
  25. ValidatePosDim = true;
  26. Text = text;
  27. Width = width;
  28. Height = height;
  29. }
  30. }
  31. // Test min - ensure that if min is specified in the DimAuto constructor it is honored
  32. [Fact]
  33. public void Min_Is_Honored ()
  34. {
  35. var superView = new View
  36. {
  37. X = 0,
  38. Y = 0,
  39. Width = Dim.Auto (minimumContentDim: 10),
  40. Height = Dim.Auto (minimumContentDim: 10),
  41. ValidatePosDim = true
  42. };
  43. var subView = new View
  44. {
  45. X = 0,
  46. Y = 0,
  47. Width = 5,
  48. Height = 5
  49. };
  50. superView.Add (subView);
  51. superView.BeginInit ();
  52. superView.EndInit ();
  53. superView.SetRelativeLayout (new (10, 10));
  54. superView.LayoutSubviews (); // no throw
  55. Assert.Equal (10, superView.Frame.Width);
  56. Assert.Equal (10, superView.Frame.Height);
  57. }
  58. [Theory]
  59. [InlineData (0, 2, 4)]
  60. [InlineData (1, 2, 4)]
  61. [InlineData (2, 2, 4)]
  62. [InlineData (3, 2, 5)]
  63. [InlineData (1, 0, 3)]
  64. public void Min_Absolute_Is_Content_Relative (int contentSize, int minAbsolute, int expected)
  65. {
  66. var view = new View
  67. {
  68. X = 0,
  69. Y = 0,
  70. Width = Dim.Auto (minimumContentDim: minAbsolute),
  71. BorderStyle = LineStyle.Single, // a 1 thick adornment
  72. ValidatePosDim = true
  73. };
  74. view.SetContentSize (new (contentSize, 0));
  75. Assert.Equal (expected, view.Frame.Width);
  76. }
  77. [Theory]
  78. [InlineData (1, 100, 100)]
  79. [InlineData (1, 50, 50)]
  80. public void Min_Percent (int contentSize, int minPercent, int expected)
  81. {
  82. var view = new View
  83. {
  84. X = 0,
  85. Y = 0,
  86. Width = Dim.Auto (minimumContentDim: Dim.Percent (minPercent)),
  87. ValidatePosDim = true
  88. };
  89. view.SetContentSize (new (contentSize, 0));
  90. view.SetRelativeLayout (new (100, 100));
  91. Assert.Equal (expected, view.Frame.Width);
  92. }
  93. [Theory]
  94. [InlineData (1, 100, 102)]
  95. [InlineData (1, 50, 52)] // 50% of 100 is 50, but the border adds 2
  96. [InlineData (1, 30, 32)] // 30% of 100 is 30, but the border adds 2
  97. [InlineData (2, 30, 32)] // 30% of 100 is 30, but the border adds 2
  98. public void Min_Percent_Is_Content_Relative (int contentSize, int minPercent, int expected)
  99. {
  100. var view = new View
  101. {
  102. X = 0,
  103. Y = 0,
  104. Width = Dim.Auto (minimumContentDim: Dim.Percent (minPercent)),
  105. BorderStyle = LineStyle.Single, // a 1 thick adornment
  106. ValidatePosDim = true
  107. };
  108. view.SetContentSize (new (contentSize, 0));
  109. view.SetRelativeLayout (new (100, 100));
  110. Assert.Equal (expected, view.Frame.Width);
  111. }
  112. // what happens if DimAuto (min: 10) and the subview moves to a negative coord?
  113. [Fact]
  114. public void Min_Resets_If_Subview_Moves_Negative ()
  115. {
  116. var superView = new View
  117. {
  118. X = 0,
  119. Y = 0,
  120. Width = Dim.Auto (minimumContentDim: 10),
  121. Height = Dim.Auto (minimumContentDim: 10),
  122. ValidatePosDim = true
  123. };
  124. var subView = new View
  125. {
  126. X = 0,
  127. Y = 0,
  128. Width = 5,
  129. Height = 5
  130. };
  131. superView.Add (subView);
  132. superView.BeginInit ();
  133. superView.EndInit ();
  134. superView.SetRelativeLayout (new (10, 10));
  135. superView.LayoutSubviews (); // no throw
  136. Assert.Equal (10, superView.Frame.Width);
  137. Assert.Equal (10, superView.Frame.Height);
  138. subView.X = -1;
  139. subView.Y = -1;
  140. superView.SetRelativeLayout (new (10, 10));
  141. superView.LayoutSubviews (); // no throw
  142. Assert.Equal (5, subView.Frame.Width);
  143. Assert.Equal (5, subView.Frame.Height);
  144. Assert.Equal (10, superView.Frame.Width);
  145. Assert.Equal (10, superView.Frame.Height);
  146. }
  147. [Fact]
  148. public void Min_Resets_If_Subview_Shrinks ()
  149. {
  150. var superView = new View
  151. {
  152. X = 0,
  153. Y = 0,
  154. Width = Dim.Auto (minimumContentDim: 10),
  155. Height = Dim.Auto (minimumContentDim: 10),
  156. ValidatePosDim = true
  157. };
  158. var subView = new View
  159. {
  160. X = 0,
  161. Y = 0,
  162. Width = 5,
  163. Height = 5
  164. };
  165. superView.Add (subView);
  166. superView.BeginInit ();
  167. superView.EndInit ();
  168. superView.SetRelativeLayout (new (10, 10));
  169. superView.LayoutSubviews (); // no throw
  170. Assert.Equal (10, superView.Frame.Width);
  171. Assert.Equal (10, superView.Frame.Height);
  172. subView.Width = 3;
  173. subView.Height = 3;
  174. superView.SetRelativeLayout (new (10, 10));
  175. superView.LayoutSubviews (); // no throw
  176. Assert.Equal (3, subView.Frame.Width);
  177. Assert.Equal (3, subView.Frame.Height);
  178. Assert.Equal (10, superView.Frame.Width);
  179. Assert.Equal (10, superView.Frame.Height);
  180. }
  181. [Theory]
  182. [InlineData (0, 0, 0, 0, 0)]
  183. [InlineData (0, 0, 5, 0, 0)]
  184. [InlineData (0, 0, 0, 5, 5)]
  185. [InlineData (0, 0, 5, 5, 5)]
  186. [InlineData (1, 0, 5, 0, 0)]
  187. [InlineData (1, 0, 0, 5, 5)]
  188. [InlineData (1, 0, 5, 5, 5)]
  189. [InlineData (1, 1, 5, 5, 6)]
  190. [InlineData (-1, 0, 5, 0, 0)]
  191. [InlineData (-1, 0, 0, 5, 5)]
  192. [InlineData (-1, 0, 5, 5, 5)]
  193. [InlineData (-1, -1, 5, 5, 4)]
  194. public void Height_Auto_Width_Absolute_NotChanged (int subX, int subY, int subWidth, int subHeight, int expectedHeight)
  195. {
  196. var superView = new View
  197. {
  198. X = 0,
  199. Y = 0,
  200. Width = 10,
  201. Height = Dim.Auto (),
  202. ValidatePosDim = true
  203. };
  204. var subView = new View
  205. {
  206. X = subX,
  207. Y = subY,
  208. Width = subWidth,
  209. Height = subHeight,
  210. ValidatePosDim = true
  211. };
  212. superView.Add (subView);
  213. superView.BeginInit ();
  214. superView.EndInit ();
  215. superView.SetRelativeLayout (new (10, 10));
  216. Assert.Equal (new Rectangle (0, 0, 10, expectedHeight), superView.Frame);
  217. }
  218. [Fact]
  219. public void NoSubViews_Does_Nothing ()
  220. {
  221. var superView = new View
  222. {
  223. X = 0,
  224. Y = 0,
  225. Width = Dim.Auto (),
  226. Height = Dim.Auto (),
  227. ValidatePosDim = true
  228. };
  229. superView.BeginInit ();
  230. superView.EndInit ();
  231. superView.SetRelativeLayout (new (10, 10));
  232. Assert.Equal (new Rectangle (0, 0, 0, 0), superView.Frame);
  233. superView.SetRelativeLayout (new (10, 10));
  234. Assert.Equal (new Rectangle (0, 0, 0, 0), superView.Frame);
  235. }
  236. [Fact]
  237. public void NoSubViews_Does_Nothing_Vertical ()
  238. {
  239. var superView = new View
  240. {
  241. X = 0,
  242. Y = 0,
  243. Width = Dim.Auto (),
  244. Height = Dim.Auto (),
  245. TextDirection = TextDirection.TopBottom_LeftRight,
  246. ValidatePosDim = true
  247. };
  248. superView.BeginInit ();
  249. superView.EndInit ();
  250. superView.SetRelativeLayout (new (10, 10));
  251. Assert.Equal (new Rectangle (0, 0, 0, 0), superView.Frame);
  252. superView.SetRelativeLayout (new (10, 10));
  253. Assert.Equal (new Rectangle (0, 0, 0, 0), superView.Frame);
  254. }
  255. [Theory]
  256. [InlineData (0, 0, 0, 0, 0, 0)]
  257. [InlineData (0, 0, 5, 0, 5, 0)]
  258. [InlineData (0, 0, 0, 5, 0, 5)]
  259. [InlineData (0, 0, 5, 5, 5, 5)]
  260. [InlineData (1, 0, 5, 0, 6, 0)]
  261. [InlineData (1, 0, 0, 5, 1, 5)]
  262. [InlineData (1, 0, 5, 5, 6, 5)]
  263. [InlineData (1, 1, 5, 5, 6, 6)]
  264. [InlineData (-1, 0, 5, 0, 4, 0)]
  265. [InlineData (-1, 0, 0, 5, 0, 5)]
  266. [InlineData (-1, 0, 5, 5, 4, 5)]
  267. [InlineData (-1, -1, 5, 5, 4, 4)]
  268. public void SubView_Changes_SuperView_Size (int subX, int subY, int subWidth, int subHeight, int expectedWidth, int expectedHeight)
  269. {
  270. var superView = new View
  271. {
  272. X = 0,
  273. Y = 0,
  274. Width = Dim.Auto (),
  275. Height = Dim.Auto (),
  276. ValidatePosDim = true
  277. };
  278. var subView = new View
  279. {
  280. X = subX,
  281. Y = subY,
  282. Width = subWidth,
  283. Height = subHeight,
  284. ValidatePosDim = true
  285. };
  286. superView.Add (subView);
  287. superView.BeginInit ();
  288. superView.EndInit ();
  289. superView.SetRelativeLayout (new (10, 10));
  290. Assert.Equal (new Rectangle (0, 0, expectedWidth, expectedHeight), superView.Frame);
  291. }
  292. // Test validation
  293. [Fact]
  294. public void ValidatePosDim_True_Throws_When_SubView_Uses_SuperView_Dims ()
  295. {
  296. var superView = new View
  297. {
  298. X = 0,
  299. Y = 0,
  300. Width = Dim.Auto (),
  301. Height = Dim.Auto (),
  302. ValidatePosDim = true
  303. };
  304. var subView = new View
  305. {
  306. X = 0,
  307. Y = 0,
  308. Width = Dim.Fill (),
  309. Height = 10,
  310. ValidatePosDim = true
  311. };
  312. superView.BeginInit ();
  313. superView.EndInit ();
  314. superView.Add (subView);
  315. subView.Width = 10;
  316. superView.Add (subView);
  317. superView.SetRelativeLayout (new (10, 10));
  318. superView.LayoutSubviews (); // no throw
  319. subView.Width = Dim.Fill ();
  320. superView.SetRelativeLayout (new (0, 0));
  321. subView.Width = 10;
  322. subView.Height = Dim.Fill ();
  323. superView.SetRelativeLayout (new (0, 0));
  324. subView.Height = 10;
  325. subView.Height = Dim.Percent (50);
  326. Assert.Throws<InvalidOperationException> (() => superView.SetRelativeLayout (new (0, 0)));
  327. subView.Height = 10;
  328. subView.X = Pos.Center ();
  329. Assert.Throws<InvalidOperationException> (() => superView.SetRelativeLayout (new (0, 0)));
  330. subView.X = 0;
  331. subView.Y = Pos.Center ();
  332. Assert.Throws<InvalidOperationException> (() => superView.SetRelativeLayout (new (0, 0)));
  333. subView.Y = 0;
  334. subView.Width = 10;
  335. subView.Height = 10;
  336. subView.X = 0;
  337. subView.Y = 0;
  338. superView.SetRelativeLayout (new (0, 0));
  339. superView.LayoutSubviews ();
  340. }
  341. // Test validation
  342. [Fact]
  343. public void ValidatePosDim_True_Throws_When_SubView_Uses_SuperView_Dims_Combine ()
  344. {
  345. var superView = new View
  346. {
  347. X = 0,
  348. Y = 0,
  349. Width = Dim.Auto (),
  350. Height = Dim.Auto (),
  351. ValidatePosDim = true
  352. };
  353. var subView = new View
  354. {
  355. X = 0,
  356. Y = 0,
  357. Width = 10,
  358. Height = 10
  359. };
  360. var subView2 = new View
  361. {
  362. X = 0,
  363. Y = 0,
  364. Width = 10,
  365. Height = 10
  366. };
  367. superView.Add (subView, subView2);
  368. superView.BeginInit ();
  369. superView.EndInit ();
  370. superView.SetRelativeLayout (new (0, 0));
  371. superView.LayoutSubviews (); // no throw
  372. subView.Height = Dim.Fill () + 3;
  373. superView.SetRelativeLayout (new (0, 0));
  374. subView.Height = 0;
  375. subView.Height = 3 + Dim.Fill ();
  376. superView.SetRelativeLayout (new (0, 0));
  377. subView.Height = 0;
  378. subView.Height = 3 + 5 + Dim.Fill ();
  379. superView.SetRelativeLayout (new (0, 0));
  380. subView.Height = 0;
  381. subView.Height = 3 + 5 + Dim.Percent (10);
  382. Assert.Throws<InvalidOperationException> (() => superView.SetRelativeLayout (new (0, 0)));
  383. subView.Height = 0;
  384. // Tests nested Combine
  385. subView.Height = 5 + new DimCombine (AddOrSubtract.Add, 3, new DimCombine (AddOrSubtract.Add, Dim.Percent (10), 9));
  386. Assert.Throws<InvalidOperationException> (() => superView.SetRelativeLayout (new (0, 0)));
  387. }
  388. [Fact]
  389. public void ValidatePosDim_True_Throws_When_SubView_Uses_SuperView_Pos_Combine ()
  390. {
  391. var superView = new View
  392. {
  393. X = 0,
  394. Y = 0,
  395. Width = Dim.Auto (),
  396. Height = Dim.Auto (),
  397. ValidatePosDim = true
  398. };
  399. var subView = new View
  400. {
  401. X = 0,
  402. Y = 0,
  403. Width = 10,
  404. Height = 10
  405. };
  406. var subView2 = new View
  407. {
  408. X = 0,
  409. Y = 0,
  410. Width = 10,
  411. Height = 10
  412. };
  413. superView.Add (subView, subView2);
  414. superView.BeginInit ();
  415. superView.EndInit ();
  416. superView.SetRelativeLayout (new (0, 0));
  417. superView.LayoutSubviews (); // no throw
  418. subView.X = Pos.Right (subView2);
  419. superView.SetRelativeLayout (new (0, 0));
  420. superView.LayoutSubviews (); // no throw
  421. subView.X = Pos.Right (subView2) + 3;
  422. superView.SetRelativeLayout (new (0, 0)); // no throw
  423. superView.LayoutSubviews (); // no throw
  424. subView.X = new PosCombine (AddOrSubtract.Add, Pos.Right (subView2), new PosCombine (AddOrSubtract.Add, 7, 9));
  425. superView.SetRelativeLayout (new (0, 0)); // no throw
  426. subView.X = Pos.Center () + 3;
  427. Assert.Throws<InvalidOperationException> (() => superView.SetRelativeLayout (new (0, 0)));
  428. subView.X = 0;
  429. subView.X = 3 + Pos.Center ();
  430. Assert.Throws<InvalidOperationException> (() => superView.SetRelativeLayout (new (0, 0)));
  431. subView.X = 0;
  432. subView.X = 3 + 5 + Pos.Center ();
  433. Assert.Throws<InvalidOperationException> (() => superView.SetRelativeLayout (new (0, 0)));
  434. subView.X = 0;
  435. subView.X = 3 + 5 + Pos.Percent (10);
  436. Assert.Throws<InvalidOperationException> (() => superView.SetRelativeLayout (new (0, 0)));
  437. subView.X = 0;
  438. subView.X = Pos.Percent (10) + Pos.Center ();
  439. Assert.Throws<InvalidOperationException> (() => superView.SetRelativeLayout (new (0, 0)));
  440. subView.X = 0;
  441. // Tests nested Combine
  442. subView.X = 5 + new PosCombine (AddOrSubtract.Add, Pos.Right (subView2), new PosCombine (AddOrSubtract.Add, Pos.Center (), 9));
  443. Assert.Throws<InvalidOperationException> (() => superView.SetRelativeLayout (new (0, 0)));
  444. subView.X = 0;
  445. }
  446. [Theory]
  447. [InlineData (0, 0, 0, 0, 0)]
  448. [InlineData (0, 0, 5, 0, 5)]
  449. [InlineData (0, 0, 0, 5, 0)]
  450. [InlineData (0, 0, 5, 5, 5)]
  451. [InlineData (1, 0, 5, 0, 6)]
  452. [InlineData (1, 0, 0, 5, 1)]
  453. [InlineData (1, 0, 5, 5, 6)]
  454. [InlineData (1, 1, 5, 5, 6)]
  455. [InlineData (-1, 0, 5, 0, 4)]
  456. [InlineData (-1, 0, 0, 5, 0)]
  457. [InlineData (-1, 0, 5, 5, 4)]
  458. [InlineData (-1, -1, 5, 5, 4)]
  459. public void Width_Auto_Height_Absolute_NotChanged (int subX, int subY, int subWidth, int subHeight, int expectedWidth)
  460. {
  461. var superView = new View
  462. {
  463. X = 0,
  464. Y = 0,
  465. Width = Dim.Auto (),
  466. Height = 10,
  467. ValidatePosDim = true
  468. };
  469. var subView = new View
  470. {
  471. X = subX,
  472. Y = subY,
  473. Width = subWidth,
  474. Height = subHeight,
  475. ValidatePosDim = true
  476. };
  477. superView.Add (subView);
  478. superView.BeginInit ();
  479. superView.EndInit ();
  480. superView.SetRelativeLayout (new (10, 10));
  481. Assert.Equal (new Rectangle (0, 0, expectedWidth, 10), superView.Frame);
  482. }
  483. // Test that when a view has Width set to DimAuto (min: x)
  484. // the width is never < x even if SetRelativeLayout is called with smaller bounds
  485. [Theory]
  486. [InlineData (0, 0)]
  487. [InlineData (1, 1)]
  488. [InlineData (3, 3)]
  489. [InlineData (4, 4)]
  490. [InlineData (5, 5)] // No reason why it can exceed container
  491. public void Width_Auto_Min_Honored (int min, int expectedWidth)
  492. {
  493. var superView = new View
  494. {
  495. X = 0,
  496. Y = 0,
  497. Width = Dim.Auto (minimumContentDim: min),
  498. Height = 1,
  499. ValidatePosDim = true
  500. };
  501. superView.BeginInit ();
  502. superView.EndInit ();
  503. superView.SetRelativeLayout (new (4, 1));
  504. Assert.Equal (expectedWidth, superView.Frame.Width);
  505. }
  506. //// Test Dim.Fill - Fill should not impact width of the DimAuto superview
  507. //[Theory]
  508. //[InlineData (0, 0, 0, 10, 10)]
  509. //[InlineData (0, 1, 0, 10, 10)]
  510. //[InlineData (0, 11, 0, 10, 10)]
  511. //[InlineData (0, 10, 0, 10, 10)]
  512. //[InlineData (0, 5, 0, 10, 10)]
  513. //[InlineData (1, 5, 0, 10, 9)]
  514. //[InlineData (1, 10, 0, 10, 9)]
  515. //[InlineData (0, 0, 1, 10, 9)]
  516. //[InlineData (0, 10, 1, 10, 9)]
  517. //[InlineData (0, 5, 1, 10, 9)]
  518. //[InlineData (1, 5, 1, 10, 8)]
  519. //[InlineData (1, 10, 1, 10, 8)]
  520. //public void Width_Fill_Fills (int subX, int superMinWidth, int fill, int expectedSuperWidth, int expectedSubWidth)
  521. //{
  522. // var superView = new View
  523. // {
  524. // X = 0,
  525. // Y = 0,
  526. // Width = Dim.Auto (minimumContentDim: superMinWidth),
  527. // Height = 1,
  528. // ValidatePosDim = true
  529. // };
  530. // var subView = new View
  531. // {
  532. // X = subX,
  533. // Y = 0,
  534. // Width = Dim.Fill (fill),
  535. // Height = 1,
  536. // ValidatePosDim = true
  537. // };
  538. // superView.Add (subView);
  539. // superView.BeginInit ();
  540. // superView.EndInit ();
  541. // superView.SetRelativeLayout (new (10, 1));
  542. // Assert.Equal (expectedSuperWidth, superView.Frame.Width);
  543. // superView.LayoutSubviews ();
  544. // Assert.Equal (expectedSubWidth, subView.Frame.Width);
  545. // Assert.Equal (expectedSuperWidth, superView.Frame.Width);
  546. //}
  547. [Theory]
  548. [InlineData (0, 1, 1)]
  549. [InlineData (1, 1, 1)]
  550. [InlineData (9, 1, 1)]
  551. [InlineData (10, 1, 1)]
  552. [InlineData (0, 10, 10)]
  553. [InlineData (1, 10, 10)]
  554. [InlineData (9, 10, 10)]
  555. [InlineData (10, 10, 10)]
  556. public void Width_Auto_Text_Does_Not_Constrain_To_SuperView (int subX, int textLen, int expectedSubWidth)
  557. {
  558. var superView = new View
  559. {
  560. X = 0,
  561. Y = 0,
  562. Width = 10,
  563. Height = 1,
  564. ValidatePosDim = true
  565. };
  566. var subView = new View
  567. {
  568. Text = new string ('*', textLen),
  569. X = subX,
  570. Y = 0,
  571. Width = Dim.Auto (DimAutoStyle.Text),
  572. Height = 1,
  573. ValidatePosDim = true
  574. };
  575. superView.Add (subView);
  576. superView.BeginInit ();
  577. superView.EndInit ();
  578. superView.SetRelativeLayout (superView.GetContentSize ());
  579. superView.LayoutSubviews ();
  580. Assert.Equal (expectedSubWidth, subView.Frame.Width);
  581. }
  582. [Theory]
  583. [InlineData (0, 1, 1)]
  584. [InlineData (1, 1, 1)]
  585. [InlineData (9, 1, 1)]
  586. [InlineData (10, 1, 1)]
  587. [InlineData (0, 10, 10)]
  588. [InlineData (1, 10, 10)]
  589. [InlineData (9, 10, 10)]
  590. [InlineData (10, 10, 10)]
  591. public void Width_Auto_Subviews_Does_Not_Constrain_To_SuperView (int subX, int subSubViewWidth, int expectedSubWidth)
  592. {
  593. var superView = new View
  594. {
  595. X = 0,
  596. Y = 0,
  597. Width = 10,
  598. Height = 1,
  599. ValidatePosDim = true
  600. };
  601. var subView = new View
  602. {
  603. X = subX,
  604. Y = 0,
  605. Width = Dim.Auto (DimAutoStyle.Content),
  606. Height = 1,
  607. ValidatePosDim = true
  608. };
  609. var subSubView = new View
  610. {
  611. X = 0,
  612. Y = 0,
  613. Width = subSubViewWidth,
  614. Height = 1,
  615. ValidatePosDim = true
  616. };
  617. subView.Add (subSubView);
  618. superView.Add (subView);
  619. superView.BeginInit ();
  620. superView.EndInit ();
  621. superView.SetRelativeLayout (superView.GetContentSize ());
  622. superView.LayoutSubviews ();
  623. Assert.Equal (expectedSubWidth, subView.Frame.Width);
  624. }
  625. [Fact]
  626. public void DimAutoStyle_Text_Viewport_Stays_Set ()
  627. {
  628. var super = new View ()
  629. {
  630. Width = Dim.Fill (),
  631. Height = Dim.Fill ()
  632. };
  633. var view = new View ()
  634. {
  635. Text = "01234567",
  636. Width = Auto (DimAutoStyle.Text),
  637. Height = Auto (DimAutoStyle.Text),
  638. };
  639. super.Add (view);
  640. Rectangle expectedViewport = new (0, 0, 8, 1);
  641. Assert.Equal (expectedViewport.Size, view.GetContentSize ());
  642. Assert.Equal (expectedViewport, view.Frame);
  643. Assert.Equal (expectedViewport, view.Viewport);
  644. super.LayoutSubviews ();
  645. Assert.Equal (expectedViewport, view.Viewport);
  646. super.Dispose ();
  647. }
  648. // TextFormatter.Size normally tracks ContentSize, but with DimAuto, tracks the text size
  649. [Theory]
  650. [InlineData ("", 0, 0)]
  651. [InlineData (" ", 1, 1)]
  652. [InlineData ("01234", 5, 1)]
  653. public void DimAutoStyle_Text_TextFormatter_Size_Ignores_ContentSize (string text, int expectedW, int expectedH)
  654. {
  655. var view = new View ();
  656. view.Width = Auto (DimAutoStyle.Text);
  657. view.Height = Auto (DimAutoStyle.Text);
  658. view.SetContentSize (new (1, 1));
  659. view.Text = text;
  660. Assert.Equal (new (expectedW, expectedH), view.TextFormatter.Size);
  661. }
  662. // Test that changing TextFormatter does not impact View dimensions if Dim.Auto is not in play
  663. [Fact]
  664. public void Not_Used_TextFormatter_Does_Not_Change_View_Size ()
  665. {
  666. View view = new ()
  667. {
  668. Text = "_1234"
  669. };
  670. Assert.False (view.TextFormatter.AutoSize);
  671. Assert.Equal (Size.Empty, view.Frame.Size);
  672. view.TextFormatter.Text = "ABC";
  673. Assert.False (view.TextFormatter.AutoSize);
  674. Assert.Equal (Size.Empty, view.Frame.Size);
  675. view.TextFormatter.Alignment = Alignment.Fill;
  676. Assert.False (view.TextFormatter.AutoSize);
  677. Assert.Equal (Size.Empty, view.Frame.Size);
  678. view.TextFormatter.VerticalAlignment = Alignment.Center;
  679. Assert.False (view.TextFormatter.AutoSize);
  680. Assert.Equal (Size.Empty, view.Frame.Size);
  681. view.TextFormatter.HotKeySpecifier = (Rune)'*';
  682. Assert.False (view.TextFormatter.AutoSize);
  683. Assert.Equal (Size.Empty, view.Frame.Size);
  684. view.TextFormatter.Text = "*ABC";
  685. Assert.False (view.TextFormatter.AutoSize);
  686. Assert.Equal (Size.Empty, view.Frame.Size);
  687. }
  688. [Fact]
  689. public void Not_Used_TextSettings_Do_Not_Change_View_Size ()
  690. {
  691. View view = new ()
  692. {
  693. Text = "_1234"
  694. };
  695. Assert.False (view.TextFormatter.AutoSize);
  696. Assert.Equal (Size.Empty, view.Frame.Size);
  697. view.TextAlignment = Alignment.Fill;
  698. Assert.False (view.TextFormatter.AutoSize);
  699. Assert.Equal (Size.Empty, view.Frame.Size);
  700. view.VerticalTextAlignment = Alignment.Center;
  701. Assert.False (view.TextFormatter.AutoSize);
  702. Assert.Equal (Size.Empty, view.Frame.Size);
  703. view.HotKeySpecifier = (Rune)'*';
  704. Assert.False (view.TextFormatter.AutoSize);
  705. Assert.Equal (Size.Empty, view.Frame.Size);
  706. view.Text = "*ABC";
  707. Assert.False (view.TextFormatter.AutoSize);
  708. Assert.Equal (Size.Empty, view.Frame.Size);
  709. }
  710. [Fact]
  711. public void TextFormatter_Settings_Change_View_Size ()
  712. {
  713. View view = new ()
  714. {
  715. Text = "_1234",
  716. Width = Dim.Auto ()
  717. };
  718. Assert.False (view.TextFormatter.AutoSize);
  719. Assert.NotEqual (Size.Empty, view.Frame.Size);
  720. view.TextAlignment = Alignment.Fill;
  721. Assert.False (view.TextFormatter.AutoSize);
  722. Assert.NotEqual (Size.Empty, view.Frame.Size);
  723. view = new ()
  724. {
  725. Text = "_1234",
  726. Width = Dim.Auto ()
  727. };
  728. view.VerticalTextAlignment = Alignment.Center;
  729. Assert.False (view.TextFormatter.AutoSize);
  730. Assert.NotEqual (Size.Empty, view.Frame.Size);
  731. view = new ()
  732. {
  733. Text = "_1234",
  734. Width = Dim.Auto ()
  735. };
  736. view.HotKeySpecifier = (Rune)'*';
  737. Assert.False (view.TextFormatter.AutoSize);
  738. Assert.NotEqual (Size.Empty, view.Frame.Size);
  739. view = new ()
  740. {
  741. Text = "_1234",
  742. Width = Dim.Auto ()
  743. };
  744. view.Text = "*ABC";
  745. Assert.False (view.TextFormatter.AutoSize);
  746. Assert.NotEqual (Size.Empty, view.Frame.Size);
  747. }
  748. // Ensure TextFormatter.AutoSize is never used for View.Text
  749. [Fact]
  750. public void TextFormatter_Is_Not_Auto ()
  751. {
  752. View view = new ();
  753. Assert.False (view.TextFormatter.AutoSize);
  754. view.Width = Dim.Auto ();
  755. Assert.False (view.TextFormatter.AutoSize);
  756. view = new ();
  757. Assert.False (view.TextFormatter.AutoSize);
  758. view.Height = Dim.Auto ();
  759. Assert.False (view.TextFormatter.AutoSize);
  760. }
  761. [Theory]
  762. [InlineData ("1234", 4)]
  763. [InlineData ("_1234", 4)]
  764. public void Width_Auto_HotKey_TextFormatter_Size_Correct (string text, int expected)
  765. {
  766. View view = new ()
  767. {
  768. Text = text,
  769. Height = 1,
  770. Width = Dim.Auto ()
  771. };
  772. Assert.Equal (new (expected, 1), view.TextFormatter.Size);
  773. }
  774. [Theory]
  775. [InlineData ("1234", 4)]
  776. [InlineData ("_1234", 4)]
  777. public void Height_Auto_HotKey_TextFormatter_Size_Correct (string text, int expected)
  778. {
  779. View view = new ()
  780. {
  781. HotKeySpecifier = (Rune)'_',
  782. Text = text,
  783. Width = Auto (),
  784. Height = 1,
  785. };
  786. Assert.Equal (new (expected, 1), view.TextFormatter.Size);
  787. view = new ()
  788. {
  789. HotKeySpecifier = (Rune)'_',
  790. TextDirection = TextDirection.TopBottom_LeftRight,
  791. Text = text,
  792. Width = 1,
  793. Height = Auto (),
  794. };
  795. Assert.Equal (new (1, expected), view.TextFormatter.Size);
  796. }
  797. [SetupFakeDriver]
  798. [Fact]
  799. public void Change_To_Non_Auto_Resets_ContentSize ()
  800. {
  801. View view = new ()
  802. {
  803. Width = Auto (),
  804. Height = Auto (),
  805. Text = "01234"
  806. };
  807. Assert.Equal (new Rectangle (0, 0, 5, 1), view.Frame);
  808. Assert.Equal (new Size (5, 1), view.GetContentSize ());
  809. // Change text to a longer string
  810. view.Text = "0123456789";
  811. Assert.Equal (new Rectangle (0, 0, 10, 1), view.Frame);
  812. Assert.Equal (new Size (10, 1), view.GetContentSize ());
  813. // If ContentSize was reset, these should cause it to update
  814. view.Width = 5;
  815. view.Height = 1;
  816. Assert.Equal (new Size (5, 1), view.GetContentSize ());
  817. }
  818. // DimAutoStyle.Content tests
  819. [Fact]
  820. public void DimAutoStyle_Content_UsesContentSize_WhenSet ()
  821. {
  822. var view = new View ();
  823. view.SetContentSize (new (10, 5));
  824. var dim = Dim.Auto (DimAutoStyle.Content);
  825. int calculatedWidth = dim.Calculate (0, 100, view, Dimension.Width);
  826. Assert.Equal (10, calculatedWidth);
  827. }
  828. [Fact]
  829. public void DimAutoStyle_Content_IgnoresSubviews_When_ContentSize_Is_Set ()
  830. {
  831. var view = new View ();
  832. var subview = new View ()
  833. {
  834. Frame = new Rectangle (50, 50, 1, 1)
  835. };
  836. view.SetContentSize (new (10, 5));
  837. var dim = Dim.Auto (DimAutoStyle.Content);
  838. int calculatedWidth = dim.Calculate (0, 100, view, Dimension.Width);
  839. Assert.Equal (10, calculatedWidth);
  840. }
  841. [Fact]
  842. public void DimAutoStyle_Content_IgnoresText_WhenContentSizeNotSet ()
  843. {
  844. var view = new View () { Text = "This is a test" };
  845. var dim = Dim.Auto (DimAutoStyle.Content);
  846. int calculatedWidth = dim.Calculate (0, 100, view, Dimension.Width);
  847. Assert.Equal (0, calculatedWidth); // Assuming 0 is the default when no ContentSize or Subviews are set
  848. }
  849. [Fact]
  850. public void DimAutoStyle_Content_UsesLargestSubview_WhenContentSizeNotSet ()
  851. {
  852. var view = new View ();
  853. view.Add (new View () { Frame = new Rectangle (0, 0, 5, 5) }); // Smaller subview
  854. view.Add (new View () { Frame = new Rectangle (0, 0, 10, 10) }); // Larger subview
  855. var dim = Dim.Auto (DimAutoStyle.Content);
  856. int calculatedWidth = dim.Calculate (0, 100, view, Dimension.Width);
  857. Assert.Equal (10, calculatedWidth); // Expecting the size of the largest subview
  858. }
  859. // All the Dim types
  860. [Theory]
  861. [InlineData (0, 15, 15)]
  862. [InlineData (1, 15, 16)]
  863. [InlineData (-1, 15, 14)]
  864. public void With_Subview_Using_DimAbsolute (int subViewOffset, int dimAbsoluteSize, int expectedSize)
  865. {
  866. var view = new View ();
  867. var subview = new View ()
  868. {
  869. X = subViewOffset,
  870. Y = subViewOffset,
  871. Width = Dim.Absolute (dimAbsoluteSize),
  872. Height = Dim.Absolute (dimAbsoluteSize)
  873. };
  874. view.Add (subview);
  875. var dim = Dim.Auto (DimAutoStyle.Content);
  876. int calculatedWidth = dim.Calculate (0, 100, view, Dimension.Width);
  877. int calculatedHeight = dim.Calculate (0, 100, view, Dimension.Height);
  878. Assert.Equal (expectedSize, calculatedWidth);
  879. Assert.Equal (expectedSize, calculatedHeight);
  880. }
  881. [Theory]
  882. [InlineData (0, 50, 50)]
  883. [InlineData (1, 50, 51)]
  884. [InlineData (0, 25, 25)]
  885. [InlineData (-1, 50, 49)]
  886. public void With_Subview_Using_DimPercent (int subViewOffset, int percent, int expectedSize)
  887. {
  888. var view = new View ()
  889. {
  890. Width = 100, Height = 100
  891. };
  892. var subview = new View ()
  893. {
  894. X = subViewOffset,
  895. Y = subViewOffset,
  896. Width = Dim.Percent (percent),
  897. Height = Dim.Percent (percent)
  898. };
  899. view.Add (subview);
  900. view.BeginInit ();
  901. view.EndInit ();
  902. // Assuming the calculation is done after layout
  903. int calculatedX = subview.X.Calculate (100, subview.Width, subview, Dimension.Width);
  904. int calculatedY = subview.Y.Calculate (100, subview.Height, subview, Dimension.Height);
  905. int calculatedWidth = subview.Width.Calculate (0, 100, view, Dimension.Width);
  906. int calculatedHeight = subview.Height.Calculate (0, 100, view, Dimension.Height);
  907. Assert.Equal (20, calculatedWidth); // subview's width
  908. Assert.Equal (10, calculatedHeight); // subview's height
  909. Assert.Equal (50, calculatedX); // 50% of 100 (Width)
  910. Assert.Equal (50, calculatedY); // 50% of 100 (Height)
  911. }
  912. [Theory]
  913. [InlineData (0, 0, 100)]
  914. [InlineData (1, 0, 100)]
  915. [InlineData (0, 1, 100)]
  916. [InlineData (1, 1, 100)]
  917. public void With_Subview_Using_DimFill (int subViewOffset, int dimFillMargin, int expectedSize)
  918. {
  919. // BUGBUG: THis test is totally bogus. Dim.Fill isnot working right yet.
  920. var view = new View ()
  921. {
  922. Width = Dim.Auto (DimAutoStyle.Content, minimumContentDim: 100, maximumContentDim: 100),
  923. Height = Dim.Auto (DimAutoStyle.Content, minimumContentDim: 100, maximumContentDim: 100),
  924. };
  925. var subview = new View ()
  926. {
  927. X = subViewOffset,
  928. Y = subViewOffset,
  929. Width = Dim.Fill (dimFillMargin),
  930. Height = Dim.Fill (dimFillMargin)
  931. };
  932. view.Add (subview);
  933. //view.LayoutSubviews ();
  934. view.SetRelativeLayout (new (200, 200));
  935. Assert.Equal (expectedSize, view.Frame.Width);
  936. }
  937. [Fact]
  938. public void With_Subview_Using_DimFunc ()
  939. {
  940. var view = new View ();
  941. var subview = new View () { Width = Dim.Func (() => 20), Height = Dim.Func (() => 25) };
  942. view.Add (subview);
  943. subview.SetRelativeLayout (new (100, 100));
  944. var dim = Dim.Auto (DimAutoStyle.Content);
  945. int calculatedWidth = dim.Calculate (0, 100, view, Dimension.Width);
  946. int calculatedHeight = dim.Calculate (0, 100, view, Dimension.Height);
  947. Assert.Equal (20, calculatedWidth);
  948. Assert.Equal (25, calculatedHeight);
  949. }
  950. [Fact]
  951. public void With_Subview_Using_DimView ()
  952. {
  953. var view = new View ();
  954. var subview = new View () { Width = 30, Height = 40 };
  955. var subSubview = new View () { Width = Dim.Width (subview), Height = Dim.Height (subview) };
  956. view.Add (subview);
  957. view.Add (subSubview);
  958. subview.SetRelativeLayout (new (100, 100));
  959. var dim = Dim.Auto (DimAutoStyle.Content);
  960. int calculatedWidth = dim.Calculate (0, 100, view, Dimension.Width);
  961. int calculatedHeight = dim.Calculate (0, 100, view, Dimension.Height);
  962. // Expecting the size to match the subview, which is the largest
  963. Assert.Equal (30, calculatedWidth);
  964. Assert.Equal (40, calculatedHeight);
  965. }
  966. // Testing all Pos combinations
  967. [Theory]
  968. [InlineData (0, 0, 0, 0, 0, 0)]
  969. [InlineData (0, 19, 0, 9, 19, 9)]
  970. [InlineData (0, 20, 0, 10, 20, 10)]
  971. [InlineData (0, 21, 0, 11, 21, 11)]
  972. [InlineData (1, 21, 1, 11, 21, 11)]
  973. [InlineData (100, 21, 100, 11, 21, 11)]
  974. public void With_Subview_Using_PosAbsolute (int minWidth, int maxWidth, int minHeight, int maxHeight, int expectedWidth, int expectedHeight)
  975. {
  976. var view = new View ()
  977. {
  978. Width = Dim.Auto (minimumContentDim: minWidth, maximumContentDim: maxWidth),
  979. Height = Dim.Auto (minimumContentDim: minHeight, maximumContentDim: maxHeight)
  980. };
  981. var subview = new View ()
  982. {
  983. X = Pos.Absolute (10),
  984. Y = Pos.Absolute (5),
  985. Width = 20,
  986. Height = 10
  987. };
  988. view.Add (subview);
  989. // Assuming the calculation is done after layout
  990. int calculatedX = view.X.Calculate (100, view.Width, view, Dimension.Width);
  991. int calculatedY = view.Y.Calculate (100, view.Height, view, Dimension.Height);
  992. int calculatedWidth = view.Width.Calculate (0, 100, view, Dimension.Width);
  993. int calculatedHeight = view.Height.Calculate (0, 100, view, Dimension.Height);
  994. Assert.Equal (expectedWidth, calculatedWidth);
  995. Assert.Equal (expectedHeight, calculatedHeight);
  996. Assert.Equal (0, calculatedX);
  997. Assert.Equal (0, calculatedY);
  998. }
  999. [Theory]
  1000. [InlineData (0, 0, 0, 0, 0, 0)]
  1001. [InlineData (0, 19, 0, 9, 19, 9)]
  1002. [InlineData (0, 20, 0, 10, 20, 10)]
  1003. [InlineData (0, 21, 0, 11, 20, 10)]
  1004. [InlineData (1, 21, 1, 11, 20, 10)]
  1005. [InlineData (100, 21, 100, 11, 21, 11)]
  1006. public void With_Subview_Using_PosPercent (int minWidth, int maxWidth, int minHeight, int maxHeight, int expectedWidth, int expectedHeight)
  1007. {
  1008. var view = new View ()
  1009. {
  1010. Width = Dim.Auto (minimumContentDim: minWidth, maximumContentDim: maxWidth),
  1011. Height = Dim.Auto (minimumContentDim: minHeight, maximumContentDim: maxHeight)
  1012. };
  1013. var subview = new View ()
  1014. {
  1015. X = Pos.Percent (50),
  1016. Y = Pos.Percent (50),
  1017. Width = 20,
  1018. Height = 10
  1019. };
  1020. view.Add (subview);
  1021. // Assuming the calculation is done after layout
  1022. int calculatedX = view.X.Calculate (100, view.Width, view, Dimension.Width);
  1023. int calculatedY = view.Y.Calculate (100, view.Height, view, Dimension.Height);
  1024. int calculatedWidth = view.Width.Calculate (0, 100, view, Dimension.Width);
  1025. int calculatedHeight = view.Height.Calculate (0, 100, view, Dimension.Height);
  1026. Assert.Equal (expectedWidth, calculatedWidth);
  1027. Assert.Equal (expectedHeight, calculatedHeight);
  1028. Assert.Equal (0, calculatedX);
  1029. Assert.Equal (0, calculatedY);
  1030. view.BeginInit ();
  1031. view.EndInit ();
  1032. // subview should be at 50% in the parent view
  1033. Assert.Equal ((int)(view.Viewport.Width * .50), subview.Frame.X);
  1034. Assert.Equal ((int)(view.Viewport.Height * .50), subview.Frame.Y);
  1035. }
  1036. [Theory]
  1037. [InlineData (0, 0, 0, 0, 0, 0)]
  1038. [InlineData (0, 19, 0, 9, 19, 9)]
  1039. [InlineData (0, 20, 0, 10, 20, 10)]
  1040. [InlineData (0, 21, 0, 11, 21, 11)]
  1041. [InlineData (1, 21, 1, 11, 21, 11)]
  1042. [InlineData (100, 21, 100, 11, 21, 11)]
  1043. public void With_Subview_Using_PosPercent_Combine (int minWidth, int maxWidth, int minHeight, int maxHeight, int expectedWidth, int expectedHeight)
  1044. {
  1045. var view = new View ()
  1046. {
  1047. Width = Dim.Auto (minimumContentDim: minWidth, maximumContentDim: maxWidth),
  1048. Height = Dim.Auto (minimumContentDim: minHeight, maximumContentDim: maxHeight)
  1049. };
  1050. var subview = new View ()
  1051. {
  1052. X = Pos.Percent (50) + 1,
  1053. Y = 1 + Pos.Percent (50),
  1054. Width = 20,
  1055. Height = 10
  1056. };
  1057. view.Add (subview);
  1058. // Assuming the calculation is done after layout
  1059. int calculatedX = view.X.Calculate (100, view.Width, view, Dimension.Width);
  1060. int calculatedY = view.Y.Calculate (100, view.Height, view, Dimension.Height);
  1061. int calculatedWidth = view.Width.Calculate (0, 100, view, Dimension.Width);
  1062. int calculatedHeight = view.Height.Calculate (0, 100, view, Dimension.Height);
  1063. Assert.Equal (expectedWidth, calculatedWidth);
  1064. Assert.Equal (expectedHeight, calculatedHeight);
  1065. Assert.Equal (0, calculatedX);
  1066. Assert.Equal (0, calculatedY);
  1067. view.BeginInit ();
  1068. view.EndInit ();
  1069. // subview should be at 50% in the parent view
  1070. Assert.Equal ((int)(view.Viewport.Width * .50) + 1, subview.Frame.X);
  1071. Assert.Equal ((int)(view.Viewport.Height * .50) + 1, subview.Frame.Y);
  1072. }
  1073. [Theory]
  1074. [InlineData (0, 0, 0, 0, 0, 0)]
  1075. [InlineData (0, 19, 0, 9, 19, 9)]
  1076. [InlineData (0, 20, 0, 10, 20, 10)]
  1077. [InlineData (0, 21, 0, 11, 21, 11)]
  1078. [InlineData (1, 21, 1, 11, 21, 11)]
  1079. [InlineData (100, 21, 100, 11, 21, 11)]
  1080. public void With_Subview_Using_PosCenter (int minWidth, int maxWidth, int minHeight, int maxHeight, int expectedWidth, int expectedHeight)
  1081. {
  1082. var view = new View ()
  1083. {
  1084. Width = Dim.Auto (minimumContentDim: minWidth, maximumContentDim: maxWidth),
  1085. Height = Dim.Auto (minimumContentDim: minHeight, maximumContentDim: maxHeight)
  1086. };
  1087. var subview = new View ()
  1088. {
  1089. X = Pos.Center (),
  1090. Y = Pos.Center (),
  1091. Width = 20,
  1092. Height = 10
  1093. };
  1094. view.Add (subview);
  1095. // Assuming the calculation is done after layout
  1096. int calculatedX = view.X.Calculate (100, view.Width, view, Dimension.Width);
  1097. int calculatedY = view.Y.Calculate (100, view.Height, view, Dimension.Height);
  1098. int calculatedWidth = view.Width.Calculate (0, 100, view, Dimension.Width);
  1099. int calculatedHeight = view.Height.Calculate (0, 100, view, Dimension.Height);
  1100. Assert.Equal (expectedWidth, calculatedWidth);
  1101. Assert.Equal (expectedHeight, calculatedHeight);
  1102. Assert.Equal (0, calculatedX);
  1103. Assert.Equal (0, calculatedY);
  1104. view.BeginInit ();
  1105. view.EndInit ();
  1106. // subview should be centered in the parent view + 1
  1107. Assert.Equal (view.Viewport.Width / 2 - subview.Frame.Width / 2, subview.Frame.X);
  1108. Assert.Equal (view.Viewport.Height / 2 - subview.Frame.Height / 2, subview.Frame.Y);
  1109. }
  1110. [Theory]
  1111. [InlineData (0, 0, 0, 0, 0, 0)]
  1112. [InlineData (0, 19, 0, 9, 19, 9)]
  1113. [InlineData (0, 18, 0, 8, 18, 8)]
  1114. [InlineData (0, 20, 0, 10, 20, 10)]
  1115. [InlineData (0, 21, 0, 11, 21, 11)]
  1116. [InlineData (1, 21, 1, 11, 21, 11)]
  1117. [InlineData (100, 21, 100, 11, 21, 11)]
  1118. public void With_Subview_Using_PosCenter_Combine (int minWidth, int maxWidth, int minHeight, int maxHeight, int expectedWidth, int expectedHeight)
  1119. {
  1120. var view = new View ()
  1121. {
  1122. Width = Dim.Auto (minimumContentDim: minWidth, maximumContentDim: maxWidth),
  1123. Height = Dim.Auto (minimumContentDim: minHeight, maximumContentDim: maxHeight)
  1124. };
  1125. var subview = new View ()
  1126. {
  1127. X = Pos.Center () + 1,
  1128. Y = 1 + Pos.Center (),
  1129. Width = 20,
  1130. Height = 10
  1131. };
  1132. view.Add (subview);
  1133. // Assuming the calculation is done after layout
  1134. int calculatedX = view.X.Calculate (100, view.Width, view, Dimension.Width);
  1135. int calculatedY = view.Y.Calculate (100, view.Height, view, Dimension.Height);
  1136. int calculatedWidth = view.Width.Calculate (0, 100, view, Dimension.Width);
  1137. int calculatedHeight = view.Height.Calculate (0, 100, view, Dimension.Height);
  1138. Assert.Equal (expectedWidth, calculatedWidth);
  1139. Assert.Equal (expectedHeight, calculatedHeight);
  1140. Assert.Equal (0, calculatedX);
  1141. Assert.Equal (0, calculatedY);
  1142. view.BeginInit ();
  1143. view.EndInit ();
  1144. // subview should be centered in the parent view + 1
  1145. Assert.Equal (view.Viewport.Width / 2 - subview.Frame.Width / 2 + 1, subview.Frame.X);
  1146. Assert.Equal (view.Viewport.Height / 2 - subview.Frame.Height / 2 + 1, subview.Frame.Y);
  1147. }
  1148. [Theory]
  1149. [InlineData (0, 0, 0, 0, 0, 0)]
  1150. [InlineData (0, 19, 0, 9, 19, 9)]
  1151. [InlineData (0, 18, 0, 8, 18, 8)]
  1152. [InlineData (0, 20, 0, 10, 20, 10)]
  1153. [InlineData (0, 21, 0, 11, 20, 10)]
  1154. [InlineData (1, 21, 1, 11, 20, 10)]
  1155. [InlineData (100, 21, 100, 11, 21, 11)]
  1156. public void With_Subview_Using_PosAnchorEnd (int minWidth, int maxWidth, int minHeight, int maxHeight, int expectedWidth, int expectedHeight)
  1157. {
  1158. var view = new View ()
  1159. {
  1160. Width = Dim.Auto (minimumContentDim: minWidth, maximumContentDim: maxWidth),
  1161. Height = Dim.Auto (minimumContentDim: minHeight, maximumContentDim: maxHeight)
  1162. };
  1163. var subview = new View ()
  1164. {
  1165. X = Pos.AnchorEnd (),
  1166. Y = Pos.AnchorEnd (),
  1167. Width = 20,
  1168. Height = 10
  1169. };
  1170. view.Add (subview);
  1171. // Assuming the calculation is done after layout
  1172. int calculatedX = view.X.Calculate (100, view.Width, view, Dimension.Width);
  1173. int calculatedY = view.Y.Calculate (100, view.Height, view, Dimension.Height);
  1174. int calculatedWidth = view.Width.Calculate (0, 100, view, Dimension.Width);
  1175. int calculatedHeight = view.Height.Calculate (0, 100, view, Dimension.Height);
  1176. Assert.Equal (expectedWidth, calculatedWidth);
  1177. Assert.Equal (expectedHeight, calculatedHeight);
  1178. Assert.Equal (0, calculatedX);
  1179. Assert.Equal (0, calculatedY);
  1180. view.BeginInit ();
  1181. view.EndInit ();
  1182. // subview should be at the end of the view
  1183. Assert.Equal (view.Viewport.Width - subview.Frame.Width, subview.Frame.X);
  1184. Assert.Equal (view.Viewport.Height - subview.Frame.Height, subview.Frame.Y);
  1185. }
  1186. [Fact]
  1187. public void DimAutoStyle_Text_Pos_AnchorEnd_Locates_Correctly ()
  1188. {
  1189. DimAutoTestView view = new ("01234", Auto (DimAutoStyle.Text), Auto (DimAutoStyle.Text));
  1190. view.SetRelativeLayout (new (10, 10));
  1191. Assert.Equal (new (5, 1), view.Frame.Size);
  1192. Assert.Equal (new (0, 0), view.Frame.Location);
  1193. view.X = 0;
  1194. view.Y = Pos.AnchorEnd (1);
  1195. view.SetRelativeLayout (new (10, 10));
  1196. Assert.Equal (new (5, 1), view.Frame.Size);
  1197. Assert.Equal (new (0, 9), view.Frame.Location);
  1198. view.Y = Pos.AnchorEnd ();
  1199. view.SetRelativeLayout (new (10, 10));
  1200. Assert.Equal (new (5, 1), view.Frame.Size);
  1201. Assert.Equal (new (0, 9), view.Frame.Location);
  1202. view.Y = Pos.AnchorEnd () - 1;
  1203. view.SetRelativeLayout (new (10, 10));
  1204. Assert.Equal (new (5, 1), view.Frame.Size);
  1205. Assert.Equal (new (0, 8), view.Frame.Location);
  1206. }
  1207. [Fact]
  1208. public void DimAutoStyle_Content_Pos_AnchorEnd_Locates_Correctly ()
  1209. {
  1210. DimAutoTestView view = new (Auto (DimAutoStyle.Content), Auto (DimAutoStyle.Content));
  1211. View subView = new ()
  1212. {
  1213. Width = 5,
  1214. Height = 1
  1215. };
  1216. view.Add (subView);
  1217. view.SetRelativeLayout (new (10, 10));
  1218. Assert.Equal (new (5, 1), view.Frame.Size);
  1219. Assert.Equal (new (0, 0), view.Frame.Location);
  1220. view.X = 0;
  1221. view.Y = Pos.AnchorEnd (1);
  1222. view.SetRelativeLayout (new (10, 10));
  1223. Assert.Equal (new (5, 1), view.Frame.Size);
  1224. Assert.Equal (new (0, 9), view.Frame.Location);
  1225. view.Y = Pos.AnchorEnd ();
  1226. view.SetRelativeLayout (new (10, 10));
  1227. Assert.Equal (new (5, 1), view.Frame.Size);
  1228. Assert.Equal (new (0, 9), view.Frame.Location);
  1229. view.Y = Pos.AnchorEnd () - 1;
  1230. view.SetRelativeLayout (new (10, 10));
  1231. Assert.Equal (new (5, 1), view.Frame.Size);
  1232. Assert.Equal (new (0, 8), view.Frame.Location);
  1233. }
  1234. [Theory]
  1235. [InlineData ("01234", 5, 5)]
  1236. [InlineData ("01234", 6, 6)]
  1237. [InlineData ("01234", 4, 5)]
  1238. [InlineData ("01234", 0, 5)]
  1239. [InlineData ("", 5, 5)]
  1240. [InlineData ("", 0, 0)]
  1241. public void DimAutoStyle_Auto_Larger_Wins (string text, int dimension, int expected)
  1242. {
  1243. View view = new ()
  1244. {
  1245. Width = Auto (),
  1246. Text = text
  1247. };
  1248. View subView = new ()
  1249. {
  1250. Width = dimension,
  1251. };
  1252. view.Add (subView);
  1253. view.SetRelativeLayout (new (10, 10));
  1254. Assert.Equal (expected, view.Frame.Width);
  1255. }
  1256. [Fact]
  1257. public void DimAutoStyle_Content_UsesContentSize_If_No_Subviews ()
  1258. {
  1259. DimAutoTestView view = new (Auto (DimAutoStyle.Content), Auto (DimAutoStyle.Content));
  1260. view.SetContentSize (new (5, 5));
  1261. view.SetRelativeLayout (new (10, 10));
  1262. Assert.Equal (new (5, 5), view.Frame.Size);
  1263. }
  1264. // Test variations of Frame
  1265. }