Dim.AutoTests.cs 29 KB

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