Skip to content

Commit 50a7885

Browse files
authored
Adding UI Test for the condition (#3079)
1 parent 5a5c3be commit 50a7885

File tree

2 files changed

+83
-41
lines changed

2 files changed

+83
-41
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using System.ComponentModel;
2+
3+
namespace MaterialDesignThemes.UITests.WPF.ContentControls;
4+
5+
public class ContentControlTests : TestBase
6+
{
7+
public ContentControlTests(ITestOutputHelper output)
8+
: base(output)
9+
{
10+
}
11+
12+
13+
[Fact]
14+
[Description("Issue 2510")]
15+
public async Task ClearButton_InsideOfControlTemplate_CanStillClearContent()
16+
{
17+
await using var recorder = new TestRecorder(App);
18+
19+
//Arrange
20+
var grid = await LoadXaml<ContentControl>(@"
21+
<ContentControl>
22+
<ContentControl.ContentTemplate>
23+
<DataTemplate>
24+
<TextBox Text=""Some Text"" materialDesign:TextFieldAssist.HasClearButton=""True""/>
25+
</DataTemplate>
26+
</ContentControl.ContentTemplate>
27+
</ContentControl>");
28+
var textBox = await grid.GetElement<TextBox>("/TextBox");
29+
var clearButton = await grid.GetElement<Button>("PART_ClearButton");
30+
31+
string? initial = await textBox.GetText();
32+
33+
//Act
34+
await clearButton.LeftClick();
35+
36+
//Assert
37+
Assert.Equal("Some Text", initial);
38+
Assert.True(string.IsNullOrEmpty(await textBox.GetText()));
39+
40+
recorder.Success();
41+
}
42+
43+
}
Lines changed: 40 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,58 @@
1-
namespace MaterialDesignThemes.Wpf.Internal
1+
namespace MaterialDesignThemes.Wpf.Internal;
2+
3+
public static class ClearText
24
{
3-
public static class ClearText
4-
{
5-
public static readonly RoutedCommand ClearCommand = new();
5+
public static readonly RoutedCommand ClearCommand = new();
66

7-
public static bool GetHandlesClearCommand(DependencyObject obj)
8-
=> (bool)obj.GetValue(HandlesClearCommandProperty);
7+
public static bool GetHandlesClearCommand(DependencyObject obj)
8+
=> (bool)obj.GetValue(HandlesClearCommandProperty);
99

10-
public static void SetHandlesClearCommand(DependencyObject obj, bool value)
11-
=> obj.SetValue(HandlesClearCommandProperty, value);
10+
public static void SetHandlesClearCommand(DependencyObject obj, bool value)
11+
=> obj.SetValue(HandlesClearCommandProperty, value);
1212

13-
public static readonly DependencyProperty HandlesClearCommandProperty =
14-
DependencyProperty.RegisterAttached("HandlesClearCommand", typeof(bool), typeof(ClearText), new PropertyMetadata(false, OnHandlesClearCommandChanged));
13+
public static readonly DependencyProperty HandlesClearCommandProperty =
14+
DependencyProperty.RegisterAttached("HandlesClearCommand", typeof(bool), typeof(ClearText), new PropertyMetadata(false, OnHandlesClearCommandChanged));
1515

16-
private static void OnHandlesClearCommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
16+
private static void OnHandlesClearCommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
17+
{
18+
if (d is UIElement element)
1719
{
18-
if (d is UIElement element)
20+
if ((bool)e.NewValue)
1921
{
20-
if ((bool)e.NewValue)
21-
{
22-
element.CommandBindings.Add(new CommandBinding(ClearCommand, OnClearCommand));
23-
}
24-
else
22+
element.CommandBindings.Add(new CommandBinding(ClearCommand, OnClearCommand));
23+
}
24+
else
25+
{
26+
for (int i = element.CommandBindings.Count - 1; i >= 0; i--)
2527
{
26-
for (int i = element.CommandBindings.Count - 1; i >= 0; i--)
28+
if (element.CommandBindings[i].Command == ClearCommand)
2729
{
28-
if (element.CommandBindings[i].Command == ClearCommand)
29-
{
30-
element.CommandBindings.RemoveAt(i);
31-
}
30+
element.CommandBindings.RemoveAt(i);
3231
}
3332
}
3433
}
34+
}
3535

36-
static void OnClearCommand(object sender, ExecutedRoutedEventArgs e)
36+
static void OnClearCommand(object sender, ExecutedRoutedEventArgs e)
37+
{
38+
switch (sender)
3739
{
38-
switch (sender)
39-
{
40-
case DatePicker datePicker:
41-
datePicker.SetCurrentValue(DatePicker.SelectedDateProperty, null);
42-
break;
43-
case TextBox textBox:
44-
textBox.SetCurrentValue(TextBox.TextProperty, null);
45-
break;
46-
case ComboBox comboBox:
47-
comboBox.SetCurrentValue(ComboBox.TextProperty, null);
48-
comboBox.SetCurrentValue(Selector.SelectedItemProperty, null);
49-
break;
50-
case PasswordBox passwordBox:
51-
passwordBox.Password = null;
52-
break;
53-
}
54-
e.Handled = true;
40+
case DatePicker datePicker:
41+
datePicker.SetCurrentValue(DatePicker.SelectedDateProperty, null);
42+
break;
43+
case TextBox textBox:
44+
textBox.SetCurrentValue(TextBox.TextProperty, null);
45+
break;
46+
case ComboBox comboBox:
47+
comboBox.SetCurrentValue(ComboBox.TextProperty, null);
48+
comboBox.SetCurrentValue(Selector.SelectedItemProperty, null);
49+
break;
50+
case PasswordBox passwordBox:
51+
passwordBox.Password = null;
52+
break;
5553
}
54+
e.Handled = true;
5655
}
57-
5856
}
57+
5958
}

0 commit comments

Comments
 (0)