AvaloniaObjectTests_Coercion.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Reactive.Subjects;
  4. using Avalonia.Data;
  5. using Xunit;
  6. namespace Avalonia.Base.UnitTests
  7. {
  8. public class AvaloniaObjectTests_Coercion
  9. {
  10. [Fact]
  11. public void Coerces_Set_Value()
  12. {
  13. var target = new Class1();
  14. target.Foo = 150;
  15. Assert.Equal(100, target.Foo);
  16. }
  17. [Fact]
  18. public void Coerces_Set_Value_Attached()
  19. {
  20. var target = new Class1();
  21. target.SetValue(Class1.AttachedProperty, 150);
  22. Assert.Equal(100, target.GetValue(Class1.AttachedProperty));
  23. }
  24. [Fact]
  25. public void Coerces_Bound_Value()
  26. {
  27. var target = new Class1();
  28. var source = new Subject<BindingValue<int>>();
  29. target.Bind(Class1.FooProperty, source);
  30. source.OnNext(150);
  31. Assert.Equal(100, target.Foo);
  32. }
  33. [Fact]
  34. public void CoerceValue_Updates_Value()
  35. {
  36. var target = new Class1 { Foo = 99 };
  37. Assert.Equal(99, target.Foo);
  38. target.MaxFoo = 50;
  39. target.CoerceValue(Class1.FooProperty);
  40. Assert.Equal(50, target.Foo);
  41. }
  42. [Fact]
  43. public void CoerceValue_Updates_Base_Value()
  44. {
  45. var target = new Class1 { Foo = 99 };
  46. target.SetValue(Class1.FooProperty, 88, BindingPriority.Animation);
  47. Assert.Equal(88, target.Foo);
  48. Assert.Equal(99, target.GetBaseValue(Class1.FooProperty));
  49. target.MaxFoo = 50;
  50. target.CoerceValue(Class1.FooProperty);
  51. Assert.Equal(50, target.Foo);
  52. Assert.Equal(50, target.GetBaseValue(Class1.FooProperty));
  53. }
  54. [Fact]
  55. public void CoerceValue_Raises_PropertyChanged()
  56. {
  57. var target = new Class1 { Foo = 99 };
  58. var raised = 0;
  59. target.PropertyChanged += (s, e) =>
  60. {
  61. Assert.Equal(Class1.FooProperty, e.Property);
  62. Assert.Equal(99, e.OldValue);
  63. Assert.Equal(50, e.NewValue);
  64. Assert.Equal(BindingPriority.LocalValue, e.Priority);
  65. ++raised;
  66. };
  67. Assert.Equal(99, target.Foo);
  68. target.MaxFoo = 50;
  69. target.CoerceValue(Class1.FooProperty);
  70. Assert.Equal(50, target.Foo);
  71. Assert.Equal(1, raised);
  72. }
  73. [Fact]
  74. public void CoerceValue_Raises_PropertyChangedCore_For_Base_Value()
  75. {
  76. var target = new Class1 { Foo = 99 };
  77. target.SetValue(Class1.FooProperty, 88, BindingPriority.Animation);
  78. Assert.Equal(88, target.Foo);
  79. Assert.Equal(99, target.GetBaseValue(Class1.FooProperty));
  80. target.MaxFoo = 50;
  81. target.CoreChanges.Clear();
  82. target.CoerceValue(Class1.FooProperty);
  83. Assert.Equal(2, target.CoreChanges.Count);
  84. }
  85. [Fact]
  86. public void Coerced_Value_Can_Be_Restored_If_Limit_Changed()
  87. {
  88. var target = new Class1();
  89. target.Foo = 150;
  90. Assert.Equal(100, target.Foo);
  91. target.MaxFoo = 200;
  92. target.CoerceValue(Class1.FooProperty);
  93. Assert.Equal(150, target.Foo);
  94. }
  95. [Fact]
  96. public void Coerced_Value_Can_Be_Restored_From_Previously_Active_Binding()
  97. {
  98. var target = new Class1();
  99. var source1 = new Subject<BindingValue<int>>();
  100. var source2 = new Subject<BindingValue<int>>();
  101. target.Bind(Class1.FooProperty, source1, BindingPriority.Style);
  102. source1.OnNext(150);
  103. target.Bind(Class1.FooProperty, source2);
  104. source2.OnNext(160);
  105. Assert.Equal(100, target.Foo);
  106. target.MaxFoo = 200;
  107. source2.OnCompleted();
  108. Assert.Equal(150, target.Foo);
  109. }
  110. [Fact]
  111. public void CoerceValue_Updates_Inherited_Value()
  112. {
  113. var parent = new Class1 { Inherited = 99 };
  114. var child = new AvaloniaObject { InheritanceParent = parent };
  115. var raised = 0;
  116. child.InheritanceParent = parent;
  117. child.PropertyChanged += (s, e) =>
  118. {
  119. Assert.Equal(Class1.InheritedProperty, e.Property);
  120. Assert.Equal(99, e.OldValue);
  121. Assert.Equal(50, e.NewValue);
  122. Assert.Equal(BindingPriority.Inherited, e.Priority);
  123. ++raised;
  124. };
  125. Assert.Equal(99, child.GetValue(Class1.InheritedProperty));
  126. parent.MaxFoo = 50;
  127. parent.CoerceValue(Class1.InheritedProperty);
  128. Assert.Equal(50, child.GetValue(Class1.InheritedProperty));
  129. Assert.Equal(1, raised);
  130. }
  131. [Fact]
  132. public void Coercion_Can_Be_Overridden()
  133. {
  134. var target = new Class2();
  135. target.Foo = 150;
  136. Assert.Equal(-150, target.Foo);
  137. }
  138. private class Class1 : AvaloniaObject
  139. {
  140. public static readonly StyledProperty<int> FooProperty =
  141. AvaloniaProperty.Register<Class1, int>(
  142. "Qux",
  143. defaultValue: 11,
  144. coerce: CoerceFoo);
  145. public static readonly AttachedProperty<int> AttachedProperty =
  146. AvaloniaProperty.RegisterAttached<Class1, Class1, int>(
  147. "Attached",
  148. defaultValue: 11,
  149. coerce: CoerceFoo);
  150. public static readonly StyledProperty<int> InheritedProperty =
  151. AvaloniaProperty.RegisterAttached<Class1, Class1, int>(
  152. "Attached",
  153. defaultValue: 11,
  154. inherits: true,
  155. coerce: CoerceFoo);
  156. public int Foo
  157. {
  158. get => GetValue(FooProperty);
  159. set => SetValue(FooProperty, value);
  160. }
  161. public int Inherited
  162. {
  163. get => GetValue(InheritedProperty);
  164. set => SetValue(InheritedProperty, value);
  165. }
  166. public int MaxFoo { get; set; } = 100;
  167. public List<AvaloniaPropertyChangedEventArgs> CoreChanges { get; } = new();
  168. public static int CoerceFoo(AvaloniaObject instance, int value)
  169. {
  170. return Math.Min(((Class1)instance).MaxFoo, value);
  171. }
  172. protected override void OnPropertyChangedCore(AvaloniaPropertyChangedEventArgs change)
  173. {
  174. CoreChanges.Add(Clone(change));
  175. base.OnPropertyChangedCore(change);
  176. }
  177. private static AvaloniaPropertyChangedEventArgs Clone(AvaloniaPropertyChangedEventArgs change)
  178. {
  179. var e = (AvaloniaPropertyChangedEventArgs<int>)change;
  180. return new AvaloniaPropertyChangedEventArgs<int>(
  181. change.Sender,
  182. e.Property,
  183. e.OldValue,
  184. e.NewValue,
  185. change.Priority,
  186. change.IsEffectiveValueChange);
  187. }
  188. }
  189. private class Class2 : AvaloniaObject
  190. {
  191. public static readonly StyledProperty<int> FooProperty =
  192. Class1.FooProperty.AddOwner<Class2>();
  193. static Class2()
  194. {
  195. FooProperty.OverrideMetadata<Class2>(
  196. new StyledPropertyMetadata<int>(
  197. coerce: CoerceFoo));
  198. }
  199. public int Foo
  200. {
  201. get => GetValue(FooProperty);
  202. set => SetValue(FooProperty, value);
  203. }
  204. public static int CoerceFoo(AvaloniaObject instance, int value)
  205. {
  206. return -value;
  207. }
  208. }
  209. }
  210. }