PerspexPropertyTests.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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;
  4. using Xunit;
  5. namespace Perspex.Base.UnitTests
  6. {
  7. public class PerspexPropertyTests
  8. {
  9. [Fact]
  10. public void Constructor_Sets_Properties()
  11. {
  12. PerspexProperty<string> target = new PerspexProperty<string>(
  13. "test",
  14. typeof(Class1),
  15. "Foo",
  16. false,
  17. BindingMode.OneWay,
  18. null);
  19. Assert.Equal("test", target.Name);
  20. Assert.Equal(typeof(string), target.PropertyType);
  21. Assert.Equal(typeof(Class1), target.OwnerType);
  22. Assert.Equal(false, target.Inherits);
  23. }
  24. [Fact]
  25. public void Name_Cannot_Contain_Periods()
  26. {
  27. Assert.Throws<ArgumentException>(() => new PerspexProperty<string>(
  28. "Foo.Bar",
  29. typeof(Class1),
  30. "Foo",
  31. false,
  32. BindingMode.OneWay,
  33. null));
  34. }
  35. [Fact]
  36. public void GetDefaultValue_Returns_Registered_Value()
  37. {
  38. PerspexProperty<string> target = new PerspexProperty<string>(
  39. "test",
  40. typeof(Class1),
  41. "Foo",
  42. false,
  43. BindingMode.OneWay,
  44. null);
  45. Assert.Equal("Foo", target.GetDefaultValue<Class1>());
  46. }
  47. [Fact]
  48. public void GetDefaultValue_Returns_Registered_Value_For_Not_Overridden_Class()
  49. {
  50. PerspexProperty<string> target = new PerspexProperty<string>(
  51. "test",
  52. typeof(Class1),
  53. "Foo",
  54. false,
  55. BindingMode.OneWay,
  56. null);
  57. Assert.Equal("Foo", target.GetDefaultValue<Class2>());
  58. }
  59. [Fact]
  60. public void GetDefaultValue_Returns_Registered_Value_For_Unrelated_Class()
  61. {
  62. PerspexProperty<string> target = new PerspexProperty<string>(
  63. "test",
  64. typeof(Class3),
  65. "Foo",
  66. false,
  67. BindingMode.OneWay,
  68. null);
  69. Assert.Equal("Foo", target.GetDefaultValue<Class2>());
  70. }
  71. [Fact]
  72. public void GetDefaultValue_Returns_Overridden_Value()
  73. {
  74. PerspexProperty<string> target = new PerspexProperty<string>(
  75. "test",
  76. typeof(Class1),
  77. "Foo",
  78. false,
  79. BindingMode.OneWay,
  80. null);
  81. target.OverrideDefaultValue(typeof(Class2), "Bar");
  82. Assert.Equal("Bar", target.GetDefaultValue<Class2>());
  83. }
  84. [Fact]
  85. public void Initialized_Observable_Fired()
  86. {
  87. bool invoked = false;
  88. Class1.FooProperty.Initialized.Subscribe(x =>
  89. {
  90. Assert.Equal(PerspexProperty.UnsetValue, x.OldValue);
  91. Assert.Equal("default", x.NewValue);
  92. Assert.Equal(BindingPriority.Unset, x.Priority);
  93. invoked = true;
  94. });
  95. var target = new Class1();
  96. Assert.True(invoked);
  97. }
  98. [Fact]
  99. public void Changed_Observable_Fired()
  100. {
  101. var target = new Class1();
  102. string value = null;
  103. Class1.FooProperty.Changed.Subscribe(x => value = (string)x.NewValue);
  104. target.SetValue(Class1.FooProperty, "newvalue");
  105. Assert.Equal("newvalue", value);
  106. }
  107. [Fact]
  108. public void IsDirect_Property_Set_On_Direct_PerspexProperty()
  109. {
  110. PerspexProperty<string> target = new PerspexProperty<string>(
  111. "test",
  112. typeof(Class1),
  113. o => null,
  114. (o, v) => { });
  115. Assert.True(target.IsDirect);
  116. }
  117. [Fact]
  118. public void Property_Equals_Should_Handle_Null()
  119. {
  120. var p1 = new PerspexProperty<string>("p1", typeof(Class1));
  121. Assert.NotEqual(p1, null);
  122. Assert.NotEqual(null, p1);
  123. Assert.False(p1 == null);
  124. Assert.False(null == p1);
  125. Assert.False(p1.Equals(null));
  126. Assert.True((PerspexProperty)null == (PerspexProperty)null);
  127. }
  128. [Fact]
  129. public void AddOwnered_Property_Should_Equal_Original()
  130. {
  131. var p1 = new PerspexProperty<string>("p1", typeof(Class1));
  132. var p2 = p1.AddOwner<Class3>();
  133. Assert.Equal(p1, p2);
  134. Assert.Equal(p1.GetHashCode(), p2.GetHashCode());
  135. Assert.True(p1 == p2);
  136. }
  137. [Fact]
  138. public void AddOwnered_Property_Should_Have_OwnerType_Set()
  139. {
  140. var p1 = new PerspexProperty<string>("p1", typeof(Class1));
  141. var p2 = p1.AddOwner<Class3>();
  142. Assert.Equal(typeof(Class3), p2.OwnerType);
  143. }
  144. [Fact]
  145. public void AddOwnered_Direct_Property_Should_Equal_Original()
  146. {
  147. var p1 = new PerspexProperty<string>("d1", typeof(Class1), o => null, (o,v) => { });
  148. var p2 = p1.AddOwner<Class3>(o => null, (o, v) => { });
  149. Assert.Equal(p1, p2);
  150. Assert.Equal(p1.GetHashCode(), p2.GetHashCode());
  151. Assert.True(p1 == p2);
  152. }
  153. [Fact]
  154. public void AddOwnered_Direct_Property_Should_Have_OwnerType_Set()
  155. {
  156. var p1 = new PerspexProperty<string>("d1", typeof(Class1), o => null, (o, v) => { });
  157. var p2 = p1.AddOwner<Class3>(o => null, (o, v) => { });
  158. Assert.Equal(typeof(Class3), p2.OwnerType);
  159. }
  160. [Fact]
  161. public void AddOwner_With_Getter_And_Setter_On_Standard_Property_Should_Throw()
  162. {
  163. var p1 = new PerspexProperty<string>("p1", typeof(Class1));
  164. Assert.Throws<InvalidOperationException>(() => p1.AddOwner<Class3>(o => null, (o, v) => { }));
  165. }
  166. [Fact]
  167. public void AddOwner_On_Direct_Property_Without_Getter_Or_Setter_Should_Throw()
  168. {
  169. var p1 = new PerspexProperty<string>("e1", typeof(Class1), o => null, (o, v) => { });
  170. Assert.Throws<InvalidOperationException>(() => p1.AddOwner<Class3>());
  171. }
  172. private class Class1 : PerspexObject
  173. {
  174. public static readonly PerspexProperty<string> FooProperty =
  175. PerspexProperty.Register<Class1, string>("Foo", "default");
  176. }
  177. private class Class2 : Class1
  178. {
  179. }
  180. private class Class3 : PerspexObject
  181. {
  182. }
  183. }
  184. }