AvaloniaObjectTests_DataValidation.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Reactive.Subjects;
  4. using Avalonia.Data;
  5. using Avalonia.UnitTests;
  6. using Xunit;
  7. #nullable enable
  8. namespace Avalonia.Base.UnitTests
  9. {
  10. public class AvaloniaObjectTests_DataValidation
  11. {
  12. public abstract class TestBase<T>
  13. where T : AvaloniaProperty<int>
  14. {
  15. [Fact]
  16. public void Binding_Non_Validated_Property_Does_Not_Call_UpdateDataValidation()
  17. {
  18. var target = new Class1();
  19. var source = new Subject<BindingValue<int>>();
  20. var property = GetNonValidatedProperty();
  21. target.Bind(property, source);
  22. source.OnNext(6);
  23. source.OnNext(BindingValue<int>.BindingError(new Exception()));
  24. source.OnNext(BindingValue<int>.DataValidationError(new Exception()));
  25. source.OnNext(6);
  26. Assert.Empty(target.Notifications);
  27. }
  28. [Fact]
  29. public void Binding_Validated_Property_Calls_UpdateDataValidation()
  30. {
  31. var target = new Class1();
  32. var source = new Subject<BindingValue<int>>();
  33. var property = GetProperty();
  34. var error1 = new Exception();
  35. var error2 = new Exception();
  36. target.Bind(property, source);
  37. source.OnNext(6);
  38. source.OnNext(BindingValue<int>.DataValidationError(error1));
  39. source.OnNext(BindingValue<int>.BindingError(error2));
  40. source.OnNext(7);
  41. Assert.Equal(new Notification[]
  42. {
  43. new(BindingValueType.Value, 6, null),
  44. new(BindingValueType.DataValidationError, 6, error1),
  45. new(BindingValueType.BindingError, 0, error2),
  46. new(BindingValueType.Value, 7, null),
  47. }, target.Notifications);
  48. }
  49. [Fact]
  50. public void Binding_Validated_Property_Calls_UpdateDataValidation_Untyped()
  51. {
  52. var target = new Class1();
  53. var source = new Subject<object>();
  54. var property = GetProperty();
  55. var error1 = new Exception();
  56. var error2 = new Exception();
  57. target.Bind(property, source);
  58. source.OnNext(6);
  59. source.OnNext(new BindingNotification(error1, BindingErrorType.DataValidationError));
  60. source.OnNext(new BindingNotification(error2, BindingErrorType.Error));
  61. source.OnNext(7);
  62. Assert.Equal(new Notification[]
  63. {
  64. new(BindingValueType.Value, 6, null),
  65. new(BindingValueType.DataValidationError, 6, error1),
  66. new(BindingValueType.BindingError, 0, error2),
  67. new(BindingValueType.Value, 7, null),
  68. }, target.Notifications);
  69. }
  70. [Fact]
  71. public void Binding_Overridden_Validated_Property_Calls_UpdateDataValidation()
  72. {
  73. var target = new Class2();
  74. var source = new Subject<BindingValue<int>>();
  75. var property = GetNonValidatedProperty();
  76. // Class2 overrides the non-validated property metadata to enable data validation.
  77. target.Bind(property, source);
  78. source.OnNext(1);
  79. Assert.Equal(1, target.Notifications.Count);
  80. }
  81. [Fact]
  82. public void Disposing_Binding_Subscription_Clears_DataValidation()
  83. {
  84. var target = new Class1();
  85. var source = new Subject<BindingValue<int>>();
  86. var property = GetProperty();
  87. var error = new Exception();
  88. var sub = target.Bind(property, source);
  89. source.OnNext(6);
  90. source.OnNext(BindingValue<int>.DataValidationError(error));
  91. sub.Dispose();
  92. Assert.Equal(new Notification[]
  93. {
  94. new(BindingValueType.Value, 6, null),
  95. new(BindingValueType.DataValidationError, 6, error),
  96. new(BindingValueType.UnsetValue, 6, null),
  97. }, target.Notifications);
  98. }
  99. [Fact]
  100. public void Completing_Binding_Clears_DataValidation()
  101. {
  102. var target = new Class1();
  103. var source = new Subject<BindingValue<int>>();
  104. var property = GetProperty();
  105. var error = new Exception();
  106. target.Bind(property, source);
  107. source.OnNext(6);
  108. source.OnNext(BindingValue<int>.DataValidationError(error));
  109. source.OnCompleted();
  110. Assert.Equal(new Notification[]
  111. {
  112. new(BindingValueType.Value, 6, null),
  113. new(BindingValueType.DataValidationError, 6, error),
  114. new(BindingValueType.UnsetValue, 6, null),
  115. }, target.Notifications);
  116. }
  117. protected abstract T GetProperty();
  118. protected abstract T GetNonValidatedProperty();
  119. }
  120. public class DirectPropertyTests : TestBase<DirectPropertyBase<int>>
  121. {
  122. [Fact]
  123. public void Bound_Validated_String_Property_Can_Be_Set_To_Null()
  124. {
  125. var source = new ViewModel
  126. {
  127. StringValue = "foo",
  128. };
  129. var target = new Class1
  130. {
  131. [!Class1.ValidatedDirectStringProperty] = new Binding
  132. {
  133. Path = nameof(ViewModel.StringValue),
  134. Source = source,
  135. },
  136. };
  137. Assert.Equal("foo", target.ValidatedDirectString);
  138. source.StringValue = null;
  139. Assert.Null(target.ValidatedDirectString);
  140. }
  141. protected override DirectPropertyBase<int> GetProperty() => Class1.ValidatedDirectIntProperty;
  142. protected override DirectPropertyBase<int> GetNonValidatedProperty() => Class1.NonValidatedDirectIntProperty;
  143. }
  144. public class StyledPropertyTests : TestBase<StyledProperty<int>>
  145. {
  146. [Fact]
  147. public void Bound_Validated_String_Property_Can_Be_Set_To_Null()
  148. {
  149. var source = new ViewModel
  150. {
  151. StringValue = "foo",
  152. };
  153. var target = new Class1
  154. {
  155. [!Class1.ValidatedDirectStringProperty] = new Binding
  156. {
  157. Path = nameof(ViewModel.StringValue),
  158. Source = source,
  159. },
  160. };
  161. Assert.Equal("foo", target.ValidatedDirectString);
  162. source.StringValue = null;
  163. Assert.Null(target.ValidatedDirectString);
  164. }
  165. protected override StyledProperty<int> GetProperty() => Class1.ValidatedStyledIntProperty;
  166. protected override StyledProperty<int> GetNonValidatedProperty() => Class1.NonValidatedStyledIntProperty;
  167. }
  168. private record class Notification(BindingValueType type, object? value, Exception? error);
  169. private class Class1 : AvaloniaObject
  170. {
  171. public static readonly DirectProperty<Class1, int> NonValidatedDirectIntProperty =
  172. AvaloniaProperty.RegisterDirect<Class1, int>(
  173. nameof(NonValidatedDirectInt),
  174. o => o.NonValidatedDirectInt,
  175. (o, v) => o.NonValidatedDirectInt = v);
  176. public static readonly DirectProperty<Class1, int> ValidatedDirectIntProperty =
  177. AvaloniaProperty.RegisterDirect<Class1, int>(
  178. nameof(ValidatedDirectInt),
  179. o => o.ValidatedDirectInt,
  180. (o, v) => o.ValidatedDirectInt = v,
  181. enableDataValidation: true);
  182. public static readonly DirectProperty<Class1, string?> ValidatedDirectStringProperty =
  183. AvaloniaProperty.RegisterDirect<Class1, string?>(
  184. nameof(ValidatedDirectString),
  185. o => o.ValidatedDirectString,
  186. (o, v) => o.ValidatedDirectString = v,
  187. enableDataValidation: true);
  188. public static readonly StyledProperty<int> NonValidatedStyledIntProperty =
  189. AvaloniaProperty.Register<Class1, int>(
  190. nameof(NonValidatedStyledInt));
  191. public static readonly StyledProperty<int> ValidatedStyledIntProperty =
  192. AvaloniaProperty.Register<Class1, int>(
  193. nameof(ValidatedStyledInt),
  194. enableDataValidation: true);
  195. private int _nonValidatedDirect;
  196. private int _directInt;
  197. private string? _directString;
  198. public int NonValidatedDirectInt
  199. {
  200. get { return _directInt; }
  201. set { SetAndRaise(NonValidatedDirectIntProperty, ref _nonValidatedDirect, value); }
  202. }
  203. public int ValidatedDirectInt
  204. {
  205. get { return _directInt; }
  206. set { SetAndRaise(ValidatedDirectIntProperty, ref _directInt, value); }
  207. }
  208. public string? ValidatedDirectString
  209. {
  210. get { return _directString; }
  211. set { SetAndRaise(ValidatedDirectStringProperty, ref _directString, value); }
  212. }
  213. public int NonValidatedStyledInt
  214. {
  215. get { return GetValue(NonValidatedStyledIntProperty); }
  216. set { SetValue(NonValidatedStyledIntProperty, value); }
  217. }
  218. public int ValidatedStyledInt
  219. {
  220. get => GetValue(ValidatedStyledIntProperty);
  221. set => SetValue(ValidatedStyledIntProperty, value);
  222. }
  223. public List<Notification> Notifications { get; } = new();
  224. protected override void UpdateDataValidation(
  225. AvaloniaProperty property,
  226. BindingValueType state,
  227. Exception? error)
  228. {
  229. Notifications.Add(new(state, GetValue(property), error));
  230. }
  231. }
  232. private class Class2 : Class1
  233. {
  234. static Class2()
  235. {
  236. NonValidatedDirectIntProperty.OverrideMetadata<Class2>(
  237. new DirectPropertyMetadata<int>(enableDataValidation: true));
  238. NonValidatedStyledIntProperty.OverrideMetadata<Class2>(
  239. new StyledPropertyMetadata<int>(enableDataValidation: true));
  240. }
  241. }
  242. public class ViewModel : NotifyingBase
  243. {
  244. private string? _stringValue;
  245. public string? StringValue
  246. {
  247. get { return _stringValue; }
  248. set { _stringValue = value; RaisePropertyChanged(); }
  249. }
  250. }
  251. }
  252. }