AvaloniaObjectTests_SetCurrentValue.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. using System;
  2. using Avalonia.Data;
  3. using Avalonia.Diagnostics;
  4. using Xunit;
  5. using Observable = Avalonia.Reactive.Observable;
  6. namespace Avalonia.Base.UnitTests
  7. {
  8. public class AvaloniaObjectTests_SetCurrentValue
  9. {
  10. [Fact]
  11. public void SetCurrentValue_Sets_Unset_Value()
  12. {
  13. var target = new Class1();
  14. target.SetCurrentValue(Class1.FooProperty, "newvalue");
  15. Assert.Equal("newvalue", target.GetValue(Class1.FooProperty));
  16. Assert.True(target.IsSet(Class1.FooProperty));
  17. Assert.Equal(BindingPriority.Unset, GetPriority(target, Class1.FooProperty));
  18. Assert.True(IsOverridden(target, Class1.FooProperty));
  19. }
  20. [Fact]
  21. public void SetCurrentValue_Sets_Unset_Value_Untyped()
  22. {
  23. var target = new Class1();
  24. target.SetCurrentValue((AvaloniaProperty)Class1.FooProperty, "newvalue");
  25. Assert.Equal("newvalue", target.GetValue(Class1.FooProperty));
  26. Assert.True(target.IsSet(Class1.FooProperty));
  27. Assert.Equal(BindingPriority.Unset, GetPriority(target, Class1.FooProperty));
  28. Assert.True(IsOverridden(target, Class1.FooProperty));
  29. }
  30. [Theory]
  31. [InlineData(BindingPriority.LocalValue)]
  32. [InlineData(BindingPriority.Style)]
  33. [InlineData(BindingPriority.Animation)]
  34. public void SetCurrentValue_Overrides_Existing_Value(BindingPriority priority)
  35. {
  36. var target = new Class1();
  37. target.SetValue(Class1.FooProperty, "oldvalue", priority);
  38. target.SetCurrentValue(Class1.FooProperty, "newvalue");
  39. Assert.Equal("newvalue", target.GetValue(Class1.FooProperty));
  40. Assert.True(target.IsSet(Class1.FooProperty));
  41. Assert.Equal(priority, GetPriority(target, Class1.FooProperty));
  42. Assert.True(IsOverridden(target, Class1.FooProperty));
  43. }
  44. [Fact]
  45. public void SetCurrentValue_Overrides_Inherited_Value()
  46. {
  47. var parent = new Class1();
  48. var target = new Class1 { InheritanceParent = parent };
  49. parent.SetValue(Class1.InheritedProperty, "inheritedvalue");
  50. target.SetCurrentValue(Class1.InheritedProperty, "newvalue");
  51. Assert.Equal("newvalue", target.GetValue(Class1.InheritedProperty));
  52. Assert.True(target.IsSet(Class1.InheritedProperty));
  53. Assert.Equal(BindingPriority.Unset, GetPriority(target, Class1.InheritedProperty));
  54. Assert.True(IsOverridden(target, Class1.InheritedProperty));
  55. }
  56. [Fact]
  57. public void SetCurrentValue_Is_Inherited()
  58. {
  59. var parent = new Class1();
  60. var target = new Class1 { InheritanceParent = parent };
  61. parent.SetCurrentValue(Class1.InheritedProperty, "newvalue");
  62. Assert.Equal("newvalue", target.GetValue(Class1.InheritedProperty));
  63. Assert.False(target.IsSet(Class1.FooProperty));
  64. Assert.Equal(BindingPriority.Inherited, GetPriority(target, Class1.InheritedProperty));
  65. Assert.False(IsOverridden(target, Class1.InheritedProperty));
  66. }
  67. [Fact]
  68. public void ClearValue_Clears_CurrentValue_With_Unset_Priority()
  69. {
  70. var target = new Class1();
  71. target.SetCurrentValue(Class1.FooProperty, "newvalue");
  72. target.ClearValue(Class1.FooProperty);
  73. Assert.Equal("foodefault", target.Foo);
  74. Assert.False(target.IsSet(Class1.FooProperty));
  75. Assert.False(IsOverridden(target, Class1.FooProperty));
  76. }
  77. [Fact]
  78. public void ClearValue_Clears_CurrentValue_With_Inherited_Priority()
  79. {
  80. var parent = new Class1();
  81. var target = new Class1 { InheritanceParent = parent };
  82. parent.SetValue(Class1.InheritedProperty, "inheritedvalue");
  83. target.SetCurrentValue(Class1.InheritedProperty, "newvalue");
  84. target.ClearValue(Class1.InheritedProperty);
  85. Assert.Equal("inheritedvalue", target.Inherited);
  86. Assert.False(target.IsSet(Class1.FooProperty));
  87. Assert.False(IsOverridden(target, Class1.FooProperty));
  88. }
  89. [Fact]
  90. public void ClearValue_Clears_CurrentValue_With_LocalValue_Priority()
  91. {
  92. var target = new Class1();
  93. target.SetValue(Class1.FooProperty, "localvalue");
  94. target.SetCurrentValue(Class1.FooProperty, "newvalue");
  95. target.ClearValue(Class1.FooProperty);
  96. Assert.Equal("foodefault", target.Foo);
  97. Assert.False(target.IsSet(Class1.FooProperty));
  98. Assert.False(IsOverridden(target, Class1.FooProperty));
  99. }
  100. [Fact]
  101. public void ClearValue_Clears_CurrentValue_With_Style_Priority()
  102. {
  103. var target = new Class1();
  104. target.SetValue(Class1.FooProperty, "stylevalue", BindingPriority.Style);
  105. target.SetCurrentValue(Class1.FooProperty, "newvalue");
  106. target.ClearValue(Class1.FooProperty);
  107. Assert.Equal("stylevalue", target.Foo);
  108. Assert.True(target.IsSet(Class1.FooProperty));
  109. Assert.False(IsOverridden(target, Class1.FooProperty));
  110. }
  111. [Fact]
  112. public void SetCurrentValue_Can_Be_Coerced()
  113. {
  114. var target = new Class1();
  115. target.SetCurrentValue(Class1.CoercedProperty, 60);
  116. Assert.Equal(60, target.GetValue(Class1.CoercedProperty));
  117. target.CoerceMax = 50;
  118. target.CoerceValue(Class1.CoercedProperty);
  119. Assert.Equal(50, target.GetValue(Class1.CoercedProperty));
  120. target.CoerceMax = 100;
  121. target.CoerceValue(Class1.CoercedProperty);
  122. Assert.Equal(60, target.GetValue(Class1.CoercedProperty));
  123. }
  124. [Fact]
  125. public void SetCurrentValue_Unset_Clears_CurrentValue()
  126. {
  127. var target = new Class1();
  128. target.SetCurrentValue(Class1.FooProperty, "newvalue");
  129. target.SetCurrentValue(Class1.FooProperty, AvaloniaProperty.UnsetValue);
  130. Assert.Equal("foodefault", target.Foo);
  131. Assert.False(target.IsSet(Class1.FooProperty));
  132. Assert.False(IsOverridden(target, Class1.FooProperty));
  133. }
  134. [Theory]
  135. [InlineData(BindingPriority.LocalValue)]
  136. [InlineData(BindingPriority.Style)]
  137. [InlineData(BindingPriority.Animation)]
  138. public void SetValue_Overrides_CurrentValue_With_Unset_Priority(BindingPriority priority)
  139. {
  140. var target = new Class1();
  141. target.SetCurrentValue(Class1.FooProperty, "current");
  142. target.SetValue(Class1.FooProperty, "setvalue", priority);
  143. Assert.Equal("setvalue", target.Foo);
  144. Assert.True(target.IsSet(Class1.FooProperty));
  145. Assert.Equal(priority, GetPriority(target, Class1.FooProperty));
  146. Assert.False(IsOverridden(target, Class1.FooProperty));
  147. }
  148. [Fact]
  149. public void Animation_Value_Overrides_CurrentValue_With_LocalValue_Priority()
  150. {
  151. var target = new Class1();
  152. target.SetValue(Class1.FooProperty, "localvalue");
  153. target.SetCurrentValue(Class1.FooProperty, "current");
  154. target.SetValue(Class1.FooProperty, "setvalue", BindingPriority.Animation);
  155. Assert.Equal("setvalue", target.Foo);
  156. Assert.True(target.IsSet(Class1.FooProperty));
  157. Assert.Equal(BindingPriority.Animation, GetPriority(target, Class1.FooProperty));
  158. Assert.False(IsOverridden(target, Class1.FooProperty));
  159. }
  160. [Fact]
  161. public void StyleTrigger_Value_Overrides_CurrentValue_With_Style_Priority()
  162. {
  163. var target = new Class1();
  164. target.SetValue(Class1.FooProperty, "style", BindingPriority.Style);
  165. target.SetCurrentValue(Class1.FooProperty, "current");
  166. target.SetValue(Class1.FooProperty, "setvalue", BindingPriority.StyleTrigger);
  167. Assert.Equal("setvalue", target.Foo);
  168. Assert.True(target.IsSet(Class1.FooProperty));
  169. Assert.Equal(BindingPriority.StyleTrigger, GetPriority(target, Class1.FooProperty));
  170. Assert.False(IsOverridden(target, Class1.FooProperty));
  171. }
  172. [Theory]
  173. [InlineData(BindingPriority.LocalValue)]
  174. [InlineData(BindingPriority.Style)]
  175. [InlineData(BindingPriority.Animation)]
  176. public void Binding_Overrides_CurrentValue_With_Unset_Priority(BindingPriority priority)
  177. {
  178. var target = new Class1();
  179. target.SetCurrentValue(Class1.FooProperty, "current");
  180. var s = target.Bind(Class1.FooProperty, Observable.SingleValue("binding"), priority);
  181. Assert.Equal("binding", target.Foo);
  182. Assert.True(target.IsSet(Class1.FooProperty));
  183. Assert.Equal(priority, GetPriority(target, Class1.FooProperty));
  184. Assert.False(IsOverridden(target, Class1.FooProperty));
  185. s.Dispose();
  186. Assert.Equal("foodefault", target.Foo);
  187. }
  188. [Fact]
  189. public void Animation_Binding_Overrides_CurrentValue_With_LocalValue_Priority()
  190. {
  191. var target = new Class1();
  192. target.SetValue(Class1.FooProperty, "localvalue");
  193. target.SetCurrentValue(Class1.FooProperty, "current");
  194. var s = target.Bind(Class1.FooProperty, Observable.SingleValue("binding"), BindingPriority.Animation);
  195. Assert.Equal("binding", target.Foo);
  196. Assert.True(target.IsSet(Class1.FooProperty));
  197. Assert.Equal(BindingPriority.Animation, GetPriority(target, Class1.FooProperty));
  198. Assert.False(IsOverridden(target, Class1.FooProperty));
  199. s.Dispose();
  200. Assert.Equal("current", target.Foo);
  201. }
  202. [Fact]
  203. public void StyleTrigger_Binding_Overrides_CurrentValue_With_Style_Priority()
  204. {
  205. var target = new Class1();
  206. target.SetValue(Class1.FooProperty, "style", BindingPriority.Style);
  207. target.SetCurrentValue(Class1.FooProperty, "current");
  208. var s = target.Bind(Class1.FooProperty, Observable.SingleValue("binding"), BindingPriority.StyleTrigger);
  209. Assert.Equal("binding", target.Foo);
  210. Assert.True(target.IsSet(Class1.FooProperty));
  211. Assert.Equal(BindingPriority.StyleTrigger, GetPriority(target, Class1.FooProperty));
  212. Assert.False(IsOverridden(target, Class1.FooProperty));
  213. s.Dispose();
  214. Assert.Equal("style", target.Foo);
  215. }
  216. private BindingPriority GetPriority(AvaloniaObject target, AvaloniaProperty property)
  217. {
  218. return target.GetDiagnostic(property).Priority;
  219. }
  220. private bool IsOverridden(AvaloniaObject target, AvaloniaProperty property)
  221. {
  222. return target.GetDiagnostic(property).IsOverriddenCurrentValue;
  223. }
  224. private class Class1 : AvaloniaObject
  225. {
  226. public static readonly StyledProperty<string> FooProperty =
  227. AvaloniaProperty.Register<Class1, string>(nameof(Foo), "foodefault");
  228. public static readonly StyledProperty<string> InheritedProperty =
  229. AvaloniaProperty.Register<Class1, string>(nameof(Inherited), "inheriteddefault", inherits: true);
  230. public static readonly StyledProperty<double> CoercedProperty =
  231. AvaloniaProperty.Register<Class1, double>(nameof(Coerced), coerce: Coerce);
  232. public string Foo => GetValue(FooProperty);
  233. public string Inherited => GetValue(InheritedProperty);
  234. public double Coerced => GetValue(CoercedProperty);
  235. public double CoerceMax { get; set; } = 100;
  236. private static double Coerce(AvaloniaObject sender, double value)
  237. {
  238. return Math.Min(value, ((Class1)sender).CoerceMax);
  239. }
  240. }
  241. }
  242. }