PerspexObjectTests_SetValue.cs 6.0 KB

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