AvaloniaPropertyRegistryTests.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. using System.Linq;
  2. using System.Reactive.Linq;
  3. using Xunit;
  4. using Xunit.Abstractions;
  5. namespace Avalonia.Base.UnitTests
  6. {
  7. public class AvaloniaPropertyRegistryTests
  8. {
  9. public AvaloniaPropertyRegistryTests(ITestOutputHelper s)
  10. {
  11. // Ensure properties are registered.
  12. AvaloniaProperty p;
  13. p = Class1.FooProperty;
  14. p = Class2.BarProperty;
  15. p = AttachedOwner.AttachedProperty;
  16. }
  17. [Fact]
  18. public void Registered_Properties_Count_Reflects_Newly_Added_Attached_Property()
  19. {
  20. var registry = new AvaloniaPropertyRegistry();
  21. var metadata = new StyledPropertyMetadata<int>();
  22. var property = new AttachedProperty<int>("test", typeof(object), metadata, true);
  23. registry.Register(typeof(object), property);
  24. registry.RegisterAttached(typeof(AvaloniaPropertyRegistryTests), property);
  25. property.AddOwner<Class4>();
  26. Assert.Equal(1, registry.Properties.Count);
  27. }
  28. [Fact]
  29. public void GetRegistered_Returns_Registered_Properties()
  30. {
  31. string[] names = AvaloniaPropertyRegistry.Instance.GetRegistered(typeof(Class1))
  32. .Select(x => x.Name)
  33. .ToArray();
  34. Assert.Equal(new[] { "Foo", "Baz", "Qux" }, names);
  35. }
  36. [Fact]
  37. public void GetRegistered_Returns_Registered_Properties_For_Base_Types()
  38. {
  39. string[] names = AvaloniaPropertyRegistry.Instance.GetRegistered(typeof(Class2))
  40. .Select(x => x.Name)
  41. .ToArray();
  42. Assert.Equal(new[] { "Bar", "Flob", "Fred", "Foo", "Baz", "Qux" }, names);
  43. }
  44. [Fact]
  45. public void GetRegisteredAttached_Returns_Registered_Properties()
  46. {
  47. string[] names = AvaloniaPropertyRegistry.Instance.GetRegisteredAttached(typeof(Class1))
  48. .Select(x => x.Name)
  49. .ToArray();
  50. Assert.Equal(new[] { "Attached" }, names);
  51. }
  52. [Fact]
  53. public void GetRegisteredAttached_Returns_Registered_Properties_For_Base_Types()
  54. {
  55. string[] names = AvaloniaPropertyRegistry.Instance.GetRegisteredAttached(typeof(Class2))
  56. .Select(x => x.Name)
  57. .ToArray();
  58. Assert.Equal(new[] { "Attached" }, names);
  59. }
  60. [Fact]
  61. public void FindRegistered_Finds_Property()
  62. {
  63. var result = AvaloniaPropertyRegistry.Instance.FindRegistered(typeof(Class1), "Foo");
  64. Assert.Equal(Class1.FooProperty, result);
  65. }
  66. [Fact]
  67. public void FindRegistered_Doesnt_Find_Nonregistered_Property()
  68. {
  69. var result = AvaloniaPropertyRegistry.Instance.FindRegistered(typeof(Class1), "Bar");
  70. Assert.Null(result);
  71. }
  72. [Fact]
  73. public void FindRegistered_Finds_Unqualified_Attached_Property_On_Registering_Type()
  74. {
  75. var result = AvaloniaPropertyRegistry.Instance.FindRegistered(typeof(AttachedOwner), "Attached");
  76. Assert.Same(AttachedOwner.AttachedProperty, result);
  77. }
  78. [Fact]
  79. public void FindRegistered_Finds_AddOwnered_Attached_Property()
  80. {
  81. var result = AvaloniaPropertyRegistry.Instance.FindRegistered(typeof(Class3), "Attached");
  82. Assert.Same(AttachedOwner.AttachedProperty, result);
  83. }
  84. [Fact]
  85. public void FindRegistered_Doesnt_Find_Non_AddOwnered_Attached_Property()
  86. {
  87. var result = AvaloniaPropertyRegistry.Instance.FindRegistered(typeof(Class2), "Attached");
  88. Assert.Null(result);
  89. }
  90. private class Class1 : AvaloniaObject
  91. {
  92. public static readonly StyledProperty<string> FooProperty =
  93. AvaloniaProperty.Register<Class1, string>("Foo");
  94. public static readonly StyledProperty<string> BazProperty =
  95. AvaloniaProperty.Register<Class1, string>("Baz");
  96. public static readonly StyledProperty<int> QuxProperty =
  97. AvaloniaProperty.Register<Class1, int>("Qux");
  98. }
  99. private class Class2 : Class1
  100. {
  101. public static readonly StyledProperty<string> BarProperty =
  102. AvaloniaProperty.Register<Class2, string>("Bar");
  103. public static readonly StyledProperty<double> FlobProperty =
  104. AvaloniaProperty.Register<Class2, double>("Flob");
  105. public static readonly StyledProperty<double?> FredProperty =
  106. AvaloniaProperty.Register<Class2, double?>("Fred");
  107. }
  108. private class Class3 : Class1
  109. {
  110. public static readonly AttachedProperty<string> AttachedProperty =
  111. AttachedOwner.AttachedProperty.AddOwner<Class3>();
  112. }
  113. private class AttachedOwner : Class1
  114. {
  115. public static readonly AttachedProperty<string> AttachedProperty =
  116. AvaloniaProperty.RegisterAttached<AttachedOwner, Class1, string>("Attached");
  117. }
  118. private class AttachedOwner2 : AttachedOwner
  119. {
  120. }
  121. private class Class4 : AvaloniaObject
  122. {
  123. }
  124. }
  125. }