AvaloniaObjectTests_SetValue.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. using System;
  2. using Avalonia.Data;
  3. using Xunit;
  4. namespace Avalonia.Base.UnitTests
  5. {
  6. public class AvaloniaObjectTests_SetValue
  7. {
  8. [Fact]
  9. public void ClearValue_Clears_Value()
  10. {
  11. Class1 target = new Class1();
  12. target.SetValue(Class1.FooProperty, "newvalue");
  13. target.ClearValue(Class1.FooProperty);
  14. Assert.Equal("foodefault", target.GetValue(Class1.FooProperty));
  15. }
  16. [Fact]
  17. public void ClearValue_Resets_Value_To_Style_value()
  18. {
  19. Class1 target = new Class1();
  20. target.SetValue(Class1.FooProperty, "style", BindingPriority.Style);
  21. target.SetValue(Class1.FooProperty, "local");
  22. Assert.Equal("local", target.GetValue(Class1.FooProperty));
  23. target.ClearValue(Class1.FooProperty);
  24. Assert.Equal("style", target.GetValue(Class1.FooProperty));
  25. }
  26. [Fact]
  27. public void ClearValue_Raises_PropertyChanged()
  28. {
  29. Class1 target = new Class1();
  30. var raised = 0;
  31. target.SetValue(Class1.FooProperty, "newvalue");
  32. target.PropertyChanged += (s, e) =>
  33. {
  34. Assert.Same(target, s);
  35. Assert.Equal(BindingPriority.Unset, e.Priority);
  36. Assert.Equal(Class1.FooProperty, e.Property);
  37. Assert.Equal("newvalue", (string)e.OldValue);
  38. Assert.Equal("foodefault", (string)e.NewValue);
  39. ++raised;
  40. };
  41. target.ClearValue(Class1.FooProperty);
  42. Assert.Equal(1, raised);
  43. }
  44. [Fact]
  45. public void IsSet_Returns_False_For_Unset_Property()
  46. {
  47. var target = new Class1();
  48. Assert.False(target.IsSet(Class1.FooProperty));
  49. }
  50. [Fact]
  51. public void IsSet_Returns_False_For_Set_Property()
  52. {
  53. var target = new Class1();
  54. target.SetValue(Class1.FooProperty, "foo");
  55. Assert.True(target.IsSet(Class1.FooProperty));
  56. }
  57. [Fact]
  58. public void IsSet_Returns_False_For_Cleared_Property()
  59. {
  60. var target = new Class1();
  61. target.SetValue(Class1.FooProperty, "foo");
  62. target.SetValue(Class1.FooProperty, AvaloniaProperty.UnsetValue);
  63. Assert.False(target.IsSet(Class1.FooProperty));
  64. }
  65. [Fact]
  66. public void SetValue_Sets_Value()
  67. {
  68. Class1 target = new Class1();
  69. target.SetValue(Class1.FooProperty, "newvalue");
  70. Assert.Equal("newvalue", target.GetValue(Class1.FooProperty));
  71. }
  72. [Fact]
  73. public void SetValue_Sets_Attached_Value()
  74. {
  75. Class2 target = new Class2();
  76. target.SetValue(AttachedOwner.AttachedProperty, "newvalue");
  77. Assert.Equal("newvalue", target.GetValue(AttachedOwner.AttachedProperty));
  78. }
  79. [Fact]
  80. public void SetValue_Raises_PropertyChanged()
  81. {
  82. Class1 target = new Class1();
  83. bool raised = false;
  84. target.PropertyChanged += (s, e) =>
  85. {
  86. raised = s == target &&
  87. e.Property == Class1.FooProperty &&
  88. (string)e.OldValue == "foodefault" &&
  89. (string)e.NewValue == "newvalue";
  90. };
  91. target.SetValue(Class1.FooProperty, "newvalue");
  92. Assert.True(raised);
  93. }
  94. [Fact]
  95. public void SetValue_Style_Priority_Raises_PropertyChanged()
  96. {
  97. Class1 target = new Class1();
  98. bool raised = false;
  99. target.PropertyChanged += (s, e) =>
  100. {
  101. raised = s == target &&
  102. e.Property == Class1.FooProperty &&
  103. (string)e.OldValue == "foodefault" &&
  104. (string)e.NewValue == "newvalue";
  105. };
  106. target.SetValue(Class1.FooProperty, "newvalue", BindingPriority.Style);
  107. Assert.True(raised);
  108. }
  109. [Fact]
  110. public void SetValue_Doesnt_Raise_PropertyChanged_If_Value_Not_Changed()
  111. {
  112. Class1 target = new Class1();
  113. bool raised = false;
  114. target.SetValue(Class1.FooProperty, "bar");
  115. target.PropertyChanged += (s, e) =>
  116. {
  117. raised = true;
  118. };
  119. target.SetValue(Class1.FooProperty, "bar");
  120. Assert.False(raised);
  121. }
  122. [Fact]
  123. public void SetValue_Doesnt_Raise_PropertyChanged_If_Value_Not_Changed_From_Default()
  124. {
  125. Class1 target = new Class1();
  126. bool raised = false;
  127. target.PropertyChanged += (s, e) =>
  128. {
  129. raised = true;
  130. };
  131. target.SetValue(Class1.FooProperty, "foodefault");
  132. Assert.False(raised);
  133. }
  134. [Fact]
  135. public void SetValue_Allows_Setting_Unregistered_Property()
  136. {
  137. Class1 target = new Class1();
  138. Assert.False(AvaloniaPropertyRegistry.Instance.IsRegistered(target, Class2.BarProperty));
  139. target.SetValue(Class2.BarProperty, "bar");
  140. Assert.Equal("bar", target.GetValue(Class2.BarProperty));
  141. }
  142. [Fact]
  143. public void SetValue_Allows_Setting_Unregistered_Attached_Property()
  144. {
  145. Class1 target = new Class1();
  146. Assert.False(AvaloniaPropertyRegistry.Instance.IsRegistered(target, AttachedOwner.AttachedProperty));
  147. target.SetValue(AttachedOwner.AttachedProperty, "bar");
  148. Assert.Equal("bar", target.GetValue(AttachedOwner.AttachedProperty));
  149. }
  150. [Fact]
  151. public void SetValue_Throws_Exception_For_Invalid_Value_Type()
  152. {
  153. Class1 target = new Class1();
  154. Assert.Throws<ArgumentException>(() =>
  155. {
  156. target.SetValue(Class1.FooProperty, 123);
  157. });
  158. }
  159. [Fact]
  160. public void SetValue_Of_Integer_On_Double_Property_Works()
  161. {
  162. Class2 target = new Class2();
  163. target.SetValue((AvaloniaProperty)Class2.FlobProperty, 4);
  164. var value = target.GetValue(Class2.FlobProperty);
  165. Assert.IsType<double>(value);
  166. Assert.Equal(4, value);
  167. }
  168. [Fact]
  169. public void SetValue_Respects_Implicit_Conversions()
  170. {
  171. Class2 target = new Class2();
  172. target.SetValue((AvaloniaProperty)Class2.FlobProperty, new ImplicitDouble(4));
  173. var value = target.GetValue(Class2.FlobProperty);
  174. Assert.IsType<double>(value);
  175. Assert.Equal(4, value);
  176. }
  177. [Fact]
  178. public void SetValue_Can_Convert_To_Nullable()
  179. {
  180. Class2 target = new Class2();
  181. target.SetValue((AvaloniaProperty)Class2.FredProperty, 4.0);
  182. var value = target.GetValue(Class2.FredProperty);
  183. Assert.IsType<double>(value);
  184. Assert.Equal(4, value);
  185. }
  186. [Fact]
  187. public void SetValue_Respects_Priority()
  188. {
  189. Class1 target = new Class1();
  190. target.SetValue(Class1.FooProperty, "one", BindingPriority.Template);
  191. Assert.Equal("one", target.GetValue(Class1.FooProperty));
  192. target.SetValue(Class1.FooProperty, "two", BindingPriority.Style);
  193. Assert.Equal("one", target.GetValue(Class1.FooProperty));
  194. target.SetValue(Class1.FooProperty, "three", BindingPriority.StyleTrigger);
  195. Assert.Equal("three", target.GetValue(Class1.FooProperty));
  196. }
  197. [Fact]
  198. public void SetValue_Style_Doesnt_Override_LocalValue()
  199. {
  200. Class1 target = new Class1();
  201. target.SetValue(Class1.FooProperty, "one", BindingPriority.LocalValue);
  202. Assert.Equal("one", target.GetValue(Class1.FooProperty));
  203. target.SetValue(Class1.FooProperty, "two", BindingPriority.Style);
  204. Assert.Equal("one", target.GetValue(Class1.FooProperty));
  205. }
  206. [Fact]
  207. public void SetValue_LocalValue_Overrides_Style()
  208. {
  209. Class1 target = new Class1();
  210. target.SetValue(Class1.FooProperty, "one", BindingPriority.Style);
  211. Assert.Equal("one", target.GetValue(Class1.FooProperty));
  212. target.SetValue(Class1.FooProperty, "two", BindingPriority.LocalValue);
  213. Assert.Equal("two", target.GetValue(Class1.FooProperty));
  214. }
  215. [Fact]
  216. public void SetValue_Animation_Overrides_LocalValue()
  217. {
  218. Class1 target = new Class1();
  219. target.SetValue(Class1.FooProperty, "one", BindingPriority.LocalValue);
  220. Assert.Equal("one", target.GetValue(Class1.FooProperty));
  221. target.SetValue(Class1.FooProperty, "two", BindingPriority.Animation);
  222. Assert.Equal("two", target.GetValue(Class1.FooProperty));
  223. }
  224. [Fact]
  225. public void Setting_UnsetValue_Reverts_To_Default_Value()
  226. {
  227. Class1 target = new Class1();
  228. target.SetValue(Class1.FooProperty, "newvalue");
  229. target.SetValue(Class1.FooProperty, AvaloniaProperty.UnsetValue);
  230. Assert.Equal("foodefault", target.GetValue(Class1.FooProperty));
  231. }
  232. [Fact]
  233. public void Setting_Object_Property_To_UnsetValue_Reverts_To_Default_Value()
  234. {
  235. Class1 target = new Class1();
  236. target.SetValue(Class1.FrankProperty, "newvalue");
  237. target.SetValue(Class1.FrankProperty, AvaloniaProperty.UnsetValue);
  238. Assert.Equal("Kups", target.GetValue(Class1.FrankProperty));
  239. }
  240. [Fact]
  241. public void Setting_Object_Property_To_DoNothing_Does_Nothing()
  242. {
  243. Class1 target = new Class1();
  244. target.SetValue(Class1.FrankProperty, "newvalue");
  245. target.SetValue(Class1.FrankProperty, BindingOperations.DoNothing);
  246. Assert.Equal("newvalue", target.GetValue(Class1.FrankProperty));
  247. }
  248. [Fact]
  249. public void Disposing_Style_SetValue_Reverts_To_DefaultValue()
  250. {
  251. Class1 target = new Class1();
  252. var d = target.SetValue(Class1.FooProperty, "foo", BindingPriority.Style);
  253. d.Dispose();
  254. Assert.Equal("foodefault", target.GetValue(Class1.FooProperty));
  255. }
  256. [Fact]
  257. public void Disposing_Style_SetValue_Reverts_To_Previous_Style_Value()
  258. {
  259. Class1 target = new Class1();
  260. target.SetValue(Class1.FooProperty, "foo", BindingPriority.Style);
  261. var d = target.SetValue(Class1.FooProperty, "bar", BindingPriority.Style);
  262. d.Dispose();
  263. Assert.Equal("foo", target.GetValue(Class1.FooProperty));
  264. }
  265. [Fact]
  266. public void Disposing_Animation_SetValue_Reverts_To_Previous_Local_Value()
  267. {
  268. Class1 target = new Class1();
  269. target.SetValue(Class1.FooProperty, "foo", BindingPriority.LocalValue);
  270. var d = target.SetValue(Class1.FooProperty, "bar", BindingPriority.Animation);
  271. d.Dispose();
  272. Assert.Equal("foo", target.GetValue(Class1.FooProperty));
  273. }
  274. private class Class1 : AvaloniaObject
  275. {
  276. public static readonly StyledProperty<string> FooProperty =
  277. AvaloniaProperty.Register<Class1, string>("Foo", "foodefault");
  278. public static readonly StyledProperty<object> FrankProperty =
  279. AvaloniaProperty.Register<Class1, object>("Frank", "Kups");
  280. }
  281. private class Class2 : Class1
  282. {
  283. public static readonly StyledProperty<string> BarProperty =
  284. AvaloniaProperty.Register<Class2, string>("Bar", "bardefault");
  285. public static readonly StyledProperty<double> FlobProperty =
  286. AvaloniaProperty.Register<Class2, double>("Flob");
  287. public static readonly StyledProperty<double?> FredProperty =
  288. AvaloniaProperty.Register<Class2, double?>("Fred");
  289. public Class1 Parent
  290. {
  291. get { return (Class1)InheritanceParent; }
  292. set { InheritanceParent = value; }
  293. }
  294. }
  295. private class AttachedOwner
  296. {
  297. public static readonly AttachedProperty<string> AttachedProperty =
  298. AvaloniaProperty.RegisterAttached<AttachedOwner, Class2, string>("Attached");
  299. }
  300. private class ImplicitDouble
  301. {
  302. public ImplicitDouble(double value)
  303. {
  304. Value = value;
  305. }
  306. public double Value { get; }
  307. public static implicit operator double (ImplicitDouble v)
  308. {
  309. return v.Value;
  310. }
  311. }
  312. }
  313. }