AvaloniaObjectTests_DataValidation.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reactive.Subjects;
  5. using Avalonia.Data;
  6. using Avalonia.UnitTests;
  7. using Xunit;
  8. namespace Avalonia.Base.UnitTests
  9. {
  10. public class AvaloniaObjectTests_DataValidation
  11. {
  12. [Fact]
  13. public void Binding_Non_Validated_Styled_Property_Does_Not_Call_UpdateDataValidation()
  14. {
  15. var target = new Class1();
  16. var source = new Subject<BindingValue<int>>();
  17. target.Bind(Class1.NonValidatedProperty, source);
  18. source.OnNext(6);
  19. source.OnNext(BindingValue<int>.BindingError(new Exception()));
  20. source.OnNext(BindingValue<int>.DataValidationError(new Exception()));
  21. source.OnNext(6);
  22. Assert.Empty(target.Notifications);
  23. }
  24. [Fact]
  25. public void Binding_Non_Validated_Direct_Property_Does_Not_Call_UpdateDataValidation()
  26. {
  27. var target = new Class1();
  28. var source = new Subject<BindingValue<int>>();
  29. target.Bind(Class1.NonValidatedDirectProperty, source);
  30. source.OnNext(6);
  31. source.OnNext(BindingValue<int>.BindingError(new Exception()));
  32. source.OnNext(BindingValue<int>.DataValidationError(new Exception()));
  33. source.OnNext(6);
  34. Assert.Empty(target.Notifications);
  35. }
  36. [Fact]
  37. public void Binding_Validated_Direct_Property_Calls_UpdateDataValidation()
  38. {
  39. var target = new Class1();
  40. var source = new Subject<BindingValue<int>>();
  41. target.Bind(Class1.ValidatedDirectIntProperty, source);
  42. source.OnNext(6);
  43. source.OnNext(BindingValue<int>.BindingError(new Exception()));
  44. source.OnNext(BindingValue<int>.DataValidationError(new Exception()));
  45. source.OnNext(7);
  46. var result = target.Notifications.Cast<BindingValue<int>>().ToList();
  47. Assert.Equal(4, result.Count);
  48. Assert.Equal(BindingValueType.Value, result[0].Type);
  49. Assert.Equal(6, result[0].Value);
  50. Assert.Equal(BindingValueType.BindingError, result[1].Type);
  51. Assert.Equal(BindingValueType.DataValidationError, result[2].Type);
  52. Assert.Equal(BindingValueType.Value, result[3].Type);
  53. Assert.Equal(7, result[3].Value);
  54. }
  55. [Fact]
  56. public void Bound_Validated_Direct_String_Property_Can_Be_Set_To_Null()
  57. {
  58. var source = new ViewModel
  59. {
  60. StringValue = "foo",
  61. };
  62. var target = new Class1
  63. {
  64. [!Class1.ValidatedDirectStringProperty] = new Binding
  65. {
  66. Path = nameof(ViewModel.StringValue),
  67. Source = source,
  68. },
  69. };
  70. Assert.Equal("foo", target.ValidatedDirectString);
  71. source.StringValue = null;
  72. Assert.Null(target.ValidatedDirectString);
  73. }
  74. private class Class1 : AvaloniaObject
  75. {
  76. public static readonly StyledProperty<int> NonValidatedProperty =
  77. AvaloniaProperty.Register<Class1, int>(
  78. nameof(NonValidated));
  79. public static readonly DirectProperty<Class1, int> NonValidatedDirectProperty =
  80. AvaloniaProperty.RegisterDirect<Class1, int>(
  81. nameof(NonValidatedDirect),
  82. o => o.NonValidatedDirect,
  83. (o, v) => o.NonValidatedDirect = v);
  84. public static readonly DirectProperty<Class1, int> ValidatedDirectIntProperty =
  85. AvaloniaProperty.RegisterDirect<Class1, int>(
  86. nameof(ValidatedDirectInt),
  87. o => o.ValidatedDirectInt,
  88. (o, v) => o.ValidatedDirectInt = v,
  89. enableDataValidation: true);
  90. public static readonly DirectProperty<Class1, string> ValidatedDirectStringProperty =
  91. AvaloniaProperty.RegisterDirect<Class1, string>(
  92. nameof(ValidatedDirectString),
  93. o => o.ValidatedDirectString,
  94. (o, v) => o.ValidatedDirectString = v,
  95. enableDataValidation: true);
  96. private int _nonValidatedDirect;
  97. private int _directInt;
  98. private string _directString;
  99. public int NonValidated
  100. {
  101. get { return GetValue(NonValidatedProperty); }
  102. set { SetValue(NonValidatedProperty, value); }
  103. }
  104. public int NonValidatedDirect
  105. {
  106. get { return _directInt; }
  107. set { SetAndRaise(NonValidatedDirectProperty, ref _nonValidatedDirect, value); }
  108. }
  109. public int ValidatedDirectInt
  110. {
  111. get { return _directInt; }
  112. set { SetAndRaise(ValidatedDirectIntProperty, ref _directInt, value); }
  113. }
  114. public string ValidatedDirectString
  115. {
  116. get { return _directString; }
  117. set { SetAndRaise(ValidatedDirectStringProperty, ref _directString, value); }
  118. }
  119. public IList<object> Notifications { get; } = new List<object>();
  120. protected override void UpdateDataValidation<T>(
  121. AvaloniaProperty<T> property,
  122. BindingValue<T> value)
  123. {
  124. Notifications.Add(value);
  125. }
  126. }
  127. public class ViewModel : NotifyingBase
  128. {
  129. private string _stringValue;
  130. public string StringValue
  131. {
  132. get { return _stringValue; }
  133. set { _stringValue = value; RaisePropertyChanged(); }
  134. }
  135. }
  136. }
  137. }