Pos.Tests.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. using UnitTests;
  2. using Xunit.Abstractions;
  3. using static Terminal.Gui.ViewBase.Dim;
  4. using static Terminal.Gui.ViewBase.Pos;
  5. namespace UnitTests.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 ("fake");
  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.StopAfterFirstIteration = true;
  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 ("fake");
  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. [Fact]
  54. [SetupFakeApplication]
  55. public void Pos_Add_Operator ()
  56. {
  57. Toplevel top = new ();
  58. var view = new View { X = 0, Y = 0, Width = 20, Height = 20 };
  59. var field = new TextField { X = 0, Y = 0, Width = 20 };
  60. var count = 0;
  61. field.KeyDown += (s, k) =>
  62. {
  63. if (k.KeyCode == KeyCode.Enter)
  64. {
  65. field.Text = $"View {count}";
  66. var view2 = new View { X = 0, Y = field.Y, Width = 20, Text = field.Text };
  67. view.Add (view2);
  68. Assert.Equal ($"View {count}", view2.Text);
  69. Assert.Equal ($"Absolute({count})", view2.Y.ToString ());
  70. Assert.Equal ($"Absolute({count})", field.Y.ToString ());
  71. field.Y += 1;
  72. count++;
  73. Assert.Equal ($"Absolute({count})", field.Y.ToString ());
  74. }
  75. };
  76. Application.Iteration += OnInstanceOnIteration;
  77. var win = new Window ();
  78. win.Add (view);
  79. win.Add (field);
  80. top.Add (win);
  81. Application.Run (top);
  82. Application.Iteration -= OnInstanceOnIteration;
  83. Assert.Equal (20, count);
  84. top.Dispose ();
  85. // Shutdown must be called to safely clean up Application if Init has been called
  86. Application.Shutdown ();
  87. return;
  88. void OnInstanceOnIteration (object s, IterationEventArgs a)
  89. {
  90. while (count < 20)
  91. {
  92. field.NewKeyDownEvent (Key.Enter);
  93. }
  94. Application.RequestStop ();
  95. }
  96. }
  97. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  98. // TODO: A new test that calls SetRelativeLayout directly is needed.
  99. [Fact]
  100. [TestRespondersDisposed]
  101. public void Pos_Subtract_Operator ()
  102. {
  103. Application.Init ("fake");
  104. Toplevel top = new ();
  105. var view = new View { X = 0, Y = 0, Width = 20, Height = 20 };
  106. var field = new TextField { X = 0, Y = 0, Width = 20 };
  107. var count = 20;
  108. List<View> listViews = new ();
  109. for (var i = 0; i < count; i++)
  110. {
  111. field.Text = $"View {i}";
  112. var view2 = new View { X = 0, Y = field.Y, Width = 20, Text = field.Text };
  113. view.Add (view2);
  114. Assert.Equal ($"View {i}", view2.Text);
  115. Assert.Equal ($"Absolute({i})", field.Y.ToString ());
  116. listViews.Add (view2);
  117. Assert.Equal ($"Absolute({i})", field.Y.ToString ());
  118. field.Y += 1;
  119. Assert.Equal ($"Absolute({i + 1})", field.Y.ToString ());
  120. }
  121. field.KeyDown += (s, k) =>
  122. {
  123. if (k.KeyCode == KeyCode.Enter)
  124. {
  125. Assert.Equal ($"View {count - 1}", listViews [count - 1].Text);
  126. view.Remove (listViews [count - 1]);
  127. listViews [count - 1].Dispose ();
  128. Assert.Equal ($"Absolute({count})", field.Y.ToString ());
  129. field.Y -= 1;
  130. count--;
  131. Assert.Equal ($"Absolute({count})", field.Y.ToString ());
  132. }
  133. };
  134. Application.Iteration += OnApplicationOnIteration;
  135. var win = new Window ();
  136. win.Add (view);
  137. win.Add (field);
  138. top.Add (win);
  139. Application.Run (top);
  140. Application.Iteration -= OnApplicationOnIteration;
  141. Assert.Equal (0, count);
  142. top.Dispose ();
  143. // Shutdown must be called to safely clean up Application if Init has been called
  144. Application.Shutdown ();
  145. return;
  146. void OnApplicationOnIteration (object s, IterationEventArgs a)
  147. {
  148. while (count > 0)
  149. {
  150. field.NewKeyDownEvent (Key.Enter);
  151. }
  152. Application.RequestStop ();
  153. }
  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. [Fact]
  158. public void Pos_Validation_Do_Not_Throws_If_NewValue_Is_PosAbsolute_And_OldValue_Is_Null ()
  159. {
  160. Application.Init ("fake");
  161. Toplevel t = new ();
  162. var w = new Window { X = 1, Y = 2, Width = 3, Height = 5 };
  163. t.Add (w);
  164. t.Ready += (s, e) =>
  165. {
  166. Assert.Equal (2, w.X = 2);
  167. Assert.Equal (2, w.Y = 2);
  168. };
  169. Application.StopAfterFirstIteration = true;
  170. Application.Run (t);
  171. t.Dispose ();
  172. Application.Shutdown ();
  173. }
  174. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  175. // TODO: A new test that calls SetRelativeLayout directly is needed.
  176. [Fact]
  177. public void Validation_Does_Not_Throw_If_NewValue_Is_PosAbsolute_And_OldValue_Is_Null ()
  178. {
  179. Application.Init ("fake");
  180. Toplevel t = new Toplevel ();
  181. var w = new Window { X = 1, Y = 2, Width = 3, Height = 5 };
  182. t.Add (w);
  183. t.Ready += (s, e) =>
  184. {
  185. Assert.Equal (2, w.X = 2);
  186. Assert.Equal (2, w.Y = 2);
  187. };
  188. Application.StopAfterFirstIteration = true;
  189. Application.Run (t);
  190. t.Dispose ();
  191. Application.Shutdown ();
  192. }
  193. }