A cross-platform UI framework for .NET
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

143 lines
4.7 KiB

// Copyright (c) The Avalonia Project. All rights reserved.
// Licensed under the MIT license. See licence.md file in the project root for full license information.
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using Avalonia.Controls.Presenters;
using Avalonia.Controls.Templates;
using Avalonia.Data;
using Avalonia.Markup.Xaml.Data;
using Avalonia.Platform;
using Avalonia.UnitTests;
using Moq;
using Xunit;
namespace Avalonia.Controls.UnitTests
{
public class TextBoxTests_DataValidation
{
[Fact]
public void Setter_Exceptions_Should_Set_Error_Pseudoclass()
{
using (UnitTestApplication.Start(Services))
{
var target = new TextBox
{
DataContext = new ExceptionTest(),
[!TextBox.TextProperty] = new Binding(nameof(ExceptionTest.LessThan10), BindingMode.TwoWay),
Template = CreateTemplate(),
};
target.ApplyTemplate();
Assert.False(target.Classes.Contains(":error"));
target.Text = "20";
Assert.True(target.Classes.Contains(":error"));
target.Text = "1";
Assert.False(target.Classes.Contains(":error"));
}
}
[Fact]
public void Setter_Exceptions_Should_Set_DataValidationErrors()
{
using (UnitTestApplication.Start(Services))
{
var target = new TextBox
{
DataContext = new ExceptionTest(),
[!TextBox.TextProperty] = new Binding(nameof(ExceptionTest.LessThan10), BindingMode.TwoWay),
Template = CreateTemplate(),
};
target.ApplyTemplate();
Assert.Null(target.DataValidationErrors);
target.Text = "20";
Assert.Equal(1, target.DataValidationErrors.Count());
Assert.IsType<InvalidOperationException>(target.DataValidationErrors.Single());
target.Text = "1";
Assert.Null(target.DataValidationErrors);
}
}
private static TestServices Services => TestServices.MockThreadingInterface.With(
standardCursorFactory: Mock.Of<IStandardCursorFactory>());
private IControlTemplate CreateTemplate()
{
return new FuncControlTemplate<TextBox>(control =>
new TextPresenter
{
Name = "PART_TextPresenter",
[!!TextPresenter.TextProperty] = new Binding
{
Path = "Text",
Mode = BindingMode.TwoWay,
Priority = BindingPriority.TemplatedParent,
RelativeSource = new RelativeSource(RelativeSourceMode.TemplatedParent),
},
});
}
private class ExceptionTest
{
private int _lessThan10;
public int LessThan10
{
get { return _lessThan10; }
set
{
if (value < 10)
{
_lessThan10 = value;
}
else
{
throw new InvalidOperationException("More than 10.");
}
}
}
}
private class IndeiTest : INotifyDataErrorInfo
{
private int _lessThan10;
private Dictionary<string, IList<string>> _errors = new Dictionary<string, IList<string>>();
public int LessThan10
{
get { return _lessThan10; }
set
{
if (value < 10)
{
_lessThan10 = value;
_errors.Remove(nameof(LessThan10));
ErrorsChanged?.Invoke(this, new DataErrorsChangedEventArgs(nameof(LessThan10)));
}
else
{
_errors[nameof(LessThan10)] = new[] { "More than 10" };
ErrorsChanged?.Invoke(this, new DataErrorsChangedEventArgs(nameof(LessThan10)));
}
}
}
public bool HasErrors => _lessThan10 >= 10;
public event EventHandler<DataErrorsChangedEventArgs> ErrorsChanged;
public IEnumerable GetErrors(string propertyName)
{
IList<string> result;
_errors.TryGetValue(propertyName, out result);
return result;
}
}
}
}