AvaloniaObjectTests_SetValue.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. // Copyright (c) The Avalonia 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 Avalonia.Data;
  5. using Xunit;
  6. namespace Avalonia.Base.UnitTests
  7. {
  8. public class AvaloniaObjectTests_SetValue
  9. {
  10. [Fact]
  11. public void ClearValue_Clears_Value()
  12. {
  13. Class1 target = new Class1();
  14. target.SetValue(Class1.FooProperty, "newvalue");
  15. target.ClearValue(Class1.FooProperty);
  16. Assert.Equal("foodefault", target.GetValue(Class1.FooProperty));
  17. }
  18. [Fact]
  19. public void SetValue_Sets_Value()
  20. {
  21. Class1 target = new Class1();
  22. target.SetValue(Class1.FooProperty, "newvalue");
  23. Assert.Equal("newvalue", target.GetValue(Class1.FooProperty));
  24. }
  25. [Fact]
  26. public void SetValue_Sets_Attached_Value()
  27. {
  28. Class2 target = new Class2();
  29. target.SetValue(AttachedOwner.AttachedProperty, "newvalue");
  30. Assert.Equal("newvalue", target.GetValue(AttachedOwner.AttachedProperty));
  31. }
  32. [Fact]
  33. public void SetValue_Raises_PropertyChanged()
  34. {
  35. Class1 target = new Class1();
  36. bool raised = false;
  37. target.PropertyChanged += (s, e) =>
  38. {
  39. raised = s == target &&
  40. e.Property == Class1.FooProperty &&
  41. (string)e.OldValue == "foodefault" &&
  42. (string)e.NewValue == "newvalue";
  43. };
  44. target.SetValue(Class1.FooProperty, "newvalue");
  45. Assert.True(raised);
  46. }
  47. [Fact]
  48. public void SetValue_Doesnt_Raise_PropertyChanged_If_Value_Not_Changed()
  49. {
  50. Class1 target = new Class1();
  51. bool raised = false;
  52. target.SetValue(Class1.FooProperty, "bar");
  53. target.PropertyChanged += (s, e) =>
  54. {
  55. raised = true;
  56. };
  57. target.SetValue(Class1.FooProperty, "bar");
  58. Assert.False(raised);
  59. }
  60. [Fact]
  61. public void SetValue_Doesnt_Raise_PropertyChanged_If_Value_Not_Changed_From_Default()
  62. {
  63. Class1 target = new Class1();
  64. bool raised = false;
  65. target.PropertyChanged += (s, e) =>
  66. {
  67. raised = true;
  68. };
  69. target.SetValue(Class1.FooProperty, "foodefault");
  70. Assert.False(raised);
  71. }
  72. [Fact]
  73. public void SetValue_Allows_Setting_Unregistered_Property()
  74. {
  75. Class1 target = new Class1();
  76. Assert.False(AvaloniaPropertyRegistry.Instance.IsRegistered(target, Class2.BarProperty));
  77. target.SetValue(Class2.BarProperty, "bar");
  78. Assert.Equal("bar", target.GetValue(Class2.BarProperty));
  79. }
  80. [Fact]
  81. public void SetValue_Allows_Setting_Unregistered_Attached_Property()
  82. {
  83. Class1 target = new Class1();
  84. Assert.False(AvaloniaPropertyRegistry.Instance.IsRegistered(target, AttachedOwner.AttachedProperty));
  85. target.SetValue(AttachedOwner.AttachedProperty, "bar");
  86. Assert.Equal("bar", target.GetValue(AttachedOwner.AttachedProperty));
  87. }
  88. [Fact]
  89. public void SetValue_Throws_Exception_For_Invalid_Value_Type()
  90. {
  91. Class1 target = new Class1();
  92. Assert.Throws<ArgumentException>(() =>
  93. {
  94. target.SetValue(Class1.FooProperty, 123);
  95. });
  96. }
  97. [Fact]
  98. public void SetValue_Of_Integer_On_Double_Property_Works()
  99. {
  100. Class2 target = new Class2();
  101. target.SetValue((AvaloniaProperty)Class2.FlobProperty, 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_Respects_Implicit_Conversions()
  108. {
  109. Class2 target = new Class2();
  110. target.SetValue((AvaloniaProperty)Class2.FlobProperty, new ImplictDouble(4));
  111. var value = target.GetValue(Class2.FlobProperty);
  112. Assert.IsType<double>(value);
  113. Assert.Equal(4, value);
  114. }
  115. [Fact]
  116. public void SetValue_Can_Convert_To_Nullable()
  117. {
  118. Class2 target = new Class2();
  119. target.SetValue((AvaloniaProperty)Class2.FredProperty, 4.0);
  120. var value = target.GetValue(Class2.FredProperty);
  121. Assert.IsType<double>(value);
  122. Assert.Equal(4, value);
  123. }
  124. [Fact]
  125. public void SetValue_Respects_Priority()
  126. {
  127. Class1 target = new Class1();
  128. target.SetValue(Class1.FooProperty, "one", BindingPriority.TemplatedParent);
  129. Assert.Equal("one", target.GetValue(Class1.FooProperty));
  130. target.SetValue(Class1.FooProperty, "two", BindingPriority.Style);
  131. Assert.Equal("one", target.GetValue(Class1.FooProperty));
  132. target.SetValue(Class1.FooProperty, "three", BindingPriority.StyleTrigger);
  133. Assert.Equal("three", target.GetValue(Class1.FooProperty));
  134. }
  135. [Fact]
  136. public void Setting_UnsetValue_Reverts_To_Default_Value()
  137. {
  138. Class1 target = new Class1();
  139. target.SetValue(Class1.FooProperty, "newvalue");
  140. target.SetValue(Class1.FooProperty, AvaloniaProperty.UnsetValue);
  141. Assert.Equal("foodefault", target.GetValue(Class1.FooProperty));
  142. }
  143. private class Class1 : AvaloniaObject
  144. {
  145. public static readonly StyledProperty<string> FooProperty =
  146. AvaloniaProperty.Register<Class1, string>("Foo", "foodefault");
  147. }
  148. private class Class2 : Class1
  149. {
  150. public static readonly StyledProperty<string> BarProperty =
  151. AvaloniaProperty.Register<Class2, string>("Bar", "bardefault");
  152. public static readonly StyledProperty<double> FlobProperty =
  153. AvaloniaProperty.Register<Class2, double>("Flob");
  154. public static readonly StyledProperty<double?> FredProperty =
  155. AvaloniaProperty.Register<Class2, double?>("Fred");
  156. public Class1 Parent
  157. {
  158. get { return (Class1)InheritanceParent; }
  159. set { InheritanceParent = value; }
  160. }
  161. }
  162. private class AttachedOwner
  163. {
  164. public static readonly AttachedProperty<string> AttachedProperty =
  165. AvaloniaProperty.RegisterAttached<AttachedOwner, Class2, string>("Attached");
  166. }
  167. private class ImplictDouble
  168. {
  169. public ImplictDouble(double value)
  170. {
  171. Value = value;
  172. }
  173. public double Value { get; }
  174. public static implicit operator double (ImplictDouble v)
  175. {
  176. return v.Value;
  177. }
  178. }
  179. }
  180. }