AvaloniaObjectTests_SetValue.cs 11 KB

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