SetRelativeLayoutTests.cs 14 KB

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