SetRelativeLayoutTests.cs 14 KB

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