AvaloniaObjectTests_SetCurrentValue.cs 10 KB

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