ViewLayoutEventTests.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. #nullable enable
  2. using UnitTests.Parallelizable;
  3. namespace UnitTests_Parallelizable.ViewLayoutEventTests;
  4. public class ViewLayoutEventTests
  5. {
  6. [Fact]
  7. public void View_WidthChanging_Event_Fires ()
  8. {
  9. var view = new View ();
  10. bool eventFired = false;
  11. Dim? oldValue = null;
  12. Dim? newValue = null;
  13. view.WidthChanging += (sender, args) =>
  14. {
  15. eventFired = true;
  16. oldValue = args.CurrentValue;
  17. newValue = args.NewValue;
  18. };
  19. view.Width = 10;
  20. Assert.True (eventFired);
  21. Assert.NotNull (oldValue);
  22. Assert.NotNull (newValue);
  23. }
  24. [Fact]
  25. public void View_WidthChanged_Event_Fires ()
  26. {
  27. var view = new View ();
  28. bool eventFired = false;
  29. Dim? oldValue = null;
  30. Dim? newValue = null;
  31. view.WidthChanged += (sender, args) =>
  32. {
  33. eventFired = true;
  34. oldValue = args.OldValue;
  35. newValue = args.NewValue;
  36. };
  37. view.Width = 10;
  38. Assert.True (eventFired);
  39. Assert.NotNull (oldValue);
  40. Assert.NotNull (newValue);
  41. }
  42. [Fact]
  43. public void View_WidthChanging_CanCancel ()
  44. {
  45. var view = new View ();
  46. Dim? originalWidth = view.Width;
  47. view.WidthChanging += (sender, args) =>
  48. {
  49. args.Handled = true; // Cancel the change
  50. };
  51. view.Width = 10;
  52. // Width should not have changed
  53. Assert.Equal (originalWidth, view.Width);
  54. }
  55. [Fact]
  56. public void View_WidthChanging_CanModify ()
  57. {
  58. var view = new View ();
  59. view.WidthChanging += (sender, args) =>
  60. {
  61. // Modify the proposed value
  62. args.NewValue = 20;
  63. };
  64. view.Width = 10;
  65. // Width should be 20 (the modified value), not 10
  66. var container = new View { Width = 50, Height = 20 };
  67. container.Add (view);
  68. container.Layout ();
  69. Assert.Equal (20, view.Frame.Width);
  70. }
  71. [Fact]
  72. public void View_HeightChanging_Event_Fires ()
  73. {
  74. var view = new View ();
  75. bool eventFired = false;
  76. Dim? oldValue = null;
  77. Dim? newValue = null;
  78. view.HeightChanging += (sender, args) =>
  79. {
  80. eventFired = true;
  81. oldValue = args.CurrentValue;
  82. newValue = args.NewValue;
  83. };
  84. view.Height = 10;
  85. Assert.True (eventFired);
  86. Assert.NotNull (oldValue);
  87. Assert.NotNull (newValue);
  88. }
  89. [Fact]
  90. public void View_HeightChanged_Event_Fires ()
  91. {
  92. var view = new View ();
  93. bool eventFired = false;
  94. Dim? oldValue = null;
  95. Dim? newValue = null;
  96. view.HeightChanged += (sender, args) =>
  97. {
  98. eventFired = true;
  99. oldValue = args.OldValue;
  100. newValue = args.NewValue;
  101. };
  102. view.Height = 10;
  103. Assert.True (eventFired);
  104. Assert.NotNull (oldValue);
  105. Assert.NotNull (newValue);
  106. }
  107. [Fact]
  108. public void View_HeightChanging_CanCancel ()
  109. {
  110. var view = new View ();
  111. Dim? originalHeight = view.Height;
  112. view.HeightChanging += (sender, args) =>
  113. {
  114. args.Handled = true; // Cancel the change
  115. };
  116. view.Height = 10;
  117. // Height should not have changed
  118. Assert.Equal (originalHeight, view.Height);
  119. }
  120. [Fact]
  121. public void View_HeightChanging_CanModify ()
  122. {
  123. var view = new View ();
  124. view.HeightChanging += (sender, args) =>
  125. {
  126. // Modify the proposed value
  127. args.NewValue = 20;
  128. };
  129. view.Height = 10;
  130. // Height should be 20 (the modified value), not 10
  131. var container = new View { Width = 50, Height = 40 };
  132. container.Add (view);
  133. container.Layout ();
  134. Assert.Equal (20, view.Frame.Height);
  135. }
  136. [Fact]
  137. public void View_OnWidthChanging_CanCancel ()
  138. {
  139. var testView = new TestView ();
  140. testView.CancelWidthChange = true;
  141. Dim? originalWidth = testView.Width;
  142. testView.Width = 10;
  143. // Width should not have changed
  144. Assert.Equal (originalWidth, testView.Width);
  145. }
  146. [Fact]
  147. public void View_OnHeightChanging_CanCancel ()
  148. {
  149. var testView = new TestView ();
  150. testView.CancelHeightChange = true;
  151. Dim originalHeight = testView.Height;
  152. testView.Height = 10;
  153. // Height should not have changed
  154. Assert.Equal (originalHeight, testView.Height);
  155. }
  156. [Fact]
  157. public void View_WidthChanged_BackingFieldSetBeforeEvent ()
  158. {
  159. var view = new View ();
  160. Dim? widthInChangedEvent = null;
  161. view.WidthChanged += (sender, args) =>
  162. {
  163. // The backing field should already be set when Changed event fires
  164. widthInChangedEvent = view.Width;
  165. };
  166. view.Width = 25;
  167. // The width seen in the Changed event should be the new value
  168. var container = new View { Width = 50, Height = 20 };
  169. container.Add (view);
  170. container.Layout ();
  171. Assert.Equal (25, view.Frame.Width);
  172. }
  173. [Fact]
  174. public void View_HeightChanged_BackingFieldSetBeforeEvent ()
  175. {
  176. var view = new View ();
  177. Dim? heightInChangedEvent = null;
  178. view.HeightChanged += (sender, args) =>
  179. {
  180. // The backing field should already be set when Changed event fires
  181. heightInChangedEvent = view.Height;
  182. };
  183. view.Height = 30;
  184. // The height seen in the Changed event should be the new value
  185. var container = new View { Width = 50, Height = 40 };
  186. container.Add (view);
  187. container.Layout ();
  188. Assert.Equal (30, view.Frame.Height);
  189. }
  190. private class TestView : View
  191. {
  192. public bool CancelWidthChange { get; set; }
  193. public bool CancelHeightChange { get; set; }
  194. protected override bool OnWidthChanging (ValueChangingEventArgs<Dim> args)
  195. {
  196. return CancelWidthChange;
  197. }
  198. protected override bool OnHeightChanging (ValueChangingEventArgs<Dim> args)
  199. {
  200. return CancelHeightChange;
  201. }
  202. }
  203. }