Pos.Tests.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  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<InvalidOperationException> (() => 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_Equal ()
  115. {
  116. Func<int> f1 = () => 0;
  117. Func<int> f2 = () => 0;
  118. Pos pos1 = Pos.Func (f1);
  119. Pos pos2 = Pos.Func (f2);
  120. Assert.Equal (pos1, pos2);
  121. f2 = () => 1;
  122. pos2 = Pos.Func (f2);
  123. Assert.NotEqual (pos1, pos2);
  124. }
  125. [Fact]
  126. public void PosFunction_SetsValue ()
  127. {
  128. var text = "Test";
  129. Pos pos = Pos.Func (() => text.Length);
  130. Assert.Equal ("PosFunc(4)", pos.ToString ());
  131. text = "New Test";
  132. Assert.Equal ("PosFunc(8)", pos.ToString ());
  133. text = "";
  134. Assert.Equal ("PosFunc(0)", pos.ToString ());
  135. }
  136. [Fact]
  137. [TestRespondersDisposed]
  138. public void Internal_Tests ()
  139. {
  140. var posFactor = new PosPercent (10);
  141. Assert.Equal (10, posFactor.GetAnchor (100));
  142. var posAnchorEnd = new PosAnchorEnd (1);
  143. Assert.Equal (99, posAnchorEnd.GetAnchor (100));
  144. var posCenter = new PosCenter ();
  145. Assert.Equal (50, posCenter.GetAnchor (100));
  146. var posAbsolute = new PosAbsolute (10);
  147. Assert.Equal (10, posAbsolute.GetAnchor (0));
  148. var posCombine = new PosCombine (AddOrSubtract.Add, posFactor, posAbsolute);
  149. Assert.Equal (posCombine.Left, posFactor);
  150. Assert.Equal (posCombine.Right, posAbsolute);
  151. Assert.Equal (20, posCombine.GetAnchor (100));
  152. posCombine = new (AddOrSubtract.Add, posAbsolute, posFactor);
  153. Assert.Equal (posCombine.Left, posAbsolute);
  154. Assert.Equal (posCombine.Right, posFactor);
  155. Assert.Equal (20, posCombine.GetAnchor (100));
  156. var view = new View { Frame = new (20, 10, 20, 1) };
  157. var posViewX = new PosView (view, Side.Left);
  158. Assert.Equal (20, posViewX.GetAnchor (0));
  159. var posViewY = new PosView (view, Side.Top);
  160. Assert.Equal (10, posViewY.GetAnchor (0));
  161. var posRight = new PosView (view, Side.Right);
  162. Assert.Equal (40, posRight.GetAnchor (0));
  163. var posViewBottom = new PosView (view, Side.Bottom);
  164. Assert.Equal (11, posViewBottom.GetAnchor (0));
  165. view.Dispose ();
  166. }
  167. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  168. // TODO: A new test that calls SetRelativeLayout directly is needed.
  169. // See: https://github.com/gui-cs/Terminal.Gui/issues/504
  170. [Fact]
  171. [TestRespondersDisposed]
  172. public void LeftTopBottomRight_Win_ShouldNotThrow ()
  173. {
  174. // Test cases:
  175. (Toplevel top, Window win, Button button) app = Setup ();
  176. app.button.Y = Pos.Left (app.win);
  177. RunState 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.X (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.Top (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.Y (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. app = Setup ();
  200. app.button.Y = Pos.Bottom (app.win);
  201. rs = Application.Begin (app.top);
  202. // If Application.RunState is used then we must use Application.RunLoop with the rs parameter
  203. Application.RunLoop (rs);
  204. Cleanup (rs);
  205. app = Setup ();
  206. app.button.Y = Pos.Right (app.win);
  207. rs = Application.Begin (app.top);
  208. // If Application.RunState is used then we must use Application.RunLoop with the rs parameter
  209. Application.RunLoop (rs);
  210. Cleanup (rs);
  211. return;
  212. void Cleanup (RunState rs)
  213. {
  214. // Cleanup
  215. Application.End (rs);
  216. Application.Top.Dispose ();
  217. // Shutdown must be called to safely clean up Application if Init has been called
  218. Application.Shutdown ();
  219. }
  220. // Setup Fake driver
  221. (Toplevel top, Window win, Button button) Setup ()
  222. {
  223. Application.Init (new FakeDriver ());
  224. Application.Iteration += (s, a) => { Application.RequestStop (); };
  225. var win = new Window { X = 0, Y = 0, Width = Dim.Fill (), Height = Dim.Fill () };
  226. var top = new Toplevel ();
  227. top.Add (win);
  228. var button = new Button { X = Pos.Center (), Text = "button" };
  229. win.Add (button);
  230. return (top, win, button);
  231. }
  232. }
  233. [Fact]
  234. public void PosPercent_Equal ()
  235. {
  236. int n1 = 0;
  237. int n2 = 0;
  238. Pos pos1 = Pos.Percent (n1);
  239. Pos pos2 = Pos.Percent (n2);
  240. Assert.Equal (pos1, pos2);
  241. n1 = n2 = 1;
  242. pos1 = Pos.Percent (n1);
  243. pos2 = Pos.Percent (n2);
  244. Assert.Equal (pos1, pos2);
  245. n1 = n2 = 50;
  246. pos1 = Pos.Percent (n1);
  247. pos2 = Pos.Percent (n2);
  248. Assert.Equal (pos1, pos2);
  249. n1 = n2 = 100;
  250. pos1 = Pos.Percent (n1);
  251. pos2 = Pos.Percent (n2);
  252. Assert.Equal (pos1, pos2);
  253. n1 = 0;
  254. n2 = 1;
  255. pos1 = Pos.Percent (n1);
  256. pos2 = Pos.Percent (n2);
  257. Assert.NotEqual (pos1, pos2);
  258. n1 = 50;
  259. n2 = 150;
  260. pos1 = Pos.Percent (n1);
  261. pos2 = Pos.Percent (n2);
  262. Assert.NotEqual (pos1, pos2);
  263. }
  264. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  265. // TODO: A new test that calls SetRelativeLayout directly is needed.
  266. [Fact]
  267. [TestRespondersDisposed]
  268. public void Pos_Add_Operator ()
  269. {
  270. Application.Init (new FakeDriver ());
  271. Toplevel top = new ();
  272. var view = new View { X = 0, Y = 0, Width = 20, Height = 20 };
  273. var field = new TextField { X = 0, Y = 0, Width = 20 };
  274. var count = 0;
  275. field.KeyDown += (s, k) =>
  276. {
  277. if (k.KeyCode == KeyCode.Enter)
  278. {
  279. field.Text = $"View {count}";
  280. var view2 = new View { X = 0, Y = field.Y, Width = 20, Text = field.Text };
  281. view.Add (view2);
  282. Assert.Equal ($"View {count}", view2.Text);
  283. Assert.Equal ($"Absolute({count})", view2.Y.ToString ());
  284. Assert.Equal ($"Absolute({count})", field.Y.ToString ());
  285. field.Y += 1;
  286. count++;
  287. Assert.Equal ($"Absolute({count})", field.Y.ToString ());
  288. }
  289. };
  290. Application.Iteration += (s, a) =>
  291. {
  292. while (count < 20)
  293. {
  294. field.NewKeyDownEvent (Key.Enter);
  295. }
  296. Application.RequestStop ();
  297. };
  298. var win = new Window ();
  299. win.Add (view);
  300. win.Add (field);
  301. top.Add (win);
  302. Application.Run (top);
  303. Assert.Equal (20, count);
  304. top.Dispose ();
  305. // Shutdown must be called to safely clean up Application if Init has been called
  306. Application.Shutdown ();
  307. }
  308. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  309. // TODO: A new test that calls SetRelativeLayout directly is needed.
  310. [Fact]
  311. [TestRespondersDisposed]
  312. public void Pos_Subtract_Operator ()
  313. {
  314. Application.Init (new FakeDriver ());
  315. Toplevel top = new ();
  316. var view = new View { X = 0, Y = 0, Width = 20, Height = 20 };
  317. var field = new TextField { X = 0, Y = 0, Width = 20 };
  318. var count = 20;
  319. List<View> listViews = new ();
  320. for (var i = 0; i < count; i++)
  321. {
  322. field.Text = $"View {i}";
  323. var view2 = new View { X = 0, Y = field.Y, Width = 20, Text = field.Text };
  324. view.Add (view2);
  325. Assert.Equal ($"View {i}", view2.Text);
  326. Assert.Equal ($"Absolute({i})", field.Y.ToString ());
  327. listViews.Add (view2);
  328. Assert.Equal ($"Absolute({i})", field.Y.ToString ());
  329. field.Y += 1;
  330. Assert.Equal ($"Absolute({i + 1})", field.Y.ToString ());
  331. }
  332. field.KeyDown += (s, k) =>
  333. {
  334. if (k.KeyCode == KeyCode.Enter)
  335. {
  336. Assert.Equal ($"View {count - 1}", listViews [count - 1].Text);
  337. view.Remove (listViews [count - 1]);
  338. listViews [count - 1].Dispose ();
  339. Assert.Equal ($"Absolute({count})", field.Y.ToString ());
  340. field.Y -= 1;
  341. count--;
  342. Assert.Equal ($"Absolute({count})", field.Y.ToString ());
  343. }
  344. };
  345. Application.Iteration += (s, a) =>
  346. {
  347. while (count > 0)
  348. {
  349. field.NewKeyDownEvent (Key.Enter);
  350. }
  351. Application.RequestStop ();
  352. };
  353. var win = new Window ();
  354. win.Add (view);
  355. win.Add (field);
  356. top.Add (win);
  357. Application.Run (top);
  358. Assert.Equal (0, count);
  359. top.Dispose ();
  360. // Shutdown must be called to safely clean up Application if Init has been called
  361. Application.Shutdown ();
  362. }
  363. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  364. // TODO: A new test that calls SetRelativeLayout directly is needed.
  365. [Fact]
  366. public void Pos_Validation_Do_Not_Throws_If_NewValue_Is_PosAbsolute_And_OldValue_Is_Null ()
  367. {
  368. Application.Init (new FakeDriver ());
  369. Toplevel t = new ();
  370. var w = new Window { X = 1, Y = 2, Width = 3, Height = 5 };
  371. t.Add (w);
  372. t.Ready += (s, e) =>
  373. {
  374. Assert.Equal (2, w.X = 2);
  375. Assert.Equal (2, w.Y = 2);
  376. };
  377. Application.Iteration += (s, a) => Application.RequestStop ();
  378. Application.Run (t);
  379. t.Dispose ();
  380. Application.Shutdown ();
  381. }
  382. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  383. // TODO: A new test that calls SetRelativeLayout directly is needed.
  384. [Fact]
  385. public void Validation_Does_Not_Throw_If_NewValue_Is_PosAbsolute_And_OldValue_Is_Null ()
  386. {
  387. Application.Init (new FakeDriver ());
  388. Toplevel t = new Toplevel ();
  389. var w = new Window { X = 1, Y = 2, Width = 3, Height = 5 };
  390. t.Add (w);
  391. t.Ready += (s, e) =>
  392. {
  393. Assert.Equal (2, w.X = 2);
  394. Assert.Equal (2, w.Y = 2);
  395. };
  396. Application.Iteration += (s, a) => Application.RequestStop ();
  397. Application.Run (t);
  398. t.Dispose ();
  399. Application.Shutdown ();
  400. }
  401. }