SelectorTests_PropertyEquals.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. using System.Linq;
  2. using System.Reactive.Linq;
  3. using System.Threading.Tasks;
  4. using Avalonia.Controls;
  5. using Xunit;
  6. namespace Avalonia.Styling.UnitTests
  7. {
  8. public class SelectorTests_PropertyEquals
  9. {
  10. static SelectorTests_PropertyEquals()
  11. {
  12. //Ensure the attached properties are registered before run tests
  13. System.Runtime.CompilerServices.RuntimeHelpers.RunClassConstructor(typeof(Grid).TypeHandle);
  14. System.Runtime.CompilerServices.RuntimeHelpers.RunClassConstructor(typeof(Auth).TypeHandle);
  15. }
  16. class Auth
  17. {
  18. public readonly static AttachedProperty<string> NameProperty =
  19. AvaloniaProperty.RegisterAttached<Auth, AvaloniaObject, string>("Name");
  20. public static string GetName(AvaloniaObject avaloniaObject) =>
  21. avaloniaObject.GetValue(NameProperty);
  22. public static void SetName(AvaloniaObject avaloniaObject, string value) =>
  23. avaloniaObject.SetValue(NameProperty, value);
  24. }
  25. [Fact]
  26. public async Task PropertyEquals_Attached_Property_Matching_Value()
  27. {
  28. var target = new Markup.Parsers.SelectorParser((ns, type) =>
  29. {
  30. return (ns, type) switch
  31. {
  32. ("", nameof(TextBlock)) => typeof(TextBlock),
  33. ("", nameof(Grid)) => typeof(Grid),
  34. _ => null
  35. };
  36. }).Parse("TextBlock[(Grid.Column)=1]");
  37. var control = new TextBlock();
  38. var activator = target.Match(control).Activator.ToObservable();
  39. Assert.False(await activator.Take(1));
  40. Grid.SetColumn(control, 1);
  41. Assert.True(await activator.Take(1));
  42. Grid.SetColumn(control, 0);
  43. Assert.False(await activator.Take(1));
  44. }
  45. [Fact]
  46. public async Task PropertyEquals_Attached_Property_With_Namespace_Matching_Value()
  47. {
  48. var target = new Markup.Parsers.SelectorParser((ns, type) =>
  49. {
  50. return (ns, type) switch
  51. {
  52. ("", nameof(TextBlock)) => typeof(TextBlock),
  53. ("l", nameof(Auth)) => typeof(Auth),
  54. _ => null
  55. };
  56. }).Parse("TextBlock[(l|Auth.Name)=Admin]");
  57. var control = new TextBlock();
  58. var activator = target.Match(control).Activator.ToObservable();
  59. Assert.False(await activator.Take(1));
  60. Auth.SetName(control, "Admin");
  61. Assert.True(await activator.Take(1));
  62. Auth.SetName(control, null);
  63. Assert.False(await activator.Take(1));
  64. }
  65. [Fact]
  66. public async Task PropertyEquals_Matches_When_Property_Has_Matching_Value()
  67. {
  68. var control = new TextBlock();
  69. var target = default(Selector).PropertyEquals(TextBlock.TextProperty, "foo");
  70. var activator = target.Match(control).Activator.ToObservable();
  71. Assert.False(await activator.Take(1));
  72. control.Text = "foo";
  73. Assert.True(await activator.Take(1));
  74. control.Text = null;
  75. Assert.False(await activator.Take(1));
  76. }
  77. [Theory]
  78. [InlineData("Bar", FooBar.Bar)]
  79. [InlineData("352", 352)]
  80. [InlineData("0.1", 0.1)]
  81. public async Task PropertyEquals_Matches_When_Property_Has_Matching_Value_And_Different_Type(string literal, object value)
  82. {
  83. var control = new TextBlock();
  84. var target = default(Selector).PropertyEquals(TextBlock.TagProperty, literal);
  85. var activator = target.Match(control).Activator.ToObservable();
  86. Assert.False(await activator.Take(1));
  87. control.Tag = value;
  88. Assert.True(await activator.Take(1));
  89. control.Tag = null;
  90. Assert.False(await activator.Take(1));
  91. }
  92. [Fact]
  93. public void OfType_PropertyEquals_Doesnt_Match_Control_Of_Wrong_Type()
  94. {
  95. var control = new TextBlock();
  96. var target = default(Selector).OfType<Border>().PropertyEquals(TextBlock.TextProperty, "foo");
  97. Assert.Equal(SelectorMatchResult.NeverThisType, target.Match(control).Result);
  98. }
  99. [Fact]
  100. public void PropertyEquals_Selector_Should_Have_Correct_String_Representation()
  101. {
  102. var target = default(Selector)
  103. .OfType<TextBlock>()
  104. .PropertyEquals(TextBlock.TextProperty, "foo");
  105. Assert.Equal("TextBlock[Text=foo]", target.ToString());
  106. }
  107. private enum FooBar
  108. {
  109. Foo,
  110. Bar
  111. }
  112. }
  113. }