ExpressionObserverTests_DataValidation.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. // Copyright (c) The Avalonia Project. All rights reserved.
  2. // Licensed under the MIT license. See licence.md file in the project root for full license information.
  3. using System;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using System.ComponentModel;
  7. using System.Linq;
  8. using System.Reactive.Linq;
  9. using System.Runtime.CompilerServices;
  10. using Avalonia.Data;
  11. using Avalonia.Markup.Data;
  12. using Avalonia.UnitTests;
  13. using Xunit;
  14. namespace Avalonia.Markup.UnitTests.Data
  15. {
  16. public class ExpressionObserverTests_DataValidation
  17. {
  18. [Fact]
  19. public void Doesnt_Send_DataValidationError_When_DataValidatation_Not_Enabled()
  20. {
  21. var data = new ExceptionTest { MustBePositive = 5 };
  22. var observer = new ExpressionObserver(data, nameof(data.MustBePositive), false);
  23. var validationMessageFound = false;
  24. observer.OfType<BindingNotification>()
  25. .Where(x => x.ErrorType == BindingErrorType.DataValidationError)
  26. .Subscribe(_ => validationMessageFound = true);
  27. observer.SetValue(-5);
  28. Assert.False(validationMessageFound);
  29. }
  30. [Fact]
  31. public void Exception_Validation_Sends_DataValidationError()
  32. {
  33. var data = new ExceptionTest { MustBePositive = 5 };
  34. var observer = new ExpressionObserver(data, nameof(data.MustBePositive), true);
  35. var validationMessageFound = false;
  36. observer.OfType<BindingNotification>()
  37. .Where(x => x.ErrorType == BindingErrorType.DataValidationError)
  38. .Subscribe(_ => validationMessageFound = true);
  39. observer.SetValue(-5);
  40. Assert.True(validationMessageFound);
  41. }
  42. [Fact]
  43. public void Indei_Validation_Does_Not_Subscribe_When_DataValidatation_Not_Enabled()
  44. {
  45. var data = new IndeiTest { MustBePositive = 5 };
  46. var observer = new ExpressionObserver(data, nameof(data.MustBePositive), false);
  47. observer.Subscribe(_ => { });
  48. Assert.Equal(0, data.ErrorsChangedSubscriptionCount);
  49. }
  50. [Fact]
  51. public void Enabled_Indei_Validation_Subscribes()
  52. {
  53. var data = new IndeiTest { MustBePositive = 5 };
  54. var observer = new ExpressionObserver(data, nameof(data.MustBePositive), true);
  55. var sub = observer.Subscribe(_ => { });
  56. Assert.Equal(1, data.ErrorsChangedSubscriptionCount);
  57. sub.Dispose();
  58. Assert.Equal(0, data.ErrorsChangedSubscriptionCount);
  59. }
  60. [Fact]
  61. public void Validation_Plugins_Send_Correct_Notifications()
  62. {
  63. var data = new IndeiTest();
  64. var observer = new ExpressionObserver(data, nameof(data.MustBePositive), true);
  65. var result = new List<object>();
  66. observer.Subscribe(x => result.Add(x));
  67. observer.SetValue(5);
  68. observer.SetValue(-5);
  69. observer.SetValue("foo");
  70. observer.SetValue(5);
  71. Assert.Equal(new[]
  72. {
  73. new BindingNotification(0),
  74. // Value is notified twice as ErrorsChanged is always called by IndeiTest.
  75. new BindingNotification(5),
  76. new BindingNotification(5),
  77. // Value is first signalled without an error as validation hasn't been updated.
  78. new BindingNotification(-5),
  79. new BindingNotification(new Exception("Must be positive"), BindingErrorType.DataValidationError, -5),
  80. // Exception is thrown by trying to set value to "foo".
  81. new BindingNotification(
  82. new ArgumentException("Object of type 'System.String' cannot be converted to type 'System.Int32'."),
  83. BindingErrorType.DataValidationError),
  84. // Value is set then validation is updated.
  85. new BindingNotification(new Exception("Must be positive"), BindingErrorType.DataValidationError, 5),
  86. new BindingNotification(5),
  87. }, result);
  88. }
  89. public class ExceptionTest : NotifyingBase
  90. {
  91. private int _mustBePositive;
  92. public int MustBePositive
  93. {
  94. get { return _mustBePositive; }
  95. set
  96. {
  97. if (value <= 0)
  98. {
  99. throw new ArgumentOutOfRangeException(nameof(value));
  100. }
  101. _mustBePositive = value;
  102. RaisePropertyChanged();
  103. }
  104. }
  105. }
  106. private class IndeiTest : IndeiBase
  107. {
  108. private int _mustBePositive;
  109. private Dictionary<string, IList<string>> _errors = new Dictionary<string, IList<string>>();
  110. public int MustBePositive
  111. {
  112. get { return _mustBePositive; }
  113. set
  114. {
  115. _mustBePositive = value;
  116. RaisePropertyChanged();
  117. if (value >= 0)
  118. {
  119. _errors.Remove(nameof(MustBePositive));
  120. RaiseErrorsChanged(nameof(MustBePositive));
  121. }
  122. else
  123. {
  124. _errors[nameof(MustBePositive)] = new[] { "Must be positive" };
  125. RaiseErrorsChanged(nameof(MustBePositive));
  126. }
  127. }
  128. }
  129. public override bool HasErrors => _mustBePositive >= 0;
  130. public override IEnumerable GetErrors(string propertyName)
  131. {
  132. IList<string> result;
  133. _errors.TryGetValue(propertyName, out result);
  134. return result;
  135. }
  136. }
  137. }
  138. }