PerspexObjectTests_SetValue.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. // -----------------------------------------------------------------------
  2. // <copyright file="PerspexObjectTests_SetValue.cs" company="Steven Kirk">
  3. // Copyright 2015 MIT Licence. See licence.md for more information.
  4. // </copyright>
  5. // -----------------------------------------------------------------------
  6. namespace Perspex.Base.UnitTests
  7. {
  8. using System;
  9. using System.Linq;
  10. using System.Reactive.Linq;
  11. using System.Reactive.Subjects;
  12. using Xunit;
  13. public class PerspexObjectTests_SetValue
  14. {
  15. [Fact]
  16. public void ClearValue_Clears_Value()
  17. {
  18. Class1 target = new Class1();
  19. target.SetValue(Class1.FooProperty, "newvalue");
  20. target.ClearValue(Class1.FooProperty);
  21. Assert.Equal("foodefault", target.GetValue(Class1.FooProperty));
  22. }
  23. [Fact]
  24. public void SetValue_Sets_Value()
  25. {
  26. Class1 target = new Class1();
  27. target.SetValue(Class1.FooProperty, "newvalue");
  28. Assert.Equal("newvalue", target.GetValue(Class1.FooProperty));
  29. }
  30. [Fact]
  31. public void SetValue_Raises_PropertyChanged()
  32. {
  33. Class1 target = new Class1();
  34. bool raised = false;
  35. target.PropertyChanged += (s, e) =>
  36. {
  37. raised = s == target &&
  38. e.Property == Class1.FooProperty &&
  39. (string)e.OldValue == "foodefault" &&
  40. (string)e.NewValue == "newvalue";
  41. };
  42. target.SetValue(Class1.FooProperty, "newvalue");
  43. Assert.True(raised);
  44. }
  45. [Fact]
  46. public void SetValue_Doesnt_Raise_PropertyChanged_If_Value_Not_Changed()
  47. {
  48. Class1 target = new Class1();
  49. bool raised = false;
  50. target.SetValue(Class1.FooProperty, "bar");
  51. target.PropertyChanged += (s, e) =>
  52. {
  53. raised = true;
  54. };
  55. target.SetValue(Class1.FooProperty, "bar");
  56. Assert.False(raised);
  57. }
  58. [Fact]
  59. public void SetValue_Doesnt_Raise_PropertyChanged_If_Value_Not_Changed_From_Default()
  60. {
  61. Class1 target = new Class1();
  62. bool raised = false;
  63. target.PropertyChanged += (s, e) =>
  64. {
  65. raised = true;
  66. };
  67. target.SetValue(Class1.FooProperty, "foodefault");
  68. Assert.False(raised);
  69. }
  70. [Fact]
  71. public void SetValue_Throws_Exception_For_Unregistered_Property()
  72. {
  73. Class1 target = new Class1();
  74. Assert.Throws<InvalidOperationException>(() =>
  75. {
  76. target.SetValue(Class2.BarProperty, "invalid");
  77. });
  78. }
  79. [Fact]
  80. public void SetValue_Throws_Exception_For_Invalid_Value_Type()
  81. {
  82. Class1 target = new Class1();
  83. Assert.Throws<InvalidOperationException>(() =>
  84. {
  85. target.SetValue(Class1.FooProperty, 123);
  86. });
  87. }
  88. [Fact]
  89. public void SetValue_Of_Integer_On_Double_Property_Works()
  90. {
  91. Class2 target = new Class2();
  92. target.SetValue((PerspexProperty)Class2.FlobProperty, 4);
  93. var value = target.GetValue(Class2.FlobProperty);
  94. Assert.IsType<double>(value);
  95. Assert.Equal(4, value);
  96. }
  97. [Fact]
  98. public void SetValue_Respects_Implicit_Conversions()
  99. {
  100. Class2 target = new Class2();
  101. target.SetValue((PerspexProperty)Class2.FlobProperty, new ImplictDouble(4));
  102. var value = target.GetValue(Class2.FlobProperty);
  103. Assert.IsType<double>(value);
  104. Assert.Equal(4, value);
  105. }
  106. [Fact]
  107. public void SetValue_Can_Convert_To_Nullable()
  108. {
  109. Class2 target = new Class2();
  110. target.SetValue((PerspexProperty)Class2.FredProperty, 4.0);
  111. var value = target.GetValue(Class2.FredProperty);
  112. Assert.IsType<double>(value);
  113. Assert.Equal(4, value);
  114. }
  115. [Fact]
  116. public void SetValue_Respects_Priority()
  117. {
  118. Class1 target = new Class1();
  119. target.SetValue(Class1.FooProperty, "one", BindingPriority.TemplatedParent);
  120. Assert.Equal("one", target.GetValue(Class1.FooProperty));
  121. target.SetValue(Class1.FooProperty, "two", BindingPriority.Style);
  122. Assert.Equal("one", target.GetValue(Class1.FooProperty));
  123. target.SetValue(Class1.FooProperty, "three", BindingPriority.StyleTrigger);
  124. Assert.Equal("three", target.GetValue(Class1.FooProperty));
  125. }
  126. [Fact]
  127. public void Setting_UnsetValue_Reverts_To_Default_Value()
  128. {
  129. Class1 target = new Class1();
  130. target.SetValue(Class1.FooProperty, "newvalue");
  131. target.SetValue(Class1.FooProperty, PerspexProperty.UnsetValue);
  132. Assert.Equal("foodefault", target.GetValue(Class1.FooProperty));
  133. }
  134. private class Class1 : PerspexObject
  135. {
  136. public static readonly PerspexProperty<string> FooProperty =
  137. PerspexProperty.Register<Class1, string>("Foo", "foodefault");
  138. }
  139. private class Class2 : Class1
  140. {
  141. public static readonly PerspexProperty<string> BarProperty =
  142. PerspexProperty.Register<Class2, string>("Bar", "bardefault");
  143. public static readonly PerspexProperty<double> FlobProperty =
  144. PerspexProperty.Register<Class2, double>("Flob");
  145. public static readonly PerspexProperty<double?> FredProperty =
  146. PerspexProperty.Register<Class2, double?>("Fred");
  147. public Class1 Parent
  148. {
  149. get { return (Class1)this.InheritanceParent; }
  150. set { this.InheritanceParent = value; }
  151. }
  152. }
  153. private class ImplictDouble
  154. {
  155. public ImplictDouble(double value)
  156. {
  157. this.Value = value;
  158. }
  159. public double Value { get; }
  160. public static implicit operator double(ImplictDouble v)
  161. {
  162. return v.Value;
  163. }
  164. }
  165. }
  166. }