ButtonTests.cs 12 KB

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