DimAutoTests.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  1. using System.Globalization;
  2. using System.Text;
  3. using Xunit.Abstractions;
  4. using static Terminal.Gui.Dim;
  5. // Alias Console to MockConsole so we don't accidentally use Console
  6. using Console = Terminal.Gui.FakeConsole;
  7. namespace Terminal.Gui.ViewTests;
  8. public class DimAutoTests
  9. {
  10. private readonly ITestOutputHelper _output;
  11. public DimAutoTests (ITestOutputHelper output)
  12. {
  13. _output = output;
  14. Console.OutputEncoding = Encoding.Default;
  15. // Change current culture
  16. var culture = CultureInfo.CreateSpecificCulture ("en-US");
  17. Thread.CurrentThread.CurrentCulture = culture;
  18. Thread.CurrentThread.CurrentUICulture = culture;
  19. }
  20. // Test min - ensure that if min is specified in the DimAuto constructor it is honored
  21. [Fact]
  22. public void DimAuto_Min ()
  23. {
  24. var superView = new View
  25. {
  26. X = 0,
  27. Y = 0,
  28. Width = Dim.Auto (min: 10),
  29. Height = Dim.Auto (min: 10),
  30. ValidatePosDim = true
  31. };
  32. var subView = new View
  33. {
  34. X = 0,
  35. Y = 0,
  36. Width = 5,
  37. Height = 5
  38. };
  39. superView.Add (subView);
  40. superView.BeginInit ();
  41. superView.EndInit ();
  42. superView.SetRelativeLayout (new (10, 10));
  43. superView.LayoutSubviews (); // no throw
  44. Assert.Equal (10, superView.Frame.Width);
  45. Assert.Equal (10, superView.Frame.Height);
  46. }
  47. // what happens if DimAuto (min: 10) and the subview moves to a negative coord?
  48. [Fact]
  49. public void DimAuto_Min_Resets_If_Subview_Moves_Negative ()
  50. {
  51. var superView = new View
  52. {
  53. X = 0,
  54. Y = 0,
  55. Width = Dim.Auto (min: 10),
  56. Height = Dim.Auto (min: 10),
  57. ValidatePosDim = true
  58. };
  59. var subView = new View
  60. {
  61. X = 0,
  62. Y = 0,
  63. Width = 5,
  64. Height = 5
  65. };
  66. superView.Add (subView);
  67. superView.BeginInit ();
  68. superView.EndInit ();
  69. superView.SetRelativeLayout (new ( 10, 10));
  70. superView.LayoutSubviews (); // no throw
  71. Assert.Equal (10, superView.Frame.Width);
  72. Assert.Equal (10, superView.Frame.Height);
  73. subView.X = -1;
  74. subView.Y = -1;
  75. superView.SetRelativeLayout (new ( 10, 10));
  76. superView.LayoutSubviews (); // no throw
  77. Assert.Equal (5, subView.Frame.Width);
  78. Assert.Equal (5, subView.Frame.Height);
  79. Assert.Equal (10, superView.Frame.Width);
  80. Assert.Equal (10, superView.Frame.Height);
  81. }
  82. [Fact]
  83. public void DimAuto_Min_Resets_If_Subview_Shrinks ()
  84. {
  85. var superView = new View
  86. {
  87. X = 0,
  88. Y = 0,
  89. Width = Dim.Auto (min: 10),
  90. Height = Dim.Auto (min: 10),
  91. ValidatePosDim = true
  92. };
  93. var subView = new View
  94. {
  95. X = 0,
  96. Y = 0,
  97. Width = 5,
  98. Height = 5
  99. };
  100. superView.Add (subView);
  101. superView.BeginInit ();
  102. superView.EndInit ();
  103. superView.SetRelativeLayout (new ( 10, 10));
  104. superView.LayoutSubviews (); // no throw
  105. Assert.Equal (10, superView.Frame.Width);
  106. Assert.Equal (10, superView.Frame.Height);
  107. subView.Width = 3;
  108. subView.Height = 3;
  109. superView.SetRelativeLayout (new ( 10, 10));
  110. superView.LayoutSubviews (); // no throw
  111. Assert.Equal (3, subView.Frame.Width);
  112. Assert.Equal (3, subView.Frame.Height);
  113. Assert.Equal (10, superView.Frame.Width);
  114. Assert.Equal (10, superView.Frame.Height);
  115. }
  116. [Theory]
  117. [InlineData (0, 0, 0, 0, 0)]
  118. [InlineData (0, 0, 5, 0, 0)]
  119. [InlineData (0, 0, 0, 5, 5)]
  120. [InlineData (0, 0, 5, 5, 5)]
  121. [InlineData (1, 0, 5, 0, 0)]
  122. [InlineData (1, 0, 0, 5, 5)]
  123. [InlineData (1, 0, 5, 5, 5)]
  124. [InlineData (1, 1, 5, 5, 6)]
  125. [InlineData (-1, 0, 5, 0, 0)]
  126. [InlineData (-1, 0, 0, 5, 5)]
  127. [InlineData (-1, 0, 5, 5, 5)]
  128. [InlineData (-1, -1, 5, 5, 4)]
  129. public void Height_Auto_Width_NotChanged (int subX, int subY, int subWidth, int subHeight, int expectedHeight)
  130. {
  131. var superView = new View
  132. {
  133. X = 0,
  134. Y = 0,
  135. Width = 10,
  136. Height = Dim.Auto (),
  137. ValidatePosDim = true
  138. };
  139. var subView = new View
  140. {
  141. X = subX,
  142. Y = subY,
  143. Width = subWidth,
  144. Height = subHeight,
  145. ValidatePosDim = true
  146. };
  147. superView.Add (subView);
  148. superView.BeginInit ();
  149. superView.EndInit ();
  150. superView.SetRelativeLayout (new ( 10, 10));
  151. Assert.Equal (new Rectangle (0, 0, 10, expectedHeight), superView.Frame);
  152. }
  153. [Fact]
  154. public void NoSubViews_Does_Nothing ()
  155. {
  156. var superView = new View
  157. {
  158. X = 0,
  159. Y = 0,
  160. Width = Dim.Auto (),
  161. Height = Dim.Auto (),
  162. ValidatePosDim = true
  163. };
  164. superView.BeginInit ();
  165. superView.EndInit ();
  166. superView.SetRelativeLayout (new ( 10, 10));
  167. Assert.Equal (new Rectangle (0, 0, 0, 0), superView.Frame);
  168. superView.SetRelativeLayout (new ( 10, 10));
  169. Assert.Equal (new Rectangle (0, 0, 0, 0), superView.Frame);
  170. }
  171. [Theory]
  172. [InlineData (0, 0, 0, 0, 0, 0)]
  173. [InlineData (0, 0, 5, 0, 5, 0)]
  174. [InlineData (0, 0, 0, 5, 0, 5)]
  175. [InlineData (0, 0, 5, 5, 5, 5)]
  176. [InlineData (1, 0, 5, 0, 6, 0)]
  177. [InlineData (1, 0, 0, 5, 1, 5)]
  178. [InlineData (1, 0, 5, 5, 6, 5)]
  179. [InlineData (1, 1, 5, 5, 6, 6)]
  180. [InlineData (-1, 0, 5, 0, 4, 0)]
  181. [InlineData (-1, 0, 0, 5, 0, 5)]
  182. [InlineData (-1, 0, 5, 5, 4, 5)]
  183. [InlineData (-1, -1, 5, 5, 4, 4)]
  184. public void SubView_ChangesSuperViewSize (int subX, int subY, int subWidth, int subHeight, int expectedWidth, int expectedHeight)
  185. {
  186. var superView = new View
  187. {
  188. X = 0,
  189. Y = 0,
  190. Width = Dim.Auto (),
  191. Height = Dim.Auto (),
  192. ValidatePosDim = true
  193. };
  194. var subView = new View
  195. {
  196. X = subX,
  197. Y = subY,
  198. Width = subWidth,
  199. Height = subHeight,
  200. ValidatePosDim = true
  201. };
  202. superView.Add (subView);
  203. superView.BeginInit ();
  204. superView.EndInit ();
  205. superView.SetRelativeLayout (new ( 10, 10));
  206. Assert.Equal (new Rectangle (0, 0, expectedWidth, expectedHeight), superView.Frame);
  207. }
  208. // Test validation
  209. [Fact]
  210. public void ValidatePosDim_True_Throws_When_SubView_Uses_SuperView_Dims ()
  211. {
  212. var superView = new View
  213. {
  214. X = 0,
  215. Y = 0,
  216. Width = Dim.Auto (),
  217. Height = Dim.Auto (),
  218. ValidatePosDim = true
  219. };
  220. var subView = new View
  221. {
  222. X = 0,
  223. Y = 0,
  224. Width = Dim.Fill (),
  225. Height = 10,
  226. ValidatePosDim = true
  227. };
  228. superView.BeginInit ();
  229. superView.EndInit ();
  230. Assert.Throws<InvalidOperationException> (() => superView.Add (subView));
  231. subView.Width = 10;
  232. superView.Add (subView);
  233. superView.SetRelativeLayout (new ( 10, 10));
  234. superView.LayoutSubviews (); // no throw
  235. subView.Width = Dim.Fill ();
  236. Assert.Throws<InvalidOperationException> (() => superView.SetRelativeLayout (new ( 0, 0)));
  237. subView.Width = 10;
  238. subView.Height = Dim.Fill ();
  239. Assert.Throws<InvalidOperationException> (() => superView.SetRelativeLayout (new ( 0, 0)));
  240. subView.Height = 10;
  241. subView.Height = Dim.Percent (50);
  242. Assert.Throws<InvalidOperationException> (() => superView.SetRelativeLayout (new ( 0, 0)));
  243. subView.Height = 10;
  244. subView.X = Pos.Center ();
  245. Assert.Throws<InvalidOperationException> (() => superView.SetRelativeLayout (new ( 0, 0)));
  246. subView.X = 0;
  247. subView.Y = Pos.Center ();
  248. Assert.Throws<InvalidOperationException> (() => superView.SetRelativeLayout (new ( 0, 0)));
  249. subView.Y = 0;
  250. subView.Width = 10;
  251. subView.Height = 10;
  252. subView.X = 0;
  253. subView.Y = 0;
  254. superView.SetRelativeLayout (new ( 0, 0));
  255. superView.LayoutSubviews ();
  256. }
  257. // Test validation
  258. [Fact]
  259. public void ValidatePosDim_True_Throws_When_SubView_Uses_SuperView_Dims_Combine ()
  260. {
  261. var superView = new View
  262. {
  263. X = 0,
  264. Y = 0,
  265. Width = Dim.Auto (),
  266. Height = Dim.Auto (),
  267. ValidatePosDim = true
  268. };
  269. var subView = new View
  270. {
  271. X = 0,
  272. Y = 0,
  273. Width = 10,
  274. Height = 10
  275. };
  276. var subView2 = new View
  277. {
  278. X = 0,
  279. Y = 0,
  280. Width = 10,
  281. Height = 10
  282. };
  283. superView.Add (subView, subView2);
  284. superView.BeginInit ();
  285. superView.EndInit ();
  286. superView.SetRelativeLayout (new ( 0, 0));
  287. superView.LayoutSubviews (); // no throw
  288. subView.Height = Dim.Fill () + 3;
  289. Assert.Throws<InvalidOperationException> (() => superView.SetRelativeLayout (new ( 0, 0)));
  290. subView.Height = 0;
  291. subView.Height = 3 + Dim.Fill ();
  292. Assert.Throws<InvalidOperationException> (() => superView.SetRelativeLayout (new ( 0, 0)));
  293. subView.Height = 0;
  294. subView.Height = 3 + 5 + Dim.Fill ();
  295. Assert.Throws<InvalidOperationException> (() => superView.SetRelativeLayout (new ( 0, 0)));
  296. subView.Height = 0;
  297. subView.Height = 3 + 5 + Dim.Percent (10);
  298. Assert.Throws<InvalidOperationException> (() => superView.SetRelativeLayout (new ( 0, 0)));
  299. subView.Height = 0;
  300. // Tests nested Combine
  301. subView.Height = 5 + new Dim.DimCombine (true, 3, new Dim.DimCombine (true, Dim.Percent (10), 9));
  302. Assert.Throws<InvalidOperationException> (() => superView.SetRelativeLayout (new ( 0, 0)));
  303. }
  304. [Fact]
  305. public void ValidatePosDim_True_Throws_When_SubView_Uses_SuperView_Pos_Combine ()
  306. {
  307. var superView = new View
  308. {
  309. X = 0,
  310. Y = 0,
  311. Width = Dim.Auto (),
  312. Height = Dim.Auto (),
  313. ValidatePosDim = true
  314. };
  315. var subView = new View
  316. {
  317. X = 0,
  318. Y = 0,
  319. Width = 10,
  320. Height = 10
  321. };
  322. var subView2 = new View
  323. {
  324. X = 0,
  325. Y = 0,
  326. Width = 10,
  327. Height = 10
  328. };
  329. superView.Add (subView, subView2);
  330. superView.BeginInit ();
  331. superView.EndInit ();
  332. superView.SetRelativeLayout (new ( 0, 0));
  333. superView.LayoutSubviews (); // no throw
  334. subView.X = Pos.Right (subView2);
  335. superView.SetRelativeLayout (new ( 0, 0));
  336. superView.LayoutSubviews (); // no throw
  337. subView.X = Pos.Right (subView2) + 3;
  338. superView.SetRelativeLayout (new ( 0, 0)); // no throw
  339. superView.LayoutSubviews (); // no throw
  340. subView.X = new Pos.PosCombine (true, Pos.Right (subView2), new Pos.PosCombine (true, 7, 9));
  341. superView.SetRelativeLayout (new ( 0, 0)); // no throw
  342. subView.X = Pos.Center () + 3;
  343. Assert.Throws<InvalidOperationException> (() => superView.SetRelativeLayout (new ( 0, 0)));
  344. subView.X = 0;
  345. subView.X = 3 + Pos.Center ();
  346. Assert.Throws<InvalidOperationException> (() => superView.SetRelativeLayout (new ( 0, 0)));
  347. subView.X = 0;
  348. subView.X = 3 + 5 + Pos.Center ();
  349. Assert.Throws<InvalidOperationException> (() => superView.SetRelativeLayout (new ( 0, 0)));
  350. subView.X = 0;
  351. subView.X = 3 + 5 + Pos.Percent (10);
  352. Assert.Throws<InvalidOperationException> (() => superView.SetRelativeLayout (new ( 0, 0)));
  353. subView.X = 0;
  354. subView.X = Pos.Percent (10) + Pos.Center ();
  355. Assert.Throws<InvalidOperationException> (() => superView.SetRelativeLayout (new ( 0, 0)));
  356. subView.X = 0;
  357. // Tests nested Combine
  358. subView.X = 5 + new Pos.PosCombine (true, Pos.Right (subView2), new Pos.PosCombine (true, Pos.Center (), 9));
  359. Assert.Throws<InvalidOperationException> (() => superView.SetRelativeLayout (new ( 0, 0)));
  360. subView.X = 0;
  361. }
  362. [Theory]
  363. [InlineData (0, 0, 0, 0, 0)]
  364. [InlineData (0, 0, 5, 0, 5)]
  365. [InlineData (0, 0, 0, 5, 0)]
  366. [InlineData (0, 0, 5, 5, 5)]
  367. [InlineData (1, 0, 5, 0, 6)]
  368. [InlineData (1, 0, 0, 5, 1)]
  369. [InlineData (1, 0, 5, 5, 6)]
  370. [InlineData (1, 1, 5, 5, 6)]
  371. [InlineData (-1, 0, 5, 0, 4)]
  372. [InlineData (-1, 0, 0, 5, 0)]
  373. [InlineData (-1, 0, 5, 5, 4)]
  374. [InlineData (-1, -1, 5, 5, 4)]
  375. public void Width_Auto_Height_NotChanged (int subX, int subY, int subWidth, int subHeight, int expectedWidth)
  376. {
  377. var superView = new View
  378. {
  379. X = 0,
  380. Y = 0,
  381. Width = Dim.Auto (),
  382. Height = 10,
  383. ValidatePosDim = true
  384. };
  385. var subView = new View
  386. {
  387. X = subX,
  388. Y = subY,
  389. Width = subWidth,
  390. Height = subHeight,
  391. ValidatePosDim = true
  392. };
  393. superView.Add (subView);
  394. superView.BeginInit ();
  395. superView.EndInit ();
  396. superView.SetRelativeLayout (new ( 10, 10));
  397. Assert.Equal (new Rectangle (0, 0, expectedWidth, 10), superView.Frame);
  398. }
  399. // 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
  400. [Theory]
  401. [InlineData (0, 0)]
  402. [InlineData (1, 1)]
  403. [InlineData (3, 3)]
  404. [InlineData (4, 4)]
  405. [InlineData (5, 4)] // This is clearly invalid, but we choose to not throw but log a debug message
  406. public void Width_Auto_Min (int min, int expectedWidth)
  407. {
  408. var superView = new View
  409. {
  410. X = 0,
  411. Y = 0,
  412. Width = Dim.Auto (min: min),
  413. Height = 1,
  414. ValidatePosDim = true
  415. };
  416. superView.BeginInit ();
  417. superView.EndInit ();
  418. superView.SetRelativeLayout (new ( 4, 1));
  419. Assert.Equal (expectedWidth, superView.Frame.Width);
  420. }
  421. // Test Dim.Fill - Fill should not impact width of the DimAuto superview
  422. [Theory]
  423. [InlineData (0, 0, 0, 10, 10)]
  424. [InlineData (0, 1, 0, 10, 10)]
  425. [InlineData (0, 11, 0, 10, 10)]
  426. [InlineData (0, 10, 0, 10, 10)]
  427. [InlineData (0, 5, 0, 10, 10)]
  428. [InlineData (1, 5, 0, 10, 9)]
  429. [InlineData (1, 10, 0, 10, 9)]
  430. [InlineData (0, 0, 1, 10, 9)]
  431. [InlineData (0, 10, 1, 10, 9)]
  432. [InlineData (0, 5, 1, 10, 9)]
  433. [InlineData (1, 5, 1, 10, 8)]
  434. [InlineData (1, 10, 1, 10, 8)]
  435. public void Width_Fill_Fills (int subX, int superMinWidth, int fill, int expectedSuperWidth, int expectedSubWidth)
  436. {
  437. var superView = new View
  438. {
  439. X = 0,
  440. Y = 0,
  441. Width = Dim.Auto (min:superMinWidth),
  442. Height = 1,
  443. ValidatePosDim = true
  444. };
  445. var subView = new View
  446. {
  447. X = subX,
  448. Y = 0,
  449. Width = Dim.Fill (fill),
  450. Height = 1,
  451. ValidatePosDim = true
  452. };
  453. superView.Add (subView);
  454. superView.BeginInit ();
  455. superView.EndInit ();
  456. superView.SetRelativeLayout (new ( 10, 1));
  457. Assert.Equal (expectedSuperWidth, superView.Frame.Width);
  458. superView.LayoutSubviews ();
  459. Assert.Equal (expectedSubWidth, subView.Frame.Width);
  460. Assert.Equal (expectedSuperWidth, superView.Frame.Width);
  461. }
  462. [Theory]
  463. [InlineData (0, 1, 1)]
  464. [InlineData (1, 1, 1)]
  465. [InlineData (9, 1, 1)]
  466. [InlineData (10, 1, 1)]
  467. [InlineData (0, 10, 10)]
  468. [InlineData (1, 10, 10)]
  469. [InlineData (9, 10, 10)]
  470. [InlineData (10, 10, 10)]
  471. public void Width_Auto_Text_Does_Not_Constrain_To_SuperView (int subX, int textLen, int expectedSubWidth)
  472. {
  473. var superView = new View
  474. {
  475. X = 0,
  476. Y = 0,
  477. Width = 10,
  478. Height = 1,
  479. ValidatePosDim = true
  480. };
  481. var subView = new View
  482. {
  483. Text = new string ('*', textLen),
  484. X = subX,
  485. Y = 0,
  486. Width = Dim.Auto (Dim.DimAutoStyle.Text),
  487. Height = 1,
  488. ValidatePosDim = true
  489. };
  490. superView.Add (subView);
  491. superView.BeginInit ();
  492. superView.EndInit ();
  493. superView.SetRelativeLayout (superView.ContentSize);
  494. superView.LayoutSubviews ();
  495. Assert.Equal (expectedSubWidth, subView.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 = Dim.Auto (Dim.DimAutoStyle.Subviews),
  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.ContentSize);
  537. superView.LayoutSubviews ();
  538. Assert.Equal (expectedSubWidth, subView.Frame.Width);
  539. }
  540. [Fact]
  541. public void DimAuto_Text_Viewport_Stays_Set ()
  542. {
  543. var super = new View ()
  544. {
  545. Width = Dim.Fill (),
  546. Height = Dim.Fill ()
  547. };
  548. var view = new View ()
  549. {
  550. Width = Auto (DimAutoStyle.Text),
  551. Height = Auto (DimAutoStyle.Text),
  552. Text = "New text"
  553. };
  554. Rectangle expectedViewport = new (0, 0, 8, 1);
  555. Assert.Equal (expectedViewport, view.Viewport);
  556. super.Add (view);
  557. Assert.Equal (expectedViewport, view.Viewport);
  558. super.LayoutSubviews ();
  559. Assert.Equal (expectedViewport, view.Viewport);
  560. super.Dispose ();
  561. }
  562. // Test variations of Frame
  563. }