ButtonTests.cs 11 KB

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