-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Expand file tree
/
Copy pathTextBoxTests.cs
More file actions
631 lines (506 loc) · 23.3 KB
/
TextBoxTests.cs
File metadata and controls
631 lines (506 loc) · 23.3 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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
using System.ComponentModel;
using System.Globalization;
using System.Windows.Media;
using MaterialDesignThemes.UITests.Samples.Validation;
namespace MaterialDesignThemes.UITests.WPF.TextBoxes;
public class TextBoxTests : TestBase
{
[Test]
[Description("Issue 1883")]
public async Task OnClearButtonShown_ControlHeightDoesNotChange()
{
await using var recorder = new TestRecorder(App);
//Arrange
var grid = await LoadXaml<Grid>(@"
<Grid Margin=""30"">
<TextBox VerticalAlignment=""Top""
Text=""Some Text""
materialDesign:TextFieldAssist.HasClearButton=""True"">
</TextBox>
</Grid>");
var textBox = await grid.GetElement<TextBox>("/TextBox");
var clearButton = await grid.GetElement<Button>("PART_ClearButton");
await textBox.MoveKeyboardFocus();
//Delay needed to account for transition storyboard
await Task.Delay(MaterialDesignTextBox.FocusedAnimationTime, TestContext.Current!.CancellationToken);
double initialHeight = await textBox.GetActualHeight();
//Act
await clearButton.LeftClick();
//Assert
await Task.Delay(MaterialDesignTextBox.FocusedAnimationTime, TestContext.Current.CancellationToken);
double height = await textBox.GetActualHeight();
await Assert.That(height).IsEqualTo(initialHeight);
recorder.Success();
}
[Test]
[Description("Issue 1883")]
public async Task OnClearButtonWithHintShown_ControlHeightDoesNotChange()
{
await using var recorder = new TestRecorder(App);
//Arrange
var grid = await LoadXaml<Grid>(@"
<Grid Margin=""30"">
<TextBox Style=""{StaticResource MaterialDesignFloatingHintTextBox}""
VerticalAlignment=""Top""
materialDesign:TextFieldAssist.HasClearButton=""True""
materialDesign:TextFieldAssist.PrefixText =""$""
materialDesign:TextFieldAssist.SuffixText = ""mm"">
<materialDesign:HintAssist.Hint>
<StackPanel Orientation=""Horizontal"" Margin=""-2 0 0 0"">
<materialDesign:PackIcon Kind=""AccessPoint"" />
<TextBlock>WiFi</TextBlock>
</StackPanel>
</materialDesign:HintAssist.Hint >
</TextBox>
</Grid>");
var textBox = await grid.GetElement<TextBox>("/TextBox");
var clearButton = await grid.GetElement<Button>("PART_ClearButton");
double initialHeight = await textBox.GetActualHeight();
//Act
await textBox.MoveKeyboardFocus();
//Delay needed to account for transition storyboard
await Task.Delay(MaterialDesignTextBox.FocusedAnimationTime, TestContext.Current!.CancellationToken);
//Assert
double height = await textBox.GetActualHeight();
await Assert.That(height).IsEqualTo(initialHeight);
recorder.Success();
}
[Test]
[Description("Issue 1979")]
public async Task OnTextCleared_MultilineTextBox()
{
await using var recorder = new TestRecorder(App);
//Arrange
var grid = await LoadXaml<Grid>(@"
<Grid>
<TextBox Style=""{StaticResource MaterialDesignFilledTextBox}""
materialDesign:HintAssist.Hint=""Floating hint in a box""
VerticalAlignment=""Top""/>
</Grid>");
var textBox = await grid.GetElement<TextBox>("/TextBox");
Rect initialRect = await textBox.GetCoordinates();
double initialHeight = await textBox.GetActualHeight();
await textBox.SetText($"Line 1{Environment.NewLine}Line 2");
double twoLineHeight = await textBox.GetActualHeight();
//Act
await textBox.SetText("");
//Assert
await Wait.For(async () => await Assert.That(await textBox.GetActualHeight()).IsEqualTo(initialHeight));
Rect rect = await textBox.GetCoordinates();
await Assert.That(rect).IsEqualTo(initialRect);
recorder.Success();
}
[Test]
[Description("Issue 2495")]
public async Task OnTextBox_WithClearButton_ClearsText()
{
await using var recorder = new TestRecorder(App);
var grid = await LoadXaml<Grid>(@"
<Grid Margin=""30"">
<TextBox VerticalAlignment=""Top""
Text=""Some Text""
materialDesign:TextFieldAssist.HasClearButton=""True"">
</TextBox>
</Grid>");
var textBox = await grid.GetElement<TextBox>("/TextBox");
var clearButton = await textBox.GetElement<Button>("PART_ClearButton");
string? text = await textBox.GetText();
await Assert.That(text).IsNotNull();
await clearButton.LeftClick();
await Wait.For(async () =>
{
text = await textBox.GetText();
await Assert.That(text).IsNull();
});
recorder.Success();
}
[Test]
[Description("Issue 2002")]
public async Task OnTextBoxDisabled_FloatingHintBackgroundIsOpaque()
{
await using var recorder = new TestRecorder(App);
var grid = await LoadXaml<Grid>(@"
<Grid Background=""Red"">
<TextBox
Style=""{StaticResource MaterialDesignOutlinedTextBox}""
VerticalAlignment=""Top""
Height=""100""
Text=""Some content to force hint to float""
IsEnabled=""false""
Margin=""30""
materialDesign:HintAssist.Hint=""This is a text area""/>
</Grid>");
var textBox = await grid.GetElement<TextBox>("/TextBox");
//contentGrid is the element just inside of the border
var contentGrid = await textBox.GetElement<Grid>("ContentGrid");
var hintBackground = await textBox.GetElement<Border>("HintBackgroundBorder");
Color background = await hintBackground.GetEffectiveBackground(contentGrid);
await Assert.That(background.A).IsEqualTo<byte>(255);
recorder.Success();
}
[Test]
[Description("Pull Request 2192")]
public async Task OnTextBoxHelperTextFontSize_ChangesHelperTextFontSize()
{
await using var recorder = new TestRecorder(App);
var grid = await LoadXaml<Grid>(@"
<Grid Margin=""30"">
<TextBox VerticalAlignment=""Top""
Text=""Some Text""
materialDesign:HintAssist.HelperTextFontSize=""20"">
</TextBox>
</Grid>");
var textBox = await grid.GetElement<TextBox>("/TextBox");
var helpTextBlock = await textBox.GetElement<TextBlock>("/Grid/Canvas/TextBlock");
double fontSize = await helpTextBlock.GetFontSize();
await Assert.That(fontSize).IsEqualTo(20);
recorder.Success();
}
[Test]
public async Task CharacterCount_WithMaxLengthSet_IsDisplayed()
{
await using var recorder = new TestRecorder(App);
var grid = await LoadXaml<Grid>(@"
<Grid Margin=""30"">
<TextBox
MaxLength=""10""
/>
</Grid>");
var textBox = await grid.GetElement<TextBox>("/TextBox");
var characterCounter = await textBox.GetElement<TextBlock>("CharacterCounterTextBlock");
await Assert.That(await characterCounter.GetText()).IsEqualTo("0 / 10");
await textBox.SetText("12345");
await Assert.That(await characterCounter.GetText()).IsEqualTo("5 / 10");
recorder.Success();
}
[Test]
public async Task CharacterCount_WithoutMaxLengthSet_IsCollapsed()
{
await using var recorder = new TestRecorder(App);
var grid = await LoadXaml<Grid>(@"
<Grid Margin=""30"">
<TextBox />
</Grid>");
var textBox = await grid.GetElement<TextBox>("/TextBox");
var characterCounter = await textBox.GetElement<TextBlock>("CharacterCounterTextBlock");
await Assert.That(await characterCounter.GetIsVisible()).IsFalse();
recorder.Success();
}
[Test]
public async Task CharacterCount_WithMaxLengthSetAndCharacterCounterVisibilityCollapsed_IsNotDisplayed()
{
await using var recorder = new TestRecorder(App);
var grid = await LoadXaml<Grid>(@"
<Grid Margin=""30"">
<TextBox
MaxLength=""10""
materialDesign:TextFieldAssist.CharacterCounterVisibility=""Collapsed""
/>
</Grid>");
var textBox = await grid.GetElement<TextBox>("/TextBox");
var characterCounter = await textBox.GetElement<TextBlock>("CharacterCounterTextBlock");
await Assert.That(await characterCounter.GetIsVisible()).IsFalse();
recorder.Success();
}
[Test]
[Description("Issue 2300")]
public async Task HelperText_CanSetFontColorWithAttachedStyle()
{
await using var recorder = new TestRecorder(App);
var grid = await LoadXaml<Grid>(@"
<Grid Margin=""30"">
<TextBox
materialDesign:HintAssist.HelperText=""Test"">
<materialDesign:HintAssist.HelperTextStyle>
<Style TargetType=""TextBlock"" BasedOn=""{StaticResource MaterialDesignHelperTextBlock}"">
<Setter Property=""Foreground"" Value=""Red"" />
</Style>
</materialDesign:HintAssist.HelperTextStyle>
</TextBox>
</Grid>");
var textBox = await grid.GetElement<TextBox>("/TextBox");
var helperText = await textBox.GetElement<TextBlock>("HelperTextTextBlock");
await Assert.That(await helperText.GetForegroundColor()).IsEqualTo(Colors.Red);
recorder.Success();
}
[Test]
[Description("Issue 2362")]
public async Task FloatingOffset_ValuesGetAppropriatelyApplied()
{
await using var recorder = new TestRecorder(App);
var textBox = await LoadXaml<TextBox>(@"
<TextBox Style=""{StaticResource MaterialDesignFloatingHintTextBox}""
materialDesign:HintAssist.Hint=""Hint with offset""
materialDesign:HintAssist.FloatingOffset=""1,-42""
Margin=""100"" VerticalAlignment=""Center""
Text=""Something"" />
");
var hint = await textBox.GetElement<SmartHint>("Hint");
Point offset = await hint.GetFloatingOffset();
await Assert.That(offset.X).IsEqualTo(1);
await Assert.That(offset.Y).IsEqualTo(-42);
recorder.Success();
}
[Test]
[Description("Issue 2390")]
public async Task ContextMenu_FollowsTextBoxFontFamily()
{
await using var recorder = new TestRecorder(App);
var textBox = await LoadXaml<TextBox>(@"<TextBox FontFamily=""Times New Roman""/>");
await textBox.RightClick();
var contextMenu = await textBox.GetElement<ContextMenu>(".ContextMenu");
FontFamily? textBoxFont = await textBox.GetFontFamily();
await Assert.That(textBoxFont?.FamilyNames.Values ?? []).Contains("Times New Roman");
await Wait.For(async () =>
{
await Assert.That(await contextMenu.GetFontFamily()).IsEqualTo(textBoxFont);
});
recorder.Success();
}
[Test]
[Description("Issue 2390")]
public async Task ContextMenu_UsesInheritedFontFamily()
{
await using var recorder = new TestRecorder(App);
var stackPanel = await LoadXaml<StackPanel>(@"
<StackPanel TextElement.FontFamily=""Times New Roman"">
<TextBox />
</StackPanel>
");
var textBox = await stackPanel.GetElement<TextBox>("/TextBox");
var textBoxFont = await textBox.GetFontFamily();
await Assert.That(textBoxFont?.FamilyNames.Values.First()).IsEqualTo("Times New Roman");
IVisualElement<ContextMenu> contextMenu = null!;
await Wait.For(async () =>
{
await textBox.RightClick();
contextMenu = await textBox.GetElement<ContextMenu>(".ContextMenu");
return await contextMenu.GetIsOpen();
});
await Assert.That(await contextMenu.GetFontFamily()).IsEqualTo(textBoxFont);
recorder.Success();
}
[Test]
[Description("Issue 2430")]
public async Task VerticalContentAlignment_ProperlyAlignsText()
{
await using var recorder = new TestRecorder(App);
var textBox = await LoadXaml<TextBox>($@"
<TextBox Height=""100"" Text=""Test""/>
");
var scrollViewer = await textBox.GetElement<ScrollViewer>("PART_ContentHost");
//The default for this changed with issue 2556.
//It should be stretch so that the horizontal scroll bar is at the bottom and not
//pushed to the bottom of the text.
await Assert.That(await scrollViewer.GetVerticalAlignment()).IsEqualTo(VerticalAlignment.Stretch);
foreach (var alignment in Enum.GetValues<VerticalAlignment>())
{
await textBox.SetVerticalContentAlignment(alignment);
await Assert.That(await scrollViewer.GetVerticalContentAlignment()).IsEqualTo(alignment);
}
recorder.Success();
}
[Test]
[Description("Issue 2596")]
public async Task OutlinedTextBox_ValidationErrorMargin_MatchesHelperTextMargin()
{
await using var recorder = new TestRecorder(App);
var stackPanel = await LoadXaml<StackPanel>(@"
<StackPanel>
<TextBox Style=""{StaticResource MaterialDesignOutlinedTextBox}""
materialDesign:HintAssist.Hint=""Hint text""
materialDesign:HintAssist.HelperText=""Helper text"">
<TextBox.Text>
<Binding RelativeSource=""{RelativeSource Self}"" Path=""Tag"" UpdateSourceTrigger=""PropertyChanged"">
<Binding.ValidationRules>
<local:NotEmptyValidationRule ValidatesOnTargetUpdated=""True""/>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
</StackPanel>
", ("local", typeof(NotEmptyValidationRule)));
var textBox = await stackPanel.GetElement<TextBox>("/TextBox");
var errorViewer = await textBox.GetElement<Border>("DefaultErrorViewer");
var helperTextTextBlock = await textBox.GetElement<TextBlock>("HelperTextTextBlock");
Thickness? errorMargin = await errorViewer.GetMargin();
Thickness? textBoxPadding = await textBox.GetPadding();
await Assert.That(errorMargin.HasValue).IsTrue();
await Assert.That(textBoxPadding.HasValue).IsTrue();
await Assert.That(errorMargin.Value.Left - textBoxPadding.Value.Left).IsZero().Because($"Error text does not respect the padding of the TextBox: Error text Margin.Left ({errorMargin.Value.Left}) == TextBox Padding.Left ({textBoxPadding.Value.Left})");
recorder.Success();
}
[Test]
[Description("Issue 2596")]
public async Task FilledTextBox_ValidationErrorMargin_MatchesHelperTextMargin()
{
await using var recorder = new TestRecorder(App);
var stackPanel = await LoadXaml<StackPanel>(@"
<StackPanel>
<TextBox Style=""{StaticResource MaterialDesignFilledTextBox}""
materialDesign:HintAssist.Hint=""Hint text""
materialDesign:HintAssist.HelperText=""Helper text"">
<TextBox.Text>
<Binding RelativeSource=""{RelativeSource Self}"" Path=""Tag"" UpdateSourceTrigger=""PropertyChanged"">
<Binding.ValidationRules>
<local:NotEmptyValidationRule ValidatesOnTargetUpdated=""True""/>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
</StackPanel>
", ("local", typeof(NotEmptyValidationRule)));
var textBox = await stackPanel.GetElement<TextBox>("/TextBox");
var errorViewer = await textBox.GetElement<Border>("DefaultErrorViewer");
var helperTextTextBlock = await textBox.GetElement<TextBlock>("HelperTextTextBlock");
Thickness? errorMargin = await errorViewer.GetProperty<Thickness>(FrameworkElement.MarginProperty);
Thickness? textBoxPadding = await textBox.GetProperty<Thickness>(Control.PaddingProperty);
await Assert.That(errorMargin.HasValue).IsTrue();
await Assert.That(textBoxPadding.HasValue).IsTrue();
await Assert.That(errorMargin.Value.Left - textBoxPadding.Value.Left).IsZero().Because($"Error text does not respect the padding of the TextBox: Error text Margin.Left ({errorMargin.Value.Left}) == TextBox Padding.Left ({textBoxPadding.Value.Left})");
recorder.Success();
}
[Test]
[Arguments("MaterialDesignFloatingHintTextBox", null)]
[Arguments("MaterialDesignFloatingHintTextBox", 5)]
[Arguments("MaterialDesignFloatingHintTextBox", 20)]
[Arguments("MaterialDesignFilledTextBox", null)]
[Arguments("MaterialDesignFilledTextBox", 5)]
[Arguments("MaterialDesignFilledTextBox", 20)]
[Arguments("MaterialDesignOutlinedTextBox", null)]
[Arguments("MaterialDesignOutlinedTextBox", 5)]
[Arguments("MaterialDesignOutlinedTextBox", 20)]
public async Task TextBox_WithHintAndHelperText_RespectsPadding(string styleName, int? padding)
{
await using var recorder = new TestRecorder(App);
// FIXME: Tolerance needed because TextFieldAssist.TextBoxViewMargin is in play and slightly modifies the hint text placement in certain cases.
const double tolerance = 1.5;
string styleAttribute = $"Style=\"{{StaticResource {styleName}}}\"";
string paddingAttribute = padding.HasValue ? $"Padding=\"{padding.Value}\"" : string.Empty;
var textBox = await LoadXaml<TextBox>($@"
<TextBox {styleAttribute} {paddingAttribute}
materialDesign:HintAssist.Hint=""Hint text""
materialDesign:HintAssist.HelperText=""Helper text""
Width=""200"" VerticalAlignment=""Center"" HorizontalAlignment=""Center"" />
");
var contentHost = await textBox.GetElement<ScrollViewer>("PART_ContentHost");
var hint = await textBox.GetElement<SmartHint>("Hint");
var helperText = await textBox.GetElement<TextBlock>("HelperTextTextBlock");
Rect? contentHostCoordinates = await contentHost.GetCoordinates();
Rect? hintCoordinates = await hint.GetCoordinates();
Rect? helperTextCoordinates = await helperText.GetCoordinates();
await Assert.That(Math.Abs(contentHostCoordinates.Value.Left - hintCoordinates.Value.Left)).IsCloseTo(0, tolerance);
await Assert.That(Math.Abs(contentHostCoordinates.Value.Left - helperTextCoordinates.Value.Left)).IsCloseTo(0, tolerance);
recorder.Success();
}
[Test]
[Arguments("MaterialDesignFloatingHintTextBox", null)]
[Arguments("MaterialDesignFloatingHintTextBox", 5)]
[Arguments("MaterialDesignFloatingHintTextBox", 20)]
[Arguments("MaterialDesignFilledTextBox", null)]
[Arguments("MaterialDesignFilledTextBox", 5)]
[Arguments("MaterialDesignFilledTextBox", 20)]
[Arguments("MaterialDesignOutlinedTextBox", null)]
[Arguments("MaterialDesignOutlinedTextBox", 5)]
[Arguments("MaterialDesignOutlinedTextBox", 20)]
public async Task TextBox_WithHintAndValidationError_RespectsPadding(string styleName, int? padding)
{
await using var recorder = new TestRecorder(App);
// FIXME: Tolerance needed because TextFieldAssist.TextBoxViewMargin is in play and slightly modifies the hint text placement in certain cases.
const double tolerance = 1.5;
string styleAttribute = $"Style=\"{{StaticResource {styleName}}}\"";
string paddingAttribute = padding.HasValue ? $"Padding=\"{padding.Value}\"" : string.Empty;
var textBox = await LoadXaml<TextBox>($@"
<TextBox {styleAttribute} {paddingAttribute}
materialDesign:HintAssist.Hint=""Hint text""
materialDesign:HintAssist.HelperText=""Helper text""
Width=""200"" VerticalAlignment=""Center"" HorizontalAlignment=""Center"">
<TextBox.Text>
<Binding RelativeSource=""{{RelativeSource Self}}"" Path=""Tag"" UpdateSourceTrigger=""PropertyChanged"">
<Binding.ValidationRules>
<local:NotEmptyValidationRule ValidatesOnTargetUpdated=""True""/>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
", ("local", typeof(NotEmptyValidationRule)));
var contentHost = await textBox.GetElement<ScrollViewer>("PART_ContentHost");
var hint = await textBox.GetElement<SmartHint>("Hint");
var errorViewer = await textBox.GetElement<Border>("DefaultErrorViewer");
Rect? contentHostCoordinates = await contentHost.GetCoordinates();
Rect? hintCoordinates = await hint.GetCoordinates();
Rect? errorViewerCoordinates = await errorViewer.GetCoordinates();
await Assert.That(Math.Abs(contentHostCoordinates.Value.Left - hintCoordinates.Value.Left)).IsCloseTo(0, tolerance);
await Assert.That(Math.Abs(contentHostCoordinates.Value.Left - errorViewerCoordinates.Value.Left)).IsCloseTo(0, tolerance);
recorder.Success();
}
[Test]
[Arguments(VerticalAlignment.Stretch, VerticalAlignment.Stretch)]
[Arguments(VerticalAlignment.Top, VerticalAlignment.Top)]
[Arguments(VerticalAlignment.Bottom, VerticalAlignment.Bottom)]
[Arguments(VerticalAlignment.Center, VerticalAlignment.Center)]
[Description("Issue 3161")]
public async Task TextBox_MultiLineAndFixedHeight_RespectsVerticalContentAlignment(VerticalAlignment alignment, VerticalAlignment expectedFloatingHintAlignment)
{
await using var recorder = new TestRecorder(App);
var stackPanel = await LoadXaml<StackPanel>($$"""
<StackPanel>
<TextBox Style="{StaticResource MaterialDesignFilledTextBox}"
materialDesign:HintAssist.Hint="Hint text"
VerticalContentAlignment="{{alignment}}"
AcceptsReturn="True"
Height="200" />
</StackPanel>
""");
IVisualElement<TextBox> textBox = await stackPanel.GetElement<TextBox>("/TextBox");
IVisualElement<Grid> contentGrid = await textBox.GetElement<Grid>("ContentGrid");
await Assert.That(await contentGrid.GetVerticalAlignment()).IsEqualTo(expectedFloatingHintAlignment);
recorder.Success();
}
[Test]
[Description("Issue 3176")]
public async Task ValidationErrorTemplate_WithChangingErrors_UpdatesValidation()
{
await using var recorder = new TestRecorder(App);
IVisualElement userControl = await LoadUserControl<ValidationUpdates>();
var textBox = await userControl.GetElement<TextBox>();
var button = await userControl.GetElement<Button>();
await button.LeftClick();
await Wait.For(async() =>
{
var errorViewer = await textBox.GetElement("DefaultErrorViewer");
var textBlock = await errorViewer.GetElement<TextBlock>();
await Assert.That(await textBlock.GetText()).IsEqualTo("Some error + more");
});
recorder.Success();
}
[Test]
[Description("Issue 3914")]
public async Task TextBox_ClearButtonRemainsHidden_WhenInitiallyCollapsedAndMadeVisible()
{
await using var recorder = new TestRecorder(App);
var grid = await LoadXaml<Grid>($"""
<Grid Margin="30">
<TextBox Visibility="Collapsed"
VerticalAlignment="Center"
materialDesign:TextFieldAssist.HasClearButton="True">
</TextBox>
</Grid>
""");
var textBox = await grid.GetElement<TextBox>("/TextBox");
await textBox.SetVisibility(Visibility.Visible);
var clearButton = await grid.GetElement<Button>("PART_ClearButton");
Visibility clearButtonVisibility = await clearButton.GetVisibility();
await Assert.That(clearButtonVisibility).IsEqualTo(Visibility.Collapsed);
recorder.Success();
}
}
public class NotEmptyValidationRule : ValidationRule
{
public override ValidationResult Validate(object value, CultureInfo cultureInfo)
{
return string.IsNullOrWhiteSpace((value ?? "").ToString())
? new ValidationResult(false, "Field is required.")
: ValidationResult.ValidResult;
}
}