-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
TextBoxTests_DataValidation.cs
167 lines (148 loc) · 5.61 KB
/
TextBoxTests_DataValidation.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
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.Headless;
using Avalonia.Markup.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.DoesNotContain(":error", target.Classes);
target.Text = "20";
Assert.Contains(":error", target.Classes);
target.Text = "1";
Assert.DoesNotContain(":error", target.Classes);
}
}
[Fact]
public void Setter_Exceptions_Should_Set_DataValidationErrors_Errors()
{
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(DataValidationErrors.GetErrors(target));
target.Text = "20";
IEnumerable<object> errors = DataValidationErrors.GetErrors(target);
Assert.Single(errors);
Assert.IsType<InvalidOperationException>(errors.Single());
target.Text = "1";
Assert.Null(DataValidationErrors.GetErrors(target));
}
}
[Fact]
public void Setter_Exceptions_Should_Set_DataValidationErrors_HasErrors()
{
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(DataValidationErrors.GetHasErrors(target));
target.Text = "20";
Assert.True(DataValidationErrors.GetHasErrors(target));
target.Text = "1";
Assert.False(DataValidationErrors.GetHasErrors(target));
}
}
private static TestServices Services => TestServices.MockThreadingInterface.With(
standardCursorFactory: Mock.Of<ICursorFactory>(),
textShaperImpl: new HeadlessTextShaperStub(),
fontManagerImpl: new HeadlessFontManagerStub());
private static IControlTemplate CreateTemplate()
{
return new FuncControlTemplate<TextBox>((control, scope) =>
new TextPresenter
{
Name = "PART_TextPresenter",
[!!TextPresenter.TextProperty] = new Binding
{
Path = "Text",
Mode = BindingMode.TwoWay,
Priority = BindingPriority.Template,
RelativeSource = new RelativeSource(RelativeSourceMode.TemplatedParent),
},
}.RegisterInNameScope(scope));
}
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;
}
}
}
}