ButtonTests.cs 11 KB

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