Dim.AutoTests.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999
  1. using System.Text;
  2. using Xunit.Abstractions;
  3. using static Terminal.Gui.ViewBase.Dim;
  4. namespace ViewBaseTests.Layout;
  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. view.Layout ();
  81. Assert.Equal (4, view.TextFormatter.ConstrainToWidth);
  82. Assert.Equal (1, view.TextFormatter.ConstrainToHeight);
  83. view = new ()
  84. {
  85. HotKeySpecifier = (Rune)'_',
  86. TextDirection = TextDirection.TopBottom_LeftRight,
  87. Text = text,
  88. Width = 1,
  89. Height = Auto ()
  90. };
  91. view.Layout ();
  92. Assert.Equal (1, view.TextFormatter.ConstrainToWidth);
  93. Assert.Equal (4, view.TextFormatter.ConstrainToHeight);
  94. }
  95. [Theory]
  96. [CombinatorialData]
  97. public void HotKey_TextFormatter_Width_Correct ([CombinatorialValues ("1234", "_1234", "1_234", "____")] string text)
  98. {
  99. View view = new ()
  100. {
  101. Text = text,
  102. Height = 1,
  103. Width = Auto ()
  104. };
  105. view.Layout ();
  106. Assert.Equal (4, view.TextFormatter.ConstrainToWidth);
  107. Assert.Equal (1, view.TextFormatter.ConstrainToHeight);
  108. }
  109. [Fact]
  110. public void NoSubViews_Does_Nothing ()
  111. {
  112. var superView = new View
  113. {
  114. X = 0,
  115. Y = 0,
  116. Width = Auto (),
  117. Height = Auto (),
  118. ValidatePosDim = true
  119. };
  120. superView.BeginInit ();
  121. superView.EndInit ();
  122. superView.SetRelativeLayout (new (10, 10));
  123. Assert.Equal (new (0, 0, 0, 0), superView.Frame);
  124. superView.SetRelativeLayout (new (10, 10));
  125. Assert.Equal (new (0, 0, 0, 0), superView.Frame);
  126. }
  127. [Fact]
  128. public void NoSubViews_Does_Nothing_Vertical ()
  129. {
  130. var superView = new View
  131. {
  132. X = 0,
  133. Y = 0,
  134. Width = Auto (),
  135. Height = Auto (),
  136. TextDirection = TextDirection.TopBottom_LeftRight,
  137. ValidatePosDim = true
  138. };
  139. superView.BeginInit ();
  140. superView.EndInit ();
  141. superView.SetRelativeLayout (new (10, 10));
  142. Assert.Equal (new (0, 0, 0, 0), superView.Frame);
  143. superView.SetRelativeLayout (new (10, 10));
  144. Assert.Equal (new (0, 0, 0, 0), superView.Frame);
  145. }
  146. [Theory]
  147. [InlineData (0, 0, 0, 0, 0, 0)]
  148. [InlineData (0, 0, 5, 0, 5, 0)]
  149. [InlineData (0, 0, 0, 5, 0, 5)]
  150. [InlineData (0, 0, 5, 5, 5, 5)]
  151. [InlineData (1, 0, 5, 0, 6, 0)]
  152. [InlineData (1, 0, 0, 5, 1, 5)]
  153. [InlineData (1, 0, 5, 5, 6, 5)]
  154. [InlineData (1, 1, 5, 5, 6, 6)]
  155. [InlineData (-1, 0, 5, 0, 4, 0)]
  156. [InlineData (-1, 0, 0, 5, 0, 5)]
  157. [InlineData (-1, 0, 5, 5, 4, 5)]
  158. [InlineData (-1, -1, 5, 5, 4, 4)]
  159. public void SubView_Changes_SuperView_Size (int subX, int subY, int subWidth, int subHeight, int expectedWidth, int expectedHeight)
  160. {
  161. var superView = new View
  162. {
  163. X = 0,
  164. Y = 0,
  165. Width = Auto (),
  166. Height = Auto (),
  167. ValidatePosDim = true
  168. };
  169. var subView = new View
  170. {
  171. X = subX,
  172. Y = subY,
  173. Width = subWidth,
  174. Height = subHeight,
  175. ValidatePosDim = true
  176. };
  177. superView.Add (subView);
  178. superView.BeginInit ();
  179. superView.EndInit ();
  180. superView.SetRelativeLayout (new (10, 10));
  181. Assert.Equal (new (0, 0, expectedWidth, expectedHeight), superView.Frame);
  182. }
  183. [Fact]
  184. public void TestEquality ()
  185. {
  186. var a = new DimAuto (
  187. MaximumContentDim: null,
  188. MinimumContentDim: 1,
  189. Style: DimAutoStyle.Auto
  190. );
  191. var b = new DimAuto (
  192. MaximumContentDim: null,
  193. MinimumContentDim: 1,
  194. Style: DimAutoStyle.Auto
  195. );
  196. var c = new DimAuto (
  197. MaximumContentDim: 2,
  198. MinimumContentDim: 1,
  199. Style: DimAutoStyle.Auto
  200. );
  201. var d = new DimAuto (
  202. MaximumContentDim: null,
  203. MinimumContentDim: 1,
  204. Style: DimAutoStyle.Content
  205. );
  206. var e = new DimAuto (
  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 inequality with null
  224. Assert.False (a.Equals (null));
  225. }
  226. [Fact]
  227. public void TestEquality_Simple ()
  228. {
  229. Dim a = Auto ();
  230. Dim b = Auto ();
  231. Assert.True (a.Equals (b));
  232. Assert.True (a.GetHashCode () == b.GetHashCode ());
  233. }
  234. [Fact]
  235. public void TextFormatter_Settings_Change_View_Size ()
  236. {
  237. View view = new ()
  238. {
  239. Text = "_1234",
  240. Width = Auto ()
  241. };
  242. view.Layout ();
  243. Assert.Equal (new (4, 0), view.Frame.Size);
  244. view.Height = 1;
  245. view.Layout ();
  246. Assert.Equal (new (4, 1), view.Frame.Size);
  247. Size lastSize = view.Frame.Size;
  248. view.TextAlignment = Alignment.Fill;
  249. Assert.Equal (lastSize, view.Frame.Size);
  250. view = new ()
  251. {
  252. Text = "_1234",
  253. Width = Auto (),
  254. Height = 1
  255. };
  256. view.Layout ();
  257. lastSize = view.Frame.Size;
  258. view.VerticalTextAlignment = Alignment.Center;
  259. Assert.Equal (lastSize, view.Frame.Size);
  260. view = new ()
  261. {
  262. Text = "_1234",
  263. Width = Auto (),
  264. Height = 1
  265. };
  266. view.SetRelativeLayout (new (100, 100));
  267. lastSize = view.Frame.Size;
  268. view.HotKeySpecifier = (Rune)'*';
  269. view.SetRelativeLayout (new (100, 100));
  270. Assert.NotEqual (lastSize, view.Frame.Size);
  271. view = new ()
  272. {
  273. Text = "_1234",
  274. Width = Auto (),
  275. Height = 1
  276. };
  277. view.SetRelativeLayout (new (100, 100));
  278. lastSize = view.Frame.Size;
  279. view.Text = "*ABCD";
  280. view.SetRelativeLayout (new (100, 100));
  281. Assert.NotEqual (lastSize, view.Frame.Size);
  282. }
  283. // Test validation
  284. [Fact]
  285. public void ValidatePosDim_True_Throws_When_SubView_Uses_SuperView_Dims ()
  286. {
  287. var superView = new View
  288. {
  289. X = 0,
  290. Y = 0,
  291. Width = Auto (),
  292. Height = Auto (),
  293. ValidatePosDim = true
  294. };
  295. var subView = new View
  296. {
  297. X = 0,
  298. Y = 0,
  299. Width = Fill (),
  300. Height = 10,
  301. ValidatePosDim = true
  302. };
  303. superView.BeginInit ();
  304. superView.EndInit ();
  305. superView.Add (subView);
  306. subView.Width = 10;
  307. superView.SetRelativeLayout (new (10, 10));
  308. superView.LayoutSubViews (); // no throw
  309. subView.Width = Fill ();
  310. superView.SetRelativeLayout (new (0, 0));
  311. subView.Width = 10;
  312. subView.Height = Fill ();
  313. superView.SetRelativeLayout (new (0, 0));
  314. subView.Height = 10;
  315. subView.Height = Percent (50);
  316. Assert.Throws<LayoutException> (() => superView.SetRelativeLayout (new (0, 0)));
  317. subView.Height = 10;
  318. subView.X = Pos.Center ();
  319. Assert.Throws<LayoutException> (() => superView.SetRelativeLayout (new (0, 0)));
  320. subView.X = 0;
  321. subView.Y = Pos.Center ();
  322. Assert.Throws<LayoutException> (() => superView.SetRelativeLayout (new (0, 0)));
  323. subView.Y = 0;
  324. subView.Width = 10;
  325. subView.Height = 10;
  326. subView.X = 0;
  327. subView.Y = 0;
  328. superView.SetRelativeLayout (new (0, 0));
  329. superView.LayoutSubViews ();
  330. }
  331. // Test validation
  332. [Fact]
  333. public void ValidatePosDim_True_Throws_When_SubView_Uses_SuperView_Dims_Combine ()
  334. {
  335. var superView = new View
  336. {
  337. X = 0,
  338. Y = 0,
  339. Width = Auto (),
  340. Height = Auto (),
  341. ValidatePosDim = true
  342. };
  343. var subView = new View
  344. {
  345. X = 0,
  346. Y = 0,
  347. Width = 10,
  348. Height = 10
  349. };
  350. var subView2 = new View
  351. {
  352. X = 0,
  353. Y = 0,
  354. Width = 10,
  355. Height = 10
  356. };
  357. superView.Add (subView, subView2);
  358. superView.BeginInit ();
  359. superView.EndInit ();
  360. superView.SetRelativeLayout (new (0, 0));
  361. superView.LayoutSubViews (); // no throw
  362. subView.Height = Fill () + 3;
  363. superView.SetRelativeLayout (new (0, 0));
  364. subView.Height = 0;
  365. subView.Height = 3 + Fill ();
  366. superView.SetRelativeLayout (new (0, 0));
  367. subView.Height = 0;
  368. subView.Height = 3 + 5 + Fill ();
  369. superView.SetRelativeLayout (new (0, 0));
  370. subView.Height = 0;
  371. subView.Height = 3 + 5 + Percent (10);
  372. Assert.Throws<LayoutException> (() => superView.SetRelativeLayout (new (0, 0)));
  373. subView.Height = 0;
  374. // Tests nested Combine
  375. subView.Height = 5 + new DimCombine (AddOrSubtract.Add, 3, new DimCombine (AddOrSubtract.Add, Percent (10), 9));
  376. Assert.Throws<LayoutException> (() => superView.SetRelativeLayout (new (0, 0)));
  377. }
  378. [Fact]
  379. public void ValidatePosDim_True_Throws_When_SubView_Uses_SuperView_Pos_Combine ()
  380. {
  381. var superView = new View
  382. {
  383. X = 0,
  384. Y = 0,
  385. Width = Auto (),
  386. Height = Auto (),
  387. ValidatePosDim = true
  388. };
  389. var subView = new View
  390. {
  391. X = 0,
  392. Y = 0,
  393. Width = 10,
  394. Height = 10
  395. };
  396. var subView2 = new View
  397. {
  398. X = 0,
  399. Y = 0,
  400. Width = 10,
  401. Height = 10
  402. };
  403. superView.Add (subView, subView2);
  404. superView.BeginInit ();
  405. superView.EndInit ();
  406. superView.SetRelativeLayout (new (0, 0));
  407. superView.LayoutSubViews (); // no throw
  408. subView.X = Pos.Right (subView2);
  409. superView.SetRelativeLayout (new (0, 0));
  410. superView.LayoutSubViews (); // no throw
  411. subView.X = Pos.Right (subView2) + 3;
  412. superView.SetRelativeLayout (new (0, 0)); // no throw
  413. superView.LayoutSubViews (); // no throw
  414. subView.X = new PosCombine (AddOrSubtract.Add, Pos.Right (subView2), new PosCombine (AddOrSubtract.Add, 7, 9));
  415. superView.SetRelativeLayout (new (0, 0)); // no throw
  416. subView.X = Pos.Center () + 3;
  417. Assert.Throws<LayoutException> (() => superView.SetRelativeLayout (new (0, 0)));
  418. subView.X = 0;
  419. subView.X = 3 + Pos.Center ();
  420. Assert.Throws<LayoutException> (() => superView.SetRelativeLayout (new (0, 0)));
  421. subView.X = 0;
  422. subView.X = 3 + 5 + Pos.Center ();
  423. Assert.Throws<LayoutException> (() => superView.SetRelativeLayout (new (0, 0)));
  424. subView.X = 0;
  425. subView.X = 3 + 5 + Pos.Percent (10);
  426. Assert.Throws<LayoutException> (() => superView.SetRelativeLayout (new (0, 0)));
  427. subView.X = 0;
  428. subView.X = Pos.Percent (10) + Pos.Center ();
  429. Assert.Throws<LayoutException> (() => superView.SetRelativeLayout (new (0, 0)));
  430. subView.X = 0;
  431. // Tests nested Combine
  432. subView.X = 5 + new PosCombine (AddOrSubtract.Add, Pos.Right (subView2), new PosCombine (AddOrSubtract.Add, Pos.Center (), 9));
  433. Assert.Throws<LayoutException> (() => superView.SetRelativeLayout (new (0, 0)));
  434. subView.X = 0;
  435. }
  436. [Theory]
  437. [InlineData (0, 0, 0, 0, 0)]
  438. [InlineData (0, 0, 5, 0, 5)]
  439. [InlineData (0, 0, 0, 5, 0)]
  440. [InlineData (0, 0, 5, 5, 5)]
  441. [InlineData (1, 0, 5, 0, 6)]
  442. [InlineData (1, 0, 0, 5, 1)]
  443. [InlineData (1, 0, 5, 5, 6)]
  444. [InlineData (1, 1, 5, 5, 6)]
  445. [InlineData (-1, 0, 5, 0, 4)]
  446. [InlineData (-1, 0, 0, 5, 0)]
  447. [InlineData (-1, 0, 5, 5, 4)]
  448. [InlineData (-1, -1, 5, 5, 4)]
  449. public void Width_Auto_Height_Absolute_NotChanged (int subX, int subY, int subWidth, int subHeight, int expectedWidth)
  450. {
  451. var superView = new View
  452. {
  453. X = 0,
  454. Y = 0,
  455. Width = Auto (),
  456. Height = 10,
  457. ValidatePosDim = true
  458. };
  459. var subView = new View
  460. {
  461. X = subX,
  462. Y = subY,
  463. Width = subWidth,
  464. Height = subHeight,
  465. ValidatePosDim = true
  466. };
  467. superView.Add (subView);
  468. superView.BeginInit ();
  469. superView.EndInit ();
  470. superView.SetRelativeLayout (new (10, 10));
  471. Assert.Equal (new (0, 0, expectedWidth, 10), superView.Frame);
  472. }
  473. // Test that when a view has Width set to DimAuto (min: x)
  474. // the width is never < x even if SetRelativeLayout is called with smaller bounds
  475. [Theory]
  476. [InlineData (0, 0)]
  477. [InlineData (1, 1)]
  478. [InlineData (3, 3)]
  479. [InlineData (4, 4)]
  480. [InlineData (5, 5)] // No reason why it can exceed container
  481. public void Width_Auto_Min_Honored (int min, int expectedWidth)
  482. {
  483. var superView = new View
  484. {
  485. X = 0,
  486. Y = 0,
  487. Width = Auto (minimumContentDim: min),
  488. Height = 1,
  489. ValidatePosDim = true
  490. };
  491. superView.BeginInit ();
  492. superView.EndInit ();
  493. superView.SetRelativeLayout (new (4, 1));
  494. Assert.Equal (expectedWidth, superView.Frame.Width);
  495. }
  496. [Theory]
  497. [InlineData (0, 1, 1)]
  498. [InlineData (1, 1, 1)]
  499. [InlineData (9, 1, 1)]
  500. [InlineData (10, 1, 1)]
  501. [InlineData (0, 10, 10)]
  502. [InlineData (1, 10, 10)]
  503. [InlineData (9, 10, 10)]
  504. [InlineData (10, 10, 10)]
  505. public void Width_Auto_SubViews_Does_Not_Constrain_To_SuperView (int subX, int subSubViewWidth, int expectedSubWidth)
  506. {
  507. var superView = new View
  508. {
  509. X = 0,
  510. Y = 0,
  511. Width = 10,
  512. Height = 1,
  513. ValidatePosDim = true
  514. };
  515. var subView = new View
  516. {
  517. X = subX,
  518. Y = 0,
  519. Width = Auto (DimAutoStyle.Content),
  520. Height = 1,
  521. ValidatePosDim = true
  522. };
  523. var subSubView = new View
  524. {
  525. X = 0,
  526. Y = 0,
  527. Width = subSubViewWidth,
  528. Height = 1,
  529. ValidatePosDim = true
  530. };
  531. subView.Add (subSubView);
  532. superView.Add (subView);
  533. superView.BeginInit ();
  534. superView.EndInit ();
  535. superView.SetRelativeLayout (superView.GetContentSize ());
  536. superView.LayoutSubViews ();
  537. Assert.Equal (expectedSubWidth, subView.Frame.Width);
  538. }
  539. [Theory]
  540. [InlineData (0, 1, 1)]
  541. [InlineData (1, 1, 1)]
  542. [InlineData (9, 1, 1)]
  543. [InlineData (10, 1, 1)]
  544. [InlineData (0, 10, 10)]
  545. [InlineData (1, 10, 10)]
  546. [InlineData (9, 10, 10)]
  547. [InlineData (10, 10, 10)]
  548. public void Width_Auto_Text_Does_Not_Constrain_To_SuperView (int subX, int textLen, int expectedSubWidth)
  549. {
  550. var superView = new View
  551. {
  552. X = 0,
  553. Y = 0,
  554. Width = 10,
  555. Height = 1,
  556. ValidatePosDim = true
  557. };
  558. var subView = new View
  559. {
  560. Text = new ('*', textLen),
  561. X = subX,
  562. Y = 0,
  563. Width = Auto (DimAutoStyle.Text),
  564. Height = 1,
  565. ValidatePosDim = true
  566. };
  567. superView.Add (subView);
  568. superView.BeginInit ();
  569. superView.EndInit ();
  570. superView.SetRelativeLayout (superView.GetContentSize ());
  571. superView.LayoutSubViews ();
  572. Assert.Equal (expectedSubWidth, subView.Frame.Width);
  573. }
  574. private class DimAutoTestView : View
  575. {
  576. public DimAutoTestView ()
  577. {
  578. ValidatePosDim = true;
  579. Width = Auto ();
  580. Height = Auto ();
  581. }
  582. public DimAutoTestView (Dim width, Dim height)
  583. {
  584. ValidatePosDim = true;
  585. Width = width;
  586. Height = height;
  587. }
  588. public DimAutoTestView (string text, Dim width, Dim height)
  589. {
  590. ValidatePosDim = true;
  591. Text = text;
  592. Width = width;
  593. Height = height;
  594. }
  595. }
  596. #region DimAutoStyle.Auto tests
  597. [Theory]
  598. [InlineData ("", 0, 0)]
  599. [InlineData (" ", 1, 1)]
  600. [InlineData ("01234", 5, 1)]
  601. [InlineData ("01234\nABCDE", 5, 2)]
  602. public void DimAutoStyle_Auto_JustText_Sizes_Correctly (string text, int expectedW, int expectedH)
  603. {
  604. var view = new View ();
  605. view.Width = Auto ();
  606. view.Height = Auto ();
  607. view.Text = text;
  608. view.SetRelativeLayout (new (100, 100));
  609. Assert.Equal (new (expectedW, expectedH), view.Frame.Size);
  610. }
  611. [Fact]
  612. public void DimAutoStyle_Auto_Text_Size_Is_Used ()
  613. {
  614. var view = new View
  615. {
  616. Text = "0123\n4567",
  617. Width = Auto (),
  618. Height = Auto ()
  619. };
  620. view.SetRelativeLayout (new (100, 100));
  621. Assert.Equal (new (4, 2), view.Frame.Size);
  622. var subView = new View
  623. {
  624. Text = "ABCD",
  625. Width = Auto (),
  626. Height = Auto ()
  627. };
  628. view.Add (subView);
  629. view.SetRelativeLayout (new (100, 100));
  630. Assert.Equal (new (4, 2), view.Frame.Size);
  631. subView.Text = "ABCDE";
  632. view.SetRelativeLayout (new (100, 100));
  633. Assert.Equal (new (5, 2), view.Frame.Size);
  634. }
  635. [Theory]
  636. [InlineData ("01234", 5, 5)]
  637. [InlineData ("01234", 6, 6)]
  638. [InlineData ("01234", 4, 5)]
  639. [InlineData ("01234", 0, 5)]
  640. [InlineData ("", 5, 5)]
  641. [InlineData ("", 0, 0)]
  642. public void DimAutoStyle_Auto_Larger_Wins (string text, int dimension, int expected)
  643. {
  644. View view = new ()
  645. {
  646. Width = Auto (),
  647. Height = 1,
  648. Text = text
  649. };
  650. View subView = new ()
  651. {
  652. Width = dimension,
  653. Height = 1
  654. };
  655. view.Add (subView);
  656. view.SetRelativeLayout (new (10, 10));
  657. Assert.Equal (expected, view.Frame.Width);
  658. }
  659. #endregion
  660. #region DimAutoStyle.Text tests
  661. [Fact]
  662. public void DimAutoStyle_Text_Viewport_Stays_Set ()
  663. {
  664. var super = new View
  665. {
  666. Width = Fill (),
  667. Height = Fill ()
  668. };
  669. var view = new View
  670. {
  671. Text = "01234567",
  672. Width = Auto (DimAutoStyle.Text),
  673. Height = Auto (DimAutoStyle.Text)
  674. };
  675. super.Add (view);
  676. super.Layout ();
  677. Rectangle expectedViewport = new (0, 0, 8, 1);
  678. Assert.Equal (expectedViewport.Size, view.GetContentSize ());
  679. Assert.Equal (expectedViewport, view.Frame);
  680. Assert.Equal (expectedViewport, view.Viewport);
  681. super.Layout ();
  682. Assert.Equal (expectedViewport, view.Viewport);
  683. super.Dispose ();
  684. }
  685. [Theory]
  686. [InlineData ("", 0, 0)]
  687. [InlineData (" ", 1, 1)]
  688. [InlineData ("01234", 5, 1)]
  689. [InlineData ("01234\nABCDE", 5, 2)]
  690. public void DimAutoStyle_Text_Sizes_Correctly (string text, int expectedW, int expectedH)
  691. {
  692. var view = new View ();
  693. view.Width = Auto (DimAutoStyle.Text);
  694. view.Height = Auto (DimAutoStyle.Text);
  695. view.Text = text;
  696. view.SetRelativeLayout (new (100, 100));
  697. Assert.Equal (new (expectedW, expectedH), view.Frame.Size);
  698. }
  699. [Theory]
  700. [InlineData ("", 0, 0, 0, 0)]
  701. [InlineData (" ", 5, 5, 5, 5)]
  702. [InlineData ("01234", 5, 5, 5, 5)]
  703. [InlineData ("01234", 4, 3, 5, 3)]
  704. [InlineData ("01234ABCDE", 5, 0, 10, 1)]
  705. public void DimAutoStyle_Text_Sizes_Correctly_With_Min (string text, int minWidth, int minHeight, int expectedW, int expectedH)
  706. {
  707. var view = new View ();
  708. view.Width = Auto (DimAutoStyle.Text, minWidth);
  709. view.Height = Auto (DimAutoStyle.Text, minHeight);
  710. view.Text = text;
  711. view.SetRelativeLayout (new (100, 100));
  712. Assert.Equal (new (expectedW, expectedH), view.Frame.Size);
  713. }
  714. [Theory]
  715. [InlineData ("", 0, 0, 0)]
  716. [InlineData (" ", 5, 1, 1)]
  717. [InlineData ("01234", 5, 5, 1)]
  718. [InlineData ("01234", 4, 4, 2)]
  719. [InlineData ("01234ABCDE", 5, 5, 2)]
  720. [InlineData ("01234ABCDE", 1, 1, 10)]
  721. public void DimAutoStyle_Text_Sizes_Correctly_With_Max_Width (string text, int maxWidth, int expectedW, int expectedH)
  722. {
  723. var view = new View ();
  724. view.Width = Auto (DimAutoStyle.Text, maximumContentDim: maxWidth);
  725. view.Height = Auto (DimAutoStyle.Text);
  726. view.Text = text;
  727. view.Layout ();
  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.Layout ();
  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.Layout ();
  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 { Id = "view" };
  824. view.Add (new View { Id = "smaller", Frame = new (0, 0, 5, 5) }); // Smaller subview
  825. view.Add (new View { Id = "larger", 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. #endregion DimAutoStyle.Content tests
  839. // Test variations of Frame
  840. }