Dim.AutoTests.cs 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104
  1. using System.Globalization;
  2. using System.Text;
  3. using Xunit.Abstractions;
  4. using static Terminal.Gui.Dim;
  5. namespace Terminal.Gui.PosDimTests;
  6. public class DimAutoTests (ITestOutputHelper output)
  7. {
  8. private readonly ITestOutputHelper _output = output;
  9. // Test min - ensure that if min is specified in the DimAuto constructor it is honored
  10. [Fact]
  11. public void DimAuto_Min ()
  12. {
  13. var superView = new View
  14. {
  15. X = 0,
  16. Y = 0,
  17. Width = Dim.Auto (min: 10),
  18. Height = Dim.Auto (min: 10),
  19. ValidatePosDim = true
  20. };
  21. var subView = new View
  22. {
  23. X = 0,
  24. Y = 0,
  25. Width = 5,
  26. Height = 5
  27. };
  28. superView.Add (subView);
  29. superView.BeginInit ();
  30. superView.EndInit ();
  31. superView.SetRelativeLayout (new (10, 10));
  32. superView.LayoutSubviews (); // no throw
  33. Assert.Equal (10, superView.Frame.Width);
  34. Assert.Equal (10, superView.Frame.Height);
  35. }
  36. // what happens if DimAuto (min: 10) and the subview moves to a negative coord?
  37. [Fact]
  38. public void DimAuto_Min_Resets_If_Subview_Moves_Negative ()
  39. {
  40. var superView = new View
  41. {
  42. X = 0,
  43. Y = 0,
  44. Width = Dim.Auto (min: 10),
  45. Height = Dim.Auto (min: 10),
  46. ValidatePosDim = true
  47. };
  48. var subView = new View
  49. {
  50. X = 0,
  51. Y = 0,
  52. Width = 5,
  53. Height = 5
  54. };
  55. superView.Add (subView);
  56. superView.BeginInit ();
  57. superView.EndInit ();
  58. superView.SetRelativeLayout (new (10, 10));
  59. superView.LayoutSubviews (); // no throw
  60. Assert.Equal (10, superView.Frame.Width);
  61. Assert.Equal (10, superView.Frame.Height);
  62. subView.X = -1;
  63. subView.Y = -1;
  64. superView.SetRelativeLayout (new (10, 10));
  65. superView.LayoutSubviews (); // no throw
  66. Assert.Equal (5, subView.Frame.Width);
  67. Assert.Equal (5, subView.Frame.Height);
  68. Assert.Equal (10, superView.Frame.Width);
  69. Assert.Equal (10, superView.Frame.Height);
  70. }
  71. [Fact]
  72. public void DimAuto_Min_Resets_If_Subview_Shrinks ()
  73. {
  74. var superView = new View
  75. {
  76. X = 0,
  77. Y = 0,
  78. Width = Dim.Auto (min: 10),
  79. Height = Dim.Auto (min: 10),
  80. ValidatePosDim = true
  81. };
  82. var subView = new View
  83. {
  84. X = 0,
  85. Y = 0,
  86. Width = 5,
  87. Height = 5
  88. };
  89. superView.Add (subView);
  90. superView.BeginInit ();
  91. superView.EndInit ();
  92. superView.SetRelativeLayout (new (10, 10));
  93. superView.LayoutSubviews (); // no throw
  94. Assert.Equal (10, superView.Frame.Width);
  95. Assert.Equal (10, superView.Frame.Height);
  96. subView.Width = 3;
  97. subView.Height = 3;
  98. superView.SetRelativeLayout (new (10, 10));
  99. superView.LayoutSubviews (); // no throw
  100. Assert.Equal (3, subView.Frame.Width);
  101. Assert.Equal (3, subView.Frame.Height);
  102. Assert.Equal (10, superView.Frame.Width);
  103. Assert.Equal (10, superView.Frame.Height);
  104. }
  105. [Theory]
  106. [InlineData (0, 0, 0, 0, 0)]
  107. [InlineData (0, 0, 5, 0, 0)]
  108. [InlineData (0, 0, 0, 5, 5)]
  109. [InlineData (0, 0, 5, 5, 5)]
  110. [InlineData (1, 0, 5, 0, 0)]
  111. [InlineData (1, 0, 0, 5, 5)]
  112. [InlineData (1, 0, 5, 5, 5)]
  113. [InlineData (1, 1, 5, 5, 6)]
  114. [InlineData (-1, 0, 5, 0, 0)]
  115. [InlineData (-1, 0, 0, 5, 5)]
  116. [InlineData (-1, 0, 5, 5, 5)]
  117. [InlineData (-1, -1, 5, 5, 4)]
  118. public void Height_Auto_Width_NotChanged (int subX, int subY, int subWidth, int subHeight, int expectedHeight)
  119. {
  120. var superView = new View
  121. {
  122. X = 0,
  123. Y = 0,
  124. Width = 10,
  125. Height = Dim.Auto (),
  126. ValidatePosDim = true
  127. };
  128. var subView = new View
  129. {
  130. X = subX,
  131. Y = subY,
  132. Width = subWidth,
  133. Height = subHeight,
  134. ValidatePosDim = true
  135. };
  136. superView.Add (subView);
  137. superView.BeginInit ();
  138. superView.EndInit ();
  139. superView.SetRelativeLayout (new (10, 10));
  140. Assert.Equal (new Rectangle (0, 0, 10, expectedHeight), superView.Frame);
  141. }
  142. [Fact]
  143. public void NoSubViews_Does_Nothing ()
  144. {
  145. var superView = new View
  146. {
  147. X = 0,
  148. Y = 0,
  149. Width = Dim.Auto (),
  150. Height = Dim.Auto (),
  151. ValidatePosDim = true
  152. };
  153. superView.BeginInit ();
  154. superView.EndInit ();
  155. superView.SetRelativeLayout (new (10, 10));
  156. Assert.Equal (new Rectangle (0, 0, 0, 0), superView.Frame);
  157. superView.SetRelativeLayout (new (10, 10));
  158. Assert.Equal (new Rectangle (0, 0, 0, 0), superView.Frame);
  159. }
  160. [Fact]
  161. public void NoSubViews_Does_Nothing_Vertical ()
  162. {
  163. var superView = new View
  164. {
  165. X = 0,
  166. Y = 0,
  167. Width = Dim.Auto (),
  168. Height = Dim.Auto (),
  169. TextDirection = TextDirection.TopBottom_LeftRight,
  170. ValidatePosDim = true
  171. };
  172. superView.BeginInit ();
  173. superView.EndInit ();
  174. superView.SetRelativeLayout (new (10, 10));
  175. Assert.Equal (new Rectangle (0, 0, 0, 0), superView.Frame);
  176. superView.SetRelativeLayout (new (10, 10));
  177. Assert.Equal (new Rectangle (0, 0, 0, 0), superView.Frame);
  178. }
  179. [Theory]
  180. [InlineData (0, 0, 0, 0, 0, 0)]
  181. [InlineData (0, 0, 5, 0, 5, 0)]
  182. [InlineData (0, 0, 0, 5, 0, 5)]
  183. [InlineData (0, 0, 5, 5, 5, 5)]
  184. [InlineData (1, 0, 5, 0, 6, 0)]
  185. [InlineData (1, 0, 0, 5, 1, 5)]
  186. [InlineData (1, 0, 5, 5, 6, 5)]
  187. [InlineData (1, 1, 5, 5, 6, 6)]
  188. [InlineData (-1, 0, 5, 0, 4, 0)]
  189. [InlineData (-1, 0, 0, 5, 0, 5)]
  190. [InlineData (-1, 0, 5, 5, 4, 5)]
  191. [InlineData (-1, -1, 5, 5, 4, 4)]
  192. public void SubView_Changes_SuperView_Size (int subX, int subY, int subWidth, int subHeight, int expectedWidth, int expectedHeight)
  193. {
  194. var superView = new View
  195. {
  196. X = 0,
  197. Y = 0,
  198. Width = Dim.Auto (),
  199. Height = Dim.Auto (),
  200. ValidatePosDim = true
  201. };
  202. var subView = new View
  203. {
  204. X = subX,
  205. Y = subY,
  206. Width = subWidth,
  207. Height = subHeight,
  208. ValidatePosDim = true
  209. };
  210. superView.Add (subView);
  211. superView.BeginInit ();
  212. superView.EndInit ();
  213. superView.SetRelativeLayout (new (10, 10));
  214. Assert.Equal (new Rectangle (0, 0, expectedWidth, expectedHeight), superView.Frame);
  215. }
  216. // Test validation
  217. [Fact]
  218. public void ValidatePosDim_True_Throws_When_SubView_Uses_SuperView_Dims ()
  219. {
  220. var superView = new View
  221. {
  222. X = 0,
  223. Y = 0,
  224. Width = Dim.Auto (),
  225. Height = Dim.Auto (),
  226. ValidatePosDim = true
  227. };
  228. var subView = new View
  229. {
  230. X = 0,
  231. Y = 0,
  232. Width = Dim.Fill (),
  233. Height = 10,
  234. ValidatePosDim = true
  235. };
  236. superView.BeginInit ();
  237. superView.EndInit ();
  238. Assert.Throws<InvalidOperationException> (() => superView.Add (subView));
  239. subView.Width = 10;
  240. superView.Add (subView);
  241. superView.SetRelativeLayout (new (10, 10));
  242. superView.LayoutSubviews (); // no throw
  243. subView.Width = Dim.Fill ();
  244. Assert.Throws<InvalidOperationException> (() => superView.SetRelativeLayout (new (0, 0)));
  245. subView.Width = 10;
  246. subView.Height = Dim.Fill ();
  247. Assert.Throws<InvalidOperationException> (() => superView.SetRelativeLayout (new (0, 0)));
  248. subView.Height = 10;
  249. subView.Height = Dim.Percent (50);
  250. Assert.Throws<InvalidOperationException> (() => superView.SetRelativeLayout (new (0, 0)));
  251. subView.Height = 10;
  252. subView.X = Pos.Center ();
  253. Assert.Throws<InvalidOperationException> (() => superView.SetRelativeLayout (new (0, 0)));
  254. subView.X = 0;
  255. subView.Y = Pos.Center ();
  256. Assert.Throws<InvalidOperationException> (() => superView.SetRelativeLayout (new (0, 0)));
  257. subView.Y = 0;
  258. subView.Width = 10;
  259. subView.Height = 10;
  260. subView.X = 0;
  261. subView.Y = 0;
  262. superView.SetRelativeLayout (new (0, 0));
  263. superView.LayoutSubviews ();
  264. }
  265. // Test validation
  266. [Fact]
  267. public void ValidatePosDim_True_Throws_When_SubView_Uses_SuperView_Dims_Combine ()
  268. {
  269. var superView = new View
  270. {
  271. X = 0,
  272. Y = 0,
  273. Width = Dim.Auto (),
  274. Height = Dim.Auto (),
  275. ValidatePosDim = true
  276. };
  277. var subView = new View
  278. {
  279. X = 0,
  280. Y = 0,
  281. Width = 10,
  282. Height = 10
  283. };
  284. var subView2 = new View
  285. {
  286. X = 0,
  287. Y = 0,
  288. Width = 10,
  289. Height = 10
  290. };
  291. superView.Add (subView, subView2);
  292. superView.BeginInit ();
  293. superView.EndInit ();
  294. superView.SetRelativeLayout (new (0, 0));
  295. superView.LayoutSubviews (); // no throw
  296. subView.Height = Dim.Fill () + 3;
  297. Assert.Throws<InvalidOperationException> (() => superView.SetRelativeLayout (new (0, 0)));
  298. subView.Height = 0;
  299. subView.Height = 3 + Dim.Fill ();
  300. Assert.Throws<InvalidOperationException> (() => superView.SetRelativeLayout (new (0, 0)));
  301. subView.Height = 0;
  302. subView.Height = 3 + 5 + Dim.Fill ();
  303. Assert.Throws<InvalidOperationException> (() => superView.SetRelativeLayout (new (0, 0)));
  304. subView.Height = 0;
  305. subView.Height = 3 + 5 + Dim.Percent (10);
  306. Assert.Throws<InvalidOperationException> (() => superView.SetRelativeLayout (new (0, 0)));
  307. subView.Height = 0;
  308. // Tests nested Combine
  309. subView.Height = 5 + new Dim.DimCombine (true, 3, new Dim.DimCombine (true, Dim.Percent (10), 9));
  310. Assert.Throws<InvalidOperationException> (() => superView.SetRelativeLayout (new (0, 0)));
  311. }
  312. [Fact]
  313. public void ValidatePosDim_True_Throws_When_SubView_Uses_SuperView_Pos_Combine ()
  314. {
  315. var superView = new View
  316. {
  317. X = 0,
  318. Y = 0,
  319. Width = Dim.Auto (),
  320. Height = Dim.Auto (),
  321. ValidatePosDim = true
  322. };
  323. var subView = new View
  324. {
  325. X = 0,
  326. Y = 0,
  327. Width = 10,
  328. Height = 10
  329. };
  330. var subView2 = new View
  331. {
  332. X = 0,
  333. Y = 0,
  334. Width = 10,
  335. Height = 10
  336. };
  337. superView.Add (subView, subView2);
  338. superView.BeginInit ();
  339. superView.EndInit ();
  340. superView.SetRelativeLayout (new (0, 0));
  341. superView.LayoutSubviews (); // no throw
  342. subView.X = Pos.Right (subView2);
  343. superView.SetRelativeLayout (new (0, 0));
  344. superView.LayoutSubviews (); // no throw
  345. subView.X = Pos.Right (subView2) + 3;
  346. superView.SetRelativeLayout (new (0, 0)); // no throw
  347. superView.LayoutSubviews (); // no throw
  348. subView.X = new Pos.PosCombine (true, Pos.Right (subView2), new Pos.PosCombine (true, 7, 9));
  349. superView.SetRelativeLayout (new (0, 0)); // no throw
  350. subView.X = Pos.Center () + 3;
  351. Assert.Throws<InvalidOperationException> (() => superView.SetRelativeLayout (new (0, 0)));
  352. subView.X = 0;
  353. subView.X = 3 + Pos.Center ();
  354. Assert.Throws<InvalidOperationException> (() => superView.SetRelativeLayout (new (0, 0)));
  355. subView.X = 0;
  356. subView.X = 3 + 5 + Pos.Center ();
  357. Assert.Throws<InvalidOperationException> (() => superView.SetRelativeLayout (new (0, 0)));
  358. subView.X = 0;
  359. subView.X = 3 + 5 + Pos.Percent (10);
  360. Assert.Throws<InvalidOperationException> (() => superView.SetRelativeLayout (new (0, 0)));
  361. subView.X = 0;
  362. subView.X = Pos.Percent (10) + Pos.Center ();
  363. Assert.Throws<InvalidOperationException> (() => superView.SetRelativeLayout (new (0, 0)));
  364. subView.X = 0;
  365. // Tests nested Combine
  366. subView.X = 5 + new Pos.PosCombine (true, Pos.Right (subView2), new Pos.PosCombine (true, Pos.Center (), 9));
  367. Assert.Throws<InvalidOperationException> (() => superView.SetRelativeLayout (new (0, 0)));
  368. subView.X = 0;
  369. }
  370. [Theory]
  371. [InlineData (0, 0, 0, 0, 0)]
  372. [InlineData (0, 0, 5, 0, 5)]
  373. [InlineData (0, 0, 0, 5, 0)]
  374. [InlineData (0, 0, 5, 5, 5)]
  375. [InlineData (1, 0, 5, 0, 6)]
  376. [InlineData (1, 0, 0, 5, 1)]
  377. [InlineData (1, 0, 5, 5, 6)]
  378. [InlineData (1, 1, 5, 5, 6)]
  379. [InlineData (-1, 0, 5, 0, 4)]
  380. [InlineData (-1, 0, 0, 5, 0)]
  381. [InlineData (-1, 0, 5, 5, 4)]
  382. [InlineData (-1, -1, 5, 5, 4)]
  383. public void Width_Auto_Height_NotChanged (int subX, int subY, int subWidth, int subHeight, int expectedWidth)
  384. {
  385. var superView = new View
  386. {
  387. X = 0,
  388. Y = 0,
  389. Width = Dim.Auto (),
  390. Height = 10,
  391. ValidatePosDim = true
  392. };
  393. var subView = new View
  394. {
  395. X = subX,
  396. Y = subY,
  397. Width = subWidth,
  398. Height = subHeight,
  399. ValidatePosDim = true
  400. };
  401. superView.Add (subView);
  402. superView.BeginInit ();
  403. superView.EndInit ();
  404. superView.SetRelativeLayout (new (10, 10));
  405. Assert.Equal (new Rectangle (0, 0, expectedWidth, 10), superView.Frame);
  406. }
  407. // Test that when a view has Width set to DimAuto (min: x) the width is never < x even if SetRelativeLayout is called with smaller bounds
  408. [Theory]
  409. [InlineData (0, 0)]
  410. [InlineData (1, 1)]
  411. [InlineData (3, 3)]
  412. [InlineData (4, 4)]
  413. [InlineData (5, 4)] // This is clearly invalid, but we choose to not throw but log a debug message
  414. public void Width_Auto_Min (int min, int expectedWidth)
  415. {
  416. var superView = new View
  417. {
  418. X = 0,
  419. Y = 0,
  420. Width = Dim.Auto (min: min),
  421. Height = 1,
  422. ValidatePosDim = true
  423. };
  424. superView.BeginInit ();
  425. superView.EndInit ();
  426. superView.SetRelativeLayout (new (4, 1));
  427. Assert.Equal (expectedWidth, superView.Frame.Width);
  428. }
  429. // Test Dim.Fill - Fill should not impact width of the DimAuto superview
  430. [Theory]
  431. [InlineData (0, 0, 0, 10, 10)]
  432. [InlineData (0, 1, 0, 10, 10)]
  433. [InlineData (0, 11, 0, 10, 10)]
  434. [InlineData (0, 10, 0, 10, 10)]
  435. [InlineData (0, 5, 0, 10, 10)]
  436. [InlineData (1, 5, 0, 10, 9)]
  437. [InlineData (1, 10, 0, 10, 9)]
  438. [InlineData (0, 0, 1, 10, 9)]
  439. [InlineData (0, 10, 1, 10, 9)]
  440. [InlineData (0, 5, 1, 10, 9)]
  441. [InlineData (1, 5, 1, 10, 8)]
  442. [InlineData (1, 10, 1, 10, 8)]
  443. public void Width_Fill_Fills (int subX, int superMinWidth, int fill, int expectedSuperWidth, int expectedSubWidth)
  444. {
  445. var superView = new View
  446. {
  447. X = 0,
  448. Y = 0,
  449. Width = Dim.Auto (min: superMinWidth),
  450. Height = 1,
  451. ValidatePosDim = true
  452. };
  453. var subView = new View
  454. {
  455. X = subX,
  456. Y = 0,
  457. Width = Dim.Fill (fill),
  458. Height = 1,
  459. ValidatePosDim = true
  460. };
  461. superView.Add (subView);
  462. superView.BeginInit ();
  463. superView.EndInit ();
  464. superView.SetRelativeLayout (new (10, 1));
  465. Assert.Equal (expectedSuperWidth, superView.Frame.Width);
  466. superView.LayoutSubviews ();
  467. Assert.Equal (expectedSubWidth, subView.Frame.Width);
  468. Assert.Equal (expectedSuperWidth, superView.Frame.Width);
  469. }
  470. [Theory]
  471. [InlineData (0, 1, 1)]
  472. [InlineData (1, 1, 1)]
  473. [InlineData (9, 1, 1)]
  474. [InlineData (10, 1, 1)]
  475. [InlineData (0, 10, 10)]
  476. [InlineData (1, 10, 10)]
  477. [InlineData (9, 10, 10)]
  478. [InlineData (10, 10, 10)]
  479. public void Width_Auto_Text_Does_Not_Constrain_To_SuperView (int subX, int textLen, int expectedSubWidth)
  480. {
  481. var superView = new View
  482. {
  483. X = 0,
  484. Y = 0,
  485. Width = 10,
  486. Height = 1,
  487. ValidatePosDim = true
  488. };
  489. var subView = new View
  490. {
  491. Text = new string ('*', textLen),
  492. X = subX,
  493. Y = 0,
  494. Width = Dim.Auto (Dim.DimAutoStyle.Text),
  495. Height = 1,
  496. ValidatePosDim = true
  497. };
  498. superView.Add (subView);
  499. superView.BeginInit ();
  500. superView.EndInit ();
  501. superView.SetRelativeLayout (superView.ContentSize.GetValueOrDefault ());
  502. superView.LayoutSubviews ();
  503. Assert.Equal (expectedSubWidth, subView.Frame.Width);
  504. }
  505. [Theory]
  506. [InlineData (0, 1, 1)]
  507. [InlineData (1, 1, 1)]
  508. [InlineData (9, 1, 1)]
  509. [InlineData (10, 1, 1)]
  510. [InlineData (0, 10, 10)]
  511. [InlineData (1, 10, 10)]
  512. [InlineData (9, 10, 10)]
  513. [InlineData (10, 10, 10)]
  514. public void Width_Auto_Subviews_Does_Not_Constrain_To_SuperView (int subX, int subSubViewWidth, int expectedSubWidth)
  515. {
  516. var superView = new View
  517. {
  518. X = 0,
  519. Y = 0,
  520. Width = 10,
  521. Height = 1,
  522. ValidatePosDim = true
  523. };
  524. var subView = new View
  525. {
  526. X = subX,
  527. Y = 0,
  528. Width = Dim.Auto (Dim.DimAutoStyle.Content),
  529. Height = 1,
  530. ValidatePosDim = true
  531. };
  532. var subSubView = new View
  533. {
  534. X = 0,
  535. Y = 0,
  536. Width = subSubViewWidth,
  537. Height = 1,
  538. ValidatePosDim = true
  539. };
  540. subView.Add (subSubView);
  541. superView.Add (subView);
  542. superView.BeginInit ();
  543. superView.EndInit ();
  544. superView.SetRelativeLayout (superView.ContentSize);
  545. superView.LayoutSubviews ();
  546. Assert.Equal (expectedSubWidth, subView.Frame.Width);
  547. }
  548. [Fact]
  549. public void DimAuto_Text_Viewport_Stays_Set ()
  550. {
  551. var super = new View ()
  552. {
  553. Width = Dim.Fill (),
  554. Height = Dim.Fill ()
  555. };
  556. var view = new View ()
  557. {
  558. Text = "01234567",
  559. Width = Auto (DimAutoStyle.Text),
  560. Height = Auto (DimAutoStyle.Text),
  561. };
  562. super.Add (view);
  563. Rectangle expectedViewport = new (0, 0, 8, 1);
  564. Assert.Equal (expectedViewport.Size, view.ContentSize);
  565. Assert.Equal (expectedViewport, view.Frame);
  566. Assert.Equal (expectedViewport, view.Viewport);
  567. super.LayoutSubviews ();
  568. Assert.Equal (expectedViewport, view.Viewport);
  569. super.Dispose ();
  570. }
  571. // Test that changing TextFormatter does not impact View dimensions if Dim.Auto is not in play
  572. [Fact]
  573. public void DimAuto_Not_Used_TextFormatter_Does_Not_Change_View_Size ()
  574. {
  575. View view = new ()
  576. {
  577. Text = "_1234"
  578. };
  579. Assert.False (view.TextFormatter.AutoSize);
  580. Assert.Equal (Size.Empty, view.Frame.Size);
  581. view.TextFormatter.Text = "ABC";
  582. Assert.False (view.TextFormatter.AutoSize);
  583. Assert.Equal (Size.Empty, view.Frame.Size);
  584. view.TextFormatter.Alignment = Alignment.Justified;
  585. Assert.False (view.TextFormatter.AutoSize);
  586. Assert.Equal (Size.Empty, view.Frame.Size);
  587. view.TextFormatter.VerticalAlignment = Alignment.Centered;
  588. Assert.False (view.TextFormatter.AutoSize);
  589. Assert.Equal (Size.Empty, view.Frame.Size);
  590. view.TextFormatter.HotKeySpecifier = (Rune)'*';
  591. Assert.False (view.TextFormatter.AutoSize);
  592. Assert.Equal (Size.Empty, view.Frame.Size);
  593. view.TextFormatter.Text = "*ABC";
  594. Assert.False (view.TextFormatter.AutoSize);
  595. Assert.Equal (Size.Empty, view.Frame.Size);
  596. }
  597. [Fact]
  598. public void DimAuto_Not_Used_TextSettings_Do_Not_Change_View_Size ()
  599. {
  600. View view = new ()
  601. {
  602. Text = "_1234"
  603. };
  604. Assert.False (view.TextFormatter.AutoSize);
  605. Assert.Equal (Size.Empty, view.Frame.Size);
  606. view.TextAlignment = Alignment.Justified;
  607. Assert.False (view.TextFormatter.AutoSize);
  608. Assert.Equal (Size.Empty, view.Frame.Size);
  609. view.VerticalTextAlignment = Alignment.Centered;
  610. Assert.False (view.TextFormatter.AutoSize);
  611. Assert.Equal (Size.Empty, view.Frame.Size);
  612. view.HotKeySpecifier = (Rune)'*';
  613. Assert.False (view.TextFormatter.AutoSize);
  614. Assert.Equal (Size.Empty, view.Frame.Size);
  615. view.Text = "*ABC";
  616. Assert.False (view.TextFormatter.AutoSize);
  617. Assert.Equal (Size.Empty, view.Frame.Size);
  618. }
  619. [Fact]
  620. public void DimAuto_TextSettings_Change_View_Size ()
  621. {
  622. View view = new ()
  623. {
  624. Text = "_1234",
  625. Width = Dim.Auto ()
  626. };
  627. Assert.True (view.TextFormatter.AutoSize);
  628. Assert.NotEqual (Size.Empty, view.Frame.Size);
  629. view.TextAlignment = Alignment.Justified;
  630. Assert.True (view.TextFormatter.AutoSize);
  631. Assert.NotEqual (Size.Empty, view.Frame.Size);
  632. view = new ()
  633. {
  634. Text = "_1234",
  635. Width = Dim.Auto ()
  636. };
  637. view.VerticalTextAlignment = Alignment.Centered;
  638. Assert.True (view.TextFormatter.AutoSize);
  639. Assert.NotEqual (Size.Empty, view.Frame.Size);
  640. view = new ()
  641. {
  642. Text = "_1234",
  643. Width = Dim.Auto ()
  644. };
  645. view.HotKeySpecifier = (Rune)'*';
  646. Assert.True (view.TextFormatter.AutoSize);
  647. Assert.NotEqual (Size.Empty, view.Frame.Size);
  648. view = new ()
  649. {
  650. Text = "_1234",
  651. Width = Dim.Auto ()
  652. };
  653. view.Text = "*ABC";
  654. Assert.True (view.TextFormatter.AutoSize);
  655. Assert.NotEqual (Size.Empty, view.Frame.Size);
  656. }
  657. [Fact]
  658. public void DimAuto_TextFormatter_Is_Auto ()
  659. {
  660. View view = new ();
  661. Assert.False (view.TextFormatter.AutoSize);
  662. view.Width = Dim.Auto ();
  663. Assert.True (view.TextFormatter.AutoSize);
  664. view = new ();
  665. Assert.False (view.TextFormatter.AutoSize);
  666. view.Height = Dim.Auto ();
  667. Assert.True (view.TextFormatter.AutoSize);
  668. }
  669. [Theory]
  670. [InlineData ("1234", 4)]
  671. [InlineData ("_1234", 4)]
  672. public void Width_Auto_HotKey_TextFormatter_Size_Correct (string text, int expected)
  673. {
  674. View view = new ()
  675. {
  676. Text = text,
  677. Height = 1,
  678. Width = Dim.Auto ()
  679. };
  680. Assert.Equal (new (expected, 1), view.TextFormatter.Size);
  681. }
  682. [Theory]
  683. [InlineData ("1234", 4)]
  684. [InlineData ("_1234", 4)]
  685. public void Height_Auto_HotKey_TextFormatter_Size_Correct (string text, int expected)
  686. {
  687. View view = new ()
  688. {
  689. HotKeySpecifier = (Rune)'_',
  690. Text = text,
  691. Width = Auto (),
  692. Height = 1,
  693. };
  694. Assert.Equal (new (expected, 1), view.TextFormatter.Size);
  695. view = new ()
  696. {
  697. HotKeySpecifier = (Rune)'_',
  698. TextDirection = TextDirection.TopBottom_LeftRight,
  699. Text = text,
  700. Width = 1,
  701. Height = Auto (),
  702. };
  703. Assert.Equal (new (1, expected), view.TextFormatter.Size);
  704. }
  705. [SetupFakeDriver]
  706. [Fact]
  707. public void DimAuto_ChangeToANonDimAuto_Resets_ContentSize ()
  708. {
  709. View view = new ()
  710. {
  711. Width = Auto (),
  712. Height = Auto (),
  713. Text = "01234"
  714. };
  715. Assert.Equal (new Rectangle (0, 0, 5, 1), view.Frame);
  716. Assert.Equal (new Size (5, 1), view.ContentSize);
  717. // Change text to a longer string
  718. view.Text = "0123456789";
  719. Assert.Equal (new Rectangle (0, 0, 10, 1), view.Frame);
  720. Assert.Equal (new Size (10, 1), view.ContentSize);
  721. // If ContentSize was reset, these should cause it to update
  722. view.Width = 5;
  723. view.Height = 1;
  724. Assert.Equal (new Size (5, 1), view.ContentSize);
  725. }
  726. // DimAutoStyle.Content tests
  727. [Fact]
  728. public void DimAutoStyle_Content_UsesContentSize_WhenSet ()
  729. {
  730. var view = new View () { ContentSize = new Size (10, 5) };
  731. var dim = Dim.Auto (Dim.DimAutoStyle.Content);
  732. int calculatedWidth = dim.Calculate (0, 100, view, Dim.Dimension.Width);
  733. Assert.Equal (10, calculatedWidth);
  734. }
  735. [Fact]
  736. public void DimAutoStyle_Content_IgnoresText_WhenContentSizeNotSet ()
  737. {
  738. var view = new View () { Text = "This is a test" };
  739. var dim = Dim.Auto (Dim.DimAutoStyle.Content);
  740. int calculatedWidth = dim.Calculate (0, 100, view, Dim.Dimension.Width);
  741. Assert.Equal (0, calculatedWidth); // Assuming 0 is the default when no ContentSize or Subviews are set
  742. }
  743. [Fact]
  744. public void DimAutoStyle_Content_UsesLargestSubview_WhenContentSizeNotSet ()
  745. {
  746. var view = new View ();
  747. view.Add (new View () { Frame = new Rectangle (0, 0, 5, 5) }); // Smaller subview
  748. view.Add (new View () { Frame = new Rectangle (0, 0, 10, 10) }); // Larger subview
  749. var dim = Dim.Auto (Dim.DimAutoStyle.Content);
  750. int calculatedWidth = dim.Calculate (0, 100, view, Dim.Dimension.Width);
  751. Assert.Equal (10, calculatedWidth); // Expecting the size of the largest subview
  752. }
  753. // All the Dim types
  754. [Theory]
  755. [InlineData (0, 15, 15)]
  756. [InlineData (1, 15, 16)]
  757. [InlineData (-1, 15, 14)]
  758. public void DimAuto_With_Subview_Using_DimAbsolute (int subViewOffset, int dimAbsoluteSize, int expectedSize)
  759. {
  760. var view = new View ();
  761. var subview = new View ()
  762. {
  763. X = subViewOffset,
  764. Y = subViewOffset,
  765. Width = Dim.Sized (dimAbsoluteSize),
  766. Height = Dim.Sized (dimAbsoluteSize)
  767. };
  768. view.Add (subview);
  769. var dim = Dim.Auto (Dim.DimAutoStyle.Content);
  770. int calculatedWidth = dim.Calculate (0, 100, view, Dim.Dimension.Width);
  771. int calculatedHeight = dim.Calculate (0, 100, view, Dim.Dimension.Height);
  772. Assert.Equal (expectedSize, calculatedWidth);
  773. Assert.Equal (expectedSize, calculatedHeight);
  774. }
  775. [Theory]
  776. [InlineData (0, 50, 50)]
  777. [InlineData (1, 50, 51)]
  778. [InlineData (0, 25, 25)]
  779. [InlineData (-1, 50, 49)]
  780. public void DimAuto_With_Subview_Using_DimFactor (int subViewOffset, int dimFactor, int expectedSize)
  781. {
  782. var view = new View () { Width = 100, Height = 100 };
  783. var subview = new View ()
  784. {
  785. X = subViewOffset,
  786. Y = subViewOffset,
  787. Width = Dim.Percent (dimFactor),
  788. Height = Dim.Percent (dimFactor)
  789. };
  790. view.Add (subview);
  791. subview.SetRelativeLayout (new (100, 100));
  792. var dim = Dim.Auto (Dim.DimAutoStyle.Content);
  793. int calculatedWidth = dim.Calculate (0, 100, view, Dim.Dimension.Width);
  794. int calculatedHeight = dim.Calculate (0, 100, view, Dim.Dimension.Height);
  795. Assert.Equal (expectedSize, calculatedWidth);
  796. Assert.Equal (expectedSize, calculatedHeight);
  797. }
  798. [Theory]
  799. [InlineData (0, 0, 100)]
  800. [InlineData (1, 0, 100)]
  801. [InlineData (0, 1, 99)]
  802. [InlineData (1, 1, 99)]
  803. public void DimAuto_With_Subview_Using_DimFill (int subViewOffset, int dimFillMargin, int expectedSize)
  804. {
  805. var view = new View ();
  806. var subview = new View ()
  807. {
  808. X = subViewOffset,
  809. Y = subViewOffset,
  810. Width = Dim.Fill (dimFillMargin),
  811. Height = Dim.Fill (dimFillMargin)
  812. };
  813. view.Add (subview);
  814. subview.SetRelativeLayout (new (100, 100));
  815. var dim = Dim.Auto (Dim.DimAutoStyle.Content);
  816. // Assuming the view's size is 100x100 for calculation purposes
  817. int calculatedWidth = dim.Calculate (0, 100, view, Dim.Dimension.Width);
  818. int calculatedHeight = dim.Calculate (0, 100, view, Dim.Dimension.Height);
  819. Assert.Equal (expectedSize, calculatedWidth);
  820. Assert.Equal (expectedSize, calculatedHeight);
  821. }
  822. [Fact]
  823. public void DimAuto_With_Subview_Using_DimFunc ()
  824. {
  825. var view = new View ();
  826. var subview = new View () { Width = Dim.Function (() => 20), Height = Dim.Function (() => 25) };
  827. view.Add (subview);
  828. subview.SetRelativeLayout (new (100, 100));
  829. var dim = Dim.Auto (Dim.DimAutoStyle.Content);
  830. int calculatedWidth = dim.Calculate (0, 100, view, Dim.Dimension.Width);
  831. int calculatedHeight = dim.Calculate (0, 100, view, Dim.Dimension.Height);
  832. Assert.Equal (20, calculatedWidth);
  833. Assert.Equal (25, calculatedHeight);
  834. }
  835. [Fact]
  836. public void DimAuto_With_Subview_Using_DimView ()
  837. {
  838. var view = new View ();
  839. var subview = new View () { Width = 30, Height = 40 };
  840. var subSubview = new View () { Width = Dim.Width (subview), Height = Dim.Height (subview) };
  841. view.Add (subview);
  842. view.Add (subSubview);
  843. subview.SetRelativeLayout (new (100, 100));
  844. var dim = Dim.Auto (Dim.DimAutoStyle.Content);
  845. int calculatedWidth = dim.Calculate (0, 100, view, Dim.Dimension.Width);
  846. int calculatedHeight = dim.Calculate (0, 100, view, Dim.Dimension.Height);
  847. // Expecting the size to match the subview, which is the largest
  848. Assert.Equal (30, calculatedWidth);
  849. Assert.Equal (40, calculatedHeight);
  850. }
  851. // Testing all Pos combinations
  852. [Fact]
  853. public void DimAuto_With_Subview_At_PosAt ()
  854. {
  855. var view = new View ();
  856. var subview = new View () { X = Pos.At (10), Y = Pos.At (5), Width = 20, Height = 10 };
  857. view.Add (subview);
  858. var dimWidth = Dim.Auto ();
  859. var dimHeight = Dim.Auto ();
  860. int calculatedWidth = dimWidth.Calculate (0, 100, view, Dim.Dimension.Width);
  861. int calculatedHeight = dimHeight.Calculate (0, 100, view, Dim.Dimension.Height);
  862. // Expecting the size to include the subview's position and size
  863. Assert.Equal (30, calculatedWidth); // 10 (X position) + 20 (Width)
  864. Assert.Equal (15, calculatedHeight); // 5 (Y position) + 10 (Height)
  865. }
  866. [Fact (Skip = "DimAuto_TextOnly")]
  867. public void DimAuto_With_Subview_At_PosPercent ()
  868. {
  869. var view = new View () { Width = 100, Height = 100 };
  870. var subview = new View () { X = Pos.Percent (50), Y = Pos.Percent (50), Width = 20, Height = 10 };
  871. view.Add (subview);
  872. var dimWidth = Dim.Auto ();
  873. var dimHeight = Dim.Auto ();
  874. // Assuming the calculation is done after layout
  875. int calculatedWidth = dimWidth.Calculate (0, 100, view, Dim.Dimension.Width);
  876. int calculatedHeight = dimHeight.Calculate (0, 100, view, Dim.Dimension.Height);
  877. // Expecting the size to include the subview's position as a percentage of the parent view's size plus the subview's size
  878. Assert.Equal (70, calculatedWidth); // 50% of 100 (Width) + 20
  879. Assert.Equal (60, calculatedHeight); // 50% of 100 (Height) + 10
  880. }
  881. [Fact (Skip = "DimAuto_TextOnly")]
  882. public void DimAuto_With_Subview_At_PosCenter ()
  883. {
  884. var view = new View () { Width = 100, Height = 100 };
  885. var subview = new View () { X = Pos.Center (), Y = Pos.Center (), Width = 20, Height = 10 };
  886. view.Add (subview);
  887. var dimWidth = Dim.Auto ();
  888. var dimHeight = Dim.Auto ();
  889. // Assuming the calculation is done after layout
  890. int calculatedWidth = dimWidth.Calculate (0, 100, view, Dim.Dimension.Width);
  891. int calculatedHeight = dimHeight.Calculate (0, 100, view, Dim.Dimension.Height);
  892. // Expecting the size to include the subview's position at the center of the parent view plus the subview's size
  893. Assert.Equal (70, calculatedWidth); // Centered in 100 (Width) + 20
  894. Assert.Equal (60, calculatedHeight); // Centered in 100 (Height) + 10
  895. }
  896. [Fact (Skip = "DimAuto_TextOnly")]
  897. public void DimAuto_With_Subview_At_PosAnchorEnd ()
  898. {
  899. var dimWidth = Dim.Auto (min: 50);
  900. var dimHeight = Dim.Auto (min: 50);
  901. var view = new View ()
  902. {
  903. Width = dimWidth,
  904. Height = dimHeight
  905. };
  906. var subview = new View ()
  907. {
  908. X = Pos.AnchorEnd (),
  909. Y = Pos.AnchorEnd (),
  910. Width = 20,
  911. Height = 10
  912. };
  913. view.Add (subview);
  914. // Assuming the calculation is done after layout
  915. int calculatedWidth = dimWidth.Calculate (0, 100, view, Dim.Dimension.Width);
  916. int calculatedHeight = dimHeight.Calculate (0, 100, view, Dim.Dimension.Height);
  917. // Expecting the size to include the subview's position at the end of the parent view minus the offset plus the subview's size
  918. Assert.Equal (100, calculatedWidth);
  919. Assert.Equal (100, calculatedHeight);
  920. }
  921. // Test variations of Frame
  922. }