Dim.AutoTests.cs 28 KB

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