Pos.Tests.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. using UnitTests;
  2. using Xunit.Abstractions;
  3. using static Terminal.Gui.Dim;
  4. using static Terminal.Gui.Pos;
  5. namespace Terminal.Gui.LayoutTests;
  6. public class PosTests ()
  7. {
  8. [Fact]
  9. public void
  10. Pos_Validation_Do_Not_Throws_If_NewValue_Is_PosAbsolute_And_OldValue_Is_Another_Type_After_Sets_To_LayoutStyle_Absolute ()
  11. {
  12. Application.Init (new FakeDriver ());
  13. Toplevel t = new ();
  14. var w = new Window { X = Pos.Left (t) + 2, Y = Pos.Absolute (2) };
  15. var v = new View { X = Pos.Center (), Y = Pos.Percent (10) };
  16. w.Add (v);
  17. t.Add (w);
  18. t.Ready += (s, e) =>
  19. {
  20. v.Frame = new Rectangle (2, 2, 10, 10);
  21. Assert.Equal (2, v.X = 2);
  22. Assert.Equal (2, v.Y = 2);
  23. };
  24. Application.Iteration += (s, a) => Application.RequestStop ();
  25. Application.Run (t);
  26. t.Dispose ();
  27. Application.Shutdown ();
  28. }
  29. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  30. // TODO: A new test that calls SetRelativeLayout directly is needed.
  31. [Fact]
  32. [TestRespondersDisposed]
  33. public void PosCombine_WHY_Throws ()
  34. {
  35. Application.Init (new FakeDriver ());
  36. Toplevel t = new Toplevel ();
  37. var w = new Window { X = Pos.Left (t) + 2, Y = Pos.Top (t) + 2 };
  38. var f = new FrameView ();
  39. var v1 = new View { X = Pos.Left (w) + 2, Y = Pos.Top (w) + 2 };
  40. var v2 = new View { X = Pos.Left (v1) + 2, Y = Pos.Top (v1) + 2 };
  41. f.Add (v1); // v2 not added
  42. w.Add (f);
  43. t.Add (w);
  44. f.X = Pos.X (v2) - Pos.X (v1);
  45. f.Y = Pos.Y (v2) - Pos.Y (v1);
  46. Assert.Throws<LayoutException> (() => Application.Run (t));
  47. t.Dispose ();
  48. Application.Shutdown ();
  49. v2.Dispose ();
  50. }
  51. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  52. // TODO: A new test that calls SetRelativeLayout directly is needed.
  53. // See: https://github.com/gui-cs/Terminal.Gui/issues/504
  54. [Fact]
  55. [TestRespondersDisposed]
  56. public void LeftTopBottomRight_Win_ShouldNotThrow ()
  57. {
  58. // Test cases:
  59. (Toplevel top, Window win, Button button) app = Setup ();
  60. app.button.Y = Pos.Left (app.win);
  61. RunState rs = Application.Begin (app.top);
  62. // If Application.RunState is used then we must use Application.RunLoop with the rs parameter
  63. Application.RunLoop (rs);
  64. Cleanup (rs);
  65. app = Setup ();
  66. app.button.Y = Pos.X (app.win);
  67. rs = Application.Begin (app.top);
  68. // If Application.RunState is used then we must use Application.RunLoop with the rs parameter
  69. Application.RunLoop (rs);
  70. Cleanup (rs);
  71. app = Setup ();
  72. app.button.Y = Pos.Top (app.win);
  73. rs = Application.Begin (app.top);
  74. // If Application.RunState is used then we must use Application.RunLoop with the rs parameter
  75. Application.RunLoop (rs);
  76. Cleanup (rs);
  77. app = Setup ();
  78. app.button.Y = Pos.Y (app.win);
  79. rs = Application.Begin (app.top);
  80. // If Application.RunState is used then we must use Application.RunLoop with the rs parameter
  81. Application.RunLoop (rs);
  82. Cleanup (rs);
  83. app = Setup ();
  84. app.button.Y = Pos.Bottom (app.win);
  85. rs = Application.Begin (app.top);
  86. // If Application.RunState is used then we must use Application.RunLoop with the rs parameter
  87. Application.RunLoop (rs);
  88. Cleanup (rs);
  89. app = Setup ();
  90. app.button.Y = Pos.Right (app.win);
  91. rs = Application.Begin (app.top);
  92. // If Application.RunState is used then we must use Application.RunLoop with the rs parameter
  93. Application.RunLoop (rs);
  94. Cleanup (rs);
  95. return;
  96. void Cleanup (RunState rs)
  97. {
  98. // Cleanup
  99. Application.End (rs);
  100. Application.Top.Dispose ();
  101. // Shutdown must be called to safely clean up Application if Init has been called
  102. Application.Shutdown ();
  103. }
  104. // Setup Fake driver
  105. (Toplevel top, Window win, Button button) Setup ()
  106. {
  107. Application.Init (new FakeDriver ());
  108. Application.Iteration += (s, a) => { Application.RequestStop (); };
  109. var win = new Window { X = 0, Y = 0, Width = Dim.Fill (), Height = Dim.Fill () };
  110. var top = new Toplevel ();
  111. top.Add (win);
  112. var button = new Button { X = Pos.Center (), Text = "button" };
  113. win.Add (button);
  114. return (top, win, button);
  115. }
  116. }
  117. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  118. // TODO: A new test that calls SetRelativeLayout directly is needed.
  119. [Fact]
  120. [TestRespondersDisposed]
  121. public void Pos_Add_Operator ()
  122. {
  123. Application.Init (new FakeDriver ());
  124. Toplevel top = new ();
  125. var view = new View { X = 0, Y = 0, Width = 20, Height = 20 };
  126. var field = new TextField { X = 0, Y = 0, Width = 20 };
  127. var count = 0;
  128. field.KeyDown += (s, k) =>
  129. {
  130. if (k.KeyCode == KeyCode.Enter)
  131. {
  132. field.Text = $"View {count}";
  133. var view2 = new View { X = 0, Y = field.Y, Width = 20, Text = field.Text };
  134. view.Add (view2);
  135. Assert.Equal ($"View {count}", view2.Text);
  136. Assert.Equal ($"Absolute({count})", view2.Y.ToString ());
  137. Assert.Equal ($"Absolute({count})", field.Y.ToString ());
  138. field.Y += 1;
  139. count++;
  140. Assert.Equal ($"Absolute({count})", field.Y.ToString ());
  141. }
  142. };
  143. Application.Iteration += (s, a) =>
  144. {
  145. while (count < 20)
  146. {
  147. field.NewKeyDownEvent (Key.Enter);
  148. }
  149. Application.RequestStop ();
  150. };
  151. var win = new Window ();
  152. win.Add (view);
  153. win.Add (field);
  154. top.Add (win);
  155. Application.Run (top);
  156. Assert.Equal (20, count);
  157. top.Dispose ();
  158. // Shutdown must be called to safely clean up Application if Init has been called
  159. Application.Shutdown ();
  160. }
  161. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  162. // TODO: A new test that calls SetRelativeLayout directly is needed.
  163. [Fact]
  164. [TestRespondersDisposed]
  165. public void Pos_Subtract_Operator ()
  166. {
  167. Application.Init (new FakeDriver ());
  168. Toplevel top = new ();
  169. var view = new View { X = 0, Y = 0, Width = 20, Height = 20 };
  170. var field = new TextField { X = 0, Y = 0, Width = 20 };
  171. var count = 20;
  172. List<View> listViews = new ();
  173. for (var i = 0; i < count; i++)
  174. {
  175. field.Text = $"View {i}";
  176. var view2 = new View { X = 0, Y = field.Y, Width = 20, Text = field.Text };
  177. view.Add (view2);
  178. Assert.Equal ($"View {i}", view2.Text);
  179. Assert.Equal ($"Absolute({i})", field.Y.ToString ());
  180. listViews.Add (view2);
  181. Assert.Equal ($"Absolute({i})", field.Y.ToString ());
  182. field.Y += 1;
  183. Assert.Equal ($"Absolute({i + 1})", field.Y.ToString ());
  184. }
  185. field.KeyDown += (s, k) =>
  186. {
  187. if (k.KeyCode == KeyCode.Enter)
  188. {
  189. Assert.Equal ($"View {count - 1}", listViews [count - 1].Text);
  190. view.Remove (listViews [count - 1]);
  191. listViews [count - 1].Dispose ();
  192. Assert.Equal ($"Absolute({count})", field.Y.ToString ());
  193. field.Y -= 1;
  194. count--;
  195. Assert.Equal ($"Absolute({count})", field.Y.ToString ());
  196. }
  197. };
  198. Application.Iteration += (s, a) =>
  199. {
  200. while (count > 0)
  201. {
  202. field.NewKeyDownEvent (Key.Enter);
  203. }
  204. Application.RequestStop ();
  205. };
  206. var win = new Window ();
  207. win.Add (view);
  208. win.Add (field);
  209. top.Add (win);
  210. Application.Run (top);
  211. Assert.Equal (0, count);
  212. top.Dispose ();
  213. // Shutdown must be called to safely clean up Application if Init has been called
  214. Application.Shutdown ();
  215. }
  216. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  217. // TODO: A new test that calls SetRelativeLayout directly is needed.
  218. [Fact]
  219. public void Pos_Validation_Do_Not_Throws_If_NewValue_Is_PosAbsolute_And_OldValue_Is_Null ()
  220. {
  221. Application.Init (new FakeDriver ());
  222. Toplevel t = new ();
  223. var w = new Window { X = 1, Y = 2, Width = 3, Height = 5 };
  224. t.Add (w);
  225. t.Ready += (s, e) =>
  226. {
  227. Assert.Equal (2, w.X = 2);
  228. Assert.Equal (2, w.Y = 2);
  229. };
  230. Application.Iteration += (s, a) => Application.RequestStop ();
  231. Application.Run (t);
  232. t.Dispose ();
  233. Application.Shutdown ();
  234. }
  235. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  236. // TODO: A new test that calls SetRelativeLayout directly is needed.
  237. [Fact]
  238. public void Validation_Does_Not_Throw_If_NewValue_Is_PosAbsolute_And_OldValue_Is_Null ()
  239. {
  240. Application.Init (new FakeDriver ());
  241. Toplevel t = new Toplevel ();
  242. var w = new Window { X = 1, Y = 2, Width = 3, Height = 5 };
  243. t.Add (w);
  244. t.Ready += (s, e) =>
  245. {
  246. Assert.Equal (2, w.X = 2);
  247. Assert.Equal (2, w.Y = 2);
  248. };
  249. Application.Iteration += (s, a) => Application.RequestStop ();
  250. Application.Run (t);
  251. t.Dispose ();
  252. Application.Shutdown ();
  253. }
  254. }