AvaloniaObjectTests_SetValue.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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 SetValue_Animation_Overrides_LocalValue()
  188. {
  189. Class1 target = new Class1();
  190. target.SetValue(Class1.FooProperty, "one", BindingPriority.LocalValue);
  191. Assert.Equal("one", target.GetValue(Class1.FooProperty));
  192. target.SetValue(Class1.FooProperty, "two", BindingPriority.Animation);
  193. Assert.Equal("two", target.GetValue(Class1.FooProperty));
  194. }
  195. [Fact]
  196. public void Setting_UnsetValue_Reverts_To_Default_Value()
  197. {
  198. Class1 target = new Class1();
  199. target.SetValue(Class1.FooProperty, "newvalue");
  200. target.SetValue(Class1.FooProperty, AvaloniaProperty.UnsetValue);
  201. Assert.Equal("foodefault", target.GetValue(Class1.FooProperty));
  202. }
  203. [Fact]
  204. public void Setting_Object_Property_To_UnsetValue_Reverts_To_Default_Value()
  205. {
  206. Class1 target = new Class1();
  207. target.SetValue(Class1.FrankProperty, "newvalue");
  208. target.SetValue(Class1.FrankProperty, AvaloniaProperty.UnsetValue);
  209. Assert.Equal("Kups", target.GetValue(Class1.FrankProperty));
  210. }
  211. [Fact]
  212. public void Setting_Object_Property_To_DoNothing_Does_Nothing()
  213. {
  214. Class1 target = new Class1();
  215. target.SetValue(Class1.FrankProperty, "newvalue");
  216. target.SetValue(Class1.FrankProperty, BindingOperations.DoNothing);
  217. Assert.Equal("newvalue", target.GetValue(Class1.FrankProperty));
  218. }
  219. [Fact]
  220. public void Disposing_Style_SetValue_Reverts_To_DefaultValue()
  221. {
  222. Class1 target = new Class1();
  223. var d = target.SetValue(Class1.FooProperty, "foo", BindingPriority.Style);
  224. d.Dispose();
  225. Assert.Equal("foodefault", target.GetValue(Class1.FooProperty));
  226. }
  227. [Fact]
  228. public void Disposing_Style_SetValue_Reverts_To_Previous_Style_Value()
  229. {
  230. Class1 target = new Class1();
  231. target.SetValue(Class1.FooProperty, "foo", BindingPriority.Style);
  232. var d = target.SetValue(Class1.FooProperty, "bar", BindingPriority.Style);
  233. d.Dispose();
  234. Assert.Equal("foo", target.GetValue(Class1.FooProperty));
  235. }
  236. [Fact]
  237. public void Disposing_Animation_SetValue_Reverts_To_Previous_Local_Value()
  238. {
  239. Class1 target = new Class1();
  240. target.SetValue(Class1.FooProperty, "foo", BindingPriority.LocalValue);
  241. var d = target.SetValue(Class1.FooProperty, "bar", BindingPriority.Animation);
  242. d.Dispose();
  243. Assert.Equal("foo", target.GetValue(Class1.FooProperty));
  244. }
  245. private class Class1 : AvaloniaObject
  246. {
  247. public static readonly StyledProperty<string> FooProperty =
  248. AvaloniaProperty.Register<Class1, string>("Foo", "foodefault");
  249. public static readonly StyledProperty<object> FrankProperty =
  250. AvaloniaProperty.Register<Class1, object>("Frank", "Kups");
  251. }
  252. private class Class2 : Class1
  253. {
  254. public static readonly StyledProperty<string> BarProperty =
  255. AvaloniaProperty.Register<Class2, string>("Bar", "bardefault");
  256. public static readonly StyledProperty<double> FlobProperty =
  257. AvaloniaProperty.Register<Class2, double>("Flob");
  258. public static readonly StyledProperty<double?> FredProperty =
  259. AvaloniaProperty.Register<Class2, double?>("Fred");
  260. public Class1 Parent
  261. {
  262. get { return (Class1)InheritanceParent; }
  263. set { InheritanceParent = value; }
  264. }
  265. }
  266. private class AttachedOwner
  267. {
  268. public static readonly AttachedProperty<string> AttachedProperty =
  269. AvaloniaProperty.RegisterAttached<AttachedOwner, Class2, string>("Attached");
  270. }
  271. private class ImplictDouble
  272. {
  273. public ImplictDouble(double value)
  274. {
  275. Value = value;
  276. }
  277. public double Value { get; }
  278. public static implicit operator double (ImplictDouble v)
  279. {
  280. return v.Value;
  281. }
  282. }
  283. }
  284. }