Dim.AutoTests.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035
  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. 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 (Application.Screen.Size);
  267. lastSize = view.Frame.Size;
  268. view.HotKeySpecifier = (Rune)'*';
  269. view.SetRelativeLayout (Application.Screen.Size);
  270. Assert.NotEqual (lastSize, view.Frame.Size);
  271. view = new ()
  272. {
  273. Text = "_1234",
  274. Width = Auto (),
  275. Height = 1
  276. };
  277. view.SetRelativeLayout (Application.Screen.Size);
  278. lastSize = view.Frame.Size;
  279. view.Text = "*ABCD";
  280. view.SetRelativeLayout (Application.Screen.Size);
  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.Add (subView);
  308. superView.SetRelativeLayout (new (10, 10));
  309. superView.LayoutSubviews (); // no throw
  310. subView.Width = Fill ();
  311. superView.SetRelativeLayout (new (0, 0));
  312. subView.Width = 10;
  313. subView.Height = Fill ();
  314. superView.SetRelativeLayout (new (0, 0));
  315. subView.Height = 10;
  316. subView.Height = Percent (50);
  317. Assert.Throws<LayoutException> (() => superView.SetRelativeLayout (new (0, 0)));
  318. subView.Height = 10;
  319. subView.X = Pos.Center ();
  320. Assert.Throws<LayoutException> (() => superView.SetRelativeLayout (new (0, 0)));
  321. subView.X = 0;
  322. subView.Y = Pos.Center ();
  323. Assert.Throws<LayoutException> (() => superView.SetRelativeLayout (new (0, 0)));
  324. subView.Y = 0;
  325. subView.Width = 10;
  326. subView.Height = 10;
  327. subView.X = 0;
  328. subView.Y = 0;
  329. superView.SetRelativeLayout (new (0, 0));
  330. superView.LayoutSubviews ();
  331. }
  332. // Test validation
  333. [Fact]
  334. public void ValidatePosDim_True_Throws_When_SubView_Uses_SuperView_Dims_Combine ()
  335. {
  336. var superView = new View
  337. {
  338. X = 0,
  339. Y = 0,
  340. Width = Auto (),
  341. Height = Auto (),
  342. ValidatePosDim = true
  343. };
  344. var subView = new View
  345. {
  346. X = 0,
  347. Y = 0,
  348. Width = 10,
  349. Height = 10
  350. };
  351. var subView2 = new View
  352. {
  353. X = 0,
  354. Y = 0,
  355. Width = 10,
  356. Height = 10
  357. };
  358. superView.Add (subView, subView2);
  359. superView.BeginInit ();
  360. superView.EndInit ();
  361. superView.SetRelativeLayout (new (0, 0));
  362. superView.LayoutSubviews (); // no throw
  363. subView.Height = Fill () + 3;
  364. superView.SetRelativeLayout (new (0, 0));
  365. subView.Height = 0;
  366. subView.Height = 3 + Fill ();
  367. superView.SetRelativeLayout (new (0, 0));
  368. subView.Height = 0;
  369. subView.Height = 3 + 5 + Fill ();
  370. superView.SetRelativeLayout (new (0, 0));
  371. subView.Height = 0;
  372. subView.Height = 3 + 5 + Percent (10);
  373. Assert.Throws<LayoutException> (() => superView.SetRelativeLayout (new (0, 0)));
  374. subView.Height = 0;
  375. // Tests nested Combine
  376. subView.Height = 5 + new DimCombine (AddOrSubtract.Add, 3, new DimCombine (AddOrSubtract.Add, Percent (10), 9));
  377. Assert.Throws<LayoutException> (() => superView.SetRelativeLayout (new (0, 0)));
  378. }
  379. [Fact]
  380. public void ValidatePosDim_True_Throws_When_SubView_Uses_SuperView_Pos_Combine ()
  381. {
  382. var superView = new View
  383. {
  384. X = 0,
  385. Y = 0,
  386. Width = Auto (),
  387. Height = Auto (),
  388. ValidatePosDim = true
  389. };
  390. var subView = new View
  391. {
  392. X = 0,
  393. Y = 0,
  394. Width = 10,
  395. Height = 10
  396. };
  397. var subView2 = new View
  398. {
  399. X = 0,
  400. Y = 0,
  401. Width = 10,
  402. Height = 10
  403. };
  404. superView.Add (subView, subView2);
  405. superView.BeginInit ();
  406. superView.EndInit ();
  407. superView.SetRelativeLayout (new (0, 0));
  408. superView.LayoutSubviews (); // no throw
  409. subView.X = Pos.Right (subView2);
  410. superView.SetRelativeLayout (new (0, 0));
  411. superView.LayoutSubviews (); // no throw
  412. subView.X = Pos.Right (subView2) + 3;
  413. superView.SetRelativeLayout (new (0, 0)); // no throw
  414. superView.LayoutSubviews (); // no throw
  415. subView.X = new PosCombine (AddOrSubtract.Add, Pos.Right (subView2), new PosCombine (AddOrSubtract.Add, 7, 9));
  416. superView.SetRelativeLayout (new (0, 0)); // no throw
  417. subView.X = Pos.Center () + 3;
  418. Assert.Throws<LayoutException> (() => superView.SetRelativeLayout (new (0, 0)));
  419. subView.X = 0;
  420. subView.X = 3 + Pos.Center ();
  421. Assert.Throws<LayoutException> (() => superView.SetRelativeLayout (new (0, 0)));
  422. subView.X = 0;
  423. subView.X = 3 + 5 + Pos.Center ();
  424. Assert.Throws<LayoutException> (() => superView.SetRelativeLayout (new (0, 0)));
  425. subView.X = 0;
  426. subView.X = 3 + 5 + Pos.Percent (10);
  427. Assert.Throws<LayoutException> (() => superView.SetRelativeLayout (new (0, 0)));
  428. subView.X = 0;
  429. subView.X = Pos.Percent (10) + Pos.Center ();
  430. Assert.Throws<LayoutException> (() => superView.SetRelativeLayout (new (0, 0)));
  431. subView.X = 0;
  432. // Tests nested Combine
  433. subView.X = 5 + new PosCombine (AddOrSubtract.Add, Pos.Right (subView2), new PosCombine (AddOrSubtract.Add, Pos.Center (), 9));
  434. Assert.Throws<LayoutException> (() => superView.SetRelativeLayout (new (0, 0)));
  435. subView.X = 0;
  436. }
  437. [Theory]
  438. [InlineData (0, 0, 0, 0, 0)]
  439. [InlineData (0, 0, 5, 0, 5)]
  440. [InlineData (0, 0, 0, 5, 0)]
  441. [InlineData (0, 0, 5, 5, 5)]
  442. [InlineData (1, 0, 5, 0, 6)]
  443. [InlineData (1, 0, 0, 5, 1)]
  444. [InlineData (1, 0, 5, 5, 6)]
  445. [InlineData (1, 1, 5, 5, 6)]
  446. [InlineData (-1, 0, 5, 0, 4)]
  447. [InlineData (-1, 0, 0, 5, 0)]
  448. [InlineData (-1, 0, 5, 5, 4)]
  449. [InlineData (-1, -1, 5, 5, 4)]
  450. public void Width_Auto_Height_Absolute_NotChanged (int subX, int subY, int subWidth, int subHeight, int expectedWidth)
  451. {
  452. var superView = new View
  453. {
  454. X = 0,
  455. Y = 0,
  456. Width = Auto (),
  457. Height = 10,
  458. ValidatePosDim = true
  459. };
  460. var subView = new View
  461. {
  462. X = subX,
  463. Y = subY,
  464. Width = subWidth,
  465. Height = subHeight,
  466. ValidatePosDim = true
  467. };
  468. superView.Add (subView);
  469. superView.BeginInit ();
  470. superView.EndInit ();
  471. superView.SetRelativeLayout (new (10, 10));
  472. Assert.Equal (new (0, 0, expectedWidth, 10), superView.Frame);
  473. }
  474. // Test that when a view has Width set to DimAuto (min: x)
  475. // the width is never < x even if SetRelativeLayout is called with smaller bounds
  476. [Theory]
  477. [InlineData (0, 0)]
  478. [InlineData (1, 1)]
  479. [InlineData (3, 3)]
  480. [InlineData (4, 4)]
  481. [InlineData (5, 5)] // No reason why it can exceed container
  482. public void Width_Auto_Min_Honored (int min, int expectedWidth)
  483. {
  484. var superView = new View
  485. {
  486. X = 0,
  487. Y = 0,
  488. Width = Auto (minimumContentDim: min),
  489. Height = 1,
  490. ValidatePosDim = true
  491. };
  492. superView.BeginInit ();
  493. superView.EndInit ();
  494. superView.SetRelativeLayout (new (4, 1));
  495. Assert.Equal (expectedWidth, superView.Frame.Width);
  496. }
  497. [Theory]
  498. [InlineData (0, 1, 1)]
  499. [InlineData (1, 1, 1)]
  500. [InlineData (9, 1, 1)]
  501. [InlineData (10, 1, 1)]
  502. [InlineData (0, 10, 10)]
  503. [InlineData (1, 10, 10)]
  504. [InlineData (9, 10, 10)]
  505. [InlineData (10, 10, 10)]
  506. public void Width_Auto_Subviews_Does_Not_Constrain_To_SuperView (int subX, int subSubViewWidth, int expectedSubWidth)
  507. {
  508. var superView = new View
  509. {
  510. X = 0,
  511. Y = 0,
  512. Width = 10,
  513. Height = 1,
  514. ValidatePosDim = true
  515. };
  516. var subView = new View
  517. {
  518. X = subX,
  519. Y = 0,
  520. Width = Auto (DimAutoStyle.Content),
  521. Height = 1,
  522. ValidatePosDim = true
  523. };
  524. var subSubView = new View
  525. {
  526. X = 0,
  527. Y = 0,
  528. Width = subSubViewWidth,
  529. Height = 1,
  530. ValidatePosDim = true
  531. };
  532. subView.Add (subSubView);
  533. superView.Add (subView);
  534. superView.BeginInit ();
  535. superView.EndInit ();
  536. superView.SetRelativeLayout (superView.GetContentSize ());
  537. superView.LayoutSubviews ();
  538. Assert.Equal (expectedSubWidth, subView.Frame.Width);
  539. }
  540. [Theory]
  541. [InlineData (0, 1, 1)]
  542. [InlineData (1, 1, 1)]
  543. [InlineData (9, 1, 1)]
  544. [InlineData (10, 1, 1)]
  545. [InlineData (0, 10, 10)]
  546. [InlineData (1, 10, 10)]
  547. [InlineData (9, 10, 10)]
  548. [InlineData (10, 10, 10)]
  549. public void Width_Auto_Text_Does_Not_Constrain_To_SuperView (int subX, int textLen, int expectedSubWidth)
  550. {
  551. var superView = new View
  552. {
  553. X = 0,
  554. Y = 0,
  555. Width = 10,
  556. Height = 1,
  557. ValidatePosDim = true
  558. };
  559. var subView = new View
  560. {
  561. Text = new ('*', textLen),
  562. X = subX,
  563. Y = 0,
  564. Width = Auto (DimAutoStyle.Text),
  565. Height = 1,
  566. ValidatePosDim = true
  567. };
  568. superView.Add (subView);
  569. superView.BeginInit ();
  570. superView.EndInit ();
  571. superView.SetRelativeLayout (superView.GetContentSize ());
  572. superView.LayoutSubviews ();
  573. Assert.Equal (expectedSubWidth, subView.Frame.Width);
  574. }
  575. private class DimAutoTestView : View
  576. {
  577. public DimAutoTestView ()
  578. {
  579. ValidatePosDim = true;
  580. Width = Auto ();
  581. Height = Auto ();
  582. }
  583. public DimAutoTestView (Dim width, Dim height)
  584. {
  585. ValidatePosDim = true;
  586. Width = width;
  587. Height = height;
  588. }
  589. public DimAutoTestView (string text, Dim width, Dim height)
  590. {
  591. ValidatePosDim = true;
  592. Text = text;
  593. Width = width;
  594. Height = height;
  595. }
  596. }
  597. #region DimAutoStyle.Auto tests
  598. [Theory]
  599. [InlineData ("", 0, 0)]
  600. [InlineData (" ", 1, 1)]
  601. [InlineData ("01234", 5, 1)]
  602. [InlineData ("01234\nABCDE", 5, 2)]
  603. public void DimAutoStyle_Auto_JustText_Sizes_Correctly (string text, int expectedW, int expectedH)
  604. {
  605. var view = new View ();
  606. view.Width = Auto ();
  607. view.Height = Auto ();
  608. view.Text = text;
  609. view.SetRelativeLayout (Application.Screen.Size);
  610. Assert.Equal (new (expectedW, expectedH), view.Frame.Size);
  611. }
  612. [Fact]
  613. public void DimAutoStyle_Auto_Text_Size_Is_Used ()
  614. {
  615. var view = new View
  616. {
  617. Text = "0123\n4567",
  618. Width = Auto (),
  619. Height = Auto ()
  620. };
  621. view.SetRelativeLayout (new (100, 100));
  622. Assert.Equal (new (4, 2), view.Frame.Size);
  623. var subView = new View
  624. {
  625. Text = "ABCD",
  626. Width = Auto (),
  627. Height = Auto ()
  628. };
  629. view.Add (subView);
  630. view.SetRelativeLayout (new (100, 100));
  631. Assert.Equal (new (4, 2), view.Frame.Size);
  632. subView.Text = "ABCDE";
  633. view.SetRelativeLayout (new (100, 100));
  634. Assert.Equal (new (5, 2), view.Frame.Size);
  635. }
  636. [Theory]
  637. [InlineData ("01234", 5, 5)]
  638. [InlineData ("01234", 6, 6)]
  639. [InlineData ("01234", 4, 5)]
  640. [InlineData ("01234", 0, 5)]
  641. [InlineData ("", 5, 5)]
  642. [InlineData ("", 0, 0)]
  643. public void DimAutoStyle_Auto_Larger_Wins (string text, int dimension, int expected)
  644. {
  645. View view = new ()
  646. {
  647. Width = Auto (),
  648. Height = 1,
  649. Text = text
  650. };
  651. View subView = new ()
  652. {
  653. Width = dimension,
  654. Height = 1
  655. };
  656. view.Add (subView);
  657. view.SetRelativeLayout (new (10, 10));
  658. Assert.Equal (expected, view.Frame.Width);
  659. }
  660. #endregion
  661. #region DimAutoStyle.Text tests
  662. [Fact]
  663. public void DimAutoStyle_Text_Viewport_Stays_Set ()
  664. {
  665. var super = new View
  666. {
  667. Width = Fill (),
  668. Height = Fill ()
  669. };
  670. var view = new View
  671. {
  672. Text = "01234567",
  673. Width = Auto (DimAutoStyle.Text),
  674. Height = Auto (DimAutoStyle.Text)
  675. };
  676. super.Add (view);
  677. super.Layout ();
  678. Rectangle expectedViewport = new (0, 0, 8, 1);
  679. Assert.Equal (expectedViewport.Size, view.GetContentSize ());
  680. Assert.Equal (expectedViewport, view.Frame);
  681. Assert.Equal (expectedViewport, view.Viewport);
  682. super.Layout ();
  683. Assert.Equal (expectedViewport, view.Viewport);
  684. super.Dispose ();
  685. }
  686. [Theory]
  687. [InlineData ("", 0, 0)]
  688. [InlineData (" ", 1, 1)]
  689. [InlineData ("01234", 5, 1)]
  690. [InlineData ("01234\nABCDE", 5, 2)]
  691. public void DimAutoStyle_Text_Sizes_Correctly (string text, int expectedW, int expectedH)
  692. {
  693. var view = new View ();
  694. view.Width = Auto (DimAutoStyle.Text);
  695. view.Height = Auto (DimAutoStyle.Text);
  696. view.Text = text;
  697. view.SetRelativeLayout (Application.Screen.Size);
  698. Assert.Equal (new (expectedW, expectedH), view.Frame.Size);
  699. }
  700. [Theory]
  701. [InlineData ("", 0, 0, 0, 0)]
  702. [InlineData (" ", 5, 5, 5, 5)]
  703. [InlineData ("01234", 5, 5, 5, 5)]
  704. [InlineData ("01234", 4, 3, 5, 3)]
  705. [InlineData ("01234ABCDE", 5, 0, 10, 1)]
  706. public void DimAutoStyle_Text_Sizes_Correctly_With_Min (string text, int minWidth, int minHeight, int expectedW, int expectedH)
  707. {
  708. var view = new View ();
  709. view.Width = Auto (DimAutoStyle.Text, minWidth);
  710. view.Height = Auto (DimAutoStyle.Text, minHeight);
  711. view.Text = text;
  712. view.SetRelativeLayout (Application.Screen.Size);
  713. Assert.Equal (new (expectedW, expectedH), view.Frame.Size);
  714. }
  715. [Theory]
  716. [InlineData ("", 0, 0, 0)]
  717. [InlineData (" ", 5, 1, 1)]
  718. [InlineData ("01234", 5, 5, 1)]
  719. [InlineData ("01234", 4, 4, 2)]
  720. [InlineData ("01234ABCDE", 5, 5, 2)]
  721. [InlineData ("01234ABCDE", 1, 1, 10)]
  722. public void DimAutoStyle_Text_Sizes_Correctly_With_Max_Width (string text, int maxWidth, int expectedW, int expectedH)
  723. {
  724. var view = new View ();
  725. view.Width = Auto (DimAutoStyle.Text, maximumContentDim: maxWidth);
  726. view.Height = Auto (DimAutoStyle.Text);
  727. view.Text = text;
  728. view.Layout ();
  729. Assert.Equal (new (expectedW, expectedH), view.Frame.Size);
  730. }
  731. [Theory]
  732. [InlineData ("", 0, 0)]
  733. [InlineData (" ", 1, 1)]
  734. [InlineData ("01234", 5, 1)]
  735. [InlineData ("01234ABCDE", 10, 1)]
  736. [InlineData ("01234\nABCDE", 5, 2)]
  737. public void DimAutoStyle_Text_NoMin_Not_Constrained_By_ContentSize (string text, int expectedW, int expectedH)
  738. {
  739. var view = new View ();
  740. view.Width = Auto (DimAutoStyle.Text);
  741. view.Height = Auto (DimAutoStyle.Text);
  742. view.SetContentSize (new (1, 1));
  743. view.Text = text;
  744. view.Layout ();
  745. Assert.Equal (new (expectedW, expectedH), view.Frame.Size);
  746. }
  747. [Theory]
  748. [InlineData ("", 0, 0)]
  749. [InlineData (" ", 1, 1)]
  750. [InlineData ("01234", 5, 1)]
  751. [InlineData ("01234ABCDE", 10, 1)]
  752. [InlineData ("01234\nABCDE", 5, 2)]
  753. public void DimAutoStyle_Text_NoMin_Not_Constrained_By_SuperView (string text, int expectedW, int expectedH)
  754. {
  755. var superView = new View
  756. {
  757. Width = 1, Height = 1
  758. };
  759. var view = new View ();
  760. view.Width = Auto (DimAutoStyle.Text);
  761. view.Height = Auto (DimAutoStyle.Text);
  762. view.Text = text;
  763. superView.Add (view);
  764. superView.Layout ();
  765. Assert.Equal (new (expectedW, expectedH), view.Frame.Size);
  766. }
  767. [Fact]
  768. public void DimAutoStyle_Text_Pos_AnchorEnd_Locates_Correctly ()
  769. {
  770. DimAutoTestView view = new ("01234", Auto (DimAutoStyle.Text), Auto (DimAutoStyle.Text));
  771. view.SetRelativeLayout (new (10, 10));
  772. Assert.Equal (new (5, 1), view.Frame.Size);
  773. Assert.Equal (new (0, 0), view.Frame.Location);
  774. view.X = 0;
  775. view.Y = Pos.AnchorEnd (1);
  776. view.SetRelativeLayout (new (10, 10));
  777. Assert.Equal (new (5, 1), view.Frame.Size);
  778. Assert.Equal (new (0, 9), view.Frame.Location);
  779. view.Y = Pos.AnchorEnd ();
  780. view.SetRelativeLayout (new (10, 10));
  781. Assert.Equal (new (5, 1), view.Frame.Size);
  782. Assert.Equal (new (0, 9), view.Frame.Location);
  783. view.Y = Pos.AnchorEnd () - 1;
  784. view.SetRelativeLayout (new (10, 10));
  785. Assert.Equal (new (5, 1), view.Frame.Size);
  786. Assert.Equal (new (0, 8), view.Frame.Location);
  787. }
  788. #endregion DimAutoStyle.Text tests
  789. #region DimAutoStyle.Content tests
  790. // DimAutoStyle.Content tests
  791. [Fact]
  792. public void DimAutoStyle_Content_UsesContentSize_WhenSet ()
  793. {
  794. var view = new View ();
  795. view.SetContentSize (new (10, 5));
  796. Dim dim = Auto (DimAutoStyle.Content);
  797. int calculatedWidth = dim.Calculate (0, 100, view, Dimension.Width);
  798. Assert.Equal (10, calculatedWidth);
  799. }
  800. [Fact]
  801. public void DimAutoStyle_Content_IgnoresSubviews_When_ContentSize_Is_Set ()
  802. {
  803. var view = new View ();
  804. var subview = new View
  805. {
  806. Frame = new (50, 50, 1, 1)
  807. };
  808. view.SetContentSize (new (10, 5));
  809. Dim dim = Auto (DimAutoStyle.Content);
  810. int calculatedWidth = dim.Calculate (0, 100, view, Dimension.Width);
  811. Assert.Equal (10, calculatedWidth);
  812. }
  813. [Fact]
  814. public void DimAutoStyle_Content_IgnoresText_WhenContentSizeNotSet ()
  815. {
  816. var view = new View { Text = "This is a test" };
  817. Dim dim = Auto (DimAutoStyle.Content);
  818. int calculatedWidth = dim.Calculate (0, 100, view, Dimension.Width);
  819. Assert.Equal (0, calculatedWidth); // Assuming 0 is the default when no ContentSize or Subviews are set
  820. }
  821. [Fact]
  822. public void DimAutoStyle_Content_UsesLargestSubview_WhenContentSizeNotSet ()
  823. {
  824. var view = new View { Id = "view" };
  825. view.Add (new View { Id = "smaller", Frame = new (0, 0, 5, 5) }); // Smaller subview
  826. view.Add (new View { Id = "larger", Frame = new (0, 0, 10, 10) }); // Larger subview
  827. Dim dim = Auto (DimAutoStyle.Content);
  828. int calculatedWidth = dim.Calculate (0, 100, view, Dimension.Width);
  829. Assert.Equal (10, calculatedWidth); // Expecting the size of the largest subview
  830. }
  831. [Fact]
  832. public void DimAutoStyle_Content_UsesContentSize_If_No_Subviews ()
  833. {
  834. DimAutoTestView view = new (Auto (DimAutoStyle.Content), Auto (DimAutoStyle.Content));
  835. view.SetContentSize (new (5, 5));
  836. view.SetRelativeLayout (new (10, 10));
  837. Assert.Equal (new (5, 5), view.Frame.Size);
  838. }
  839. [Fact]
  840. [SetupFakeDriver]
  841. public void DimAutoStyle_Content_Pos_AnchorEnd_Locates_Correctly ()
  842. {
  843. (((FakeDriver)Application.Driver)!).SetBufferSize(10,10);
  844. DimAutoTestView view = new (Auto (DimAutoStyle.Content), Auto (DimAutoStyle.Content));
  845. View subView = new ()
  846. {
  847. Width = 5,
  848. Height = 1
  849. };
  850. view.Add (subView);
  851. view.Layout ();
  852. Assert.Equal (new (5, 1), view.Frame.Size);
  853. Assert.Equal (new (0, 0), view.Frame.Location);
  854. view.X = 0;
  855. view.Y = Pos.AnchorEnd (1);
  856. view.Layout ();
  857. Assert.Equal (new (5, 1), view.Frame.Size);
  858. Assert.Equal (new (0, 9), view.Frame.Location);
  859. view.Y = Pos.AnchorEnd ();
  860. view.Layout ();
  861. Assert.Equal (new (5, 1), view.Frame.Size);
  862. Assert.Equal (new (0, 9), view.Frame.Location);
  863. view.Y = Pos.AnchorEnd () - 1;
  864. view.Layout ();
  865. Assert.Equal (new (5, 1), view.Frame.Size);
  866. Assert.Equal (new (0, 8), view.Frame.Location);
  867. }
  868. #endregion DimAutoStyle.Content tests
  869. // Test variations of Frame
  870. }