AvaloniaPropertyRegistryTests.cs 5.2 KB

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