Pos.Tests.cs 16 KB

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