-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Expand file tree
/
Copy pathComboBoxTests.cs
More file actions
305 lines (261 loc) · 13.4 KB
/
Copy pathComboBoxTests.cs
File metadata and controls
305 lines (261 loc) · 13.4 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
using System.ComponentModel;
using System.Windows.Media;
using MaterialDesignThemes.UITests.WPF.TextBoxes;
namespace MaterialDesignThemes.UITests.WPF.ComboBoxes;
public class ComboBoxTests : TestBase
{
[Test]
[Description("Pull Request 2192")]
public async Task OnComboBoxHelperTextFontSize_ChangesHelperTextFontSize()
{
var stackPanel = await LoadXaml<StackPanel>(@"
<StackPanel>
<ComboBox
materialDesign:HintAssist.HelperTextFontSize=""20""/>
</StackPanel>");
var comboBox = await stackPanel.GetElement<ComboBox>("/ComboBox");
var helpTextBlock = await comboBox.GetElement<TextBlock>("/Grid/Canvas/TextBlock");
double fontSize = await helpTextBlock.GetFontSize();
await Assert.That(fontSize).IsEqualTo(20);
}
[Test]
[Description("Pull Request 2192")]
public async Task OnFilledComboBoxHelperTextFontSize_ChangesHelperTextFontSize()
{
var stackPanel = await LoadXaml<StackPanel>(@"
<StackPanel>
<ComboBox
Style=""{StaticResource MaterialDesignFilledComboBox}""
materialDesign:HintAssist.HelperTextFontSize=""20""/>
</StackPanel>");
var comboBox = await stackPanel.GetElement<ComboBox>("/ComboBox");
var helpTextBlock = await comboBox.GetElement<TextBlock>("/Grid/Canvas/TextBlock");
double fontSize = await helpTextBlock.GetFontSize();
await Assert.That(fontSize).IsEqualTo(20);
}
[Test]
[Description("Issue 2495")]
public async Task OnComboBox_WithClearButton_ClearsSelection()
{
var stackPanel = await LoadXaml<StackPanel>($@"
<StackPanel>
<ComboBox materialDesign:HintAssist.Hint=""OS""
materialDesign:TextFieldAssist.HasClearButton=""True""
SelectedIndex=""1"">
<ComboBoxItem Content=""Android"" />
<ComboBoxItem Content=""iOS"" />
<ComboBoxItem Content=""Linux"" />
<ComboBoxItem Content=""Windows"" />
</ComboBox>
</StackPanel>");
var comboBox = await stackPanel.GetElement<ComboBox>("/ComboBox");
var clearButton = await comboBox.GetElement<Button>("PART_ClearButton");
int? selectedIndex = await comboBox.GetSelectedIndex();
object? text = await comboBox.GetText();
await Assert.That(selectedIndex >= 0).IsTrue();
await Assert.That(text).IsNotNull();
await clearButton.LeftClick();
await Wait.For(async () =>
{
text = await comboBox.GetText();
await Assert.That(text).IsNull();
selectedIndex = await comboBox.GetSelectedIndex();
await Assert.That(selectedIndex >= 0).IsFalse();
});
}
[Test]
[Description("Issue 2690")]
public async Task OnEditableComboBox_WithDefaultContextMenu_ShowsCutCopyPaste()
{
var comboBox = await LoadXaml<ComboBox>(@"
<ComboBox IsEditable=""True"" Width=""200"" Style=""{StaticResource MaterialDesignComboBox}"">
<ComboBoxItem Content=""Select1"" />
<ComboBoxItem>Select2</ComboBoxItem>
<ComboBoxItem>Select3</ComboBoxItem>
<ComboBoxItem>Select4</ComboBoxItem>
<ComboBoxItem>Select5</ComboBoxItem>
</ComboBox>");
await comboBox.RightClick();
IVisualElement<ContextMenu>? contextMenu = await comboBox.GetContextMenu();
await Assert.That(contextMenu).IsNotNull().Because("No context menu set on the ComboBox");
await Wait.For(async () => await contextMenu!.GetIsVisible());
await AssertMenu("Cut");
await AssertMenu("Copy");
await AssertMenu("Paste");
async Task AssertMenu(string menuHeader)
{
var menuItem = await contextMenu!.GetElement(ElementQuery.PropertyExpression<MenuItem>(x => x.Header, menuHeader));
await Assert.That(menuItem).IsNotNull().Because($"{menuHeader} menu item not found");
}
}
[Test]
[Description("Issue 2713")]
public async Task OnEditableComboBox_ClickInTextArea_FocusesTextBox()
{
var stackPanel = await LoadXaml<StackPanel>(@"
<StackPanel Orientation=""Horizontal"">
<ComboBox x:Name=""EditableComboBox"" IsEditable=""True"" Style=""{StaticResource MaterialDesignComboBox}"">
<ComboBoxItem>Select1</ComboBoxItem>
<ComboBoxItem>Select2</ComboBoxItem>
<ComboBoxItem IsSelected=""True"">Select3</ComboBoxItem>
<ComboBoxItem>Select4</ComboBoxItem>
<ComboBoxItem>Select5</ComboBoxItem>
</ComboBox>
<Button x:Name=""Button"" />
</StackPanel>");
var comboBox = await stackPanel.GetElement<ComboBox>("EditableComboBox");
var editableTextBox = await comboBox.GetElement<TextBox>("PART_EditableTextBox");
var button = await stackPanel.GetElement<Button>("Button");
// Open the ComboBox initially
await comboBox.LeftClick(Position.RightCenter);
await Task.Delay(50, TestContext.Current!.Execution.CancellationToken); // Allow a little time for the drop-down to open (and property to change)
bool wasOpenAfterClickOnToggleButton = await comboBox.GetIsDropDownOpen();
// Focus (i.e. click) another element
await button.LeftClick();
// Click the editable TextBox of the ComboBox
await editableTextBox.LeftClick();
await Task.Delay(50, TestContext.Current.Execution.CancellationToken); // Allow a little time for the drop-down to open (and property to change)
bool wasOpenAfterClickOnEditableTextBox = await comboBox.GetIsDropDownOpen();
bool textBoxHasFocus = await editableTextBox.GetIsFocused();
bool textBoxHasKeyboardFocus = await editableTextBox.GetIsKeyboardFocused();
await Assert.That(wasOpenAfterClickOnToggleButton).IsTrue().Because("ComboBox should have opened drop down when clicking the toggle button");
await Assert.That(wasOpenAfterClickOnEditableTextBox).IsFalse().Because("ComboBox should not have opened drop down when clicking the editable ait wTextBox");
await Assert.That(textBoxHasFocus).IsTrue().Because("Editable TextBox should have focus");
await Assert.That(textBoxHasKeyboardFocus).IsTrue().Because("Editable TextBox should have keyboard focus");
}
[Test]
[Arguments("MaterialDesignFloatingHintComboBox", null)]
[Arguments("MaterialDesignFloatingHintComboBox", 5)]
[Arguments("MaterialDesignFloatingHintComboBox", 20)]
[Arguments("MaterialDesignFilledComboBox", null)]
[Arguments("MaterialDesignFilledComboBox", 5)]
[Arguments("MaterialDesignFilledComboBox", 20)]
[Arguments("MaterialDesignOutlinedComboBox", null)]
[Arguments("MaterialDesignOutlinedComboBox", 5)]
[Arguments("MaterialDesignOutlinedComboBox", 20)]
public async Task ComboBox_WithHintAndHelperText_RespectsPadding(string styleName, int? padding)
{
// 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 comboBox = await LoadXaml<ComboBox>($@"
<ComboBox {styleAttribute} {paddingAttribute}
materialDesign:HintAssist.Hint=""Hint text""
materialDesign:HintAssist.HelperText=""Helper text""
Width=""200"" VerticalAlignment=""Center"" HorizontalAlignment=""Center"" />
");
var contentHost = await comboBox.GetElement<ScrollViewer>("PART_ContentHost");
var hint = await comboBox.GetElement<SmartHint>("Hint");
var helperText = await comboBox.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);
}
[Test]
[Arguments("MaterialDesignFloatingHintComboBox", null)]
[Arguments("MaterialDesignFloatingHintComboBox", 5)]
[Arguments("MaterialDesignFloatingHintComboBox", 20)]
[Arguments("MaterialDesignFilledComboBox", null)]
[Arguments("MaterialDesignFilledComboBox", 5)]
[Arguments("MaterialDesignFilledComboBox", 20)]
[Arguments("MaterialDesignOutlinedComboBox", null)]
[Arguments("MaterialDesignOutlinedComboBox", 5)]
[Arguments("MaterialDesignOutlinedComboBox", 20)]
public async Task ComboBox_WithHintAndValidationError_RespectsPadding(string styleName, int? padding)
{
// 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 comboBox = await LoadXaml<ComboBox>($@"
<ComboBox {styleAttribute} {paddingAttribute}
materialDesign:HintAssist.Hint=""Hint text""
materialDesign:HintAssist.HelperText=""Helper text""
Width=""200"" VerticalAlignment=""Center"" HorizontalAlignment=""Center"">
<ComboBox.Text>
<Binding RelativeSource=""{{RelativeSource Self}}"" Path=""Tag"" UpdateSourceTrigger=""PropertyChanged"">
<Binding.ValidationRules>
<local:NotEmptyValidationRule ValidatesOnTargetUpdated=""True""/>
</Binding.ValidationRules>
</Binding>
</ComboBox.Text>
</ComboBox>
", ("local", typeof(NotEmptyValidationRule)));
var contentHost = await comboBox.GetElement<ScrollViewer>("PART_ContentHost");
var hint = await comboBox.GetElement<SmartHint>("Hint");
var errorViewer = await comboBox.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);
}
[Test]
[Arguments(HorizontalAlignment.Left)]
[Arguments(HorizontalAlignment.Right)]
[Arguments(HorizontalAlignment.Center)]
[Arguments(HorizontalAlignment.Stretch)]
[Description("Issue 3433")]
public async Task ComboBox_WithHorizontalContentAlignment_RespectsAlignment(HorizontalAlignment alignment)
{
var stackPanel = await LoadXaml<StackPanel>($"""
<StackPanel>
<ComboBox HorizontalContentAlignment="{alignment}">
<ComboBoxItem Content="TEST" IsSelected="True" />
</ComboBox>
</StackPanel>
""");
var comboBox = await stackPanel.GetElement<ComboBox>("/ComboBox");
var selectedItemPresenter = await comboBox.GetElement<ContentPresenter>("contentPresenter");
await Assert.That(await selectedItemPresenter.GetHorizontalAlignment()).IsEqualTo(alignment);
}
[Test]
[Arguments("MaterialDesignFloatingHintComboBox", 0, 0, 0, 1)]
[Arguments("MaterialDesignFilledComboBox", 0, 0, 0, 1)]
[Arguments("MaterialDesignOutlinedComboBox", 2, 2, 2, 2)]
[Description("Issue 3623")]
public async Task ComboBox_BorderShouldDependOnAppliedStyle(string style, double left, double top, double right, double bottom)
{
var stackPanel = await LoadXaml<StackPanel>($$"""
<StackPanel>
<ComboBox Style="{StaticResource {{style}}}">
<ComboBoxItem Content="TEST" IsSelected="True" />
</ComboBox>
</StackPanel>
""");
var comboBox = await stackPanel.GetElement<ComboBox>("/ComboBox");
var border = await comboBox.GetElement<Border>("OuterBorder");
await comboBox.LeftClick();
Thickness thickness = await border.GetBorderThickness();
await Assert.That(thickness).IsEqualTo(new Thickness(left, top, right, bottom));
}
[Test]
[Description("Issue 3887")]
public async Task ComboBox_UsesDropDownBackgroundResource_WhenBackgroundIsNotSet()
{
await using var recorder = new TestRecorder(App);
var stackPanel = await LoadXaml<StackPanel>($$"""
<StackPanel>
<ComboBox Width="200">
<ComboBox.Resources>
<SolidColorBrush x:Key="MaterialDesign.Brush.ComboBox.DropDown.Background"
Color="#CC336699" />
</ComboBox.Resources>
<ComboBoxItem Content="Android" />
<ComboBoxItem Content="iOS" />
<ComboBoxItem Content="Linux" />
</ComboBox>
</StackPanel>
""");
var comboBox = await stackPanel.GetElement<ComboBox>();
await comboBox.LeftClick(Position.RightCenter);
var popup = await Wait.For(async () => await comboBox.GetElement<ComboBoxPopup>("PART_Popup"));
Color? popupBackground = await popup.GetBackgroundColor();
await Assert.That(popupBackground).IsNotNull();
await Assert.That(popupBackground).IsEqualTo((Color)ColorConverter.ConvertFromString("#CC336699"));
recorder.Success();
}
}