Dim.AutoTests.cs 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034
  1. using System.Text;
  2. using Xunit.Abstractions;
  3. using static Terminal.Gui.Dim;
  4. namespace Terminal.Gui.LayoutTests;
  5. public partial class DimAutoTests (ITestOutputHelper output)
  6. {
  7. private readonly ITestOutputHelper _output = output;
  8. [SetupFakeDriver]
  9. [Fact]
  10. public void Change_To_Non_Auto_Resets_ContentSize ()
  11. {
  12. View view = new ()
  13. {
  14. Width = Auto (),
  15. Height = Auto (),
  16. Text = "01234"
  17. };
  18. view.SetRelativeLayout (new (100, 100));
  19. Assert.Equal (new (0, 0, 5, 1), view.Frame);
  20. Assert.Equal (new (5, 1), view.GetContentSize ());
  21. // Change text to a longer string
  22. view.Text = "0123456789";
  23. Assert.Equal (new (0, 0, 10, 1), view.Frame);
  24. Assert.Equal (new (10, 1), view.GetContentSize ());
  25. // If ContentSize was reset, these should cause it to update
  26. view.Width = 5;
  27. view.Height = 1;
  28. Assert.Equal (new (5, 1), view.GetContentSize ());
  29. }
  30. [Theory]
  31. [InlineData (0, 0, 0, 0, 0)]
  32. [InlineData (0, 0, 5, 0, 0)]
  33. [InlineData (0, 0, 0, 5, 5)]
  34. [InlineData (0, 0, 5, 5, 5)]
  35. [InlineData (1, 0, 5, 0, 0)]
  36. [InlineData (1, 0, 0, 5, 5)]
  37. [InlineData (1, 0, 5, 5, 5)]
  38. [InlineData (1, 1, 5, 5, 6)]
  39. [InlineData (-1, 0, 5, 0, 0)]
  40. [InlineData (-1, 0, 0, 5, 5)]
  41. [InlineData (-1, 0, 5, 5, 5)]
  42. [InlineData (-1, -1, 5, 5, 4)]
  43. public void Height_Auto_Width_Absolute_NotChanged (int subX, int subY, int subWidth, int subHeight, int expectedHeight)
  44. {
  45. var superView = new View
  46. {
  47. X = 0,
  48. Y = 0,
  49. Width = 10,
  50. Height = Auto (),
  51. ValidatePosDim = true
  52. };
  53. var subView = new View
  54. {
  55. X = subX,
  56. Y = subY,
  57. Width = subWidth,
  58. Height = subHeight,
  59. ValidatePosDim = true
  60. };
  61. superView.Add (subView);
  62. superView.BeginInit ();
  63. superView.EndInit ();
  64. superView.SetRelativeLayout (new (10, 10));
  65. Assert.Equal (new (0, 0, 10, expectedHeight), superView.Frame);
  66. }
  67. [Theory]
  68. [CombinatorialData]
  69. public void HotKey_TextFormatter_Height_Correct ([CombinatorialValues ("1234", "_1234", "1_234", "____")] string text)
  70. {
  71. View view = new ()
  72. {
  73. HotKeySpecifier = (Rune)'_',
  74. Text = text,
  75. Width = Auto (),
  76. Height = 1
  77. };
  78. Assert.Equal (4, view.TextFormatter.ConstrainToWidth);
  79. Assert.Equal (1, view.TextFormatter.ConstrainToHeight);
  80. view = new ()
  81. {
  82. HotKeySpecifier = (Rune)'_',
  83. TextDirection = TextDirection.TopBottom_LeftRight,
  84. Text = text,
  85. Width = 1,
  86. Height = Auto ()
  87. };
  88. Assert.Equal (1, view.TextFormatter.ConstrainToWidth);
  89. Assert.Equal (4, view.TextFormatter.ConstrainToHeight);
  90. }
  91. [Theory]
  92. [CombinatorialData]
  93. public void HotKey_TextFormatter_Width_Correct ([CombinatorialValues ("1234", "_1234", "1_234", "____")] string text)
  94. {
  95. View view = new ()
  96. {
  97. Text = text,
  98. Height = 1,
  99. Width = Auto ()
  100. };
  101. Assert.Equal (4, view.TextFormatter.ConstrainToWidth);
  102. Assert.Equal (1, view.TextFormatter.ConstrainToHeight);
  103. }
  104. [Fact]
  105. public void NoSubViews_Does_Nothing ()
  106. {
  107. var superView = new View
  108. {
  109. X = 0,
  110. Y = 0,
  111. Width = Auto (),
  112. Height = Auto (),
  113. ValidatePosDim = true
  114. };
  115. superView.BeginInit ();
  116. superView.EndInit ();
  117. superView.SetRelativeLayout (new (10, 10));
  118. Assert.Equal (new (0, 0, 0, 0), superView.Frame);
  119. superView.SetRelativeLayout (new (10, 10));
  120. Assert.Equal (new (0, 0, 0, 0), superView.Frame);
  121. }
  122. [Fact]
  123. public void NoSubViews_Does_Nothing_Vertical ()
  124. {
  125. var superView = new View
  126. {
  127. X = 0,
  128. Y = 0,
  129. Width = Auto (),
  130. Height = Auto (),
  131. TextDirection = TextDirection.TopBottom_LeftRight,
  132. ValidatePosDim = true
  133. };
  134. superView.BeginInit ();
  135. superView.EndInit ();
  136. superView.SetRelativeLayout (new (10, 10));
  137. Assert.Equal (new (0, 0, 0, 0), superView.Frame);
  138. superView.SetRelativeLayout (new (10, 10));
  139. Assert.Equal (new (0, 0, 0, 0), superView.Frame);
  140. }
  141. [Theory]
  142. [InlineData (0, 0, 0, 0, 0, 0)]
  143. [InlineData (0, 0, 5, 0, 5, 0)]
  144. [InlineData (0, 0, 0, 5, 0, 5)]
  145. [InlineData (0, 0, 5, 5, 5, 5)]
  146. [InlineData (1, 0, 5, 0, 6, 0)]
  147. [InlineData (1, 0, 0, 5, 1, 5)]
  148. [InlineData (1, 0, 5, 5, 6, 5)]
  149. [InlineData (1, 1, 5, 5, 6, 6)]
  150. [InlineData (-1, 0, 5, 0, 4, 0)]
  151. [InlineData (-1, 0, 0, 5, 0, 5)]
  152. [InlineData (-1, 0, 5, 5, 4, 5)]
  153. [InlineData (-1, -1, 5, 5, 4, 4)]
  154. public void SubView_Changes_SuperView_Size (int subX, int subY, int subWidth, int subHeight, int expectedWidth, int expectedHeight)
  155. {
  156. var superView = new View
  157. {
  158. X = 0,
  159. Y = 0,
  160. Width = Auto (),
  161. Height = Auto (),
  162. ValidatePosDim = true
  163. };
  164. var subView = new View
  165. {
  166. X = subX,
  167. Y = subY,
  168. Width = subWidth,
  169. Height = subHeight,
  170. ValidatePosDim = true
  171. };
  172. superView.Add (subView);
  173. superView.BeginInit ();
  174. superView.EndInit ();
  175. superView.SetRelativeLayout (new (10, 10));
  176. Assert.Equal (new (0, 0, expectedWidth, expectedHeight), superView.Frame);
  177. }
  178. [Fact]
  179. public void TestEquality ()
  180. {
  181. var a = new DimAuto
  182. {
  183. MaximumContentDim = null,
  184. MinimumContentDim = 1,
  185. Style = DimAutoStyle.Auto
  186. };
  187. var b = new DimAuto
  188. {
  189. MaximumContentDim = null,
  190. MinimumContentDim = 1,
  191. Style = DimAutoStyle.Auto
  192. };
  193. var c = new DimAuto
  194. {
  195. MaximumContentDim = 2,
  196. MinimumContentDim = 1,
  197. Style = DimAutoStyle.Auto
  198. };
  199. var d = new DimAuto
  200. {
  201. MaximumContentDim = null,
  202. MinimumContentDim = 1,
  203. Style = DimAutoStyle.Content
  204. };
  205. var e = new DimAuto
  206. {
  207. MaximumContentDim = null,
  208. MinimumContentDim = 2,
  209. Style = DimAutoStyle.Auto
  210. };
  211. // Test equality with same values
  212. Assert.True (a.Equals (b));
  213. Assert.True (a.GetHashCode () == b.GetHashCode ());
  214. // Test inequality with different MaximumContentDim
  215. Assert.False (a.Equals (c));
  216. Assert.False (a.GetHashCode () == c.GetHashCode ());
  217. // Test inequality with different Style
  218. Assert.False (a.Equals (d));
  219. Assert.False (a.GetHashCode () == d.GetHashCode ());
  220. // Test inequality with different MinimumContentDim
  221. Assert.False (a.Equals (e));
  222. Assert.False (a.GetHashCode () == e.GetHashCode ());
  223. // Test equality with self
  224. Assert.True (a.Equals (a));
  225. Assert.True (a.GetHashCode () == a.GetHashCode ());
  226. // Test inequality with null
  227. Assert.False (a.Equals (null));
  228. }
  229. [Fact]
  230. public void TestEquality_Simple ()
  231. {
  232. Dim a = Auto ();
  233. Dim b = Auto ();
  234. Assert.True (a.Equals (b));
  235. Assert.True (a.GetHashCode () == b.GetHashCode ());
  236. }
  237. [Fact]
  238. public void TextFormatter_Settings_Change_View_Size ()
  239. {
  240. View view = new ()
  241. {
  242. Text = "_1234",
  243. Width = Auto ()
  244. };
  245. Assert.Equal (new (4, 0), view.Frame.Size);
  246. view.Height = 1;
  247. view.SetRelativeLayout (Application.Screen.Size);
  248. Assert.Equal (new (4, 1), view.Frame.Size);
  249. Size lastSize = view.Frame.Size;
  250. view.TextAlignment = Alignment.Fill;
  251. Assert.Equal (lastSize, view.Frame.Size);
  252. view = new ()
  253. {
  254. Text = "_1234",
  255. Width = Auto (),
  256. Height = 1
  257. };
  258. view.SetRelativeLayout (Application.Screen.Size);
  259. lastSize = view.Frame.Size;
  260. view.VerticalTextAlignment = Alignment.Center;
  261. Assert.Equal (lastSize, view.Frame.Size);
  262. view = new ()
  263. {
  264. Text = "_1234",
  265. Width = Auto (),
  266. Height = 1
  267. };
  268. view.SetRelativeLayout (Application.Screen.Size);
  269. lastSize = view.Frame.Size;
  270. view.HotKeySpecifier = (Rune)'*';
  271. view.SetRelativeLayout (Application.Screen.Size);
  272. Assert.NotEqual (lastSize, view.Frame.Size);
  273. view = new ()
  274. {
  275. Text = "_1234",
  276. Width = Auto (),
  277. Height = 1
  278. };
  279. view.SetRelativeLayout (Application.Screen.Size);
  280. lastSize = view.Frame.Size;
  281. view.Text = "*ABCD";
  282. Assert.NotEqual (lastSize, view.Frame.Size);
  283. }
  284. // Test validation
  285. [Fact]
  286. public void ValidatePosDim_True_Throws_When_SubView_Uses_SuperView_Dims ()
  287. {
  288. var superView = new View
  289. {
  290. X = 0,
  291. Y = 0,
  292. Width = Auto (),
  293. Height = Auto (),
  294. ValidatePosDim = true
  295. };
  296. var subView = new View
  297. {
  298. X = 0,
  299. Y = 0,
  300. Width = Fill (),
  301. Height = 10,
  302. ValidatePosDim = true
  303. };
  304. superView.BeginInit ();
  305. superView.EndInit ();
  306. superView.Add (subView);
  307. subView.Width = 10;
  308. superView.Add (subView);
  309. superView.SetRelativeLayout (new (10, 10));
  310. superView.LayoutSubviews (); // no throw
  311. subView.Width = Fill ();
  312. superView.SetRelativeLayout (new (0, 0));
  313. subView.Width = 10;
  314. subView.Height = Fill ();
  315. superView.SetRelativeLayout (new (0, 0));
  316. subView.Height = 10;
  317. subView.Height = Percent (50);
  318. Assert.Throws<InvalidOperationException> (() => superView.SetRelativeLayout (new (0, 0)));
  319. subView.Height = 10;
  320. subView.X = Pos.Center ();
  321. Assert.Throws<InvalidOperationException> (() => superView.SetRelativeLayout (new (0, 0)));
  322. subView.X = 0;
  323. subView.Y = Pos.Center ();
  324. Assert.Throws<InvalidOperationException> (() => superView.SetRelativeLayout (new (0, 0)));
  325. subView.Y = 0;
  326. subView.Width = 10;
  327. subView.Height = 10;
  328. subView.X = 0;
  329. subView.Y = 0;
  330. superView.SetRelativeLayout (new (0, 0));
  331. superView.LayoutSubviews ();
  332. }
  333. // Test validation
  334. [Fact]
  335. public void ValidatePosDim_True_Throws_When_SubView_Uses_SuperView_Dims_Combine ()
  336. {
  337. var superView = new View
  338. {
  339. X = 0,
  340. Y = 0,
  341. Width = Auto (),
  342. Height = Auto (),
  343. ValidatePosDim = true
  344. };
  345. var subView = new View
  346. {
  347. X = 0,
  348. Y = 0,
  349. Width = 10,
  350. Height = 10
  351. };
  352. var subView2 = new View
  353. {
  354. X = 0,
  355. Y = 0,
  356. Width = 10,
  357. Height = 10
  358. };
  359. superView.Add (subView, subView2);
  360. superView.BeginInit ();
  361. superView.EndInit ();
  362. superView.SetRelativeLayout (new (0, 0));
  363. superView.LayoutSubviews (); // no throw
  364. subView.Height = Fill () + 3;
  365. superView.SetRelativeLayout (new (0, 0));
  366. subView.Height = 0;
  367. subView.Height = 3 + Fill ();
  368. superView.SetRelativeLayout (new (0, 0));
  369. subView.Height = 0;
  370. subView.Height = 3 + 5 + Fill ();
  371. superView.SetRelativeLayout (new (0, 0));
  372. subView.Height = 0;
  373. subView.Height = 3 + 5 + Percent (10);
  374. Assert.Throws<InvalidOperationException> (() => superView.SetRelativeLayout (new (0, 0)));
  375. subView.Height = 0;
  376. // Tests nested Combine
  377. subView.Height = 5 + new DimCombine (AddOrSubtract.Add, 3, new DimCombine (AddOrSubtract.Add, Percent (10), 9));
  378. Assert.Throws<InvalidOperationException> (() => superView.SetRelativeLayout (new (0, 0)));
  379. }
  380. [Fact]
  381. public void ValidatePosDim_True_Throws_When_SubView_Uses_SuperView_Pos_Combine ()
  382. {
  383. var superView = new View
  384. {
  385. X = 0,
  386. Y = 0,
  387. Width = Auto (),
  388. Height = Auto (),
  389. ValidatePosDim = true
  390. };
  391. var subView = new View
  392. {
  393. X = 0,
  394. Y = 0,
  395. Width = 10,
  396. Height = 10
  397. };
  398. var subView2 = new View
  399. {
  400. X = 0,
  401. Y = 0,
  402. Width = 10,
  403. Height = 10
  404. };
  405. superView.Add (subView, subView2);
  406. superView.BeginInit ();
  407. superView.EndInit ();
  408. superView.SetRelativeLayout (new (0, 0));
  409. superView.LayoutSubviews (); // no throw
  410. subView.X = Pos.Right (subView2);
  411. superView.SetRelativeLayout (new (0, 0));
  412. superView.LayoutSubviews (); // no throw
  413. subView.X = Pos.Right (subView2) + 3;
  414. superView.SetRelativeLayout (new (0, 0)); // no throw
  415. superView.LayoutSubviews (); // no throw
  416. subView.X = new PosCombine (AddOrSubtract.Add, Pos.Right (subView2), new PosCombine (AddOrSubtract.Add, 7, 9));
  417. superView.SetRelativeLayout (new (0, 0)); // no throw
  418. subView.X = Pos.Center () + 3;
  419. Assert.Throws<InvalidOperationException> (() => superView.SetRelativeLayout (new (0, 0)));
  420. subView.X = 0;
  421. subView.X = 3 + Pos.Center ();
  422. Assert.Throws<InvalidOperationException> (() => superView.SetRelativeLayout (new (0, 0)));
  423. subView.X = 0;
  424. subView.X = 3 + 5 + Pos.Center ();
  425. Assert.Throws<InvalidOperationException> (() => superView.SetRelativeLayout (new (0, 0)));
  426. subView.X = 0;
  427. subView.X = 3 + 5 + Pos.Percent (10);
  428. Assert.Throws<InvalidOperationException> (() => superView.SetRelativeLayout (new (0, 0)));
  429. subView.X = 0;
  430. subView.X = Pos.Percent (10) + Pos.Center ();
  431. Assert.Throws<InvalidOperationException> (() => superView.SetRelativeLayout (new (0, 0)));
  432. subView.X = 0;
  433. // Tests nested Combine
  434. subView.X = 5 + new PosCombine (AddOrSubtract.Add, Pos.Right (subView2), new PosCombine (AddOrSubtract.Add, Pos.Center (), 9));
  435. Assert.Throws<InvalidOperationException> (() => superView.SetRelativeLayout (new (0, 0)));
  436. subView.X = 0;
  437. }
  438. [Theory]
  439. [InlineData (0, 0, 0, 0, 0)]
  440. [InlineData (0, 0, 5, 0, 5)]
  441. [InlineData (0, 0, 0, 5, 0)]
  442. [InlineData (0, 0, 5, 5, 5)]
  443. [InlineData (1, 0, 5, 0, 6)]
  444. [InlineData (1, 0, 0, 5, 1)]
  445. [InlineData (1, 0, 5, 5, 6)]
  446. [InlineData (1, 1, 5, 5, 6)]
  447. [InlineData (-1, 0, 5, 0, 4)]
  448. [InlineData (-1, 0, 0, 5, 0)]
  449. [InlineData (-1, 0, 5, 5, 4)]
  450. [InlineData (-1, -1, 5, 5, 4)]
  451. public void Width_Auto_Height_Absolute_NotChanged (int subX, int subY, int subWidth, int subHeight, int expectedWidth)
  452. {
  453. var superView = new View
  454. {
  455. X = 0,
  456. Y = 0,
  457. Width = Auto (),
  458. Height = 10,
  459. ValidatePosDim = true
  460. };
  461. var subView = new View
  462. {
  463. X = subX,
  464. Y = subY,
  465. Width = subWidth,
  466. Height = subHeight,
  467. ValidatePosDim = true
  468. };
  469. superView.Add (subView);
  470. superView.BeginInit ();
  471. superView.EndInit ();
  472. superView.SetRelativeLayout (new (10, 10));
  473. Assert.Equal (new (0, 0, expectedWidth, 10), superView.Frame);
  474. }
  475. // Test that when a view has Width set to DimAuto (min: x)
  476. // the width is never < x even if SetRelativeLayout is called with smaller bounds
  477. [Theory]
  478. [InlineData (0, 0)]
  479. [InlineData (1, 1)]
  480. [InlineData (3, 3)]
  481. [InlineData (4, 4)]
  482. [InlineData (5, 5)] // No reason why it can exceed container
  483. public void Width_Auto_Min_Honored (int min, int expectedWidth)
  484. {
  485. var superView = new View
  486. {
  487. X = 0,
  488. Y = 0,
  489. Width = Auto (minimumContentDim: min),
  490. Height = 1,
  491. ValidatePosDim = true
  492. };
  493. superView.BeginInit ();
  494. superView.EndInit ();
  495. superView.SetRelativeLayout (new (4, 1));
  496. Assert.Equal (expectedWidth, superView.Frame.Width);
  497. }
  498. [Theory]
  499. [InlineData (0, 1, 1)]
  500. [InlineData (1, 1, 1)]
  501. [InlineData (9, 1, 1)]
  502. [InlineData (10, 1, 1)]
  503. [InlineData (0, 10, 10)]
  504. [InlineData (1, 10, 10)]
  505. [InlineData (9, 10, 10)]
  506. [InlineData (10, 10, 10)]
  507. public void Width_Auto_Subviews_Does_Not_Constrain_To_SuperView (int subX, int subSubViewWidth, int expectedSubWidth)
  508. {
  509. var superView = new View
  510. {
  511. X = 0,
  512. Y = 0,
  513. Width = 10,
  514. Height = 1,
  515. ValidatePosDim = true
  516. };
  517. var subView = new View
  518. {
  519. X = subX,
  520. Y = 0,
  521. Width = Auto (DimAutoStyle.Content),
  522. Height = 1,
  523. ValidatePosDim = true
  524. };
  525. var subSubView = new View
  526. {
  527. X = 0,
  528. Y = 0,
  529. Width = subSubViewWidth,
  530. Height = 1,
  531. ValidatePosDim = true
  532. };
  533. subView.Add (subSubView);
  534. superView.Add (subView);
  535. superView.BeginInit ();
  536. superView.EndInit ();
  537. superView.SetRelativeLayout (superView.GetContentSize ());
  538. superView.LayoutSubviews ();
  539. Assert.Equal (expectedSubWidth, subView.Frame.Width);
  540. }
  541. [Theory]
  542. [InlineData (0, 1, 1)]
  543. [InlineData (1, 1, 1)]
  544. [InlineData (9, 1, 1)]
  545. [InlineData (10, 1, 1)]
  546. [InlineData (0, 10, 10)]
  547. [InlineData (1, 10, 10)]
  548. [InlineData (9, 10, 10)]
  549. [InlineData (10, 10, 10)]
  550. public void Width_Auto_Text_Does_Not_Constrain_To_SuperView (int subX, int textLen, int expectedSubWidth)
  551. {
  552. var superView = new View
  553. {
  554. X = 0,
  555. Y = 0,
  556. Width = 10,
  557. Height = 1,
  558. ValidatePosDim = true
  559. };
  560. var subView = new View
  561. {
  562. Text = new ('*', textLen),
  563. X = subX,
  564. Y = 0,
  565. Width = Auto (DimAutoStyle.Text),
  566. Height = 1,
  567. ValidatePosDim = true
  568. };
  569. superView.Add (subView);
  570. superView.BeginInit ();
  571. superView.EndInit ();
  572. superView.SetRelativeLayout (superView.GetContentSize ());
  573. superView.LayoutSubviews ();
  574. Assert.Equal (expectedSubWidth, subView.Frame.Width);
  575. }
  576. private class DimAutoTestView : View
  577. {
  578. public DimAutoTestView ()
  579. {
  580. ValidatePosDim = true;
  581. Width = Auto ();
  582. Height = Auto ();
  583. }
  584. public DimAutoTestView (Dim width, Dim height)
  585. {
  586. ValidatePosDim = true;
  587. Width = width;
  588. Height = height;
  589. }
  590. public DimAutoTestView (string text, Dim width, Dim height)
  591. {
  592. ValidatePosDim = true;
  593. Text = text;
  594. Width = width;
  595. Height = height;
  596. }
  597. }
  598. #region DimAutoStyle.Auto tests
  599. [Theory]
  600. [InlineData ("", 0, 0)]
  601. [InlineData (" ", 1, 1)]
  602. [InlineData ("01234", 5, 1)]
  603. [InlineData ("01234\nABCDE", 5, 2)]
  604. public void DimAutoStyle_Auto_JustText_Sizes_Correctly (string text, int expectedW, int expectedH)
  605. {
  606. var view = new View ();
  607. view.Width = Auto ();
  608. view.Height = Auto ();
  609. view.Text = text;
  610. view.SetRelativeLayout (Application.Screen.Size);
  611. Assert.Equal (new (expectedW, expectedH), view.Frame.Size);
  612. }
  613. [Fact]
  614. public void DimAutoStyle_Auto_Text_Size_Is_Used ()
  615. {
  616. var view = new View
  617. {
  618. Text = "0123\n4567",
  619. Width = Auto (),
  620. Height = Auto ()
  621. };
  622. view.SetRelativeLayout (new (100, 100));
  623. Assert.Equal (new (4, 2), view.Frame.Size);
  624. var subView = new View
  625. {
  626. Text = "ABCD",
  627. Width = Auto (),
  628. Height = Auto ()
  629. };
  630. view.Add (subView);
  631. view.SetRelativeLayout (new (100, 100));
  632. Assert.Equal (new (4, 2), view.Frame.Size);
  633. subView.Text = "ABCDE";
  634. view.SetRelativeLayout (new (100, 100));
  635. Assert.Equal (new (5, 2), view.Frame.Size);
  636. }
  637. [Theory]
  638. [InlineData ("01234", 5, 5)]
  639. [InlineData ("01234", 6, 6)]
  640. [InlineData ("01234", 4, 5)]
  641. [InlineData ("01234", 0, 5)]
  642. [InlineData ("", 5, 5)]
  643. [InlineData ("", 0, 0)]
  644. public void DimAutoStyle_Auto_Larger_Wins (string text, int dimension, int expected)
  645. {
  646. View view = new ()
  647. {
  648. Width = Auto (),
  649. Height = 1,
  650. Text = text
  651. };
  652. View subView = new ()
  653. {
  654. Width = dimension,
  655. Height = 1
  656. };
  657. view.Add (subView);
  658. view.SetRelativeLayout (new (10, 10));
  659. Assert.Equal (expected, view.Frame.Width);
  660. }
  661. #endregion
  662. #region DimAutoStyle.Text tests
  663. [Fact]
  664. public void DimAutoStyle_Text_Viewport_Stays_Set ()
  665. {
  666. var super = new View
  667. {
  668. Width = Fill (),
  669. Height = Fill ()
  670. };
  671. var view = new View
  672. {
  673. Text = "01234567",
  674. Width = Auto (DimAutoStyle.Text),
  675. Height = Auto (DimAutoStyle.Text)
  676. };
  677. super.Add (view);
  678. Rectangle expectedViewport = new (0, 0, 8, 1);
  679. Assert.Equal (expectedViewport.Size, view.GetContentSize ());
  680. Assert.Equal (expectedViewport, view.Frame);
  681. Assert.Equal (expectedViewport, view.Viewport);
  682. super.LayoutSubviews ();
  683. Assert.Equal (expectedViewport, view.Viewport);
  684. super.Dispose ();
  685. }
  686. [Theory]
  687. [InlineData ("", 0, 0)]
  688. [InlineData (" ", 1, 1)]
  689. [InlineData ("01234", 5, 1)]
  690. [InlineData ("01234\nABCDE", 5, 2)]
  691. public void DimAutoStyle_Text_Sizes_Correctly (string text, int expectedW, int expectedH)
  692. {
  693. var view = new View ();
  694. view.Width = Auto (DimAutoStyle.Text);
  695. view.Height = Auto (DimAutoStyle.Text);
  696. view.Text = text;
  697. view.SetRelativeLayout (Application.Screen.Size);
  698. Assert.Equal (new (expectedW, expectedH), view.Frame.Size);
  699. }
  700. [Theory]
  701. [InlineData ("", 0, 0, 0, 0)]
  702. [InlineData (" ", 5, 5, 5, 5)]
  703. [InlineData ("01234", 5, 5, 5, 5)]
  704. [InlineData ("01234", 4, 3, 5, 3)]
  705. [InlineData ("01234ABCDE", 5, 0, 10, 1)]
  706. public void DimAutoStyle_Text_Sizes_Correctly_With_Min (string text, int minWidth, int minHeight, int expectedW, int expectedH)
  707. {
  708. var view = new View ();
  709. view.Width = Auto (DimAutoStyle.Text, minWidth);
  710. view.Height = Auto (DimAutoStyle.Text, minHeight);
  711. view.Text = text;
  712. view.SetRelativeLayout (Application.Screen.Size);
  713. Assert.Equal (new (expectedW, expectedH), view.Frame.Size);
  714. }
  715. [Theory]
  716. [InlineData ("", 0, 0, 0)]
  717. [InlineData (" ", 5, 1, 1)]
  718. [InlineData ("01234", 5, 5, 1)]
  719. [InlineData ("01234", 4, 4, 2)]
  720. [InlineData ("01234ABCDE", 5, 5, 2)]
  721. [InlineData ("01234ABCDE", 1, 1, 10)]
  722. public void DimAutoStyle_Text_Sizes_Correctly_With_Max_Width (string text, int maxWidth, int expectedW, int expectedH)
  723. {
  724. var view = new View ();
  725. view.Width = Auto (DimAutoStyle.Text, maximumContentDim: maxWidth);
  726. view.Height = Auto (DimAutoStyle.Text);
  727. view.Text = text;
  728. Assert.Equal (new (expectedW, expectedH), view.Frame.Size);
  729. }
  730. [Theory]
  731. [InlineData ("", 0, 0)]
  732. [InlineData (" ", 1, 1)]
  733. [InlineData ("01234", 5, 1)]
  734. [InlineData ("01234ABCDE", 10, 1)]
  735. [InlineData ("01234\nABCDE", 5, 2)]
  736. public void DimAutoStyle_Text_NoMin_Not_Constrained_By_ContentSize (string text, int expectedW, int expectedH)
  737. {
  738. var view = new View ();
  739. view.Width = Auto (DimAutoStyle.Text);
  740. view.Height = Auto (DimAutoStyle.Text);
  741. view.SetContentSize (new (1, 1));
  742. view.Text = text;
  743. view.SetRelativeLayout (Application.Screen.Size);
  744. Assert.Equal (new (expectedW, expectedH), view.Frame.Size);
  745. }
  746. [Theory]
  747. [InlineData ("", 0, 0)]
  748. [InlineData (" ", 1, 1)]
  749. [InlineData ("01234", 5, 1)]
  750. [InlineData ("01234ABCDE", 10, 1)]
  751. [InlineData ("01234\nABCDE", 5, 2)]
  752. public void DimAutoStyle_Text_NoMin_Not_Constrained_By_SuperView (string text, int expectedW, int expectedH)
  753. {
  754. var superView = new View
  755. {
  756. Width = 1, Height = 1
  757. };
  758. var view = new View ();
  759. view.Width = Auto (DimAutoStyle.Text);
  760. view.Height = Auto (DimAutoStyle.Text);
  761. view.Text = text;
  762. superView.Add (view);
  763. superView.SetRelativeLayout (Application.Screen.Size);
  764. Assert.Equal (new (expectedW, expectedH), view.Frame.Size);
  765. }
  766. [Fact]
  767. public void DimAutoStyle_Text_Pos_AnchorEnd_Locates_Correctly ()
  768. {
  769. DimAutoTestView view = new ("01234", Auto (DimAutoStyle.Text), Auto (DimAutoStyle.Text));
  770. view.SetRelativeLayout (new (10, 10));
  771. Assert.Equal (new (5, 1), view.Frame.Size);
  772. Assert.Equal (new (0, 0), view.Frame.Location);
  773. view.X = 0;
  774. view.Y = Pos.AnchorEnd (1);
  775. view.SetRelativeLayout (new (10, 10));
  776. Assert.Equal (new (5, 1), view.Frame.Size);
  777. Assert.Equal (new (0, 9), view.Frame.Location);
  778. view.Y = Pos.AnchorEnd ();
  779. view.SetRelativeLayout (new (10, 10));
  780. Assert.Equal (new (5, 1), view.Frame.Size);
  781. Assert.Equal (new (0, 9), view.Frame.Location);
  782. view.Y = Pos.AnchorEnd () - 1;
  783. view.SetRelativeLayout (new (10, 10));
  784. Assert.Equal (new (5, 1), view.Frame.Size);
  785. Assert.Equal (new (0, 8), view.Frame.Location);
  786. }
  787. #endregion DimAutoStyle.Text tests
  788. #region DimAutoStyle.Content tests
  789. // DimAutoStyle.Content tests
  790. [Fact]
  791. public void DimAutoStyle_Content_UsesContentSize_WhenSet ()
  792. {
  793. var view = new View ();
  794. view.SetContentSize (new (10, 5));
  795. Dim dim = Auto (DimAutoStyle.Content);
  796. int calculatedWidth = dim.Calculate (0, 100, view, Dimension.Width);
  797. Assert.Equal (10, calculatedWidth);
  798. }
  799. [Fact]
  800. public void DimAutoStyle_Content_IgnoresSubviews_When_ContentSize_Is_Set ()
  801. {
  802. var view = new View ();
  803. var subview = new View
  804. {
  805. Frame = new (50, 50, 1, 1)
  806. };
  807. view.SetContentSize (new (10, 5));
  808. Dim dim = Auto (DimAutoStyle.Content);
  809. int calculatedWidth = dim.Calculate (0, 100, view, Dimension.Width);
  810. Assert.Equal (10, calculatedWidth);
  811. }
  812. [Fact]
  813. public void DimAutoStyle_Content_IgnoresText_WhenContentSizeNotSet ()
  814. {
  815. var view = new View { Text = "This is a test" };
  816. Dim dim = Auto (DimAutoStyle.Content);
  817. int calculatedWidth = dim.Calculate (0, 100, view, Dimension.Width);
  818. Assert.Equal (0, calculatedWidth); // Assuming 0 is the default when no ContentSize or Subviews are set
  819. }
  820. [Fact]
  821. public void DimAutoStyle_Content_UsesLargestSubview_WhenContentSizeNotSet ()
  822. {
  823. var view = new View ();
  824. view.Add (new View { Frame = new (0, 0, 5, 5) }); // Smaller subview
  825. view.Add (new View { Frame = new (0, 0, 10, 10) }); // Larger subview
  826. Dim dim = Auto (DimAutoStyle.Content);
  827. int calculatedWidth = dim.Calculate (0, 100, view, Dimension.Width);
  828. Assert.Equal (10, calculatedWidth); // Expecting the size of the largest subview
  829. }
  830. [Fact]
  831. public void DimAutoStyle_Content_UsesContentSize_If_No_Subviews ()
  832. {
  833. DimAutoTestView view = new (Auto (DimAutoStyle.Content), Auto (DimAutoStyle.Content));
  834. view.SetContentSize (new (5, 5));
  835. view.SetRelativeLayout (new (10, 10));
  836. Assert.Equal (new (5, 5), view.Frame.Size);
  837. }
  838. [Fact]
  839. public void DimAutoStyle_Content_Pos_AnchorEnd_Locates_Correctly ()
  840. {
  841. DimAutoTestView view = new (Auto (DimAutoStyle.Content), Auto (DimAutoStyle.Content));
  842. View subView = new ()
  843. {
  844. Width = 5,
  845. Height = 1
  846. };
  847. view.Add (subView);
  848. view.SetRelativeLayout (new (10, 10));
  849. Assert.Equal (new (5, 1), view.Frame.Size);
  850. Assert.Equal (new (0, 0), view.Frame.Location);
  851. view.X = 0;
  852. view.Y = Pos.AnchorEnd (1);
  853. view.SetRelativeLayout (new (10, 10));
  854. Assert.Equal (new (5, 1), view.Frame.Size);
  855. Assert.Equal (new (0, 9), view.Frame.Location);
  856. view.Y = Pos.AnchorEnd ();
  857. view.SetRelativeLayout (new (10, 10));
  858. Assert.Equal (new (5, 1), view.Frame.Size);
  859. Assert.Equal (new (0, 9), view.Frame.Location);
  860. view.Y = Pos.AnchorEnd () - 1;
  861. view.SetRelativeLayout (new (10, 10));
  862. Assert.Equal (new (5, 1), view.Frame.Size);
  863. Assert.Equal (new (0, 8), view.Frame.Location);
  864. }
  865. #endregion DimAutoStyle.Content tests
  866. // Test variations of Frame
  867. }