Pos.Tests.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. using Xunit.Abstractions;
  2. using static Terminal.Gui.Dim;
  3. using static Terminal.Gui.Pos;
  4. namespace Terminal.Gui.LayoutTests;
  5. public class PosTests ()
  6. {
  7. [Fact]
  8. public void
  9. Pos_Validation_Do_Not_Throws_If_NewValue_Is_PosAbsolute_And_OldValue_Is_Another_Type_After_Sets_To_LayoutStyle_Absolute ()
  10. {
  11. Application.Init (new FakeDriver ());
  12. Toplevel t = new ();
  13. var w = new Window { X = Pos.Left (t) + 2, Y = Pos.Absolute (2) };
  14. var v = new View { X = Pos.Center (), Y = Pos.Percent (10) };
  15. w.Add (v);
  16. t.Add (w);
  17. t.Ready += (s, e) =>
  18. {
  19. v.Frame = new Rectangle (2, 2, 10, 10);
  20. Assert.Equal (2, v.X = 2);
  21. Assert.Equal (2, v.Y = 2);
  22. };
  23. Application.Iteration += (s, a) => Application.RequestStop ();
  24. Application.Run (t);
  25. t.Dispose ();
  26. Application.Shutdown ();
  27. }
  28. [Fact]
  29. public void PosCombine_Calculate_ReturnsExpectedValue ()
  30. {
  31. var posCombine = new PosCombine (AddOrSubtract.Add, new PosAbsolute (5), new PosAbsolute (3));
  32. var result = posCombine.Calculate (10, new DimAbsolute (2), null, Dimension.None);
  33. Assert.Equal (8, result);
  34. }
  35. [Fact]
  36. public void PosFactor_Calculate_ReturnsExpectedValue ()
  37. {
  38. var posFactor = new PosPercent (50);
  39. var result = posFactor.Calculate (10, new DimAbsolute (2), null, Dimension.None);
  40. Assert.Equal (5, result);
  41. }
  42. [Fact]
  43. public void PosFunc_Calculate_ReturnsExpectedValue ()
  44. {
  45. var posFunc = new PosFunc (() => 5);
  46. var result = posFunc.Calculate (10, new DimAbsolute (2), null, Dimension.None);
  47. Assert.Equal (5, result);
  48. }
  49. [Fact]
  50. public void PosView_Calculate_ReturnsExpectedValue ()
  51. {
  52. var posView = new PosView (new View { Frame = new Rectangle (5, 5, 10, 10) }, 0);
  53. var result = posView.Calculate (10, new DimAbsolute (2), null, Dimension.None);
  54. Assert.Equal (5, result);
  55. }
  56. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  57. // TODO: A new test that calls SetRelativeLayout directly is needed.
  58. [Fact]
  59. [TestRespondersDisposed]
  60. public void PosCombine_WHY_Throws ()
  61. {
  62. Application.Init (new FakeDriver ());
  63. Toplevel t = new Toplevel ();
  64. var w = new Window { X = Pos.Left (t) + 2, Y = Pos.Top (t) + 2 };
  65. var f = new FrameView ();
  66. var v1 = new View { X = Pos.Left (w) + 2, Y = Pos.Top (w) + 2 };
  67. var v2 = new View { X = Pos.Left (v1) + 2, Y = Pos.Top (v1) + 2 };
  68. f.Add (v1); // v2 not added
  69. w.Add (f);
  70. t.Add (w);
  71. f.X = Pos.X (v2) - Pos.X (v1);
  72. f.Y = Pos.Y (v2) - Pos.Y (v1);
  73. Assert.Throws<LayoutException> (() => Application.Run (t));
  74. t.Dispose ();
  75. Application.Shutdown ();
  76. v2.Dispose ();
  77. }
  78. [Fact]
  79. public void PosCombine_DoesNotReturn ()
  80. {
  81. var v = new View { Id = "V" };
  82. Pos pos = Pos.Left (v);
  83. Assert.Equal (
  84. $"View(Side=Left,Target=View(V){v.Frame})",
  85. pos.ToString ()
  86. );
  87. pos = Pos.X (v);
  88. Assert.Equal (
  89. $"View(Side=Left,Target=View(V){v.Frame})",
  90. pos.ToString ()
  91. );
  92. pos = Pos.Top (v);
  93. Assert.Equal (
  94. $"View(Side=Top,Target=View(V){v.Frame})",
  95. pos.ToString ()
  96. );
  97. pos = Pos.Y (v);
  98. Assert.Equal (
  99. $"View(Side=Top,Target=View(V){v.Frame})",
  100. pos.ToString ()
  101. );
  102. pos = Pos.Right (v);
  103. Assert.Equal (
  104. $"View(Side=Right,Target=View(V){v.Frame})",
  105. pos.ToString ()
  106. );
  107. pos = Pos.Bottom (v);
  108. Assert.Equal (
  109. $"View(Side=Bottom,Target=View(V){v.Frame})",
  110. pos.ToString ()
  111. );
  112. }
  113. [Fact]
  114. public void PosFunction_SetsValue ()
  115. {
  116. var text = "Test";
  117. Pos pos = Pos.Func (() => text.Length);
  118. Assert.Equal ("PosFunc(4)", pos.ToString ());
  119. text = "New Test";
  120. Assert.Equal ("PosFunc(8)", pos.ToString ());
  121. text = "";
  122. Assert.Equal ("PosFunc(0)", pos.ToString ());
  123. }
  124. [Fact]
  125. [TestRespondersDisposed]
  126. public void Internal_Tests ()
  127. {
  128. var posFactor = new PosPercent (10);
  129. Assert.Equal (10, posFactor.GetAnchor (100));
  130. var posAnchorEnd = new PosAnchorEnd (1);
  131. Assert.Equal (99, posAnchorEnd.GetAnchor (100));
  132. var posCenter = new PosCenter ();
  133. Assert.Equal (50, posCenter.GetAnchor (100));
  134. var posAbsolute = new PosAbsolute (10);
  135. Assert.Equal (10, posAbsolute.GetAnchor (0));
  136. var posCombine = new PosCombine (AddOrSubtract.Add, posFactor, posAbsolute);
  137. Assert.Equal (posCombine.Left, posFactor);
  138. Assert.Equal (posCombine.Right, posAbsolute);
  139. Assert.Equal (20, posCombine.GetAnchor (100));
  140. posCombine = new (AddOrSubtract.Add, posAbsolute, posFactor);
  141. Assert.Equal (posCombine.Left, posAbsolute);
  142. Assert.Equal (posCombine.Right, posFactor);
  143. Assert.Equal (20, posCombine.GetAnchor (100));
  144. var view = new View { Frame = new (20, 10, 20, 1) };
  145. var posViewX = new PosView (view, Side.Left);
  146. Assert.Equal (20, posViewX.GetAnchor (0));
  147. var posViewY = new PosView (view, Side.Top);
  148. Assert.Equal (10, posViewY.GetAnchor (0));
  149. var posRight = new PosView (view, Side.Right);
  150. Assert.Equal (40, posRight.GetAnchor (0));
  151. var posViewBottom = new PosView (view, Side.Bottom);
  152. Assert.Equal (11, posViewBottom.GetAnchor (0));
  153. view.Dispose ();
  154. }
  155. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  156. // TODO: A new test that calls SetRelativeLayout directly is needed.
  157. // See: https://github.com/gui-cs/Terminal.Gui/issues/504
  158. [Fact]
  159. [TestRespondersDisposed]
  160. public void LeftTopBottomRight_Win_ShouldNotThrow ()
  161. {
  162. // Test cases:
  163. (Toplevel top, Window win, Button button) app = Setup ();
  164. app.button.Y = Pos.Left (app.win);
  165. RunState rs = Application.Begin (app.top);
  166. // If Application.RunState is used then we must use Application.RunLoop with the rs parameter
  167. Application.RunLoop (rs);
  168. Cleanup (rs);
  169. app = Setup ();
  170. app.button.Y = Pos.X (app.win);
  171. rs = Application.Begin (app.top);
  172. // If Application.RunState is used then we must use Application.RunLoop with the rs parameter
  173. Application.RunLoop (rs);
  174. Cleanup (rs);
  175. app = Setup ();
  176. app.button.Y = Pos.Top (app.win);
  177. rs = Application.Begin (app.top);
  178. // If Application.RunState is used then we must use Application.RunLoop with the rs parameter
  179. Application.RunLoop (rs);
  180. Cleanup (rs);
  181. app = Setup ();
  182. app.button.Y = Pos.Y (app.win);
  183. rs = Application.Begin (app.top);
  184. // If Application.RunState is used then we must use Application.RunLoop with the rs parameter
  185. Application.RunLoop (rs);
  186. Cleanup (rs);
  187. app = Setup ();
  188. app.button.Y = Pos.Bottom (app.win);
  189. rs = Application.Begin (app.top);
  190. // If Application.RunState is used then we must use Application.RunLoop with the rs parameter
  191. Application.RunLoop (rs);
  192. Cleanup (rs);
  193. app = Setup ();
  194. app.button.Y = Pos.Right (app.win);
  195. rs = Application.Begin (app.top);
  196. // If Application.RunState is used then we must use Application.RunLoop with the rs parameter
  197. Application.RunLoop (rs);
  198. Cleanup (rs);
  199. return;
  200. void Cleanup (RunState rs)
  201. {
  202. // Cleanup
  203. Application.End (rs);
  204. Application.Top.Dispose ();
  205. // Shutdown must be called to safely clean up Application if Init has been called
  206. Application.Shutdown ();
  207. }
  208. // Setup Fake driver
  209. (Toplevel top, Window win, Button button) Setup ()
  210. {
  211. Application.Init (new FakeDriver ());
  212. Application.Iteration += (s, a) => { Application.RequestStop (); };
  213. var win = new Window { X = 0, Y = 0, Width = Dim.Fill (), Height = Dim.Fill () };
  214. var top = new Toplevel ();
  215. top.Add (win);
  216. var button = new Button { X = Pos.Center (), Text = "button" };
  217. win.Add (button);
  218. return (top, win, button);
  219. }
  220. }
  221. [Fact]
  222. public void PosPercent_Equal ()
  223. {
  224. int n1 = 0;
  225. int n2 = 0;
  226. Pos pos1 = Pos.Percent (n1);
  227. Pos pos2 = Pos.Percent (n2);
  228. Assert.Equal (pos1, pos2);
  229. n1 = n2 = 1;
  230. pos1 = Pos.Percent (n1);
  231. pos2 = Pos.Percent (n2);
  232. Assert.Equal (pos1, pos2);
  233. n1 = n2 = 50;
  234. pos1 = Pos.Percent (n1);
  235. pos2 = Pos.Percent (n2);
  236. Assert.Equal (pos1, pos2);
  237. n1 = n2 = 100;
  238. pos1 = Pos.Percent (n1);
  239. pos2 = Pos.Percent (n2);
  240. Assert.Equal (pos1, pos2);
  241. n1 = 0;
  242. n2 = 1;
  243. pos1 = Pos.Percent (n1);
  244. pos2 = Pos.Percent (n2);
  245. Assert.NotEqual (pos1, pos2);
  246. n1 = 50;
  247. n2 = 150;
  248. pos1 = Pos.Percent (n1);
  249. pos2 = Pos.Percent (n2);
  250. Assert.NotEqual (pos1, pos2);
  251. }
  252. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  253. // TODO: A new test that calls SetRelativeLayout directly is needed.
  254. [Fact]
  255. [TestRespondersDisposed]
  256. public void Pos_Add_Operator ()
  257. {
  258. Application.Init (new FakeDriver ());
  259. Toplevel top = new ();
  260. var view = new View { X = 0, Y = 0, Width = 20, Height = 20 };
  261. var field = new TextField { X = 0, Y = 0, Width = 20 };
  262. var count = 0;
  263. field.KeyDown += (s, k) =>
  264. {
  265. if (k.KeyCode == KeyCode.Enter)
  266. {
  267. field.Text = $"View {count}";
  268. var view2 = new View { X = 0, Y = field.Y, Width = 20, Text = field.Text };
  269. view.Add (view2);
  270. Assert.Equal ($"View {count}", view2.Text);
  271. Assert.Equal ($"Absolute({count})", view2.Y.ToString ());
  272. Assert.Equal ($"Absolute({count})", field.Y.ToString ());
  273. field.Y += 1;
  274. count++;
  275. Assert.Equal ($"Absolute({count})", field.Y.ToString ());
  276. }
  277. };
  278. Application.Iteration += (s, a) =>
  279. {
  280. while (count < 20)
  281. {
  282. field.NewKeyDownEvent (Key.Enter);
  283. }
  284. Application.RequestStop ();
  285. };
  286. var win = new Window ();
  287. win.Add (view);
  288. win.Add (field);
  289. top.Add (win);
  290. Application.Run (top);
  291. Assert.Equal (20, count);
  292. top.Dispose ();
  293. // Shutdown must be called to safely clean up Application if Init has been called
  294. Application.Shutdown ();
  295. }
  296. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  297. // TODO: A new test that calls SetRelativeLayout directly is needed.
  298. [Fact]
  299. [TestRespondersDisposed]
  300. public void Pos_Subtract_Operator ()
  301. {
  302. Application.Init (new FakeDriver ());
  303. Toplevel top = new ();
  304. var view = new View { X = 0, Y = 0, Width = 20, Height = 20 };
  305. var field = new TextField { X = 0, Y = 0, Width = 20 };
  306. var count = 20;
  307. List<View> listViews = new ();
  308. for (var i = 0; i < count; i++)
  309. {
  310. field.Text = $"View {i}";
  311. var view2 = new View { X = 0, Y = field.Y, Width = 20, Text = field.Text };
  312. view.Add (view2);
  313. Assert.Equal ($"View {i}", view2.Text);
  314. Assert.Equal ($"Absolute({i})", field.Y.ToString ());
  315. listViews.Add (view2);
  316. Assert.Equal ($"Absolute({i})", field.Y.ToString ());
  317. field.Y += 1;
  318. Assert.Equal ($"Absolute({i + 1})", field.Y.ToString ());
  319. }
  320. field.KeyDown += (s, k) =>
  321. {
  322. if (k.KeyCode == KeyCode.Enter)
  323. {
  324. Assert.Equal ($"View {count - 1}", listViews [count - 1].Text);
  325. view.Remove (listViews [count - 1]);
  326. listViews [count - 1].Dispose ();
  327. Assert.Equal ($"Absolute({count})", field.Y.ToString ());
  328. field.Y -= 1;
  329. count--;
  330. Assert.Equal ($"Absolute({count})", field.Y.ToString ());
  331. }
  332. };
  333. Application.Iteration += (s, a) =>
  334. {
  335. while (count > 0)
  336. {
  337. field.NewKeyDownEvent (Key.Enter);
  338. }
  339. Application.RequestStop ();
  340. };
  341. var win = new Window ();
  342. win.Add (view);
  343. win.Add (field);
  344. top.Add (win);
  345. Application.Run (top);
  346. Assert.Equal (0, count);
  347. top.Dispose ();
  348. // Shutdown must be called to safely clean up Application if Init has been called
  349. Application.Shutdown ();
  350. }
  351. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  352. // TODO: A new test that calls SetRelativeLayout directly is needed.
  353. [Fact]
  354. public void Pos_Validation_Do_Not_Throws_If_NewValue_Is_PosAbsolute_And_OldValue_Is_Null ()
  355. {
  356. Application.Init (new FakeDriver ());
  357. Toplevel t = new ();
  358. var w = new Window { X = 1, Y = 2, Width = 3, Height = 5 };
  359. t.Add (w);
  360. t.Ready += (s, e) =>
  361. {
  362. Assert.Equal (2, w.X = 2);
  363. Assert.Equal (2, w.Y = 2);
  364. };
  365. Application.Iteration += (s, a) => Application.RequestStop ();
  366. Application.Run (t);
  367. t.Dispose ();
  368. Application.Shutdown ();
  369. }
  370. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  371. // TODO: A new test that calls SetRelativeLayout directly is needed.
  372. [Fact]
  373. public void Validation_Does_Not_Throw_If_NewValue_Is_PosAbsolute_And_OldValue_Is_Null ()
  374. {
  375. Application.Init (new FakeDriver ());
  376. Toplevel t = new Toplevel ();
  377. var w = new Window { X = 1, Y = 2, Width = 3, Height = 5 };
  378. t.Add (w);
  379. t.Ready += (s, e) =>
  380. {
  381. Assert.Equal (2, w.X = 2);
  382. Assert.Equal (2, w.Y = 2);
  383. };
  384. Application.Iteration += (s, a) => Application.RequestStop ();
  385. Application.Run (t);
  386. t.Dispose ();
  387. Application.Shutdown ();
  388. }
  389. }