PerspexPropertyRegistryTests.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. // Copyright (c) The Perspex 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 Perspex.Base.UnitTests
  7. {
  8. public class PerspexPropertyRegistryTests
  9. {
  10. public PerspexPropertyRegistryTests()
  11. {
  12. // Ensure properties are registered.
  13. PerspexProperty 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 = PerspexPropertyRegistry.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 = PerspexPropertyRegistry.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 = PerspexPropertyRegistry.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 = PerspexPropertyRegistry.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 = PerspexPropertyRegistry.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 = PerspexPropertyRegistry.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 = PerspexPropertyRegistry.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 = PerspexPropertyRegistry.Instance.FindRegistered(typeof(Class2), "AttachedOwner.Attached");
  68. Assert.Equal(AttachedOwner.AttachedProperty, result);
  69. }
  70. [Fact]
  71. public void FindRegistered_Finds_AddOwnered_Untyped_Attached_Property()
  72. {
  73. var result = PerspexPropertyRegistry.Instance.FindRegistered(typeof(Class3), "Attached");
  74. Assert.Equal(AttachedOwner.AttachedProperty, result);
  75. }
  76. [Fact]
  77. public void FindRegistered_Finds_AddOwnered_Typed_Attached_Property()
  78. {
  79. var result = PerspexPropertyRegistry.Instance.FindRegistered(typeof(Class3), "Class3.Attached");
  80. Assert.Equal(AttachedOwner.AttachedProperty, result);
  81. }
  82. [Fact]
  83. public void FindRegistered_Finds_AddOwnered_AttachedTyped_Attached_Property()
  84. {
  85. var result = PerspexPropertyRegistry.Instance.FindRegistered(typeof(Class3), "AttachedOwner.Attached");
  86. Assert.Equal(AttachedOwner.AttachedProperty, result);
  87. }
  88. [Fact]
  89. public void FindRegistered_Finds_AddOwnered_BaseTyped_Attached_Property()
  90. {
  91. var result = PerspexPropertyRegistry.Instance.FindRegistered(typeof(Class3), "Class1.Attached");
  92. Assert.Equal(AttachedOwner.AttachedProperty, result);
  93. }
  94. [Fact]
  95. public void FindRegistered_Doesnt_Find_Nonregistered_Property()
  96. {
  97. var result = PerspexPropertyRegistry.Instance.FindRegistered(typeof(Class1), "Bar");
  98. Assert.Null(result);
  99. }
  100. [Fact]
  101. public void FindRegistered_Doesnt_Find_Nonregistered_Attached_Property()
  102. {
  103. var result = PerspexPropertyRegistry.Instance.FindRegistered(typeof(Class4), "AttachedOwner.Attached");
  104. Assert.Null(result);
  105. }
  106. private class Class1 : PerspexObject
  107. {
  108. public static readonly PerspexProperty<string> FooProperty =
  109. PerspexProperty.Register<Class1, string>("Foo");
  110. public static readonly PerspexProperty<string> BazProperty =
  111. PerspexProperty.Register<Class1, string>("Baz");
  112. public static readonly PerspexProperty<int> QuxProperty =
  113. PerspexProperty.Register<Class1, int>("Qux");
  114. }
  115. private class Class2 : Class1
  116. {
  117. public static readonly PerspexProperty<string> BarProperty =
  118. PerspexProperty.Register<Class2, string>("Bar");
  119. public static readonly PerspexProperty<double> FlobProperty =
  120. PerspexProperty.Register<Class2, double>("Flob");
  121. public static readonly PerspexProperty<double?> FredProperty =
  122. PerspexProperty.Register<Class2, double?>("Fred");
  123. }
  124. private class Class3 : Class1
  125. {
  126. public static readonly PerspexProperty<string> AttachedProperty =
  127. AttachedOwner.AttachedProperty.AddOwner<Class3>();
  128. }
  129. public class Class4 : PerspexObject
  130. {
  131. }
  132. private class AttachedOwner
  133. {
  134. public static readonly PerspexProperty<string> AttachedProperty =
  135. PerspexProperty.RegisterAttached<AttachedOwner, Class1, string>("Attached");
  136. }
  137. }
  138. }