ButtonTests.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. using System;
  2. using System.Windows.Input;
  3. using Avalonia.Data;
  4. using Avalonia.Input;
  5. using Avalonia.Media;
  6. using Avalonia.Platform;
  7. using Avalonia.Rendering;
  8. using Avalonia.UnitTests;
  9. using Avalonia.VisualTree;
  10. using Moq;
  11. using Xunit;
  12. namespace Avalonia.Controls.UnitTests
  13. {
  14. public class ButtonTests
  15. {
  16. private MouseTestHelper _helper = new MouseTestHelper();
  17. [Fact]
  18. public void Button_Is_Disabled_When_Command_Is_Disabled()
  19. {
  20. var command = new TestCommand(false);
  21. var target = new Button
  22. {
  23. Command = command,
  24. };
  25. var root = new TestRoot { Child = target };
  26. Assert.False(target.IsEnabled);
  27. command.IsEnabled = true;
  28. Assert.True(target.IsEnabled);
  29. command.IsEnabled = false;
  30. Assert.False(target.IsEnabled);
  31. }
  32. [Fact]
  33. public void Button_Is_Disabled_When_Bound_Command_Doesnt_Exist()
  34. {
  35. var target = new Button
  36. {
  37. [!Button.CommandProperty] = new Binding("Command"),
  38. };
  39. Assert.False(target.IsEnabled);
  40. }
  41. [Fact]
  42. public void Button_Is_Disabled_When_Bound_Command_Is_Removed()
  43. {
  44. var viewModel = new
  45. {
  46. Command = new TestCommand(true),
  47. };
  48. var target = new Button
  49. {
  50. DataContext = viewModel,
  51. [!Button.CommandProperty] = new Binding("Command"),
  52. };
  53. Assert.True(target.IsEnabled);
  54. target.DataContext = null;
  55. Assert.False(target.IsEnabled);
  56. }
  57. [Fact]
  58. public void Button_Is_Enabled_When_Bound_Command_Is_Added()
  59. {
  60. var viewModel = new
  61. {
  62. Command = new TestCommand(true),
  63. };
  64. var target = new Button
  65. {
  66. DataContext = new object(),
  67. [!Button.CommandProperty] = new Binding("Command"),
  68. };
  69. Assert.False(target.IsEnabled);
  70. target.DataContext = viewModel;
  71. Assert.True(target.IsEnabled);
  72. }
  73. [Fact]
  74. public void Button_Is_Disabled_When_Disabled_Bound_Command_Is_Added()
  75. {
  76. var viewModel = new
  77. {
  78. Command = new TestCommand(false),
  79. };
  80. var target = new Button
  81. {
  82. DataContext = new object(),
  83. [!Button.CommandProperty] = new Binding("Command"),
  84. };
  85. Assert.False(target.IsEnabled);
  86. target.DataContext = viewModel;
  87. Assert.False(target.IsEnabled);
  88. }
  89. [Fact]
  90. public void Button_Raises_Click()
  91. {
  92. var renderer = Mock.Of<IRenderer>();
  93. var pt = new Point(50, 50);
  94. Mock.Get(renderer).Setup(r => r.HitTest(It.IsAny<Point>(), It.IsAny<IVisual>(), It.IsAny<Func<IVisual, bool>>()))
  95. .Returns<Point, IVisual, Func<IVisual, bool>>((p, r, f) =>
  96. r.Bounds.Contains(p) ? new IVisual[] { r } : new IVisual[0]);
  97. var target = new TestButton()
  98. {
  99. Bounds = new Rect(0, 0, 100, 100),
  100. Renderer = renderer
  101. };
  102. bool clicked = false;
  103. target.Click += (s, e) => clicked = true;
  104. RaisePointerEnter(target);
  105. RaisePointerMove(target, pt);
  106. RaisePointerPressed(target, 1, MouseButton.Left, pt);
  107. Assert.Equal(_helper.Captured, target);
  108. RaisePointerReleased(target, MouseButton.Left, pt);
  109. Assert.Equal(_helper.Captured, null);
  110. Assert.True(clicked);
  111. }
  112. [Fact]
  113. public void Button_Does_Not_Raise_Click_When_PointerReleased_Outside()
  114. {
  115. var renderer = Mock.Of<IRenderer>();
  116. Mock.Get(renderer).Setup(r => r.HitTest(It.IsAny<Point>(), It.IsAny<IVisual>(), It.IsAny<Func<IVisual, bool>>()))
  117. .Returns<Point, IVisual, Func<IVisual, bool>>((p, r, f) =>
  118. r.Bounds.Contains(p) ? new IVisual[] { r } : new IVisual[0]);
  119. var target = new TestButton()
  120. {
  121. Bounds = new Rect(0, 0, 100, 100),
  122. Renderer = renderer
  123. };
  124. bool clicked = false;
  125. target.Click += (s, e) => clicked = true;
  126. RaisePointerEnter(target);
  127. RaisePointerMove(target, new Point(50,50));
  128. RaisePointerPressed(target, 1, MouseButton.Left, new Point(50, 50));
  129. RaisePointerLeave(target);
  130. Assert.Equal(_helper.Captured, target);
  131. RaisePointerReleased(target, MouseButton.Left, new Point(200, 50));
  132. Assert.Equal(_helper.Captured, null);
  133. Assert.False(clicked);
  134. }
  135. [Fact]
  136. public void Button_With_RenderTransform_Raises_Click()
  137. {
  138. var renderer = Mock.Of<IRenderer>();
  139. var pt = new Point(150, 50);
  140. Mock.Get(renderer).Setup(r => r.HitTest(It.IsAny<Point>(), It.IsAny<IVisual>(), It.IsAny<Func<IVisual, bool>>()))
  141. .Returns<Point, IVisual, Func<IVisual, bool>>((p, r, f) =>
  142. r.Bounds.Contains(p.Transform(r.RenderTransform.Value.Invert())) ?
  143. new IVisual[] { r } : new IVisual[0]);
  144. var target = new TestButton()
  145. {
  146. Bounds = new Rect(0, 0, 100, 100),
  147. RenderTransform = new TranslateTransform { X = 100, Y = 0 },
  148. Renderer = renderer
  149. };
  150. //actual bounds of button should be 100,0,100,100 x -> translated 100 pixels
  151. //so mouse with x=150 coordinates should trigger click
  152. //button shouldn't count on bounds to calculate pointer is in the over or not, but
  153. //on avalonia event system, as renderer hit test will properly calculate whether to send
  154. //mouse over events to button based on rendered bounds
  155. //note: button also may have not rectangular shape and only renderer hit testing is reliable
  156. bool clicked = false;
  157. target.Click += (s, e) => clicked = true;
  158. RaisePointerEnter(target);
  159. RaisePointerMove(target, pt);
  160. RaisePointerPressed(target, 1, MouseButton.Left, pt);
  161. Assert.Equal(_helper.Captured, target);
  162. RaisePointerReleased(target, MouseButton.Left, pt);
  163. Assert.Equal(_helper.Captured, null);
  164. Assert.True(clicked);
  165. }
  166. [Fact]
  167. public void Button_Does_Not_Subscribe_To_Command_CanExecuteChanged_Until_Added_To_Logical_Tree()
  168. {
  169. var command = new TestCommand(true);
  170. var target = new Button
  171. {
  172. Command = command,
  173. };
  174. Assert.Equal(0, command.SubscriptionCount);
  175. }
  176. [Fact]
  177. public void Button_Subscribes_To_Command_CanExecuteChanged_When_Added_To_Logical_Tree()
  178. {
  179. var command = new TestCommand(true);
  180. var target = new Button { Command = command };
  181. var root = new TestRoot { Child = target };
  182. Assert.Equal(1, command.SubscriptionCount);
  183. }
  184. [Fact]
  185. public void Button_Unsubscribes_From_Command_CanExecuteChanged_When_Removed_From_Logical_Tree()
  186. {
  187. var command = new TestCommand(true);
  188. var target = new Button { Command = command };
  189. var root = new TestRoot { Child = target };
  190. root.Child = null;
  191. Assert.Equal(0, command.SubscriptionCount);
  192. }
  193. private class TestButton : Button, IRenderRoot
  194. {
  195. public TestButton()
  196. {
  197. IsVisible = true;
  198. }
  199. public new Rect Bounds
  200. {
  201. get => base.Bounds;
  202. set => base.Bounds = value;
  203. }
  204. public Size ClientSize => throw new NotImplementedException();
  205. public IRenderer Renderer { get; set; }
  206. public double RenderScaling => throw new NotImplementedException();
  207. public IRenderTarget CreateRenderTarget() => throw new NotImplementedException();
  208. public void Invalidate(Rect rect) => throw new NotImplementedException();
  209. public Point PointToClient(PixelPoint p) => throw new NotImplementedException();
  210. public PixelPoint PointToScreen(Point p) => throw new NotImplementedException();
  211. }
  212. private void RaisePointerPressed(Button button, int clickCount, MouseButton mouseButton, Point position)
  213. {
  214. _helper.Down(button, mouseButton, position, clickCount: clickCount);
  215. }
  216. private void RaisePointerReleased(Button button, MouseButton mouseButton, Point pt)
  217. {
  218. _helper.Up(button, mouseButton, pt);
  219. }
  220. private void RaisePointerEnter(Button button)
  221. {
  222. _helper.Enter(button);
  223. }
  224. private void RaisePointerLeave(Button button)
  225. {
  226. _helper.Leave(button);
  227. }
  228. private void RaisePointerMove(Button button, Point pos)
  229. {
  230. _helper.Move(button, pos);
  231. }
  232. private class TestCommand : ICommand
  233. {
  234. private EventHandler _canExecuteChanged;
  235. private bool _enabled;
  236. public TestCommand(bool enabled)
  237. {
  238. _enabled = enabled;
  239. }
  240. public bool IsEnabled
  241. {
  242. get { return _enabled; }
  243. set
  244. {
  245. if (_enabled != value)
  246. {
  247. _enabled = value;
  248. _canExecuteChanged?.Invoke(this, EventArgs.Empty);
  249. }
  250. }
  251. }
  252. public int SubscriptionCount { get; private set; }
  253. public event EventHandler CanExecuteChanged
  254. {
  255. add { _canExecuteChanged += value; ++SubscriptionCount; }
  256. remove { _canExecuteChanged -= value; --SubscriptionCount; }
  257. }
  258. public bool CanExecute(object parameter) => _enabled;
  259. public void Execute(object parameter)
  260. {
  261. }
  262. }
  263. }
  264. }