AvaloniaPropertyRegistryTests.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. // Copyright (c) The Avalonia Project. All rights reserved.
  2. // Licensed under the MIT license. See licence.md file in the project root for full license information.
  3. using System.Linq;
  4. using System.Reactive.Linq;
  5. using Xunit;
  6. namespace Avalonia.Base.UnitTests
  7. {
  8. public class AvaloniaPropertyRegistryTests
  9. {
  10. public AvaloniaPropertyRegistryTests()
  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 GetRegistered_Returns_Registered_Properties()
  20. {
  21. string[] names = AvaloniaPropertyRegistry.Instance.GetRegistered(typeof(Class1))
  22. .Select(x => x.Name)
  23. .ToArray();
  24. Assert.Equal(new[] { "Foo", "Baz", "Qux", "Attached" }, names);
  25. }
  26. [Fact]
  27. public void GetRegistered_Returns_Registered_Properties_For_Base_Types()
  28. {
  29. string[] names = AvaloniaPropertyRegistry.Instance.GetRegistered(typeof(Class2))
  30. .Select(x => x.Name)
  31. .ToArray();
  32. Assert.Equal(new[] { "Bar", "Flob", "Fred", "Foo", "Baz", "Qux", "Attached" }, names);
  33. }
  34. [Fact]
  35. public void GetAttached_Returns_Registered_Properties_For_Base_Types()
  36. {
  37. string[] names = AvaloniaPropertyRegistry.Instance.GetAttached(typeof(AttachedOwner)).Select(x => x.Name).ToArray();
  38. Assert.Equal(new[] { "Attached" }, names);
  39. }
  40. [Fact]
  41. public void FindRegistered_Finds_Untyped_Property()
  42. {
  43. var result = AvaloniaPropertyRegistry.Instance.FindRegistered(typeof(Class1), "Foo");
  44. Assert.Equal(Class1.FooProperty, result);
  45. }
  46. [Fact]
  47. public void FindRegistered_Finds_Typed_Property()
  48. {
  49. var result = AvaloniaPropertyRegistry.Instance.FindRegistered(typeof(Class1), "Class1.Foo");
  50. Assert.Equal(Class1.FooProperty, result);
  51. }
  52. [Fact]
  53. public void FindRegistered_Finds_Typed_Inherited_Property()
  54. {
  55. var result = AvaloniaPropertyRegistry.Instance.FindRegistered(typeof(Class2), "Class1.Foo");
  56. Assert.Equal(Class2.FooProperty, result);
  57. }
  58. [Fact]
  59. public void FindRegistered_Finds_Inherited_Property_With_Derived_Type_Name()
  60. {
  61. var result = AvaloniaPropertyRegistry.Instance.FindRegistered(typeof(Class2), "Class2.Foo");
  62. Assert.Equal(Class2.FooProperty, result);
  63. }
  64. [Fact]
  65. public void FindRegistered_Finds_Attached_Property()
  66. {
  67. var result = AvaloniaPropertyRegistry.Instance.FindRegistered(typeof(Class2), "AttachedOwner.Attached");
  68. Assert.Equal(AttachedOwner.AttachedProperty, result);
  69. }
  70. [Fact]
  71. public void FindRegistered_Doesnt_Finds_Unqualified_Attached_Property()
  72. {
  73. var result = AvaloniaPropertyRegistry.Instance.FindRegistered(typeof(Class2), "Attached");
  74. Assert.Null(result);
  75. }
  76. [Fact]
  77. public void FindRegistered_Finds_Unqualified_Attached_Property_On_Registering_Type()
  78. {
  79. var result = AvaloniaPropertyRegistry.Instance.FindRegistered(typeof(AttachedOwner), "Attached");
  80. Assert.True(AttachedOwner.AttachedProperty == result);
  81. }
  82. [Fact]
  83. public void FindRegistered_Finds_AddOwnered_Untyped_Attached_Property()
  84. {
  85. var result = AvaloniaPropertyRegistry.Instance.FindRegistered(typeof(Class3), "Attached");
  86. Assert.True(AttachedOwner.AttachedProperty == result);
  87. }
  88. [Fact]
  89. public void FindRegistered_Finds_AddOwnered_Typed_Attached_Property()
  90. {
  91. var result = AvaloniaPropertyRegistry.Instance.FindRegistered(typeof(Class3), "Class3.Attached");
  92. Assert.True(AttachedOwner.AttachedProperty == result);
  93. }
  94. [Fact]
  95. public void FindRegistered_Finds_AddOwnered_AttachedTyped_Attached_Property()
  96. {
  97. var result = AvaloniaPropertyRegistry.Instance.FindRegistered(typeof(Class3), "AttachedOwner.Attached");
  98. Assert.True(AttachedOwner.AttachedProperty == result);
  99. }
  100. [Fact]
  101. public void FindRegistered_Finds_AddOwnered_BaseTyped_Attached_Property()
  102. {
  103. var result = AvaloniaPropertyRegistry.Instance.FindRegistered(typeof(Class3), "Class1.Attached");
  104. Assert.True(AttachedOwner.AttachedProperty == result);
  105. }
  106. [Fact]
  107. public void FindRegistered_Doesnt_Find_Nonregistered_Property()
  108. {
  109. var result = AvaloniaPropertyRegistry.Instance.FindRegistered(typeof(Class1), "Bar");
  110. Assert.Null(result);
  111. }
  112. [Fact]
  113. public void FindRegistered_Doesnt_Find_Nonregistered_Attached_Property()
  114. {
  115. var result = AvaloniaPropertyRegistry.Instance.FindRegistered(typeof(Class4), "AttachedOwner.Attached");
  116. Assert.Null(result);
  117. }
  118. private class Class1 : AvaloniaObject
  119. {
  120. public static readonly StyledProperty<string> FooProperty =
  121. AvaloniaProperty.Register<Class1, string>("Foo");
  122. public static readonly StyledProperty<string> BazProperty =
  123. AvaloniaProperty.Register<Class1, string>("Baz");
  124. public static readonly StyledProperty<int> QuxProperty =
  125. AvaloniaProperty.Register<Class1, int>("Qux");
  126. }
  127. private class Class2 : Class1
  128. {
  129. public static readonly StyledProperty<string> BarProperty =
  130. AvaloniaProperty.Register<Class2, string>("Bar");
  131. public static readonly StyledProperty<double> FlobProperty =
  132. AvaloniaProperty.Register<Class2, double>("Flob");
  133. public static readonly StyledProperty<double?> FredProperty =
  134. AvaloniaProperty.Register<Class2, double?>("Fred");
  135. }
  136. private class Class3 : Class1
  137. {
  138. public static readonly StyledProperty<string> AttachedProperty =
  139. AttachedOwner.AttachedProperty.AddOwner<Class3>();
  140. }
  141. public class Class4 : AvaloniaObject
  142. {
  143. }
  144. private class AttachedOwner : Class1
  145. {
  146. public static readonly AttachedProperty<string> AttachedProperty =
  147. AvaloniaProperty.RegisterAttached<AttachedOwner, Class1, string>("Attached");
  148. }
  149. }
  150. }