SetRelativeLayoutTests.cs 11 KB

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