ButtonTests.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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()
  119. {
  120. Bounds = new Rect(0, 0, 100, 100),
  121. Renderer = renderer.Object
  122. };
  123. bool clicked = false;
  124. target.Click += (s, e) => clicked = true;
  125. RaisePointerEntered(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 = RendererMocks.CreateRenderer();
  137. renderer.Setup(r => r.HitTest(It.IsAny<Point>(), It.IsAny<Visual>(), It.IsAny<Func<Visual, bool>>()))
  138. .Returns<Point, Visual, Func<Visual, bool>>((p, r, f) =>
  139. r.Bounds.Contains(p) ? new Visual[] { r } : new Visual[0]);
  140. var target = new TestButton()
  141. {
  142. Bounds = new Rect(0, 0, 100, 100),
  143. Renderer = renderer.Object
  144. };
  145. bool clicked = false;
  146. target.Click += (s, e) => clicked = true;
  147. RaisePointerEntered(target);
  148. RaisePointerMove(target, new Point(50,50));
  149. RaisePointerPressed(target, 1, MouseButton.Left, new Point(50, 50));
  150. RaisePointerExited(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 = RendererMocks.CreateRenderer();
  160. var pt = new Point(150, 50);
  161. renderer.Setup(r => r.HitTest(It.IsAny<Point>(), It.IsAny<Visual>(), It.IsAny<Func<Visual, bool>>()))
  162. .Returns<Point, Visual, Func<Visual, bool>>((p, r, f) =>
  163. r.Bounds.Contains(p.Transform(r.RenderTransform.Value.Invert())) ?
  164. new Visual[] { r } : new Visual[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.Object
  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. RaisePointerEntered(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. [Fact]
  234. public void Button_Invokes_Doesnt_Execute_When_Button_Disabled()
  235. {
  236. var target = new Button();
  237. var raised = 0;
  238. target.IsEnabled = false;
  239. target.Click += (s, e) => ++raised;
  240. target.RaiseEvent(new RoutedEventArgs(AccessKeyHandler.AccessKeyPressedEvent));
  241. Assert.Equal(0, raised);
  242. }
  243. [Fact]
  244. public void Button_IsDefault_Works()
  245. {
  246. using (UnitTestApplication.Start(TestServices.StyledWindow))
  247. {
  248. var raised = 0;
  249. var target = new Button();
  250. var window = new Window { Content = target };
  251. window.Show();
  252. target.Click += (s, e) => ++raised;
  253. target.IsDefault = false;
  254. window.RaiseEvent(CreateKeyDownEvent(Key.Enter));
  255. Assert.Equal(0, raised);
  256. target.IsDefault = true;
  257. window.RaiseEvent(CreateKeyDownEvent(Key.Enter));
  258. Assert.Equal(1, raised);
  259. target.IsDefault = false;
  260. window.RaiseEvent(CreateKeyDownEvent(Key.Enter));
  261. Assert.Equal(1, raised);
  262. target.IsDefault = true;
  263. window.RaiseEvent(CreateKeyDownEvent(Key.Enter));
  264. Assert.Equal(2, raised);
  265. window.Content = null;
  266. // To check if handler was raised on the button, when it's detached, we need to pass it as a source manually.
  267. window.RaiseEvent(CreateKeyDownEvent(Key.Enter, target));
  268. Assert.Equal(2, raised);
  269. }
  270. }
  271. [Fact]
  272. public void Button_IsCancel_Works()
  273. {
  274. using (UnitTestApplication.Start(TestServices.StyledWindow))
  275. {
  276. var raised = 0;
  277. var target = new Button();
  278. var window = new Window { Content = target };
  279. window.Show();
  280. target.Click += (s, e) => ++raised;
  281. target.IsCancel = false;
  282. window.RaiseEvent(CreateKeyDownEvent(Key.Escape));
  283. Assert.Equal(0, raised);
  284. target.IsCancel = true;
  285. window.RaiseEvent(CreateKeyDownEvent(Key.Escape));
  286. Assert.Equal(1, raised);
  287. target.IsCancel = false;
  288. window.RaiseEvent(CreateKeyDownEvent(Key.Escape));
  289. Assert.Equal(1, raised);
  290. target.IsCancel = true;
  291. window.RaiseEvent(CreateKeyDownEvent(Key.Escape));
  292. Assert.Equal(2, raised);
  293. window.Content = null;
  294. window.RaiseEvent(CreateKeyDownEvent(Key.Escape, target));
  295. Assert.Equal(2, raised);
  296. }
  297. }
  298. private KeyEventArgs CreateKeyDownEvent(Key key, Interactive source = null)
  299. {
  300. return new KeyEventArgs { RoutedEvent = InputElement.KeyDownEvent, Key = key, Source = source };
  301. }
  302. private class TestButton : Button, IRenderRoot
  303. {
  304. public TestButton()
  305. {
  306. IsVisible = true;
  307. }
  308. public new Rect Bounds
  309. {
  310. get => base.Bounds;
  311. set => base.Bounds = value;
  312. }
  313. public Size ClientSize => throw new NotImplementedException();
  314. public IRenderer Renderer { get; set; }
  315. public double RenderScaling => throw new NotImplementedException();
  316. public IRenderTarget CreateRenderTarget() => throw new NotImplementedException();
  317. public void Invalidate(Rect rect) => throw new NotImplementedException();
  318. public Point PointToClient(PixelPoint p) => throw new NotImplementedException();
  319. public PixelPoint PointToScreen(Point p) => throw new NotImplementedException();
  320. }
  321. private void RaisePointerPressed(Button button, int clickCount, MouseButton mouseButton, Point position)
  322. {
  323. _helper.Down(button, mouseButton, position, clickCount: clickCount);
  324. }
  325. private void RaisePointerReleased(Button button, MouseButton mouseButton, Point pt)
  326. {
  327. _helper.Up(button, mouseButton, pt);
  328. }
  329. private void RaisePointerEntered(Button button)
  330. {
  331. _helper.Enter(button);
  332. }
  333. private void RaisePointerExited(Button button)
  334. {
  335. _helper.Leave(button);
  336. }
  337. private void RaisePointerMove(Button button, Point pos)
  338. {
  339. _helper.Move(button, pos);
  340. }
  341. private class TestCommand : ICommand
  342. {
  343. private readonly Func<object, bool> _canExecute;
  344. private readonly Action<object> _execute;
  345. private EventHandler _canExecuteChanged;
  346. private bool _enabled = true;
  347. public TestCommand(bool enabled = true)
  348. {
  349. _enabled = enabled;
  350. _canExecute = _ => _enabled;
  351. _execute = _ => { };
  352. }
  353. public TestCommand(Func<object, bool> canExecute, Action<object> execute = null)
  354. {
  355. _canExecute = canExecute;
  356. _execute = execute ?? (_ => { });
  357. }
  358. public bool IsEnabled
  359. {
  360. get { return _enabled; }
  361. set
  362. {
  363. if (_enabled != value)
  364. {
  365. _enabled = value;
  366. _canExecuteChanged?.Invoke(this, EventArgs.Empty);
  367. }
  368. }
  369. }
  370. public int SubscriptionCount { get; private set; }
  371. public event EventHandler CanExecuteChanged
  372. {
  373. add { _canExecuteChanged += value; ++SubscriptionCount; }
  374. remove { _canExecuteChanged -= value; --SubscriptionCount; }
  375. }
  376. public bool CanExecute(object parameter) => _canExecute(parameter);
  377. public void Execute(object parameter) => _execute(parameter);
  378. }
  379. }
  380. }