-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Expand file tree
/
Copy pathTabControlTests.cs
More file actions
508 lines (423 loc) · 22 KB
/
Copy pathTabControlTests.cs
File metadata and controls
508 lines (423 loc) · 22 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
using System.ComponentModel;
using System.Windows.Media;
using MaterialDesignThemes.Wpf.Internal;
namespace MaterialDesignThemes.UITests.WPF.TabControls;
public class TabControlTests : TestBase
{
[Test]
[Description("Issue 2602")]
public async Task OnLoad_ThemeBrushesSet()
{
await using var recorder = new TestRecorder(App);
//Arrange
IVisualElement<TabControl> tabControl = await LoadXaml<TabControl>(@"
<TabControl
materialDesign:ColorZoneAssist.Mode=""PrimaryMid""
Style=""{StaticResource MaterialDesignFilledTabControl}"">
<TabItem Header=""TAB 1"">
<TextBlock Margin=""8"" Text=""PrimaryMid Tab 1"" />
</TabItem>
<TabItem Header=""TAB 2"">
<TextBlock Margin=""8"" Text=""PrimaryMid Tab 2"" />
</TabItem>
</TabControl>");
IVisualElement<TextBlock> textBlock = await tabControl.GetElement<TextBlock>(@"/TabItem[0]/TextBlock[0]");
IVisualElement<Border> selectedTabBorder = await tabControl.GetElement<Border>(@"/TabItem[0]~SelectionHighlightBorder");
//Act
Color? foreground = await textBlock.GetForegroundColor();
Color? background = await textBlock.GetEffectiveBackground();
Color? selectedTabUnderline = await selectedTabBorder.GetBorderBrushColor();
//Assert
Color foregroundValue = await Assert.That(foreground).IsNotNull();
Color backgroundValue = await Assert.That(background).IsNotNull();
await MaterialDesignSpec.AssertContrastRatio(foregroundValue, backgroundValue, MaterialDesignSpec.MinimumContrastSmallText);
await Assert.That(selectedTabUnderline).IsEqualTo(foregroundValue);
recorder.Success();
}
[Description("Issue 2983")]
[Test]
[Arguments("Center", true)]
[Arguments("Center", false)]
[Arguments("Left", true)]
[Arguments("Left", false)]
[Arguments("Right", true)]
[Arguments("Right", false)]
[Arguments("Stretch", true)]
[Arguments("Stretch", false)]
[Arguments("", true)]
[Arguments("", false)]
public async Task TabItem_ShouldKeepDataContext_WhenContextMenuOpens(string horizontalContentAlignment, bool hasUniformTabWidth)
{
await using var recorder = new TestRecorder(App);
string alignment = string.Empty;
if (!string.IsNullOrEmpty(horizontalContentAlignment))
{
alignment = $"HorizontalContentAlignment=\"{horizontalContentAlignment}\"";
}
//Arrange
IVisualElement<StackPanel> stackPanel = await LoadXaml<StackPanel>(@$"
<StackPanel Orientation=""Vertical"">
<TabControl
{alignment}
materialDesign:TabAssist.HasUniformTabWidth=""{hasUniformTabWidth}""
materialDesign:ColorZoneAssist.Mode=""PrimaryMid""
Style=""{{StaticResource MaterialDesignFilledTabControl}}"">
<system:String>aaaa</system:String>
<system:String>bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb</system:String>
<TabControl.ItemTemplate>
<DataTemplate DataType=""system:String"">
<TextBlock Text=""{{Binding}}"" />
</DataTemplate>
</TabControl.ItemTemplate>
<TabControl.ContentTemplate>
<DataTemplate DataType=""system:String"">
<TextBlock Text=""{{Binding}}"" />
</DataTemplate>
</TabControl.ContentTemplate>
</TabControl>
<Button Margin=""50"" Width=""200"" Content=""Button with context menu"">
<Button.ContextMenu>
<ContextMenu>
<MenuItem Header=""Menu item"" />
</ContextMenu>
</Button.ContextMenu>
</Button>
</StackPanel>", ("system", typeof(string)));
IVisualElement<TabControl> tabControl = await stackPanel.GetElement<TabControl>();
IVisualElement<Button> button = await stackPanel.GetElement<Button>();
// Assert initial data context
IVisualElement<TabItem> tabItem = await tabControl.GetElement<TabItem>();
object? dataContext = await tabItem.GetDataContext();
await Assert.That(dataContext).IsEqualTo("aaaa");
// Act
await button.MoveCursorTo();
await button.RightClick();
await tabControl.MoveCursorTo();
await tabControl.LeftClick(Position.TopLeft);
await Task.Delay(50, TestContext.Current!.CancellationToken); // allow a little time for the disconnect to occur
// Assert data context still present
tabItem = await tabControl.GetElement<TabItem>();
dataContext = await tabItem.GetDataContext();
await Assert.That(dataContext).IsEqualTo("aaaa");
recorder.Success();
}
[Test]
[Description("Issue 3271")]
public async Task TabControl_ShouldRespectSelectedContentTemplate_WhenSetDirectlyOnTabItem()
{
await using var recorder = new TestRecorder(App);
//Arrange
IVisualElement<TabControl> tabControl = await LoadXaml<TabControl>("""
<TabControl materialDesign:ColorZoneAssist.Mode="PrimaryMid"
Style="{StaticResource MaterialDesignFilledTabControl}">
<TabControl.Resources>
<DataTemplate x:Key="CustomContentTemplate">
<Border Background="Fuchsia" Padding="10" Margin="10" CornerRadius="10">
<TextBlock Text="{Binding .}" />
</Border>
</DataTemplate>
</TabControl.Resources>
<TabItem Content="Tab content string" ContentTemplate="{StaticResource CustomContentTemplate}" />
</TabControl>
""");
IVisualElement<Border> selectedContentBorder = await tabControl.GetElement<Border>("PART_BorderSelectedContent");
//Act
var customContentBorder = await selectedContentBorder.GetElement<Border>("/Border");
IVisualElement<TextBlock> customContent = await customContentBorder.GetElement<TextBlock>(@"/TextBlock");
//Assert
await Assert.That(await customContentBorder.GetBackgroundColor()).IsEqualTo(Colors.Fuchsia);
await Assert.That(await customContent.GetText()).IsEqualTo("Tab content string");
recorder.Success();
}
[Test]
[Arguments("")] // UniformGrid style
[Arguments("HorizontalContentAlignment=\"Left\"")] // VirtualizingStackPanel style
public async Task ScrollingTabs_WithMoreTabsThanScreenRealEstate_ShouldAddLeftAndRightMarginToHeaderPanel(string additionalProperties)
{
await using var recorder = new TestRecorder(App);
//Arrange
const int numTabs = 20;
StringBuilder xaml = new($"<TabControl {additionalProperties}>");
for (int i = 1; i <= numTabs; i++)
{
xaml.Append($"""
<TabItem Header="TAB {i}">
<TextBlock Margin="8" Text="Tab {i}" />
</TabItem>
""");
}
xaml.Append("</TabControl>");
IVisualElement<TabControl> tabControl = await LoadXaml<TabControl>(xaml.ToString());
IVisualElement<PaddedBringIntoViewStackPanel> headerPanel = await tabControl.GetElement<PaddedBringIntoViewStackPanel>();
double offsetWhenOverflowingWidth = await headerPanel.GetHeaderPadding();
//Act
Thickness margin = await headerPanel.GetMargin();
// Assert
await Assert.That(offsetWhenOverflowingWidth).IsGreaterThan(0);
await Assert.That(margin.Left).IsEqualTo(offsetWhenOverflowingWidth);
await Assert.That(margin.Right).IsEqualTo(offsetWhenOverflowingWidth);
recorder.Success();
}
[Test]
[Arguments("")] // UniformGrid style
[Arguments("HorizontalContentAlignment=\"Left\"")] // VirtualizingStackPanel style
public async Task ScrollingTabs_WithLessTabsThanScreenRealEstate_ShouldNotAddLeftAndRightMarginToHeaderPanel(string additionalProperties)
{
await using var recorder = new TestRecorder(App);
//Arrange
const int numTabs = 5;
StringBuilder xaml = new($"<TabControl {additionalProperties}>");
for (int i = 1; i <= numTabs; i++)
{
xaml.Append($"""
<TabItem Header="TAB {i}">
<TextBlock Margin="8" Text="Tab {i}" />
</TabItem>
""");
}
xaml.Append("</TabControl>");
IVisualElement<TabControl> tabControl = await LoadXaml<TabControl>(xaml.ToString());
IVisualElement<PaddedBringIntoViewStackPanel> headerPanel = await tabControl.GetElement<PaddedBringIntoViewStackPanel>();
double offsetWhenOverflowingWidth = await headerPanel.GetHeaderPadding();
//Act
Thickness margin = await headerPanel.GetMargin();
// Assert
await Assert.That(offsetWhenOverflowingWidth).IsGreaterThan(0);
await Assert.That(margin.Left).IsEqualTo(0);
await Assert.That(margin.Right).IsEqualTo(0);
recorder.Success();
}
[Test]
[Arguments("", 5, false)] // UniformGrid style
[Arguments("", 20, true)] // UniformGrid style
[Arguments("HorizontalContentAlignment=\"Left\"", 5, false)] // VirtualizingStackPanel style
[Arguments("HorizontalContentAlignment=\"Left\"", 20, true)] // VirtualizingStackPanel style
public async Task ScrollingTabs_WithNavigationPanels_ShouldCorrectlySetIsOverflowingAndNavigationPanelVisibility(string additionalProperties, int numberOfTabs, bool expectedToOverflow)
{
await using var recorder = new TestRecorder(App);
//Arrange
StringBuilder xaml = new($"""
<TabControl materialDesign:TabAssist.UseNavigationPanel="True" {additionalProperties}>
""");
for (int i = 1; i <= numberOfTabs; i++)
{
xaml.Append($"""
<TabItem Header="TAB {i}">
<TextBlock Margin="8" Text="Tab {i}" />
</TabItem>
""");
}
xaml.Append("</TabControl>");
IVisualElement<TabControl> tabControl = await LoadXaml<TabControl>(xaml.ToString());
IVisualElement<StackPanel> navigationPanelLeft = await (await tabControl.GetElement<Grid>("NavigationPanelLeft")).GetElement<StackPanel>();
IVisualElement<StackPanel> navigationPanelRight = await tabControl.GetElement<StackPanel>("NavigationPanelRight");
static bool GetIsOverflowing(TabControl tc) => TabAssist.GetIsOverflowing(tc);
//Act
bool isOverflowing = await tabControl.RemoteExecute(GetIsOverflowing);
Visibility navigationPanelLeftVisibility = await navigationPanelLeft.GetVisibility();
Visibility navigationPanelRightVisibility = await navigationPanelRight.GetVisibility();
// Assert
await Assert.That(isOverflowing).IsEqualTo(expectedToOverflow);
await Assert.That(navigationPanelLeftVisibility).IsEqualTo(expectedToOverflow ? Visibility.Visible : Visibility.Collapsed);
await Assert.That(navigationPanelRightVisibility).IsEqualTo(expectedToOverflow ? Visibility.Visible : Visibility.Collapsed);
recorder.Success();
}
[Test]
[Arguments("")] // UniformGrid style
[Arguments("HorizontalContentAlignment=\"Left\"")] // VirtualizingStackPanel style
public async Task ScrollingTabs_WithNavigationPanelLeft_ShouldCorrectlySetIsOverflowingAndNavigationPanelLeftVisibility(string additionalProperties)
{
await using var recorder = new TestRecorder(App);
//Arrange
StringBuilder xaml = new($"""
<TabControl materialDesign:TabAssist.UseNavigationPanel="True" materialDesign:TabAssist.NavigationPanelPlacement="Left" {additionalProperties}>
""");
const int numTabs = 20;
for (int i = 1; i <= numTabs; i++)
{
xaml.Append($"""
<TabItem Header="TAB {i}">
<TextBlock Margin="8" Text="Tab {i}" />
</TabItem>
""");
}
xaml.Append("</TabControl>");
IVisualElement<TabControl> tabControl = await LoadXaml<TabControl>(xaml.ToString());
IVisualElement<StackPanel> navigationPanel = await (await tabControl.GetElement<Grid>("NavigationPanelLeft")).GetElement<StackPanel>();
static bool GetIsOverflowing(TabControl tc) => TabAssist.GetIsOverflowing(tc);
//Act
bool isOverflowing = await tabControl.RemoteExecute(GetIsOverflowing);
Visibility navigationPanelVisibility = await navigationPanel.GetVisibility();
// Assert
await Assert.That(isOverflowing).IsEqualTo(true);
await Assert.That(navigationPanelVisibility).IsEqualTo(Visibility.Visible);
recorder.Success();
}
[Test]
[Arguments("")] // UniformGrid style
[Arguments("HorizontalContentAlignment=\"Left\"")] // VirtualizingStackPanel style
public async Task ScrollingTabs_WithNavigationPanelRight_ShouldCorrectlySetIsOverflowingAndNavigationPanelLeftVisibility(string additionalProperties)
{
await using var recorder = new TestRecorder(App);
//Arrange
StringBuilder xaml = new($"""
<TabControl materialDesign:TabAssist.UseNavigationPanel="True" materialDesign:TabAssist.NavigationPanelPlacement="Right" {additionalProperties}>
""");
const int numTabs = 20;
for (int i = 1; i <= numTabs; i++)
{
xaml.Append($"""
<TabItem Header="TAB {i}">
<TextBlock Margin="8" Text="Tab {i}" />
</TabItem>
""");
}
xaml.Append("</TabControl>");
IVisualElement<TabControl> tabControl = await LoadXaml<TabControl>(xaml.ToString());
IVisualElement<StackPanel> navigationPanel = await tabControl.GetElement<StackPanel>("NavigationPanelRight");
static bool GetIsOverflowing(TabControl tc) => TabAssist.GetIsOverflowing(tc);
//Act
bool isOverflowing = await tabControl.RemoteExecute(GetIsOverflowing);
Visibility navigationPanelVisibility = await navigationPanel.GetVisibility();
// Assert
await Assert.That(isOverflowing).IsEqualTo(true);
await Assert.That(navigationPanelVisibility).IsEqualTo(Visibility.Visible);
recorder.Success();
}
[Test]
[Arguments("")] // UniformGrid style
[Arguments("HorizontalContentAlignment=\"Left\"")] // VirtualizingStackPanel style
public async Task ScrollingTabs_WithNavigationPanelAndSelectBehavior_ShouldChangeSelectedTabWhenClicked(string additionalProperties)
{
await using var recorder = new TestRecorder(App);
//Arrange
StringBuilder xaml = new($"""
<TabControl materialDesign:TabAssist.UseNavigationPanel="True" materialDesign:TabAssist.NavigationPanelBehavior="Select" {additionalProperties}>
""");
const int numTabs = 20;
for (int i = 1; i <= numTabs; i++)
{
xaml.Append($"""
<TabItem Header="TAB {i}">
<TextBlock Margin="8" Text="Tab {i}" />
</TabItem>
""");
}
xaml.Append("</TabControl>");
IVisualElement<TabControl> tabControl = await LoadXaml<TabControl>(xaml.ToString());
IVisualElement<ScrollViewer> scrollViewer = await tabControl.GetElement<ScrollViewer>();
IVisualElement<StackPanel> navigationPanel = await tabControl.GetElement<StackPanel>("NavigationPanelRight");
IVisualElement<Button> rightNextButton = await navigationPanel.GetElement<Button>("RightNextButton");
static bool GetIsOverflowing(TabControl tc) => TabAssist.GetIsOverflowing(tc);
//Act
await Wait.For(async () => await tabControl.RemoteExecute(GetIsOverflowing));
await Wait.For(async () => await rightNextButton.GetIsEnabled());
await Wait.For(async () =>
{
await rightNextButton.LeftClick();
return await tabControl.GetSelectedIndex() > 0;
}, new Retry(10, TimeSpan.FromSeconds(5)));
// Assert
await Assert.That(await tabControl.GetSelectedIndex()).IsGreaterThanOrEqualTo(1);
await Assert.That(await scrollViewer.GetContentHorizontalOffset()).IsEqualTo(0);
recorder.Success();
}
[Test]
[Arguments("")] // UniformGrid style
[Arguments("HorizontalContentAlignment=\"Left\"")] // VirtualizingStackPanel style
public async Task ScrollingTabs_WithNavigationPanelAndScrollBehavior_ShouldScrollWhenClicked(string additionalProperties)
{
await using var recorder = new TestRecorder(App);
//Arrange
StringBuilder xaml = new($"""
<TabControl materialDesign:TabAssist.UseNavigationPanel="True" materialDesign:TabAssist.NavigationPanelBehavior="Scroll" {additionalProperties}>
""");
const int numTabs = 20;
for (int i = 1; i <= numTabs; i++)
{
xaml.Append($"""
<TabItem Header="TAB {i}">
<TextBlock Margin="8" Text="Tab {i}" />
</TabItem>
""");
}
xaml.Append("</TabControl>");
IVisualElement<TabControl> tabControl = await LoadXaml<TabControl>(xaml.ToString());
IVisualElement<ScrollViewer> scrollViewer = await tabControl.GetElement<ScrollViewer>();
IVisualElement<StackPanel> navigationPanel = await tabControl.GetElement<StackPanel>("NavigationPanelRight");
IVisualElement<Button> rightNextButton = await navigationPanel.GetElement<Button>("RightNextButton");
static bool GetIsOverflowing(TabControl tc) => TabAssist.GetIsOverflowing(tc);
//Act
await Wait.For(async () => await tabControl.RemoteExecute(GetIsOverflowing));
await Wait.For(async() => await rightNextButton.GetIsEnabled());
await Wait.For(async () =>
{
await rightNextButton.LeftClick();
return await scrollViewer.GetContentHorizontalOffset() > 0;
}, new Retry(10, TimeSpan.FromSeconds(5)));
// Assert
await Assert.That(await tabControl.GetSelectedIndex()).IsEqualTo(0);
await Assert.That(await scrollViewer.GetContentHorizontalOffset()).IsGreaterThan(0);
recorder.Success();
}
[Test]
public async Task ScrollingTabs_NavigationPanel_ScrollsAndBringsSelectedTabIntoView()
{
await using var recorder = new TestRecorder(App);
// Arrange
const int numTabs = 20;
StringBuilder xaml = new($"<TabControl Width=\"504\" materialDesign:TabAssist.HeaderBehavior=\"Scrolling\" materialDesign:TabAssist.UseNavigationPanel=\"True\" materialDesign:TabAssist.NavigationPanelPlacement=\"Right\">");
for (int i = 1; i <= numTabs; i++)
{
xaml.Append($"""
<TabItem Header="TAB {i}">
<TextBlock Margin="8" Text="Tab {i}" />
</TabItem>
""");
}
xaml.Append("</TabControl>");
IVisualElement<TabControl> tabControl = await LoadXaml<TabControl>(xaml.ToString());
IVisualElement<PaddedBringIntoViewStackPanel> headerPanel = await tabControl.GetElement<PaddedBringIntoViewStackPanel>();
IVisualElement<ScrollViewer> scrollViewer = await tabControl.GetElement<ScrollViewer>();
IVisualElement<StackPanel> navigationPanel = await tabControl.GetElement<StackPanel>("NavigationPanelRight");
IVisualElement<Button> rightNextButton = await navigationPanel.GetElement<Button>("RightNextButton");
IVisualElement<Button> rightPreviousButton = await navigationPanel.GetElement<Button>("RightPreviousButton");
static bool GetIsOverflowing(TabControl tc) => TabAssist.GetIsOverflowing(tc);
var retry = new Retry(10, TimeSpan.FromSeconds(5));
// Act
// Wait for layout to complete and overflow to be detected
await Wait.For(async () => await tabControl.RemoteExecute(GetIsOverflowing));
// Scroll right once
double offsetBefore = await scrollViewer.GetContentHorizontalOffset();
await Wait.For(async () => await rightNextButton.GetIsEnabled());
await Task.Delay(200);
await rightNextButton.LeftClick();
await Wait.For(async () => await scrollViewer.GetContentHorizontalOffset() > offsetBefore, retry);
// Select an arbitrary tab (index 8)
IVisualElement<TabItem> someTab = await tabControl.GetElement<TabItem>("/TabItem[8]");
await someTab.LeftClick();
await Wait.For(async () => await tabControl.GetSelectedIndex() == 8, retry);
// Scroll back to the left
double offsetAfterScrollRight = await scrollViewer.GetContentHorizontalOffset();
await Wait.For(async () => await rightPreviousButton.GetIsEnabled());
await rightPreviousButton.LeftClick();
await Wait.For(async () => await scrollViewer.GetContentHorizontalOffset() < offsetAfterScrollRight, retry);
// Select the last visible tab
// NB: The last visible tab is "hardcoded" to always be index 4 due to the fixed Width of 504 on the TabControl
IVisualElement<TabItem> lastVisibleTab = await tabControl.GetElement<TabItem>($"/TabItem[4]");
await lastVisibleTab.LeftClick();
await Wait.For(async () => await tabControl.GetSelectedIndex() == 4, retry);
// Assert
int selectedTabIndex = await tabControl.GetSelectedIndex();
await Assert.That(selectedTabIndex).IsEqualTo(4);
double offsetWhenOverflowingWidth = await headerPanel.GetHeaderPadding();
Thickness margin = await headerPanel.GetMargin();
await Assert.That(offsetWhenOverflowingWidth).IsGreaterThan(0);
await Assert.That(margin.Left).IsEqualTo(offsetWhenOverflowingWidth);
static Thickness GetNavigationPanelMargin(TabControl tc) => TabAssist.GetNavigationPanelMargin(tc);
Thickness navigationPanelMargin = await tabControl.RemoteExecute(GetNavigationPanelMargin);
double navigationPanelActualWidth = await navigationPanel.GetActualWidth() + navigationPanelMargin.Left + navigationPanelMargin.Right;
await Assert.That(margin.Right).IsEqualTo(offsetWhenOverflowingWidth + navigationPanelActualWidth);
recorder.Success();
}
}