|
|
@@ -1,7 +1,10 @@
|
|
|
using System;
|
|
|
+using System.Collections;
|
|
|
+using System.Collections.Generic;
|
|
|
using Avalonia.Controls;
|
|
|
using Avalonia.Data;
|
|
|
using Avalonia.Markup.Xaml.Data;
|
|
|
+using Avalonia.UnitTests;
|
|
|
using Xunit;
|
|
|
|
|
|
namespace Avalonia.Markup.Xaml.UnitTests.Data
|
|
|
@@ -9,124 +12,173 @@ namespace Avalonia.Markup.Xaml.UnitTests.Data
|
|
|
public class BindingTests_Validation
|
|
|
{
|
|
|
[Fact]
|
|
|
- public void Disabled_Validation_Should_Trigger_Validation_Change_On_Exception()
|
|
|
+ public void Non_Validated_Property_Does_Not_Receive_BindingNotifications()
|
|
|
{
|
|
|
var source = new ValidationTestModel { MustBePositive = 5 };
|
|
|
- var target = new TestControl { DataContext = source };
|
|
|
- var binding = new Binding
|
|
|
+ var target = new TestControl
|
|
|
{
|
|
|
- Path = nameof(source.MustBePositive),
|
|
|
- Mode = BindingMode.TwoWay,
|
|
|
-
|
|
|
- // Even though EnableValidation = false, exception validation is enabled.
|
|
|
- EnableValidation = false,
|
|
|
+ DataContext = source,
|
|
|
+ [!TestControl.NonValidatedProperty] = new Binding(nameof(source.MustBePositive)),
|
|
|
};
|
|
|
|
|
|
- target.Bind(TestControl.ValidationTestProperty, binding);
|
|
|
-
|
|
|
- target.ValidationTest = -5;
|
|
|
-
|
|
|
- Assert.True(false);
|
|
|
- //Assert.False(target.ValidationStatus.IsValid);
|
|
|
+ Assert.Empty(target.Notifications);
|
|
|
}
|
|
|
|
|
|
[Fact]
|
|
|
- public void Enabled_Validation_Should_Trigger_Validation_Change_On_Exception()
|
|
|
+ public void Validated_Property_Does_Not_Receive_BindingNotifications()
|
|
|
{
|
|
|
var source = new ValidationTestModel { MustBePositive = 5 };
|
|
|
- var target = new TestControl { DataContext = source };
|
|
|
- var binding = new Binding
|
|
|
+ var target = new TestControl
|
|
|
{
|
|
|
- Path = nameof(source.MustBePositive),
|
|
|
- Mode = BindingMode.TwoWay,
|
|
|
- EnableValidation = true,
|
|
|
+ DataContext = source,
|
|
|
+ [!TestControl.ValidatedProperty] = new Binding(nameof(source.MustBePositive)),
|
|
|
};
|
|
|
|
|
|
- target.Bind(TestControl.ValidationTestProperty, binding);
|
|
|
+ source.MustBePositive = 6;
|
|
|
|
|
|
- target.ValidationTest = -5;
|
|
|
- Assert.True(false);
|
|
|
- //Assert.False(target.ValidationStatus.IsValid);
|
|
|
+ Assert.Equal(
|
|
|
+ new[]
|
|
|
+ {
|
|
|
+ new BindingNotification(5),
|
|
|
+ new BindingNotification(new ArgumentOutOfRangeException("value"), BindingErrorType.DataValidationError),
|
|
|
+ new BindingNotification(6),
|
|
|
+ },
|
|
|
+ target.Notifications);
|
|
|
}
|
|
|
|
|
|
+ //[Fact]
|
|
|
+ //public void Disabled_Validation_Should_Trigger_Validation_Change_On_Exception()
|
|
|
+ //{
|
|
|
+ // var source = new ValidationTestModel { MustBePositive = 5 };
|
|
|
+ // var target = new TestControl { DataContext = source };
|
|
|
+ // var binding = new Binding
|
|
|
+ // {
|
|
|
+ // Path = nameof(source.MustBePositive),
|
|
|
+ // Mode = BindingMode.TwoWay,
|
|
|
+
|
|
|
+ // // Even though EnableValidation = false, exception validation is enabled.
|
|
|
+ // EnableValidation = false,
|
|
|
+ // };
|
|
|
+
|
|
|
+ // target.Bind(TestControl.ValidationTestProperty, binding);
|
|
|
+
|
|
|
+ // target.ValidationTest = -5;
|
|
|
+
|
|
|
+ // Assert.True(false);
|
|
|
+ // //Assert.False(target.ValidationStatus.IsValid);
|
|
|
+ //}
|
|
|
+
|
|
|
+ //[Fact]
|
|
|
+ //public void Enabled_Validation_Should_Trigger_Validation_Change_On_Exception()
|
|
|
+ //{
|
|
|
+ // var source = new ValidationTestModel { MustBePositive = 5 };
|
|
|
+ // var target = new TestControl { DataContext = source };
|
|
|
+ // var binding = new Binding
|
|
|
+ // {
|
|
|
+ // Path = nameof(source.MustBePositive),
|
|
|
+ // Mode = BindingMode.TwoWay,
|
|
|
+ // EnableValidation = true,
|
|
|
+ // };
|
|
|
+
|
|
|
+ // target.Bind(TestControl.ValidationTestProperty, binding);
|
|
|
+
|
|
|
+ // target.ValidationTest = -5;
|
|
|
+ // Assert.True(false);
|
|
|
+ // //Assert.False(target.ValidationStatus.IsValid);
|
|
|
+ //}
|
|
|
+
|
|
|
+
|
|
|
+ //[Fact]
|
|
|
+ //public void Passed_Validation_Should_Not_Add_Invalid_Pseudo_Class()
|
|
|
+ //{
|
|
|
+ // var control = new TestControl();
|
|
|
+ // var model = new ValidationTestModel { MustBePositive = 1 };
|
|
|
+ // var binding = new Binding
|
|
|
+ // {
|
|
|
+ // Path = nameof(model.MustBePositive),
|
|
|
+ // Mode = BindingMode.TwoWay,
|
|
|
+ // EnableValidation = true,
|
|
|
+ // };
|
|
|
+
|
|
|
+ // control.Bind(TestControl.ValidationTestProperty, binding);
|
|
|
+ // control.DataContext = model;
|
|
|
+ // Assert.DoesNotContain(control.Classes, x => x == ":invalid");
|
|
|
+ //}
|
|
|
+
|
|
|
+ //[Fact]
|
|
|
+ //public void Failed_Validation_Should_Add_Invalid_Pseudo_Class()
|
|
|
+ //{
|
|
|
+ // var control = new TestControl();
|
|
|
+ // var model = new ValidationTestModel { MustBePositive = 1 };
|
|
|
+ // var binding = new Binding
|
|
|
+ // {
|
|
|
+ // Path = nameof(model.MustBePositive),
|
|
|
+ // Mode = BindingMode.TwoWay,
|
|
|
+ // EnableValidation = true,
|
|
|
+ // };
|
|
|
+
|
|
|
+ // control.Bind(TestControl.ValidationTestProperty, binding);
|
|
|
+ // control.DataContext = model;
|
|
|
+ // control.ValidationTest = -5;
|
|
|
+ // Assert.Contains(control.Classes, x => x == ":invalid");
|
|
|
+ //}
|
|
|
+
|
|
|
+ //[Fact]
|
|
|
+ //public void Failed_Then_Passed_Validation_Should_Remove_Invalid_Pseudo_Class()
|
|
|
+ //{
|
|
|
+ // var control = new TestControl();
|
|
|
+ // var model = new ValidationTestModel { MustBePositive = 1 };
|
|
|
+
|
|
|
+ // var binding = new Binding
|
|
|
+ // {
|
|
|
+ // Path = nameof(model.MustBePositive),
|
|
|
+ // Mode = BindingMode.TwoWay,
|
|
|
+ // EnableValidation = true,
|
|
|
+ // };
|
|
|
+
|
|
|
+ // control.Bind(TestControl.ValidationTestProperty, binding);
|
|
|
+ // control.DataContext = model;
|
|
|
+
|
|
|
+
|
|
|
+ // control.ValidationTest = -5;
|
|
|
+ // Assert.Contains(control.Classes, x => x == ":invalid");
|
|
|
+ // control.ValidationTest = 5;
|
|
|
+ // Assert.DoesNotContain(control.Classes, x => x == ":invalid");
|
|
|
+ //}
|
|
|
|
|
|
- [Fact]
|
|
|
- public void Passed_Validation_Should_Not_Add_Invalid_Pseudo_Class()
|
|
|
+ private class TestControl : Control
|
|
|
{
|
|
|
- var control = new TestControl();
|
|
|
- var model = new ValidationTestModel { MustBePositive = 1 };
|
|
|
- var binding = new Binding
|
|
|
- {
|
|
|
- Path = nameof(model.MustBePositive),
|
|
|
- Mode = BindingMode.TwoWay,
|
|
|
- EnableValidation = true,
|
|
|
- };
|
|
|
+ public static readonly StyledProperty<int> NonValidatedProperty =
|
|
|
+ AvaloniaProperty.Register<TestControl, int>(
|
|
|
+ nameof(Validated),
|
|
|
+ enableDataValidation: false);
|
|
|
|
|
|
- control.Bind(TestControl.ValidationTestProperty, binding);
|
|
|
- control.DataContext = model;
|
|
|
- Assert.DoesNotContain(control.Classes, x => x == ":invalid");
|
|
|
- }
|
|
|
+ public static readonly StyledProperty<int> ValidatedProperty =
|
|
|
+ AvaloniaProperty.Register<TestControl, int>(
|
|
|
+ nameof(Validated),
|
|
|
+ enableDataValidation: true);
|
|
|
|
|
|
- [Fact]
|
|
|
- public void Failed_Validation_Should_Add_Invalid_Pseudo_Class()
|
|
|
- {
|
|
|
- var control = new TestControl();
|
|
|
- var model = new ValidationTestModel { MustBePositive = 1 };
|
|
|
- var binding = new Binding
|
|
|
+ public int NonValidated
|
|
|
{
|
|
|
- Path = nameof(model.MustBePositive),
|
|
|
- Mode = BindingMode.TwoWay,
|
|
|
- EnableValidation = true,
|
|
|
- };
|
|
|
-
|
|
|
- control.Bind(TestControl.ValidationTestProperty, binding);
|
|
|
- control.DataContext = model;
|
|
|
- control.ValidationTest = -5;
|
|
|
- Assert.Contains(control.Classes, x => x == ":invalid");
|
|
|
- }
|
|
|
-
|
|
|
- [Fact]
|
|
|
- public void Failed_Then_Passed_Validation_Should_Remove_Invalid_Pseudo_Class()
|
|
|
- {
|
|
|
- var control = new TestControl();
|
|
|
- var model = new ValidationTestModel { MustBePositive = 1 };
|
|
|
+ get { return GetValue(NonValidatedProperty); }
|
|
|
+ set { SetValue(NonValidatedProperty, value); }
|
|
|
+ }
|
|
|
|
|
|
- var binding = new Binding
|
|
|
+ public int Validated
|
|
|
{
|
|
|
- Path = nameof(model.MustBePositive),
|
|
|
- Mode = BindingMode.TwoWay,
|
|
|
- EnableValidation = true,
|
|
|
- };
|
|
|
-
|
|
|
- control.Bind(TestControl.ValidationTestProperty, binding);
|
|
|
- control.DataContext = model;
|
|
|
-
|
|
|
-
|
|
|
- control.ValidationTest = -5;
|
|
|
- Assert.Contains(control.Classes, x => x == ":invalid");
|
|
|
- control.ValidationTest = 5;
|
|
|
- Assert.DoesNotContain(control.Classes, x => x == ":invalid");
|
|
|
- }
|
|
|
+ get { return GetValue(ValidatedProperty); }
|
|
|
+ set { SetValue(ValidatedProperty, value); }
|
|
|
+ }
|
|
|
|
|
|
- private class TestControl : Control
|
|
|
- {
|
|
|
- public static readonly StyledProperty<int> ValidationTestProperty
|
|
|
- = AvaloniaProperty.Register<TestControl, int>(nameof(ValidationTest), 1, defaultBindingMode: BindingMode.TwoWay);
|
|
|
+ public IList<BindingNotification> Notifications { get; } = new List<BindingNotification>();
|
|
|
|
|
|
- public int ValidationTest
|
|
|
+ protected override void BindingNotificationReceived(AvaloniaProperty property, BindingNotification notification)
|
|
|
{
|
|
|
- get
|
|
|
- {
|
|
|
- return GetValue(ValidationTestProperty);
|
|
|
- }
|
|
|
- set
|
|
|
- {
|
|
|
- SetValue(ValidationTestProperty, value);
|
|
|
- }
|
|
|
+ Notifications.Add(notification);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private class ValidationTestModel
|
|
|
+ private class ValidationTestModel : NotifyingBase
|
|
|
{
|
|
|
private int mustBePositive;
|
|
|
|
|
|
@@ -139,7 +191,9 @@ namespace Avalonia.Markup.Xaml.UnitTests.Data
|
|
|
{
|
|
|
throw new ArgumentOutOfRangeException(nameof(value));
|
|
|
}
|
|
|
+
|
|
|
mustBePositive = value;
|
|
|
+ RaisePropertyChanged();
|
|
|
}
|
|
|
}
|
|
|
}
|