Dim.AutoTests.cs 29 KB

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