MenuItemTests.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Windows.Input;
  5. using Avalonia.UnitTests;
  6. using Xunit;
  7. namespace Avalonia.Controls.UnitTests
  8. {
  9. public class MenuItemTests
  10. {
  11. [Fact]
  12. public void Header_Of_Minus_Should_Apply_Separator_Pseudoclass()
  13. {
  14. var target = new MenuItem { Header = "-" };
  15. Assert.True(target.Classes.Contains(":separator"));
  16. }
  17. [Fact]
  18. public void Separator_Item_Should_Set_Focusable_False()
  19. {
  20. var target = new MenuItem { Header = "-" };
  21. Assert.False(target.Focusable);
  22. }
  23. [Fact]
  24. public void MenuItem_Does_Not_Subscribe_To_Command_CanExecuteChanged_Until_Added_To_Logical_Tree()
  25. {
  26. var command = new TestCommand();
  27. var target = new MenuItem
  28. {
  29. Command = command,
  30. };
  31. Assert.Equal(0, command.SubscriptionCount);
  32. }
  33. [Fact]
  34. public void MenuItem_Subscribes_To_Command_CanExecuteChanged_When_Added_To_Logical_Tree()
  35. {
  36. var command = new TestCommand();
  37. var target = new MenuItem { Command = command };
  38. var root = new TestRoot { Child = target };
  39. Assert.Equal(1, command.SubscriptionCount);
  40. }
  41. [Fact]
  42. public void MenuItem_Unsubscribes_From_Command_CanExecuteChanged_When_Removed_From_Logical_Tree()
  43. {
  44. var command = new TestCommand();
  45. var target = new MenuItem { Command = command };
  46. var root = new TestRoot { Child = target };
  47. root.Child = null;
  48. Assert.Equal(0, command.SubscriptionCount);
  49. }
  50. private class TestCommand : ICommand
  51. {
  52. private EventHandler _canExecuteChanged;
  53. public int SubscriptionCount { get; private set; }
  54. public event EventHandler CanExecuteChanged
  55. {
  56. add { _canExecuteChanged += value; ++SubscriptionCount; }
  57. remove { _canExecuteChanged -= value; --SubscriptionCount; }
  58. }
  59. public bool CanExecute(object parameter) => true;
  60. public void Execute(object parameter)
  61. {
  62. }
  63. }
  64. }
  65. }