-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Expand file tree
/
Copy pathNumericUpDownTests.cs
More file actions
292 lines (232 loc) · 10 KB
/
NumericUpDownTests.cs
File metadata and controls
292 lines (232 loc) · 10 KB
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
using System;
using System.ComponentModel;
using System.Windows.Controls;
using System.Windows.Data;
using Google.Protobuf.WellKnownTypes;
using MaterialDesignThemes.UITests.Samples.UpDownControls;
namespace MaterialDesignThemes.UITests.WPF.UpDownControls;
public class NumericUpDownTests(ITestOutputHelper output) : TestBase(output)
{
[Fact]
public async Task NumericButtons_IncreaseAndDecreaseValue()
{
await using var recorder = new TestRecorder(App);
var numericUpDown = await LoadXaml<NumericUpDown>("""
<materialDesign:NumericUpDown Value="1" />
""");
var plusButton = await numericUpDown.GetElement<RepeatButton>("PART_IncreaseButton");
var minusButton = await numericUpDown.GetElement<RepeatButton>("PART_DecreaseButton");
var textBox = await numericUpDown.GetElement<TextBox>("PART_TextBox");
Assert.Equal("1", await textBox.GetText());
Assert.Equal(1, await numericUpDown.GetValue());
await plusButton.LeftClick();
await Wait.For(async () =>
{
Assert.Equal("2", await textBox.GetText());
Assert.Equal(2, await numericUpDown.GetValue());
});
await minusButton.LeftClick();
await Wait.For(async () =>
{
Assert.Equal("1", await textBox.GetText());
Assert.Equal(1, await numericUpDown.GetValue());
});
recorder.Success();
}
[Fact]
public async Task NumericButtons_WithMaximum_DisablesPlusButton()
{
await using var recorder = new TestRecorder(App);
var numericUpDown = await LoadXaml<NumericUpDown>("""
<materialDesign:NumericUpDown Value="1" Maximum="2" />
""");
var plusButton = await numericUpDown.GetElement<RepeatButton>("PART_IncreaseButton");
var minusButton = await numericUpDown.GetElement<RepeatButton>("PART_DecreaseButton");
var textBox = await numericUpDown.GetElement<TextBox>("PART_TextBox");
await plusButton.LeftClick();
await Wait.For(async () =>
{
Assert.Equal("2", await textBox.GetText());
Assert.Equal(2, await numericUpDown.GetValue());
});
Assert.False(await plusButton.GetIsEnabled());
await minusButton.LeftClick();
await Wait.For(async () =>
{
Assert.Equal("1", await textBox.GetText());
Assert.Equal(1, await numericUpDown.GetValue());
});
Assert.True(await plusButton.GetIsEnabled());
recorder.Success();
}
[Fact]
public async Task NumericButtons_WithMinimum_DisablesMinusButton()
{
await using var recorder = new TestRecorder(App);
var numericUpDown = await LoadXaml<NumericUpDown>("""
<materialDesign:NumericUpDown Value="2" Minimum="1" />
""");
var plusButton = await numericUpDown.GetElement<RepeatButton>("PART_IncreaseButton");
var minusButton = await numericUpDown.GetElement<RepeatButton>("PART_DecreaseButton");
var textBox = await numericUpDown.GetElement<TextBox>("PART_TextBox");
await minusButton.LeftClick();
await Wait.For(async () =>
{
Assert.Equal("1", await textBox.GetText());
Assert.Equal(1, await numericUpDown.GetValue());
});
Assert.False(await minusButton.GetIsEnabled());
await plusButton.LeftClick();
await Wait.For(async () =>
{
Assert.Equal("2", await textBox.GetText());
Assert.Equal(2, await numericUpDown.GetValue());
});
Assert.True(await minusButton.GetIsEnabled());
recorder.Success();
}
[Fact]
public async Task MaxAndMinAssignments_CoerceValueToBeInRange()
{
await using var recorder = new TestRecorder(App);
var numericUpDown = await LoadXaml<NumericUpDown>("""
<materialDesign:NumericUpDown Value="2" />
""");
await numericUpDown.SetMaximum(1);
Assert.Equal(1, await numericUpDown.GetValue());
await numericUpDown.SetMinimum(3);
Assert.Equal(3, await numericUpDown.GetValue());
Assert.Equal(3, await numericUpDown.GetMaximum());
await numericUpDown.SetMaximum(2);
Assert.Equal(3, await numericUpDown.GetValue());
Assert.Equal(3, await numericUpDown.GetMinimum());
Assert.Equal(3, await numericUpDown.GetMaximum());
recorder.Success();
}
[Fact]
[Description("Issue 3654")]
public async Task InternalTextBoxIsFocused_WhenGettingKeyboardFocus()
{
await using var recorder = new TestRecorder(App);
// Arrange
var stackPanel = await LoadXaml<StackPanel>("""
<StackPanel>
<TextBox />
<materialDesign:NumericUpDown />
</StackPanel>
""");
var textBox = await stackPanel.GetElement<TextBox>("/TextBox");
var part_textBox = await stackPanel.GetElement<TextBox>("PART_TextBox");
// Act
await textBox.MoveKeyboardFocus();
await Task.Delay(50);
await textBox.SendInput(new KeyboardInput(Key.Tab));
await Task.Delay(50);
// Assert
Assert.False(await textBox.GetIsFocused());
Assert.True(await part_textBox.GetIsFocused());
recorder.Success();
}
[Fact(Skip = "Needs XAMLTest 1.2.3 or later")]
public async Task NumericUpDown_ValueSetGreaterThanMaximum_CoercesToMaximum()
{
await using var recorder = new TestRecorder(App);
//Arrange
var userControl = await LoadUserControl<BoundNumericUpDown>();
var numericUpDown = await userControl.GetElement<NumericUpDown>();
var buttonToFocus = await userControl.GetElement<Button>("btnToFocus");
//Set the current value to the maximum
await numericUpDown.SetMaximum(10);
await numericUpDown.SetValue(10);
//Act
//Input a number greater than the maximum
await numericUpDown.MoveKeyboardFocus();
await numericUpDown.SendKeyboardInput($"99");
await buttonToFocus.MoveKeyboardFocus();
//Assert
//The value and bound property in the VM should be set to the maximum
Assert.Equal(10, await numericUpDown.GetValue());
var viewModel = await userControl.GetProperty<BoundNumericUpDownViewModel>(nameof(BoundNumericUpDown.ViewModel));
Assert.Equal(10, viewModel?.Value);
recorder.Success();
}
[Theory]
[InlineData(1, false, true)]
[InlineData(5, true, true)]
[InlineData(10, true, false)]
[Description("Issue 3796")]
public async Task NumericUpDown_WhenValueEqualsMinimum_DisableButtons(int value,
bool decreaseEnabled, bool increaseEnabled)
{
await using var recorder = new TestRecorder(App);
//Arrange
var numericUpDown = await LoadXaml<NumericUpDown>($"""
<materialDesign:NumericUpDown Value="{value}" Maximum="10" Minimum="1" />
""");
var increaseButton = await numericUpDown.GetElement<RepeatButton>("PART_IncreaseButton");
var decreaseButton = await numericUpDown.GetElement<RepeatButton>("PART_DecreaseButton");
//Act
bool increaseButtonEnabled = await increaseButton.GetIsEnabled();
bool decreaseButtonEnabled = await decreaseButton.GetIsEnabled();
//Assert
Assert.Equal(increaseEnabled, increaseButtonEnabled);
Assert.Equal(decreaseEnabled, decreaseButtonEnabled);
recorder.Success();
}
[Fact]
[Description("Issue 3827")]
public async Task NumericUpDown_WhenBindingUpdateTriggerIsPropertyChanged_ItUpdatesBeforeLoosingFocus()
{
await using var recorder = new TestRecorder(App);
//Arrange
var numericUpDown = await LoadXaml<NumericUpDown>("""
<materialDesign:NumericUpDown Tag="{Binding Value, RelativeSource={RelativeSource Self}, UpdateSourceTrigger=PropertyChanged}" Maximum="10" Minimum="1" />
""");
var textBox = await numericUpDown.GetElement<TextBox>("PART_TextBox");
//Act
await textBox.MoveKeyboardFocus();
await textBox.SendKeyboardInput($"{ModifierKeys.Control}{Key.A}{ModifierKeys.None}4");
//Act
object? tag = await numericUpDown.GetTag();
//Assert
Assert.Equal("4", tag?.ToString());
recorder.Success();
}
[Fact]
[Description("Issue 3827")]
public async Task NumericUpDown_WhenBindingUpdateTriggerIsLostFocus_ItDoesNotUpdateUntilItLoosesFocus()
{
await using var recorder = new TestRecorder(App);
//Arrange
var userControl = await LoadUserControl<BoundNumericUpDown>();
var numericUpDown = await userControl.GetElement<NumericUpDown>();
var buttonToFocus = await userControl.GetElement<Button>("btnToFocus");
await numericUpDown.SetValue(2);
static void SetBindingToLostFocus(NumericUpDown numericUpDown)
{
var binding = new Binding(nameof(NumericUpDown.Value))
{
Path = new(nameof(BoundNumericUpDownViewModel.Value)),
UpdateSourceTrigger = UpdateSourceTrigger.LostFocus
};
BindingOperations.SetBinding(numericUpDown, NumericUpDown.ValueProperty, binding);
}
await numericUpDown.RemoteExecute(SetBindingToLostFocus);
var textBox = await numericUpDown.GetElement<TextBox>("PART_TextBox");
static int GetViewModelValue(NumericUpDown numericUpDown)
{
return ((BoundNumericUpDownViewModel)numericUpDown.DataContext).Value;
}
//Act
await textBox.MoveKeyboardFocus();
await textBox.SendKeyboardInput($"{ModifierKeys.Control}{Key.A}{ModifierKeys.None}4");
//Act
int valueBeforeLostFocus = await numericUpDown.RemoteExecute(GetViewModelValue);
await textBox.SendKeyboardInput($"{Key.Tab}");
int valueAfterLostFocus = await numericUpDown.RemoteExecute(GetViewModelValue);
//Assert
Assert.Equal("2", valueBeforeLostFocus.ToString());
Assert.Equal("4", valueAfterLostFocus.ToString());
recorder.Success();
}
}