PerspexPropertyTests.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 AddOwnered_Property_Should_Equal_Original()
  119. {
  120. var p1 = new PerspexProperty<string>("p1", typeof(Class1));
  121. var p2 = p1.AddOwner<Class3>();
  122. Assert.Equal(p1, p2);
  123. Assert.Equal(p1.GetHashCode(), p2.GetHashCode());
  124. Assert.True(p1 == p2);
  125. }
  126. [Fact]
  127. public void AddOwnered_Direct_Property_Should_Equal_Original()
  128. {
  129. var p1 = new PerspexProperty<string>("d1", typeof(Class1), o => null, (o,v) => { });
  130. var p2 = p1.AddOwner<Class3>(o => null, (o, v) => { });
  131. Assert.Equal(p1, p2);
  132. Assert.Equal(p1.GetHashCode(), p2.GetHashCode());
  133. Assert.True(p1 == p2);
  134. }
  135. [Fact]
  136. public void AddOwner_With_Getter_And_Setter_On_Standard_Property_Should_Throw()
  137. {
  138. var p1 = new PerspexProperty<string>("p1", typeof(Class1));
  139. Assert.Throws<InvalidOperationException>(() => p1.AddOwner<Class3>(o => null, (o, v) => { }));
  140. }
  141. [Fact]
  142. public void AddOwner_On_Direct_Property_Without_Getter_Or_Setter_Should_Throw()
  143. {
  144. var p1 = new PerspexProperty<string>("e1", typeof(Class1), o => null, (o, v) => { });
  145. Assert.Throws<InvalidOperationException>(() => p1.AddOwner<Class3>());
  146. }
  147. private class Class1 : PerspexObject
  148. {
  149. public static readonly PerspexProperty<string> FooProperty =
  150. PerspexProperty.Register<Class1, string>("Foo", "default");
  151. }
  152. private class Class2 : Class1
  153. {
  154. }
  155. private class Class3 : PerspexObject
  156. {
  157. }
  158. }
  159. }