SetRelativeLayoutTests.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. using Xunit.Abstractions;
  2. using static Terminal.Gui.Dim;
  3. namespace Terminal.Gui.LayoutTests;
  4. public class SetRelativeLayoutTests
  5. {
  6. private readonly ITestOutputHelper _output;
  7. public SetRelativeLayoutTests (ITestOutputHelper output) { _output = output; }
  8. [Fact]
  9. public void AbsolutePosDim_DontChange ()
  10. {
  11. var screen = new Size (10, 15);
  12. var view = new View
  13. {
  14. X = 1, // outside of screen +10
  15. Y = 2, // outside of screen -10
  16. Width = 3,
  17. Height = 4
  18. };
  19. // Layout is Absolute. So the X and Y are not changed.
  20. view.SetRelativeLayout (screen);
  21. Assert.Equal (1, view.Frame.X);
  22. Assert.Equal (2, view.Frame.Y);
  23. Assert.Equal (3, view.Frame.Width);
  24. Assert.Equal (4, view.Frame.Height);
  25. }
  26. [Fact]
  27. public void ComputedPosDim_StayComputed ()
  28. {
  29. var screen = new Size (10, 15);
  30. var view = new View { X = 1, Y = 2, Width = Dim.Fill (), Height = Dim.Fill () };
  31. Assert.Equal ("Absolute(1)", view.X.ToString ());
  32. Assert.Equal ("Absolute(2)", view.Y.ToString ());
  33. Assert.Equal ("Fill(0)", view.Width.ToString ());
  34. Assert.Equal ("Fill(0)", view.Height.ToString ());
  35. view.SetRelativeLayout (screen);
  36. Assert.Equal ("Fill(0)", view.Width.ToString ());
  37. Assert.Equal ("Fill(0)", view.Height.ToString ());
  38. }
  39. [Fact]
  40. public void DimFill_Is_Honored ()
  41. {
  42. var view = new View { X = 1, Y = 1, Width = Dim.Fill (), Height = Dim.Fill () };
  43. view.SetRelativeLayout (new Size (80, 25));
  44. Assert.Equal ("Fill(0)", view.Width.ToString ());
  45. Assert.Equal (1, view.Frame.X);
  46. Assert.Equal (1, view.Frame.Y);
  47. Assert.Equal (79, view.Frame.Width);
  48. Assert.Equal (24, view.Frame.Height);
  49. Assert.Equal (0, view.Viewport.X);
  50. Assert.Equal (0, view.Viewport.Y);
  51. Assert.Equal (79, view.Viewport.Width);
  52. Assert.Equal (24, view.Viewport.Height);
  53. view.X = 0;
  54. view.Y = 0;
  55. Assert.Equal ("Absolute(0)", view.X.ToString ());
  56. Assert.Equal ("Fill(0)", view.Width.ToString ());
  57. view.SetRelativeLayout (new Size (80, 25));
  58. Assert.Equal (0, view.Frame.X);
  59. Assert.Equal (0, view.Frame.Y);
  60. Assert.Equal (80, view.Frame.Width);
  61. Assert.Equal (25, view.Frame.Height);
  62. Assert.Equal (0, view.Viewport.X);
  63. Assert.Equal (0, view.Viewport.Y);
  64. Assert.Equal (80, view.Viewport.Width);
  65. Assert.Equal (25, view.Viewport.Height);
  66. }
  67. [Fact]
  68. public void Fill_And_PosCenter ()
  69. {
  70. var screen = new Size (80, 25);
  71. var view = new View { X = Pos.Center (), Y = Pos.Center (), Width = Dim.Fill (), Height = Dim.Fill () };
  72. view.SetRelativeLayout (screen);
  73. Assert.Equal (0, view.Frame.X);
  74. Assert.Equal (0, view.Frame.Y);
  75. Assert.Equal (80, view.Frame.Width);
  76. Assert.Equal (25, view.Frame.Height);
  77. view.X = Pos.Center () + 1;
  78. view.SetRelativeLayout (screen);
  79. Assert.Equal (1, view.Frame.X);
  80. Assert.Equal (0, view.Frame.Y);
  81. Assert.Equal (79, view.Frame.Width);
  82. Assert.Equal (25, view.Frame.Height);
  83. view.X = Pos.Center () + 79;
  84. view.SetRelativeLayout (screen);
  85. Assert.Equal (79, view.Frame.X);
  86. Assert.Equal (0, view.Frame.Y);
  87. Assert.Equal (1, view.Frame.Width);
  88. Assert.Equal (25, view.Frame.Height);
  89. view.X = Pos.Center () + 80;
  90. view.SetRelativeLayout (screen);
  91. Assert.Equal (80, view.Frame.X);
  92. Assert.Equal (0, view.Frame.Y);
  93. Assert.Equal (0, view.Frame.Width);
  94. Assert.Equal (25, view.Frame.Height);
  95. view.X = Pos.Center () - 1;
  96. view.SetRelativeLayout (screen);
  97. Assert.Equal (-1, view.Frame.X);
  98. Assert.Equal (0, view.Frame.Y);
  99. Assert.Equal (81, view.Frame.Width);
  100. Assert.Equal (25, view.Frame.Height);
  101. view.X = Pos.Center () - 2; // Fill means all the way to right. So width will be 82. (dim gets calc'd before pos).
  102. view.SetRelativeLayout (screen);
  103. Assert.Equal (-2, view.Frame.X);
  104. Assert.Equal (0, view.Frame.Y);
  105. Assert.Equal (82, view.Frame.Width);
  106. Assert.Equal (25, view.Frame.Height);
  107. view.X = Pos.Center () - 3; // Fill means all the way to right. So width will be 83. (dim gets calc'd before pos).
  108. view.SetRelativeLayout (screen);
  109. Assert.Equal (-3, view.Frame.X);
  110. Assert.Equal (0, view.Frame.Y);
  111. Assert.Equal (83, view.Frame.Width);
  112. Assert.Equal (25, view.Frame.Height);
  113. view.X = Pos.Center () - 41; // Fill means all the way to right. So width will be . (dim gets calc'd before pos).
  114. view.SetRelativeLayout (screen);
  115. Assert.Equal (-41, view.Frame.X);
  116. Assert.Equal (0, view.Frame.Y);
  117. Assert.Equal (121, view.Frame.Width);
  118. Assert.Equal (25, view.Frame.Height);
  119. }
  120. [Fact]
  121. public void Fill_Pos_Outside_Bounds ()
  122. {
  123. var screen = new Size (80, 25);
  124. var view = new View
  125. {
  126. X = 90, // outside of screen +10
  127. Y = -10, // outside of screen -10
  128. Width = 15,
  129. Height = 15
  130. };
  131. // Layout is Absolute. So the X and Y are not changed.
  132. view.SetRelativeLayout (screen);
  133. Assert.Equal (90, view.Frame.X);
  134. Assert.Equal (-10, view.Frame.Y);
  135. Assert.Equal (15, view.Frame.Width);
  136. Assert.Equal (15, view.Frame.Height);
  137. // prove Width=Height= same as screen size
  138. view.Width = 80;
  139. view.Height = 25;
  140. view.SetRelativeLayout (screen);
  141. Assert.Equal (90, view.Frame.X);
  142. Assert.Equal (-10, view.Frame.Y);
  143. Assert.Equal (80, view.Frame.Width);
  144. Assert.Equal (25, view.Frame.Height);
  145. view.Width = Dim.Fill ();
  146. view.Height = Dim.Fill ();
  147. view.SetRelativeLayout (screen);
  148. Assert.Equal (90, view.Frame.X);
  149. Assert.Equal (-10, view.Frame.Y);
  150. Assert.Equal (
  151. 0,
  152. view.Frame
  153. .Width
  154. ); // proof: 15x15 view is placed beyond right side of screen, so fill width is 0
  155. Assert.Equal (
  156. 35,
  157. view.Frame
  158. .Height
  159. ); // proof: 15x15 view is placed beyond top of screen 10 rows, screen is 25 rows. so fill height is 25 + 10 = 35
  160. }
  161. [Fact]
  162. public void Fill_Pos_Within_Bounds ()
  163. {
  164. var screen = new Size (80, 25);
  165. var view = new View { X = 1, Y = 1, Width = 5, Height = 4 };
  166. view.SetRelativeLayout (screen);
  167. Assert.Equal (1, view.Frame.X);
  168. Assert.Equal (1, view.Frame.Y);
  169. Assert.Equal (5, view.Frame.Width);
  170. Assert.Equal (4, view.Frame.Height);
  171. view.Width = 80;
  172. view.Height = 25;
  173. view.SetRelativeLayout (screen);
  174. Assert.Equal (1, view.Frame.X);
  175. Assert.Equal (1, view.Frame.Y);
  176. Assert.Equal (80, view.Frame.Width);
  177. Assert.Equal (25, view.Frame.Height);
  178. view.Width = Dim.Fill ();
  179. view.Height = Dim.Fill ();
  180. view.SetRelativeLayout (screen);
  181. Assert.Equal (1, view.Frame.X);
  182. Assert.Equal (1, view.Frame.Y);
  183. Assert.Equal (79, view.Frame.Width); // proof (80 - 1)
  184. Assert.Equal (24, view.Frame.Height); // proof (25 - 1)
  185. view.X = 79;
  186. view.Width = Dim.Fill ();
  187. view.Height = Dim.Fill ();
  188. view.SetRelativeLayout (screen);
  189. Assert.Equal (79, view.Frame.X);
  190. Assert.Equal (1, view.Frame.Y);
  191. Assert.Equal (1, view.Frame.Width); // proof (80 - 79)
  192. Assert.Equal (24, view.Frame.Height);
  193. view.X = 80;
  194. view.Width = Dim.Fill ();
  195. view.Height = Dim.Fill ();
  196. view.SetRelativeLayout (screen);
  197. Assert.Equal (80, view.Frame.X);
  198. Assert.Equal (1, view.Frame.Y);
  199. Assert.Equal (0, view.Frame.Width); // proof (80 - 80)
  200. Assert.Equal (24, view.Frame.Height);
  201. }
  202. [Fact]
  203. [TestRespondersDisposed]
  204. public void PosCombine_Plus_Absolute ()
  205. {
  206. var superView = new View { Width = 10, Height = 10 };
  207. var testView = new View
  208. {
  209. X = Pos.Center (),
  210. Y = Pos.Center (),
  211. Width = 1,
  212. Height = 1
  213. };
  214. superView.Add (testView);
  215. testView.SetRelativeLayout (superView.Frame.Size);
  216. Assert.Equal (4, testView.Frame.X);
  217. Assert.Equal (4, testView.Frame.Y);
  218. testView = new ()
  219. {
  220. X = Pos.Center () + 1, // ((10 / 2) - (1 / 2)) + 1 = 5 - 1 + 1 = 5
  221. Y = Pos.Center () + 1,
  222. Width = 1,
  223. Height = 1
  224. };
  225. superView.Add (testView);
  226. testView.SetRelativeLayout (superView.Frame.Size);
  227. Assert.Equal (5, testView.Frame.X);
  228. Assert.Equal (5, testView.Frame.Y);
  229. testView = new ()
  230. {
  231. X = 1 + Pos.Center (),
  232. Y = 1 + Pos.Center (),
  233. Width = 1,
  234. Height = 1
  235. };
  236. superView.Add (testView);
  237. testView.SetRelativeLayout (superView.Frame.Size);
  238. Assert.Equal (5, testView.Frame.X);
  239. Assert.Equal (5, testView.Frame.Y);
  240. testView = new ()
  241. {
  242. X = 1 + Pos.Percent (50),
  243. Y = Pos.Percent (50) + 1,
  244. Width = 1,
  245. Height = 1
  246. };
  247. superView.Add (testView);
  248. testView.SetRelativeLayout (superView.Frame.Size);
  249. Assert.Equal (6, testView.Frame.X);
  250. Assert.Equal (6, testView.Frame.Y);
  251. testView = new ()
  252. {
  253. X = Pos.Percent (10) + Pos.Percent (40),
  254. Y = Pos.Percent (10) + Pos.Percent (40),
  255. Width = 1,
  256. Height = 1
  257. };
  258. superView.Add (testView);
  259. testView.SetRelativeLayout (superView.Frame.Size);
  260. Assert.Equal (5, testView.Frame.X);
  261. Assert.Equal (5, testView.Frame.Y);
  262. testView = new ()
  263. {
  264. X = 1 + Pos.Percent (10) + Pos.Percent (40) - 1,
  265. Y = 5 + Pos.Percent (10) + Pos.Percent (40) - 5,
  266. Width = 1,
  267. Height = 1
  268. };
  269. superView.Add (testView);
  270. testView.SetRelativeLayout (superView.Frame.Size);
  271. Assert.Equal (5, testView.Frame.X);
  272. Assert.Equal (5, testView.Frame.Y);
  273. testView = new ()
  274. {
  275. X = Pos.Left (testView),
  276. Y = Pos.Left (testView),
  277. Width = 1,
  278. Height = 1
  279. };
  280. superView.Add (testView);
  281. testView.SetRelativeLayout (superView.Frame.Size);
  282. Assert.Equal (5, testView.Frame.X);
  283. Assert.Equal (5, testView.Frame.Y);
  284. testView = new ()
  285. {
  286. X = 1 + Pos.Left (testView),
  287. Y = Pos.Top (testView) + 1,
  288. Width = 1,
  289. Height = 1
  290. };
  291. superView.Add (testView);
  292. testView.SetRelativeLayout (superView.Frame.Size);
  293. Assert.Equal (6, testView.Frame.X);
  294. Assert.Equal (6, testView.Frame.Y);
  295. superView.Dispose ();
  296. }
  297. [Fact]
  298. public void PosCombine_PosCenter_Minus_Absolute ()
  299. {
  300. // This test used to be in ViewTests.cs Internal_Tests. It was moved here because it is testing
  301. // SetRelativeLayout. In addition, the old test was bogus because it was testing the wrong thing (and
  302. // because in v1 Pos.Center was broken in this regard!
  303. var screen = new Size (80, 25);
  304. var view = new View
  305. {
  306. X = Pos.Center () - 41, // -2 off left edge of screen
  307. Y = Pos.Center () - 13, // -1 off top edge of screen
  308. Width = 1,
  309. Height = 1
  310. };
  311. view.SetRelativeLayout (screen);
  312. Assert.Equal (-2, view.Frame.X); // proof: 1x1 view centered in 80x25 screen has x of 39, so -41 is -2
  313. Assert.Equal (-1, view.Frame.Y); // proof: 1x1 view centered in 80x25 screen has y of 12, so -13 is -1
  314. view.Width = 80;
  315. view.Height = 25;
  316. view.SetRelativeLayout (screen);
  317. Assert.Equal (-41, view.Frame.X);
  318. Assert.Equal (-13, view.Frame.Y);
  319. Assert.Equal (80, view.Frame.Width);
  320. Assert.Equal (25, view.Frame.Height);
  321. view.Width = Dim.Fill ();
  322. view.Height = Dim.Fill ();
  323. view.SetRelativeLayout (screen);
  324. Assert.Equal (-41, view.Frame.X);
  325. Assert.Equal (-13, view.Frame.Y);
  326. Assert.Equal (121, view.Frame.Width); // 121 = screen.Width - (-Center - 41)
  327. Assert.Equal (38, view.Frame.Height);
  328. }
  329. [Fact]
  330. public void PosCombine_PosCenter_Plus_Absolute ()
  331. {
  332. var screen = new Size (80, 25);
  333. var view = new View
  334. {
  335. X = Pos.Center () + 41, // ((80 / 2) - (5 / 2)) + 41 = (40 - 3 + 41) = 78
  336. Y = Pos.Center () + 13, // ((25 / 2) - (4 / 2)) + 13 = (12 - 2 + 13) = 23
  337. Width = 5,
  338. Height = 4
  339. };
  340. view.SetRelativeLayout (screen);
  341. Assert.Equal (78, view.Frame.X);
  342. Assert.Equal (23, view.Frame.Y);
  343. }
  344. [Fact]
  345. public void PosDimFunc ()
  346. {
  347. var screen = new Size (30, 1);
  348. var view = new View
  349. {
  350. Text = "abc",
  351. Width = Auto (DimAutoStyle.Text),
  352. Height = Auto (DimAutoStyle.Text)
  353. };
  354. view.X = Pos.AnchorEnd (0) - Pos.Func (GetViewWidth);
  355. int GetViewWidth () { return view.Frame.Width; }
  356. // view will be 3 chars wide. It's X will be 27 (30 - 3).
  357. // BUGBUG: IsInitialized need to be true before calculate
  358. view.BeginInit ();
  359. view.EndInit ();
  360. view.SetRelativeLayout (screen);
  361. Assert.Equal (27, view.Frame.X);
  362. Assert.Equal (0, view.Frame.Y);
  363. Assert.Equal (3, view.Frame.Width);
  364. Assert.Equal (1, view.Frame.Height);
  365. var tf = new TextField { Text = "01234567890123456789" };
  366. tf.Width = Dim.Fill (1) - Dim.Func (GetViewWidth);
  367. // tf will fill the screen minus 1 minus the width of view (3).
  368. // so it's width will be 26 (30 - 1 - 3).
  369. tf.SetRelativeLayout (screen);
  370. Assert.Equal (0, tf.Frame.X);
  371. Assert.Equal (0, tf.Frame.Y);
  372. Assert.Equal (26, tf.Frame.Width);
  373. Assert.Equal (1, tf.Frame.Height);
  374. }
  375. }