ButtonTests.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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 = RendererMocks.CreateRenderer();
  114. var pt = new Point(50, 50);
  115. renderer.Setup(r => r.HitTest(It.IsAny<Point>(), It.IsAny<Visual>(), It.IsAny<Func<Visual, bool>>()))
  116. .Returns<Point, Visual, Func<Visual, bool>>((p, r, f) =>
  117. r.Bounds.Contains(p) ? new Visual[] { r } : new Visual[0]);
  118. var target = new TestButton(renderer.Object)
  119. {
  120. Bounds = new Rect(0, 0, 100, 100)
  121. };
  122. bool clicked = false;
  123. target.Click += (s, e) => clicked = true;
  124. RaisePointerEntered(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 = RendererMocks.CreateRenderer();
  136. renderer.Setup(r => r.HitTest(It.IsAny<Point>(), It.IsAny<Visual>(), It.IsAny<Func<Visual, bool>>()))
  137. .Returns<Point, Visual, Func<Visual, bool>>((p, r, f) =>
  138. r.Bounds.Contains(p) ? new Visual[] { r } : new Visual[0]);
  139. var target = new TestButton(renderer.Object)
  140. {
  141. Bounds = new Rect(0, 0, 100, 100)
  142. };
  143. bool clicked = false;
  144. target.Click += (s, e) => clicked = true;
  145. RaisePointerEntered(target);
  146. RaisePointerMove(target, new Point(50,50));
  147. RaisePointerPressed(target, 1, MouseButton.Left, new Point(50, 50));
  148. RaisePointerExited(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 = RendererMocks.CreateRenderer();
  158. var pt = new Point(150, 50);
  159. renderer.Setup(r => r.HitTest(It.IsAny<Point>(), It.IsAny<Visual>(), It.IsAny<Func<Visual, bool>>()))
  160. .Returns<Point, Visual, Func<Visual, bool>>((p, r, f) =>
  161. r.Bounds.Contains(p.Transform(r.RenderTransform.Value.Invert())) ?
  162. new Visual[] { r } : new Visual[0]);
  163. var target = new TestButton(renderer.Object)
  164. {
  165. Bounds = new Rect(0, 0, 100, 100),
  166. RenderTransform = new TranslateTransform { X = 100, Y = 0 }
  167. };
  168. //actual bounds of button should be 100,0,100,100 x -> translated 100 pixels
  169. //so mouse with x=150 coordinates should trigger click
  170. //button shouldn't count on bounds to calculate pointer is in the over or not, but
  171. //on avalonia event system, as renderer hit test will properly calculate whether to send
  172. //mouse over events to button based on rendered bounds
  173. //note: button also may have not rectangular shape and only renderer hit testing is reliable
  174. bool clicked = false;
  175. target.Click += (s, e) => clicked = true;
  176. RaisePointerEntered(target);
  177. RaisePointerMove(target, pt);
  178. RaisePointerPressed(target, 1, MouseButton.Left, pt);
  179. Assert.Equal(_helper.Captured, target);
  180. RaisePointerReleased(target, MouseButton.Left, pt);
  181. Assert.Equal(_helper.Captured, null);
  182. Assert.True(clicked);
  183. }
  184. [Fact]
  185. public void Button_Does_Not_Subscribe_To_Command_CanExecuteChanged_Until_Added_To_Logical_Tree()
  186. {
  187. var command = new TestCommand(true);
  188. var target = new Button
  189. {
  190. Command = command,
  191. };
  192. Assert.Equal(0, command.SubscriptionCount);
  193. }
  194. [Fact]
  195. public void Button_Subscribes_To_Command_CanExecuteChanged_When_Added_To_Logical_Tree()
  196. {
  197. var command = new TestCommand(true);
  198. var target = new Button { Command = command };
  199. var root = new TestRoot { Child = target };
  200. Assert.Equal(1, command.SubscriptionCount);
  201. }
  202. [Fact]
  203. public void Button_Unsubscribes_From_Command_CanExecuteChanged_When_Removed_From_Logical_Tree()
  204. {
  205. var command = new TestCommand(true);
  206. var target = new Button { Command = command };
  207. var root = new TestRoot { Child = target };
  208. root.Child = null;
  209. Assert.Equal(0, command.SubscriptionCount);
  210. }
  211. [Fact]
  212. public void Button_Invokes_CanExecute_When_CommandParameter_Changed()
  213. {
  214. var target = new Button();
  215. var raised = 0;
  216. target.Click += (s, e) => ++raised;
  217. target.RaiseEvent(new RoutedEventArgs(AccessKeyHandler.AccessKeyPressedEvent));
  218. Assert.Equal(1, raised);
  219. }
  220. [Fact]
  221. public void Raises_Click_When_AccessKey_Raised()
  222. {
  223. var command = new TestCommand(p => p is bool value && value);
  224. var target = new Button { Command = command };
  225. target.CommandParameter = true;
  226. Assert.True(target.IsEffectivelyEnabled);
  227. target.CommandParameter = false;
  228. Assert.False(target.IsEffectivelyEnabled);
  229. }
  230. [Fact]
  231. public void Button_Invokes_Doesnt_Execute_When_Button_Disabled()
  232. {
  233. var target = new Button();
  234. var raised = 0;
  235. target.IsEnabled = false;
  236. target.Click += (s, e) => ++raised;
  237. target.RaiseEvent(new RoutedEventArgs(AccessKeyHandler.AccessKeyPressedEvent));
  238. Assert.Equal(0, raised);
  239. }
  240. [Fact]
  241. public void Button_IsDefault_Works()
  242. {
  243. using (UnitTestApplication.Start(TestServices.StyledWindow))
  244. {
  245. var raised = 0;
  246. var target = new Button();
  247. var window = new Window { Content = target };
  248. window.Show();
  249. target.Click += (s, e) => ++raised;
  250. target.IsDefault = false;
  251. window.RaiseEvent(CreateKeyDownEvent(Key.Enter));
  252. Assert.Equal(0, raised);
  253. target.IsDefault = true;
  254. window.RaiseEvent(CreateKeyDownEvent(Key.Enter));
  255. Assert.Equal(1, raised);
  256. target.IsDefault = false;
  257. window.RaiseEvent(CreateKeyDownEvent(Key.Enter));
  258. Assert.Equal(1, raised);
  259. target.IsDefault = true;
  260. window.RaiseEvent(CreateKeyDownEvent(Key.Enter));
  261. Assert.Equal(2, raised);
  262. window.Content = null;
  263. // To check if handler was raised on the button, when it's detached, we need to pass it as a source manually.
  264. window.RaiseEvent(CreateKeyDownEvent(Key.Enter, target));
  265. Assert.Equal(2, raised);
  266. }
  267. }
  268. [Fact]
  269. public void Button_IsCancel_Works()
  270. {
  271. using (UnitTestApplication.Start(TestServices.StyledWindow))
  272. {
  273. var raised = 0;
  274. var target = new Button();
  275. var window = new Window { Content = target };
  276. window.Show();
  277. target.Click += (s, e) => ++raised;
  278. target.IsCancel = false;
  279. window.RaiseEvent(CreateKeyDownEvent(Key.Escape));
  280. Assert.Equal(0, raised);
  281. target.IsCancel = true;
  282. window.RaiseEvent(CreateKeyDownEvent(Key.Escape));
  283. Assert.Equal(1, raised);
  284. target.IsCancel = false;
  285. window.RaiseEvent(CreateKeyDownEvent(Key.Escape));
  286. Assert.Equal(1, raised);
  287. target.IsCancel = true;
  288. window.RaiseEvent(CreateKeyDownEvent(Key.Escape));
  289. Assert.Equal(2, raised);
  290. window.Content = null;
  291. window.RaiseEvent(CreateKeyDownEvent(Key.Escape, target));
  292. Assert.Equal(2, raised);
  293. }
  294. }
  295. private KeyEventArgs CreateKeyDownEvent(Key key, Interactive source = null)
  296. {
  297. return new KeyEventArgs { RoutedEvent = InputElement.KeyDownEvent, Key = key, Source = source };
  298. }
  299. private class TestButton : Button, IRenderRoot
  300. {
  301. public TestButton(IRenderer renderer)
  302. {
  303. IsVisible = true;
  304. Renderer = renderer;
  305. }
  306. public new Rect Bounds
  307. {
  308. get => base.Bounds;
  309. set => base.Bounds = value;
  310. }
  311. public Size ClientSize => throw new NotImplementedException();
  312. public IRenderer Renderer { get; }
  313. public double RenderScaling => throw new NotImplementedException();
  314. public IRenderTarget CreateRenderTarget() => throw new NotImplementedException();
  315. public void Invalidate(Rect rect) => throw new NotImplementedException();
  316. public Point PointToClient(PixelPoint p) => throw new NotImplementedException();
  317. public PixelPoint PointToScreen(Point p) => throw new NotImplementedException();
  318. }
  319. private void RaisePointerPressed(Button button, int clickCount, MouseButton mouseButton, Point position)
  320. {
  321. _helper.Down(button, mouseButton, position, clickCount: clickCount);
  322. }
  323. private void RaisePointerReleased(Button button, MouseButton mouseButton, Point pt)
  324. {
  325. _helper.Up(button, mouseButton, pt);
  326. }
  327. private void RaisePointerEntered(Button button)
  328. {
  329. _helper.Enter(button);
  330. }
  331. private void RaisePointerExited(Button button)
  332. {
  333. _helper.Leave(button);
  334. }
  335. private void RaisePointerMove(Button button, Point pos)
  336. {
  337. _helper.Move(button, pos);
  338. }
  339. private class TestCommand : ICommand
  340. {
  341. private readonly Func<object, bool> _canExecute;
  342. private readonly Action<object> _execute;
  343. private EventHandler _canExecuteChanged;
  344. private bool _enabled = true;
  345. public TestCommand(bool enabled = true)
  346. {
  347. _enabled = enabled;
  348. _canExecute = _ => _enabled;
  349. _execute = _ => { };
  350. }
  351. public TestCommand(Func<object, bool> canExecute, Action<object> execute = null)
  352. {
  353. _canExecute = canExecute;
  354. _execute = execute ?? (_ => { });
  355. }
  356. public bool IsEnabled
  357. {
  358. get { return _enabled; }
  359. set
  360. {
  361. if (_enabled != value)
  362. {
  363. _enabled = value;
  364. _canExecuteChanged?.Invoke(this, EventArgs.Empty);
  365. }
  366. }
  367. }
  368. public int SubscriptionCount { get; private set; }
  369. public event EventHandler CanExecuteChanged
  370. {
  371. add { _canExecuteChanged += value; ++SubscriptionCount; }
  372. remove { _canExecuteChanged -= value; --SubscriptionCount; }
  373. }
  374. public bool CanExecute(object parameter) => _canExecute(parameter);
  375. public void Execute(object parameter) => _execute(parameter);
  376. }
  377. }
  378. }