Pos.Tests.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. #nullable enable
  2. namespace UnitTests.LayoutTests;
  3. public class PosTests ()
  4. {
  5. [Fact]
  6. public void
  7. Pos_Validation_Do_Not_Throws_If_NewValue_Is_PosAbsolute_And_OldValue_Is_Another_Type_After_Sets_To_LayoutStyle_Absolute ()
  8. {
  9. Application.Init ("fake");
  10. Toplevel t = new ();
  11. var w = new Window { X = Pos.Left (t) + 2, Y = Pos.Absolute (2) };
  12. var v = new View { X = Pos.Center (), Y = Pos.Percent (10) };
  13. w.Add (v);
  14. t.Add (w);
  15. t.Ready += (s, e) =>
  16. {
  17. v.Frame = new Rectangle (2, 2, 10, 10);
  18. Assert.Equal (2, v.X = 2);
  19. Assert.Equal (2, v.Y = 2);
  20. };
  21. Application.StopAfterFirstIteration = true;
  22. Application.Run (t);
  23. t.Dispose ();
  24. Application.Shutdown ();
  25. }
  26. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  27. // TODO: A new test that calls SetRelativeLayout directly is needed.
  28. [Fact]
  29. [TestRespondersDisposed]
  30. public void PosCombine_WHY_Throws ()
  31. {
  32. Application.Init ("fake");
  33. Toplevel t = new Toplevel ();
  34. var w = new Window { X = Pos.Left (t) + 2, Y = Pos.Top (t) + 2 };
  35. var f = new FrameView ();
  36. var v1 = new View { X = Pos.Left (w) + 2, Y = Pos.Top (w) + 2 };
  37. var v2 = new View { X = Pos.Left (v1) + 2, Y = Pos.Top (v1) + 2 };
  38. f.Add (v1); // v2 not added
  39. w.Add (f);
  40. t.Add (w);
  41. f.X = Pos.X (v2) - Pos.X (v1);
  42. f.Y = Pos.Y (v2) - Pos.Y (v1);
  43. Assert.Throws<LayoutException> (() => Application.Run (t));
  44. t.Dispose ();
  45. Application.Shutdown ();
  46. v2.Dispose ();
  47. }
  48. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  49. // TODO: A new test that calls SetRelativeLayout directly is needed.
  50. [Fact]
  51. [SetupFakeApplication]
  52. public void Pos_Add_Operator ()
  53. {
  54. Toplevel top = new ();
  55. var view = new View { X = 0, Y = 0, Width = 20, Height = 20 };
  56. var field = new TextField { X = 0, Y = 0, Width = 20 };
  57. var count = 0;
  58. field.KeyDown += (s, k) =>
  59. {
  60. if (k.KeyCode == KeyCode.Enter)
  61. {
  62. field.Text = $"View {count}";
  63. var view2 = new View { X = 0, Y = field.Y, Width = 20, Text = field.Text };
  64. view.Add (view2);
  65. Assert.Equal ($"View {count}", view2.Text);
  66. Assert.Equal ($"Absolute({count})", view2.Y.ToString ());
  67. Assert.Equal ($"Absolute({count})", field.Y.ToString ());
  68. field.Y += 1;
  69. count++;
  70. Assert.Equal ($"Absolute({count})", field.Y.ToString ());
  71. }
  72. };
  73. Application.Iteration += OnInstanceOnIteration;
  74. var win = new Window ();
  75. win.Add (view);
  76. win.Add (field);
  77. top.Add (win);
  78. Application.Run (top);
  79. Application.Iteration -= OnInstanceOnIteration;
  80. Assert.Equal (20, count);
  81. top.Dispose ();
  82. // Shutdown must be called to safely clean up Application if Init has been called
  83. Application.Shutdown ();
  84. return;
  85. void OnInstanceOnIteration (object? s, EventArgs<IApplication?> a)
  86. {
  87. while (count < 20)
  88. {
  89. field.NewKeyDownEvent (Key.Enter);
  90. }
  91. Application.RequestStop ();
  92. }
  93. }
  94. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  95. // TODO: A new test that calls SetRelativeLayout directly is needed.
  96. [Fact]
  97. [TestRespondersDisposed]
  98. public void Pos_Subtract_Operator ()
  99. {
  100. Application.Init ("fake");
  101. Toplevel top = new ();
  102. var view = new View { X = 0, Y = 0, Width = 20, Height = 20 };
  103. var field = new TextField { X = 0, Y = 0, Width = 20 };
  104. var count = 20;
  105. List<View> listViews = new ();
  106. for (var i = 0; i < count; i++)
  107. {
  108. field.Text = $"View {i}";
  109. var view2 = new View { X = 0, Y = field.Y, Width = 20, Text = field.Text };
  110. view.Add (view2);
  111. Assert.Equal ($"View {i}", view2.Text);
  112. Assert.Equal ($"Absolute({i})", field.Y.ToString ());
  113. listViews.Add (view2);
  114. Assert.Equal ($"Absolute({i})", field.Y.ToString ());
  115. field.Y += 1;
  116. Assert.Equal ($"Absolute({i + 1})", field.Y.ToString ());
  117. }
  118. field.KeyDown += (s, k) =>
  119. {
  120. if (k.KeyCode == KeyCode.Enter)
  121. {
  122. Assert.Equal ($"View {count - 1}", listViews [count - 1].Text);
  123. view.Remove (listViews [count - 1]);
  124. listViews [count - 1].Dispose ();
  125. Assert.Equal ($"Absolute({count})", field.Y.ToString ());
  126. field.Y -= 1;
  127. count--;
  128. Assert.Equal ($"Absolute({count})", field.Y.ToString ());
  129. }
  130. };
  131. Application.Iteration += OnApplicationOnIteration;
  132. var win = new Window ();
  133. win.Add (view);
  134. win.Add (field);
  135. top.Add (win);
  136. Application.Run (top);
  137. Application.Iteration -= OnApplicationOnIteration;
  138. Assert.Equal (0, count);
  139. top.Dispose ();
  140. // Shutdown must be called to safely clean up Application if Init has been called
  141. Application.Shutdown ();
  142. return;
  143. void OnApplicationOnIteration (object? s, EventArgs<IApplication?> a)
  144. {
  145. while (count > 0)
  146. {
  147. field.NewKeyDownEvent (Key.Enter);
  148. }
  149. Application.RequestStop ();
  150. }
  151. }
  152. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  153. // TODO: A new test that calls SetRelativeLayout directly is needed.
  154. [Fact]
  155. public void Pos_Validation_Do_Not_Throws_If_NewValue_Is_PosAbsolute_And_OldValue_Is_Null ()
  156. {
  157. Application.Init ("fake");
  158. Toplevel t = new ();
  159. var w = new Window { X = 1, Y = 2, Width = 3, Height = 5 };
  160. t.Add (w);
  161. t.Ready += (s, e) =>
  162. {
  163. Assert.Equal (2, w.X = 2);
  164. Assert.Equal (2, w.Y = 2);
  165. };
  166. Application.StopAfterFirstIteration = true;
  167. Application.Run (t);
  168. t.Dispose ();
  169. Application.Shutdown ();
  170. }
  171. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  172. // TODO: A new test that calls SetRelativeLayout directly is needed.
  173. [Fact]
  174. public void Validation_Does_Not_Throw_If_NewValue_Is_PosAbsolute_And_OldValue_Is_Null ()
  175. {
  176. Application.Init ("fake");
  177. Toplevel t = new Toplevel ();
  178. var w = new Window { X = 1, Y = 2, Width = 3, Height = 5 };
  179. t.Add (w);
  180. t.Ready += (s, e) =>
  181. {
  182. Assert.Equal (2, w.X = 2);
  183. Assert.Equal (2, w.Y = 2);
  184. };
  185. Application.StopAfterFirstIteration = true;
  186. Application.Run (t);
  187. t.Dispose ();
  188. Application.Shutdown ();
  189. }
  190. }