ButtonTests.cs 12 KB

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