Pos.Tests.cs 7.5 KB

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