AvaloniaObjectTests_SetValue.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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 ClearValue_Raises_PropertyChanged()
  20. {
  21. Class1 target = new Class1();
  22. var raised = 0;
  23. target.SetValue(Class1.FooProperty, "newvalue");
  24. target.PropertyChanged += (s, e) =>
  25. {
  26. Assert.Same(target, s);
  27. Assert.Equal(BindingPriority.Unset, e.Priority);
  28. Assert.Equal(Class1.FooProperty, e.Property);
  29. Assert.Equal("newvalue", (string)e.OldValue);
  30. Assert.Equal("foodefault", (string)e.NewValue);
  31. ++raised;
  32. };
  33. target.ClearValue(Class1.FooProperty);
  34. Assert.Equal(1, raised);
  35. }
  36. [Fact]
  37. public void SetValue_Sets_Value()
  38. {
  39. Class1 target = new Class1();
  40. target.SetValue(Class1.FooProperty, "newvalue");
  41. Assert.Equal("newvalue", target.GetValue(Class1.FooProperty));
  42. }
  43. [Fact]
  44. public void SetValue_Sets_Attached_Value()
  45. {
  46. Class2 target = new Class2();
  47. target.SetValue(AttachedOwner.AttachedProperty, "newvalue");
  48. Assert.Equal("newvalue", target.GetValue(AttachedOwner.AttachedProperty));
  49. }
  50. [Fact]
  51. public void SetValue_Raises_PropertyChanged()
  52. {
  53. Class1 target = new Class1();
  54. bool raised = false;
  55. target.PropertyChanged += (s, e) =>
  56. {
  57. raised = s == target &&
  58. e.Property == Class1.FooProperty &&
  59. (string)e.OldValue == "foodefault" &&
  60. (string)e.NewValue == "newvalue";
  61. };
  62. target.SetValue(Class1.FooProperty, "newvalue");
  63. Assert.True(raised);
  64. }
  65. [Fact]
  66. public void SetValue_Style_Priority_Raises_PropertyChanged()
  67. {
  68. Class1 target = new Class1();
  69. bool raised = false;
  70. target.PropertyChanged += (s, e) =>
  71. {
  72. raised = s == target &&
  73. e.Property == Class1.FooProperty &&
  74. (string)e.OldValue == "foodefault" &&
  75. (string)e.NewValue == "newvalue";
  76. };
  77. target.SetValue(Class1.FooProperty, "newvalue", BindingPriority.Style);
  78. Assert.True(raised);
  79. }
  80. [Fact]
  81. public void SetValue_Doesnt_Raise_PropertyChanged_If_Value_Not_Changed()
  82. {
  83. Class1 target = new Class1();
  84. bool raised = false;
  85. target.SetValue(Class1.FooProperty, "bar");
  86. target.PropertyChanged += (s, e) =>
  87. {
  88. raised = true;
  89. };
  90. target.SetValue(Class1.FooProperty, "bar");
  91. Assert.False(raised);
  92. }
  93. [Fact]
  94. public void SetValue_Doesnt_Raise_PropertyChanged_If_Value_Not_Changed_From_Default()
  95. {
  96. Class1 target = new Class1();
  97. bool raised = false;
  98. target.PropertyChanged += (s, e) =>
  99. {
  100. raised = true;
  101. };
  102. target.SetValue(Class1.FooProperty, "foodefault");
  103. Assert.False(raised);
  104. }
  105. [Fact]
  106. public void SetValue_Allows_Setting_Unregistered_Property()
  107. {
  108. Class1 target = new Class1();
  109. Assert.False(AvaloniaPropertyRegistry.Instance.IsRegistered(target, Class2.BarProperty));
  110. target.SetValue(Class2.BarProperty, "bar");
  111. Assert.Equal("bar", target.GetValue(Class2.BarProperty));
  112. }
  113. [Fact]
  114. public void SetValue_Allows_Setting_Unregistered_Attached_Property()
  115. {
  116. Class1 target = new Class1();
  117. Assert.False(AvaloniaPropertyRegistry.Instance.IsRegistered(target, AttachedOwner.AttachedProperty));
  118. target.SetValue(AttachedOwner.AttachedProperty, "bar");
  119. Assert.Equal("bar", target.GetValue(AttachedOwner.AttachedProperty));
  120. }
  121. [Fact]
  122. public void SetValue_Throws_Exception_For_Invalid_Value_Type()
  123. {
  124. Class1 target = new Class1();
  125. Assert.Throws<ArgumentException>(() =>
  126. {
  127. target.SetValue(Class1.FooProperty, 123);
  128. });
  129. }
  130. [Fact]
  131. public void SetValue_Of_Integer_On_Double_Property_Works()
  132. {
  133. Class2 target = new Class2();
  134. target.SetValue((AvaloniaProperty)Class2.FlobProperty, 4);
  135. var value = target.GetValue(Class2.FlobProperty);
  136. Assert.IsType<double>(value);
  137. Assert.Equal(4, value);
  138. }
  139. [Fact]
  140. public void SetValue_Respects_Implicit_Conversions()
  141. {
  142. Class2 target = new Class2();
  143. target.SetValue((AvaloniaProperty)Class2.FlobProperty, new ImplictDouble(4));
  144. var value = target.GetValue(Class2.FlobProperty);
  145. Assert.IsType<double>(value);
  146. Assert.Equal(4, value);
  147. }
  148. [Fact]
  149. public void SetValue_Can_Convert_To_Nullable()
  150. {
  151. Class2 target = new Class2();
  152. target.SetValue((AvaloniaProperty)Class2.FredProperty, 4.0);
  153. var value = target.GetValue(Class2.FredProperty);
  154. Assert.IsType<double>(value);
  155. Assert.Equal(4, value);
  156. }
  157. [Fact]
  158. public void SetValue_Respects_Priority()
  159. {
  160. Class1 target = new Class1();
  161. target.SetValue(Class1.FooProperty, "one", BindingPriority.TemplatedParent);
  162. Assert.Equal("one", target.GetValue(Class1.FooProperty));
  163. target.SetValue(Class1.FooProperty, "two", BindingPriority.Style);
  164. Assert.Equal("one", target.GetValue(Class1.FooProperty));
  165. target.SetValue(Class1.FooProperty, "three", BindingPriority.StyleTrigger);
  166. Assert.Equal("three", target.GetValue(Class1.FooProperty));
  167. }
  168. [Fact]
  169. public void SetValue_Style_Doesnt_Override_LocalValue()
  170. {
  171. Class1 target = new Class1();
  172. target.SetValue(Class1.FooProperty, "one", BindingPriority.LocalValue);
  173. Assert.Equal("one", target.GetValue(Class1.FooProperty));
  174. target.SetValue(Class1.FooProperty, "two", BindingPriority.Style);
  175. Assert.Equal("one", target.GetValue(Class1.FooProperty));
  176. }
  177. [Fact]
  178. public void SetValue_LocalValue_Overrides_Style()
  179. {
  180. Class1 target = new Class1();
  181. target.SetValue(Class1.FooProperty, "one", BindingPriority.Style);
  182. Assert.Equal("one", target.GetValue(Class1.FooProperty));
  183. target.SetValue(Class1.FooProperty, "two", BindingPriority.LocalValue);
  184. Assert.Equal("two", target.GetValue(Class1.FooProperty));
  185. }
  186. [Fact]
  187. public void Setting_UnsetValue_Reverts_To_Default_Value()
  188. {
  189. Class1 target = new Class1();
  190. target.SetValue(Class1.FooProperty, "newvalue");
  191. target.SetValue(Class1.FooProperty, AvaloniaProperty.UnsetValue);
  192. Assert.Equal("foodefault", target.GetValue(Class1.FooProperty));
  193. }
  194. [Fact]
  195. public void Setting_Object_Property_To_UnsetValue_Reverts_To_Default_Value()
  196. {
  197. Class1 target = new Class1();
  198. target.SetValue(Class1.FrankProperty, "newvalue");
  199. target.SetValue(Class1.FrankProperty, AvaloniaProperty.UnsetValue);
  200. Assert.Equal("Kups", target.GetValue(Class1.FrankProperty));
  201. }
  202. [Fact]
  203. public void Setting_Object_Property_To_DoNothing_Does_Nothing()
  204. {
  205. Class1 target = new Class1();
  206. target.SetValue(Class1.FrankProperty, "newvalue");
  207. target.SetValue(Class1.FrankProperty, BindingOperations.DoNothing);
  208. Assert.Equal("newvalue", target.GetValue(Class1.FrankProperty));
  209. }
  210. private class Class1 : AvaloniaObject
  211. {
  212. public static readonly StyledProperty<string> FooProperty =
  213. AvaloniaProperty.Register<Class1, string>("Foo", "foodefault");
  214. public static readonly StyledProperty<object> FrankProperty =
  215. AvaloniaProperty.Register<Class1, object>("Frank", "Kups");
  216. }
  217. private class Class2 : Class1
  218. {
  219. public static readonly StyledProperty<string> BarProperty =
  220. AvaloniaProperty.Register<Class2, string>("Bar", "bardefault");
  221. public static readonly StyledProperty<double> FlobProperty =
  222. AvaloniaProperty.Register<Class2, double>("Flob");
  223. public static readonly StyledProperty<double?> FredProperty =
  224. AvaloniaProperty.Register<Class2, double?>("Fred");
  225. public Class1 Parent
  226. {
  227. get { return (Class1)InheritanceParent; }
  228. set { InheritanceParent = value; }
  229. }
  230. }
  231. private class AttachedOwner
  232. {
  233. public static readonly AttachedProperty<string> AttachedProperty =
  234. AvaloniaProperty.RegisterAttached<AttachedOwner, Class2, string>("Attached");
  235. }
  236. private class ImplictDouble
  237. {
  238. public ImplictDouble(double value)
  239. {
  240. Value = value;
  241. }
  242. public double Value { get; }
  243. public static implicit operator double (ImplictDouble v)
  244. {
  245. return v.Value;
  246. }
  247. }
  248. }
  249. }