Dim.AutoTests.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934
  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. [Fact]
  457. public void DimAutoStyle_Auto_Text_Size_Is_Used ()
  458. {
  459. var view = new View
  460. {
  461. Text = "0123\n4567",
  462. Width = Auto (),
  463. Height = Auto ()
  464. };
  465. view.SetRelativeLayout (new (100, 100));
  466. Assert.Equal (new (4, 2), view.Frame.Size);
  467. var subView = new View
  468. {
  469. Text = "ABCD",
  470. Width = Auto (),
  471. Height = Auto ()
  472. };
  473. view.Add (subView);
  474. view.SetRelativeLayout (new (100, 100));
  475. Assert.Equal (new (4, 2), view.Frame.Size);
  476. subView.Text = "ABCDE";
  477. view.SetRelativeLayout (new (100, 100));
  478. Assert.Equal (new (5, 2), view.Frame.Size);
  479. }
  480. [Theory]
  481. [InlineData ("01234", 5, 5)]
  482. [InlineData ("01234", 6, 6)]
  483. [InlineData ("01234", 4, 5)]
  484. [InlineData ("01234", 0, 5)]
  485. [InlineData ("", 5, 5)]
  486. [InlineData ("", 0, 0)]
  487. public void DimAutoStyle_Auto_Larger_Wins (string text, int dimension, int expected)
  488. {
  489. View view = new ()
  490. {
  491. Width = Auto (),
  492. Height = 1,
  493. Text = text
  494. };
  495. View subView = new ()
  496. {
  497. Width = dimension,
  498. Height = 1
  499. };
  500. view.Add (subView);
  501. view.SetRelativeLayout (new (10, 10));
  502. Assert.Equal (expected, view.Frame.Width);
  503. }
  504. #endregion
  505. #region DimAutoStyle.Text tests
  506. [Fact]
  507. public void DimAutoStyle_Text_Viewport_Stays_Set ()
  508. {
  509. var super = new View
  510. {
  511. Width = Fill (),
  512. Height = Fill ()
  513. };
  514. var view = new View
  515. {
  516. Text = "01234567",
  517. Width = Auto (DimAutoStyle.Text),
  518. Height = Auto (DimAutoStyle.Text)
  519. };
  520. super.Add (view);
  521. Rectangle expectedViewport = new (0, 0, 8, 1);
  522. Assert.Equal (expectedViewport.Size, view.GetContentSize ());
  523. Assert.Equal (expectedViewport, view.Frame);
  524. Assert.Equal (expectedViewport, view.Viewport);
  525. super.LayoutSubviews ();
  526. Assert.Equal (expectedViewport, view.Viewport);
  527. super.Dispose ();
  528. }
  529. [Theory]
  530. [InlineData ("", 0, 0)]
  531. [InlineData (" ", 1, 1)]
  532. [InlineData ("01234", 5, 1)]
  533. public void DimAutoStyle_Text_Sizes_Correctly (string text, int expectedW, int expectedH)
  534. {
  535. var view = new View ();
  536. view.Width = Auto (DimAutoStyle.Text);
  537. view.Height = Auto (DimAutoStyle.Text);
  538. view.Text = text;
  539. view.SetRelativeLayout (Application.Screen.Size);
  540. Assert.Equal (new (expectedW, expectedH), view.Frame.Size);
  541. }
  542. [Theory]
  543. [InlineData ("", 0, 0, 0, 0)]
  544. [InlineData (" ", 5, 5, 5, 5)]
  545. [InlineData ("01234", 5, 5, 5, 5)]
  546. [InlineData ("01234", 4, 3, 5, 3)]
  547. [InlineData ("01234ABCDE", 5, 0, 10, 1)]
  548. public void DimAutoStyle_Text_Sizes_Correctly_With_Min (string text, int minWidth, int minHeight, int expectedW, int expectedH)
  549. {
  550. var view = new View ();
  551. view.Width = Auto (DimAutoStyle.Text, minimumContentDim: minWidth);
  552. view.Height = Auto (DimAutoStyle.Text, minimumContentDim: minHeight);
  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)]
  559. [InlineData (" ", 5, 1, 1)]
  560. [InlineData ("01234", 5, 5, 1)]
  561. [InlineData ("01234", 4, 4, 2)]
  562. [InlineData ("01234ABCDE", 5, 5, 2)]
  563. [InlineData ("01234ABCDE", 1, 1, 10)]
  564. public void DimAutoStyle_Text_Sizes_Correctly_With_Max_Width (string text, int maxWidth, int expectedW, int expectedH)
  565. {
  566. var view = new View ();
  567. view.Width = Auto (DimAutoStyle.Text, maximumContentDim: maxWidth);
  568. view.Height = Auto (DimAutoStyle.Text);
  569. view.Text = text;
  570. Assert.Equal (new (expectedW, expectedH), view.Frame.Size);
  571. }
  572. [Theory]
  573. [InlineData ("", 0, 0)]
  574. [InlineData (" ", 1, 1)]
  575. [InlineData ("01234", 5, 1)]
  576. public void DimAutoStyle_Text_Ignores_ContentSize (string text, int expectedW, int expectedH)
  577. {
  578. var view = new View ();
  579. view.Width = Auto (DimAutoStyle.Text);
  580. view.Height = Auto (DimAutoStyle.Text);
  581. view.SetContentSize (new (1, 1));
  582. view.Text = text;
  583. view.SetRelativeLayout (Application.Screen.Size);
  584. Assert.Equal (new (expectedW, expectedH), view.Frame.Size);
  585. }
  586. [Fact]
  587. public void DimAutoStyle_Text_Pos_AnchorEnd_Locates_Correctly ()
  588. {
  589. DimAutoTestView view = new ("01234", Auto (DimAutoStyle.Text), Auto (DimAutoStyle.Text));
  590. view.SetRelativeLayout (new (10, 10));
  591. Assert.Equal (new (5, 1), view.Frame.Size);
  592. Assert.Equal (new (0, 0), view.Frame.Location);
  593. view.X = 0;
  594. view.Y = Pos.AnchorEnd (1);
  595. view.SetRelativeLayout (new (10, 10));
  596. Assert.Equal (new (5, 1), view.Frame.Size);
  597. Assert.Equal (new (0, 9), view.Frame.Location);
  598. view.Y = Pos.AnchorEnd ();
  599. view.SetRelativeLayout (new (10, 10));
  600. Assert.Equal (new (5, 1), view.Frame.Size);
  601. Assert.Equal (new (0, 9), view.Frame.Location);
  602. view.Y = Pos.AnchorEnd () - 1;
  603. view.SetRelativeLayout (new (10, 10));
  604. Assert.Equal (new (5, 1), view.Frame.Size);
  605. Assert.Equal (new (0, 8), view.Frame.Location);
  606. }
  607. #endregion DimAutoStyle.Text tests
  608. #region DimAutoStyle.Content tests
  609. // DimAutoStyle.Content tests
  610. [Fact]
  611. public void DimAutoStyle_Content_UsesContentSize_WhenSet ()
  612. {
  613. var view = new View ();
  614. view.SetContentSize (new (10, 5));
  615. Dim dim = Auto (DimAutoStyle.Content);
  616. int calculatedWidth = dim.Calculate (0, 100, view, Dimension.Width);
  617. Assert.Equal (10, calculatedWidth);
  618. }
  619. [Fact]
  620. public void DimAutoStyle_Content_IgnoresSubviews_When_ContentSize_Is_Set ()
  621. {
  622. var view = new View ();
  623. var subview = new View
  624. {
  625. Frame = new (50, 50, 1, 1)
  626. };
  627. view.SetContentSize (new (10, 5));
  628. Dim dim = Auto (DimAutoStyle.Content);
  629. int calculatedWidth = dim.Calculate (0, 100, view, Dimension.Width);
  630. Assert.Equal (10, calculatedWidth);
  631. }
  632. [Fact]
  633. public void DimAutoStyle_Content_IgnoresText_WhenContentSizeNotSet ()
  634. {
  635. var view = new View { Text = "This is a test" };
  636. Dim dim = Auto (DimAutoStyle.Content);
  637. int calculatedWidth = dim.Calculate (0, 100, view, Dimension.Width);
  638. Assert.Equal (0, calculatedWidth); // Assuming 0 is the default when no ContentSize or Subviews are set
  639. }
  640. [Fact]
  641. public void DimAutoStyle_Content_UsesLargestSubview_WhenContentSizeNotSet ()
  642. {
  643. var view = new View ();
  644. view.Add (new View { Frame = new (0, 0, 5, 5) }); // Smaller subview
  645. view.Add (new View { Frame = new (0, 0, 10, 10) }); // Larger subview
  646. Dim dim = Auto (DimAutoStyle.Content);
  647. int calculatedWidth = dim.Calculate (0, 100, view, Dimension.Width);
  648. Assert.Equal (10, calculatedWidth); // Expecting the size of the largest subview
  649. }
  650. [Fact]
  651. public void DimAutoStyle_Content_UsesContentSize_If_No_Subviews ()
  652. {
  653. DimAutoTestView view = new (Auto (DimAutoStyle.Content), Auto (DimAutoStyle.Content));
  654. view.SetContentSize (new (5, 5));
  655. view.SetRelativeLayout (new (10, 10));
  656. Assert.Equal (new (5, 5), view.Frame.Size);
  657. }
  658. [Fact]
  659. public void DimAutoStyle_Content_Pos_AnchorEnd_Locates_Correctly ()
  660. {
  661. DimAutoTestView view = new (Auto (DimAutoStyle.Content), Auto (DimAutoStyle.Content));
  662. View subView = new ()
  663. {
  664. Width = 5,
  665. Height = 1
  666. };
  667. view.Add (subView);
  668. view.SetRelativeLayout (new (10, 10));
  669. Assert.Equal (new (5, 1), view.Frame.Size);
  670. Assert.Equal (new (0, 0), view.Frame.Location);
  671. view.X = 0;
  672. view.Y = Pos.AnchorEnd (1);
  673. view.SetRelativeLayout (new (10, 10));
  674. Assert.Equal (new (5, 1), view.Frame.Size);
  675. Assert.Equal (new (0, 9), view.Frame.Location);
  676. view.Y = Pos.AnchorEnd ();
  677. view.SetRelativeLayout (new (10, 10));
  678. Assert.Equal (new (5, 1), view.Frame.Size);
  679. Assert.Equal (new (0, 9), view.Frame.Location);
  680. view.Y = Pos.AnchorEnd () - 1;
  681. view.SetRelativeLayout (new (10, 10));
  682. Assert.Equal (new (5, 1), view.Frame.Size);
  683. Assert.Equal (new (0, 8), view.Frame.Location);
  684. }
  685. #endregion DimAutoStyle.Content tests
  686. [Fact]
  687. public void TextFormatter_Settings_Change_View_Size ()
  688. {
  689. View view = new ()
  690. {
  691. Text = "_1234",
  692. Width = Auto ()
  693. };
  694. Assert.Equal (new (4, 0), view.Frame.Size);
  695. view.Height = 1;
  696. view.SetRelativeLayout (Application.Screen.Size);
  697. Assert.Equal (new (4, 1), view.Frame.Size);
  698. Size lastSize = view.Frame.Size;
  699. view.TextAlignment = Alignment.Fill;
  700. Assert.Equal (lastSize, view.Frame.Size);
  701. view = new ()
  702. {
  703. Text = "_1234",
  704. Width = Auto (),
  705. Height = 1
  706. };
  707. view.SetRelativeLayout (Application.Screen.Size);
  708. lastSize = view.Frame.Size;
  709. view.VerticalTextAlignment = Alignment.Center;
  710. Assert.Equal (lastSize, view.Frame.Size);
  711. view = new ()
  712. {
  713. Text = "_1234",
  714. Width = Auto (),
  715. Height = 1
  716. };
  717. view.SetRelativeLayout (Application.Screen.Size);
  718. lastSize = view.Frame.Size;
  719. view.HotKeySpecifier = (Rune)'*';
  720. view.SetRelativeLayout (Application.Screen.Size);
  721. Assert.NotEqual (lastSize, view.Frame.Size);
  722. view = new ()
  723. {
  724. Text = "_1234",
  725. Width = Auto (),
  726. Height = 1
  727. };
  728. view.SetRelativeLayout (Application.Screen.Size);
  729. lastSize = view.Frame.Size;
  730. view.Text = "*ABCD";
  731. Assert.NotEqual (lastSize, view.Frame.Size);
  732. }
  733. // Ensure TextFormatter.AutoSize is never used for View.Text
  734. [Fact]
  735. public void TextFormatter_Is_Not_Auto ()
  736. {
  737. View view = new ();
  738. Assert.False (view.TextFormatter.AutoSize);
  739. view.Width = Auto ();
  740. Assert.False (view.TextFormatter.AutoSize);
  741. view = new ();
  742. Assert.False (view.TextFormatter.AutoSize);
  743. view.Height = Auto ();
  744. Assert.False (view.TextFormatter.AutoSize);
  745. }
  746. [Theory]
  747. [InlineData ("1234", 4)]
  748. [InlineData ("_1234", 4)]
  749. public void HotKey_TextFormatter_Width_Correct (string text, int expected)
  750. {
  751. View view = new ()
  752. {
  753. Text = text,
  754. Height = 1,
  755. Width = Auto ()
  756. };
  757. Assert.Equal (new (expected, 1), view.TextFormatter.Size);
  758. }
  759. [Theory]
  760. [InlineData ("1234", 4)]
  761. [InlineData ("_1234", 4)]
  762. public void HotKey_TextFormatter_Height_Correct (string text, int expected)
  763. {
  764. View view = new ()
  765. {
  766. HotKeySpecifier = (Rune)'_',
  767. Text = text,
  768. Width = Auto (),
  769. Height = 1
  770. };
  771. Assert.Equal (new (expected, 1), view.TextFormatter.Size);
  772. view = new ()
  773. {
  774. HotKeySpecifier = (Rune)'_',
  775. TextDirection = TextDirection.TopBottom_LeftRight,
  776. Text = text,
  777. Width = 1,
  778. Height = Auto ()
  779. };
  780. Assert.Equal (new (1, expected), view.TextFormatter.Size);
  781. }
  782. // Test variations of Frame
  783. }