AvaloniaPropertyRegistryTests.cs 5.3 KB

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