Pos.Tests.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  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. // Setup Fake driver
  177. (Toplevel top, Window win, Button button) Setup ()
  178. {
  179. Application.Init (new FakeDriver ());
  180. Application.Iteration += (s, a) => { Application.RequestStop (); };
  181. var win = new Window { X = 0, Y = 0, Width = Dim.Fill (), Height = Dim.Fill () };
  182. var top = new Toplevel ();
  183. top.Add (win);
  184. var button = new Button { X = Pos.Center (), Text = "button" };
  185. win.Add (button);
  186. return (top, win, button);
  187. }
  188. void Cleanup (RunState rs)
  189. {
  190. // Cleanup
  191. Application.End (rs);
  192. Application.Top.Dispose ();
  193. // Shutdown must be called to safely clean up Application if Init has been called
  194. Application.Shutdown ();
  195. }
  196. // Test cases:
  197. (Toplevel top, Window win, Button button) app = Setup ();
  198. app.button.Y = Pos.Left (app.win);
  199. RunState rs = Application.Begin (app.top);
  200. // If Application.RunState is used then we must use Application.RunLoop with the rs parameter
  201. Application.RunLoop (rs);
  202. Cleanup (rs);
  203. app = Setup ();
  204. app.button.Y = Pos.X (app.win);
  205. rs = Application.Begin (app.top);
  206. // If Application.RunState is used then we must use Application.RunLoop with the rs parameter
  207. Application.RunLoop (rs);
  208. Cleanup (rs);
  209. app = Setup ();
  210. app.button.Y = Pos.Top (app.win);
  211. rs = Application.Begin (app.top);
  212. // If Application.RunState is used then we must use Application.RunLoop with the rs parameter
  213. Application.RunLoop (rs);
  214. Cleanup (rs);
  215. app = Setup ();
  216. app.button.Y = Pos.Y (app.win);
  217. rs = Application.Begin (app.top);
  218. // If Application.RunState is used then we must use Application.RunLoop with the rs parameter
  219. Application.RunLoop (rs);
  220. Cleanup (rs);
  221. app = Setup ();
  222. app.button.Y = Pos.Bottom (app.win);
  223. rs = Application.Begin (app.top);
  224. // If Application.RunState is used then we must use Application.RunLoop with the rs parameter
  225. Application.RunLoop (rs);
  226. Cleanup (rs);
  227. app = Setup ();
  228. app.button.Y = Pos.Right (app.win);
  229. rs = Application.Begin (app.top);
  230. // If Application.RunState is used then we must use Application.RunLoop with the rs parameter
  231. Application.RunLoop (rs);
  232. Cleanup (rs);
  233. return;
  234. void Cleanup (RunState rs)
  235. {
  236. // Cleanup
  237. Application.End (rs);
  238. Application.Top.Dispose ();
  239. // Shutdown must be called to safely clean up Application if Init has been called
  240. Application.Shutdown ();
  241. }
  242. // Setup Fake driver
  243. (Toplevel top, Window win, Button button) Setup ()
  244. {
  245. Application.Init (new FakeDriver ());
  246. Application.Iteration += (s, a) => { Application.RequestStop (); };
  247. var win = new Window { X = 0, Y = 0, Width = Dim.Fill (), Height = Dim.Fill () };
  248. var top = new Toplevel ();
  249. top.Add (win);
  250. var button = new Button { X = Pos.Center (), Text = "button" };
  251. win.Add (button);
  252. return (top, win, button);
  253. }
  254. }
  255. [Fact]
  256. public void PosPercent_Equal ()
  257. {
  258. int n1 = 0;
  259. int n2 = 0;
  260. Pos pos1 = Pos.Percent (n1);
  261. Pos pos2 = Pos.Percent (n2);
  262. Assert.Equal (pos1, pos2);
  263. n1 = n2 = 1;
  264. pos1 = Pos.Percent (n1);
  265. pos2 = Pos.Percent (n2);
  266. Assert.Equal (pos1, pos2);
  267. n1 = n2 = 50;
  268. pos1 = Pos.Percent (n1);
  269. pos2 = Pos.Percent (n2);
  270. Assert.Equal (pos1, pos2);
  271. n1 = n2 = 100;
  272. pos1 = Pos.Percent (n1);
  273. pos2 = Pos.Percent (n2);
  274. Assert.Equal (pos1, pos2);
  275. n1 = 0;
  276. n2 = 1;
  277. pos1 = Pos.Percent (n1);
  278. pos2 = Pos.Percent (n2);
  279. Assert.NotEqual (pos1, pos2);
  280. n1 = 50;
  281. n2 = 150;
  282. pos1 = Pos.Percent (n1);
  283. pos2 = Pos.Percent (n2);
  284. Assert.NotEqual (pos1, pos2);
  285. }
  286. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  287. // TODO: A new test that calls SetRelativeLayout directly is needed.
  288. [Fact]
  289. [TestRespondersDisposed]
  290. public void Pos_Add_Operator ()
  291. {
  292. Application.Init (new FakeDriver ());
  293. Toplevel top = new ();
  294. var view = new View { X = 0, Y = 0, Width = 20, Height = 20 };
  295. var field = new TextField { X = 0, Y = 0, Width = 20 };
  296. var count = 0;
  297. field.KeyDown += (s, k) =>
  298. {
  299. if (k.KeyCode == KeyCode.Enter)
  300. {
  301. field.Text = $"View {count}";
  302. var view2 = new View { X = 0, Y = field.Y, Width = 20, Text = field.Text };
  303. view.Add (view2);
  304. Assert.Equal ($"View {count}", view2.Text);
  305. Assert.Equal ($"Absolute({count})", view2.Y.ToString ());
  306. Assert.Equal ($"Absolute({count})", field.Y.ToString ());
  307. field.Y += 1;
  308. count++;
  309. Assert.Equal ($"Absolute({count})", field.Y.ToString ());
  310. }
  311. };
  312. Application.Iteration += (s, a) =>
  313. {
  314. while (count < 20)
  315. {
  316. field.NewKeyDownEvent (Key.Enter);
  317. }
  318. Application.RequestStop ();
  319. };
  320. var win = new Window ();
  321. win.Add (view);
  322. win.Add (field);
  323. top.Add (win);
  324. Application.Run (top);
  325. Assert.Equal (20, count);
  326. top.Dispose ();
  327. // Shutdown must be called to safely clean up Application if Init has been called
  328. Application.Shutdown ();
  329. }
  330. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  331. // TODO: A new test that calls SetRelativeLayout directly is needed.
  332. [Fact]
  333. [TestRespondersDisposed]
  334. public void Pos_Subtract_Operator ()
  335. {
  336. Application.Init (new FakeDriver ());
  337. Toplevel top = new ();
  338. var view = new View { X = 0, Y = 0, Width = 20, Height = 20 };
  339. var field = new TextField { X = 0, Y = 0, Width = 20 };
  340. var count = 20;
  341. List<View> listViews = new ();
  342. for (var i = 0; i < count; i++)
  343. {
  344. field.Text = $"View {i}";
  345. var view2 = new View { X = 0, Y = field.Y, Width = 20, Text = field.Text };
  346. view.Add (view2);
  347. Assert.Equal ($"View {i}", view2.Text);
  348. Assert.Equal ($"Absolute({i})", field.Y.ToString ());
  349. listViews.Add (view2);
  350. Assert.Equal ($"Absolute({i})", field.Y.ToString ());
  351. field.Y += 1;
  352. Assert.Equal ($"Absolute({i + 1})", field.Y.ToString ());
  353. }
  354. field.KeyDown += (s, k) =>
  355. {
  356. if (k.KeyCode == KeyCode.Enter)
  357. {
  358. Assert.Equal ($"View {count - 1}", listViews [count - 1].Text);
  359. view.Remove (listViews [count - 1]);
  360. listViews [count - 1].Dispose ();
  361. Assert.Equal ($"Absolute({count})", field.Y.ToString ());
  362. field.Y -= 1;
  363. count--;
  364. Assert.Equal ($"Absolute({count})", field.Y.ToString ());
  365. }
  366. };
  367. Application.Iteration += (s, a) =>
  368. {
  369. while (count > 0)
  370. {
  371. field.NewKeyDownEvent (Key.Enter);
  372. }
  373. Application.RequestStop ();
  374. };
  375. var win = new Window ();
  376. win.Add (view);
  377. win.Add (field);
  378. top.Add (win);
  379. Application.Run (top);
  380. Assert.Equal (0, count);
  381. top.Dispose ();
  382. // Shutdown must be called to safely clean up Application if Init has been called
  383. Application.Shutdown ();
  384. }
  385. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  386. // TODO: A new test that calls SetRelativeLayout directly is needed.
  387. [Fact]
  388. public void Pos_Validation_Do_Not_Throws_If_NewValue_Is_PosAbsolute_And_OldValue_Is_Null ()
  389. {
  390. Application.Init (new FakeDriver ());
  391. Toplevel t = new ();
  392. var w = new Window { X = 1, Y = 2, Width = 3, Height = 5 };
  393. t.Add (w);
  394. t.Ready += (s, e) =>
  395. {
  396. Assert.Equal (2, w.X = 2);
  397. Assert.Equal (2, w.Y = 2);
  398. };
  399. Application.Iteration += (s, a) => Application.RequestStop ();
  400. Application.Run (t);
  401. t.Dispose ();
  402. Application.Shutdown ();
  403. }
  404. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  405. // TODO: A new test that calls SetRelativeLayout directly is needed.
  406. [Fact]
  407. public void Validation_Does_Not_Throw_If_NewValue_Is_PosAbsolute_And_OldValue_Is_Null ()
  408. {
  409. Application.Init (new FakeDriver ());
  410. Toplevel t = new Toplevel ();
  411. var w = new Window { X = 1, Y = 2, Width = 3, Height = 5 };
  412. t.Add (w);
  413. t.Ready += (s, e) =>
  414. {
  415. Assert.Equal (2, w.X = 2);
  416. Assert.Equal (2, w.Y = 2);
  417. };
  418. Application.Iteration += (s, a) => Application.RequestStop ();
  419. Application.Run (t);
  420. t.Dispose ();
  421. Application.Shutdown ();
  422. }
  423. }