AvaloniaPropertyRegistryTests.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 GetRegistered_Returns_Registered_Properties()
  21. {
  22. string[] names = AvaloniaPropertyRegistry.Instance.GetRegistered(typeof(Class1))
  23. .Select(x => x.Name)
  24. .ToArray();
  25. Assert.Equal(new[] { "Foo", "Baz", "Qux" }, names);
  26. }
  27. [Fact]
  28. public void GetRegistered_Returns_Registered_Properties_For_Base_Types()
  29. {
  30. string[] names = AvaloniaPropertyRegistry.Instance.GetRegistered(typeof(Class2))
  31. .Select(x => x.Name)
  32. .ToArray();
  33. Assert.Equal(new[] { "Bar", "Flob", "Fred", "Foo", "Baz", "Qux" }, names);
  34. }
  35. [Fact]
  36. public void GetRegisteredAttached_Returns_Registered_Properties()
  37. {
  38. string[] names = AvaloniaPropertyRegistry.Instance.GetRegisteredAttached(typeof(Class1))
  39. .Select(x => x.Name)
  40. .ToArray();
  41. Assert.Equal(new[] { "Attached" }, names);
  42. }
  43. [Fact]
  44. public void GetRegisteredAttached_Returns_Registered_Properties_For_Base_Types()
  45. {
  46. string[] names = AvaloniaPropertyRegistry.Instance.GetRegisteredAttached(typeof(Class2))
  47. .Select(x => x.Name)
  48. .ToArray();
  49. Assert.Equal(new[] { "Attached" }, names);
  50. }
  51. [Fact]
  52. public void FindRegistered_Finds_Property()
  53. {
  54. var result = AvaloniaPropertyRegistry.Instance.FindRegistered(typeof(Class1), "Foo");
  55. Assert.Equal(Class1.FooProperty, result);
  56. }
  57. [Fact]
  58. public void FindRegistered_Doesnt_Find_Nonregistered_Property()
  59. {
  60. var result = AvaloniaPropertyRegistry.Instance.FindRegistered(typeof(Class1), "Bar");
  61. Assert.Null(result);
  62. }
  63. [Fact]
  64. public void FindRegistered_Finds_Unqualified_Attached_Property_On_Registering_Type()
  65. {
  66. var result = AvaloniaPropertyRegistry.Instance.FindRegistered(typeof(AttachedOwner), "Attached");
  67. Assert.Same(AttachedOwner.AttachedProperty, result);
  68. }
  69. [Fact]
  70. public void FindRegistered_Finds_AddOwnered_Attached_Property()
  71. {
  72. var result = AvaloniaPropertyRegistry.Instance.FindRegistered(typeof(Class3), "Attached");
  73. Assert.Same(AttachedOwner.AttachedProperty, result);
  74. }
  75. [Fact]
  76. public void FindRegistered_Doesnt_Find_Non_AddOwnered_Attached_Property()
  77. {
  78. var result = AvaloniaPropertyRegistry.Instance.FindRegistered(typeof(Class2), "Attached");
  79. Assert.Null(result);
  80. }
  81. private class Class1 : AvaloniaObject
  82. {
  83. public static readonly StyledProperty<string> FooProperty =
  84. AvaloniaProperty.Register<Class1, string>("Foo");
  85. public static readonly StyledProperty<string> BazProperty =
  86. AvaloniaProperty.Register<Class1, string>("Baz");
  87. public static readonly StyledProperty<int> QuxProperty =
  88. AvaloniaProperty.Register<Class1, int>("Qux");
  89. }
  90. private class Class2 : Class1
  91. {
  92. public static readonly StyledProperty<string> BarProperty =
  93. AvaloniaProperty.Register<Class2, string>("Bar");
  94. public static readonly StyledProperty<double> FlobProperty =
  95. AvaloniaProperty.Register<Class2, double>("Flob");
  96. public static readonly StyledProperty<double?> FredProperty =
  97. AvaloniaProperty.Register<Class2, double?>("Fred");
  98. }
  99. private class Class3 : Class1
  100. {
  101. public static readonly AttachedProperty<string> AttachedProperty =
  102. AttachedOwner.AttachedProperty.AddOwner<Class3>();
  103. }
  104. private class AttachedOwner : Class1
  105. {
  106. public static readonly AttachedProperty<string> AttachedProperty =
  107. AvaloniaProperty.RegisterAttached<AttachedOwner, Class1, string>("Attached");
  108. }
  109. private class AttachedOwner2 : AttachedOwner
  110. {
  111. }
  112. }
  113. }