-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathTabBarTests.cs
577 lines (486 loc) · 18.4 KB
/
TabBarTests.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Uno.Toolkit.RuntimeTests.Extensions;
using Uno.Toolkit.RuntimeTests.Helpers;
using Uno.Toolkit.UI;
using Uno.UI.RuntimeTests;
using Uno.UI.Extensions;
using Windows.Foundation;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.UI;
using Windows.Foundation.Metadata;
using System.ComponentModel;
#if IS_WINUI
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Data;
using Microsoft.UI;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Media.Imaging;
#else
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;
#endif
namespace Uno.Toolkit.RuntimeTests.Tests
{
[TestClass]
[RunsOnUIThread]
internal partial class TabBarTests // test cases
{
[TestMethod]
public async Task TabBar1285_ICS_With_TBI_ItemTemplate()
{
// note: this bug doesnt happen with ItemsSource = [TBI,...]
// because IsItemItsOwnContainerOverride=true. It only occurs
// with the ItemTemplate>DataTemplate>TBI setup (IsUsingOwnContainerAsTemplateRoot),
// which cause a ContentPresnter to be created as the item container.
var source = Enumerable.Range(0, 1).ToArray();
var SUT = new TabBar
{
ItemsSource = source,
ItemTemplate = XamlHelper.LoadXaml<DataTemplate>("""
<DataTemplate>
<utu:TabBarItem Content="{Binding}" />
</DataTemplate>
"""),
};
await UnitTestUIContentHelperEx.SetContentAndWait(SUT);
}
[TestMethod]
[DataRow(new int[0], null)]
[DataRow(new[] { 1 }, 1)]
[DataRow(new[] { 1, 1 }, 1)]
[DataRow(new[] { 1, 2 }, 2)]
public async Task TabBarTapSelection(int[] selectionSequence, object? expectation)
{
var source = Enumerable.Range(0, 3).ToArray();
var SUT = new TabBar
{
ItemsSource = source,
};
await UnitTestUIContentHelperEx.SetContentAndWait(SUT);
Assert.IsNull(SUT.SelectedItem);
foreach (var i in selectionSequence)
{
((TabBarItem)SUT.ContainerFromIndex(i)).ExecuteTap();
await UnitTestsUIContentHelper.WaitForIdle();
}
Assert.AreEqual((int?)expectation, SUT.SelectedItem);
}
[TestMethod]
public async Task SetSelectedItem()
{
var source = Enumerable.Range(0, 3).Select(x => new TabBarItem { Content = x }).ToArray();
var SUT = new TabBar
{
ItemsSource = source,
};
source[0].IsSelectable = false;
await UnitTestUIContentHelperEx.SetContentAndWait(SUT);
Assert.IsNull(SUT.SelectedItem);
// should not select when IsSelectable is false
SUT.SelectedItem = source[0];
await UnitTestsUIContentHelper.WaitForIdle();
Assert.AreEqual(-1, SUT.SelectedIndex);
Assert.IsFalse(source[0].IsSelected);
// should select
SUT.SelectedItem = source[1];
await UnitTestsUIContentHelper.WaitForIdle();
Assert.AreEqual(source[1], SUT.SelectedItem);
Assert.AreEqual(1, SUT.SelectedIndex);
Assert.IsTrue(source[1].IsSelected);
}
[TestMethod]
public async Task SetSelectedItem_SimpleSource()
{
var source = Enumerable.Range(0, 3).ToArray();
var SUT = new TabBar
{
ItemsSource = source,
SelectedItem = 1,
};
await UnitTestUIContentHelperEx.SetContentAndWait(SUT);
Assert.AreEqual(source[1], SUT.SelectedItem);
Assert.AreEqual(1, SUT.SelectedIndex);
}
[TestMethod]
public async Task SetSelectedIndex()
{
var source = Enumerable.Range(0, 3).Select(x => new TabBarItem { Content = x }).ToArray();
var SUT = new TabBar
{
ItemsSource = source,
};
source[0].IsSelectable = false;
await UnitTestUIContentHelperEx.SetContentAndWait(SUT);
Assert.IsNull(SUT.SelectedItem);
// should not select when IsSelectable is false
SUT.SelectedIndex = 0;
await UnitTestsUIContentHelper.WaitForIdle();
Assert.IsNull(SUT.SelectedItem);
Assert.IsFalse(source[0].IsSelected);
// should select
SUT.SelectedIndex = 1;
await UnitTestsUIContentHelper.WaitForIdle();
Assert.AreEqual(source[1], SUT.SelectedItem);
Assert.AreEqual(1, SUT.SelectedIndex);
Assert.IsTrue(source[1].IsSelected);
}
[TestMethod]
public async Task Verify_Indicator_Max_Size()
{
var source = Enumerable.Range(0, 3).Select(x => new TabBarItem { Content = x }).ToArray();
var SUT = new TabBar
{
ItemsSource = source,
SelectionIndicatorContent = "asd",
SelectionIndicatorContentTemplate = XamlHelper.LoadXaml<DataTemplate>("""
<DataTemplate>
<Border x:Name="SutIndicator" Height="5" Background="Red" />
</DataTemplate>
"""),
};
await UnitTestUIContentHelperEx.SetContentAndWait(SUT);
var presenter = SUT.GetFirstDescendant<TabBarSelectionIndicatorPresenter>(x => x.Visibility == Visibility.Visible);
var indicator = presenter?.GetFirstDescendant<Border>("SutIndicator")!;
Assert.IsNotNull(indicator, "Failed to find Border#SutIndicator");
source[0].IsSelected = true;
await UnitTestsUIContentHelper.WaitForIdle();
var expectedWidth = SUT.ActualWidth / 3;
var indicatorWidth = indicator.ActualWidth;
Assert.AreEqual(expectedWidth, indicatorWidth, delta: 1f);
source[1].Visibility = Visibility.Collapsed;
await UnitTestsUIContentHelper.WaitForIdle();
await UnitTestUIContentHelperEx.WaitFor(() => indicator.ActualWidth > indicatorWidth, timeoutMS: 2000);
expectedWidth = SUT.ActualWidth / 2;
Assert.AreEqual(expectedWidth, indicator.ActualWidth, delta: 1f);
}
[TestMethod]
[DataRow(Orientation.Horizontal, IndicatorTransitionMode.Snap)]
[DataRow(Orientation.Horizontal, IndicatorTransitionMode.Slide)]
[DataRow(Orientation.Vertical, IndicatorTransitionMode.Snap)]
[DataRow(Orientation.Vertical, IndicatorTransitionMode.Slide)]
public async Task Verify_Indicator_Transitions(Orientation orientation, IndicatorTransitionMode transitionMode)
{
const int NumItems = 3;
const double ItemSize = 100d;
var source = Enumerable.Range(0, NumItems).ToArray();
var SUT = new TabBar
{
Orientation = orientation,
ItemsSource = source,
Width = orientation == Orientation.Horizontal ? ItemSize * NumItems : double.NaN,
Height = orientation == Orientation.Vertical ? ItemSize * NumItems : double.NaN,
SelectionIndicatorContent = "asd",
SelectionIndicatorContentTemplate = XamlHelper.LoadXaml<DataTemplate>($"""
<DataTemplate>
<Border x:Name="SutIndicator"
{(orientation == Orientation.Horizontal ? "Height" : "Width")}="5"
Background="Red" />
</DataTemplate>
"""),
SelectionIndicatorTransitionMode = transitionMode,
};
await UnitTestUIContentHelperEx.SetContentAndWait(SUT);
var presenter = SUT.GetFirstDescendant<TabBarSelectionIndicatorPresenter>(x => x.Visibility == Visibility.Visible);
var indicator = presenter?.GetFirstDescendant<Border>("SutIndicator")!;
Assert.IsNotNull(indicator, "Failed to find Border#SutIndicator");
for (int i = 0; i < NumItems; i++)
{
SUT.SelectedIndex = i;
await UnitTestsUIContentHelper.WaitForIdle();
await UnitTestUIContentHelperEx.WaitFor(() => i * ItemSize == GetTestCoordinate(indicator.TransformToVisual(SUT).TransformPoint(default)), timeoutMS: 2000);
var currentPos = indicator.TransformToVisual(SUT).TransformPoint(default);
Assert.AreEqual(i * ItemSize, GetTestCoordinate(currentPos), delta: 1f);
}
double GetTestCoordinate(Windows.Foundation.Point testPoint)
{
return orientation switch
{
Orientation.Horizontal => testPoint.X,
Orientation.Vertical => testPoint.Y,
_ => throw new ArgumentOutOfRangeException(nameof(orientation))
};
}
}
[TestMethod]
[DataRow(Orientation.Horizontal, new[] { 0, 0, 0, 0 }, DisplayName = "Horizontal at [0,0,0,0]")]
[DataRow(Orientation.Horizontal, new[] { 0, 30, 0, 0 }, DisplayName = "Horizontal at [0,30,0,0]")]
[DataRow(Orientation.Horizontal, new[] { 0, 0, 0, 30 }, DisplayName = "Horizontal at [0,0,0,30]")]
[DataRow(Orientation.Horizontal, new[] { 0, 30, 0, 30 }, DisplayName = "Horizontal at [0,30,0,30]")]
[DataRow(Orientation.Horizontal, new[] { 0, 50, 0, 0 }, DisplayName = "Horizontal at [0,50,0,0]")]
[DataRow(Orientation.Horizontal, new[] { 0, 0, 0, 50 }, DisplayName = "Horizontal at [0,0,0,50]")]
[DataRow(Orientation.Horizontal, new[] { 0, 30, 0, 20 }, DisplayName = "Horizontal at [0,30,0,20]")]
[DataRow(Orientation.Vertical, new[] { 0, 0, 0, 0 }, DisplayName = "Vertical at [0,0,0,0]")]
[DataRow(Orientation.Vertical, new[] { 30, 0, 0, 0 }, DisplayName = "Vertical at [30,0,0,0]")]
[DataRow(Orientation.Vertical, new[] { 0, 0, 30, 0 }, DisplayName = "Vertical at [0,0,30,0]")]
[DataRow(Orientation.Vertical, new[] { 30, 0, 30, 0 }, DisplayName = "Vertical at [30,0,30,0]")]
[DataRow(Orientation.Vertical, new[] { 50, 0, 0, 0 }, DisplayName = "Vertical at [50,0,0,0]")]
[DataRow(Orientation.Vertical, new[] { 0, 0, 50, 0 }, DisplayName = "Vertical at [0,0,50,0]")]
[DataRow(Orientation.Vertical, new[] { 30, 0, 20, 0 }, DisplayName = "Vertical at [30,0,20,0]")]
public async Task Verify_Padding(
Orientation orientation,
int[] padding)
{
var minDimen = (double)Application.Current.Resources["TabBarHeightOrWidth"];
var styleName = orientation switch
{
Orientation.Horizontal => "BaseHorizontalTabBarStyle",
Orientation.Vertical => "BaseVerticalTabBarStyle",
_ => throw new ArgumentOutOfRangeException(nameof(orientation))
};
var rootGrid = XamlHelper.LoadXaml<Grid>(@$"
<Grid>
<utu:TabBar xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"" Padding=""{padding[0]},{padding[1]},{padding[2]},{padding[3]}"" Background=""Red"" x:Name=""MyTabBar"" Style=""{{StaticResource {styleName}}}"">
<utu:TabBar.Items>
<utu:TabBarItem Content=""1"" />
<utu:TabBarItem Content=""2"" />
<utu:TabBarItem Content=""3"" />
</utu:TabBar.Items>
</utu:TabBar>
</Grid>
");
var tabBar = (TabBar)rootGrid.FindName("MyTabBar");
await UnitTestUIContentHelperEx.SetContentAndWait(rootGrid);
var c = GetMinCalculatedDimen();
var expectedDimen = Math.Max(c, minDimen);
double actualDimen = orientation switch
{
Orientation.Horizontal => tabBar.ActualHeight,
Orientation.Vertical => tabBar.ActualWidth,
_ => throw new ArgumentOutOfRangeException(nameof(orientation))
};
Assert.AreEqual(expectedDimen, actualDimen, 1d);
double GetMinCalculatedDimen()
{
var tabBarItem = (TabBarItem)tabBar.ContainerFromIndex(0);
return orientation switch
{
Orientation.Horizontal => padding[1] + padding[3] + tabBarItem.ActualHeight,
Orientation.Vertical => padding[0] + padding[2] + tabBarItem.ActualWidth,
_ => throw new ArgumentOutOfRangeException(nameof(orientation))
};
}
}
[TestMethod]
public async Task Verify_Indicator_Display_On_Selection()
{
if (!ImageAssertHelper.IsScreenshotSupported())
{
Assert.Inconclusive(); // System.NotImplementedException: RenderTargetBitmap is not supported on this platform.;
}
const int NumItems = 3;
var SUT = new TabBar
{
Background = new SolidColorBrush(Colors.Transparent),
SelectionIndicatorContentTemplate = XamlHelper.LoadXaml<DataTemplate>(@"
<DataTemplate>
<Border Background=""Red"" />
</DataTemplate>
"),
SelectionIndicatorPlacement = IndicatorPlacement.Above,
};
foreach (var item in Enumerable.Range(0, NumItems).Select(_ => CreateItem()))
{
SUT.Items.Add(item);
}
await UnitTestUIContentHelperEx.SetContentAndWait(SUT);
for (int i = 0; i < NumItems; i++)
{
SUT.SelectedIndex = i;
await UnitTestsUIContentHelper.WaitForIdle();
var container = SUT.ContainerFromItem(SUT.SelectedItem);
var selectedItem = SUT.GetInnerContainer(container); // see comment on GetInnerContainer
Assert.IsNotNull(selectedItem);
var renderer = await SUT.TakeScreenshot();
var centerPoint = selectedItem!.TransformToVisual(SUT).TransformPoint(new Point(selectedItem.ActualWidth / 2, selectedItem.ActualHeight / 2));
await renderer.AssertColorAt(Colors.Red, (int)centerPoint.X, (int)centerPoint.Y);
foreach (var nonSelected in SUT.Items.Cast<TabBarItem>().Where(x => !x.IsSelected))
{
centerPoint = nonSelected.TransformToVisual(SUT).TransformPoint(new Point(nonSelected.ActualWidth / 2, nonSelected.ActualHeight / 2));
await renderer.AssertColorAt(Colors.Green, (int)centerPoint.X, (int)centerPoint.Y);
}
}
TabBarItem CreateItem()
{
return new TabBarItem
{
Content = new Border
{
Padding = new Thickness(20),
Background = new SolidColorBrush(Colors.Green),
}
};
}
}
[TestMethod]
[DataRow(IndicatorPlacement.Above, DisplayName = "Verify Indicator Above")]
[DataRow(IndicatorPlacement.Below, DisplayName = "Verify Indicator Below")]
public async Task Verify_Indicator_Placement(IndicatorPlacement placement)
{
var item = new TabBarItem
{
Content = new Border
{
Padding = new Thickness(20),
Background = new SolidColorBrush(Colors.Green),
}
};
var SUT = new TabBar
{
Background = new SolidColorBrush(Colors.Transparent),
SelectionIndicatorContentTemplate = XamlHelper.LoadXaml<DataTemplate>(@"
<DataTemplate>
<Border Background=""Red"" />
</DataTemplate>
"),
SelectedIndex = 0,
SelectionIndicatorPlacement = placement,
};
SUT.Items.Add(item);
await UnitTestUIContentHelperEx.SetContentAndWait(SUT);
var belowPresenter = VisualTreeHelperEx
.GetFirstDescendant<TabBarSelectionIndicatorPresenter>(SUT, x => x.Name == "BelowSelectionIndicatorPresenter");
var abovePresenter = VisualTreeHelperEx
.GetFirstDescendant<TabBarSelectionIndicatorPresenter>(SUT, x => x.Name == "AboveSelectionIndicatorPresenter");
var renderer = await SUT.TakeScreenshot();
var centerPoint = item.TransformToVisual(SUT).TransformPoint(new Point(item.ActualWidth / 2, item.ActualHeight / 2));
if (placement == IndicatorPlacement.Above)
{
Assert.AreEqual(Visibility.Collapsed, belowPresenter!.Visibility);
Assert.AreEqual(Visibility.Visible, abovePresenter!.Visibility);
if (renderer is { })
{
await renderer.AssertColorAt(Colors.Red, (int)centerPoint.X, (int)centerPoint.Y);
}
}
else
{
Assert.AreEqual(Visibility.Visible, belowPresenter!.Visibility);
Assert.AreEqual(Visibility.Collapsed, abovePresenter!.Visibility);
if (renderer is { })
{
await renderer.AssertColorAt(Colors.Green, (int)centerPoint.X, (int)centerPoint.Y);
}
}
}
[TestMethod]
public async Task Verify_SelectedIndex_Not_Set_Unnecessarily()
{
var item1 = new TabBarItem();
var item2 = new TabBarItem();
var SUT = new TabBar();
SUT.Items.Add(item1);
SUT.Items.Add(item2);
SUT.DataContext = new SelectedIndexTestViewModel();
SUT.SetBinding(TabBar.SelectedIndexProperty, new Binding() { Mode = BindingMode.OneWay, Path = new PropertyPath(nameof(SelectedIndexTestViewModel.P)) });
await UnitTestUIContentHelperEx.SetContentAndWait(SUT);
Assert.IsNotNull(SUT.GetBindingExpression(TabBar.SelectedIndexProperty));
SUT.SelectedIndex = 0;
// Possible Uno bug? Explicit setting of SelectedIndex should have cleared the binding.
//Assert.IsNull(SUT.GetBindingExpression(TabBar.SelectedIndexProperty));
}
[TestMethod]
public async Task Verify_ItemTemplate_Has_No_Nested_TabBarItem()
{
var source = new[]
{
new TestRecord("True", true),
new TestRecord("False", false),
new TestRecord("True", true)
};
var dt = XamlHelper.LoadXaml<DataTemplate>("""
<DataTemplate>
<utu:TabBarItem Content="{Binding Name}" IsSelectable="{Binding IsSelectable}" />
</DataTemplate>
""");
var SUT = new TabBar
{
Style = (Style)Application.Current.Resources["TopTabBarStyle"],
ItemsSource = source,
ItemTemplate = dt,
SelectedIndex = 0
};
await UnitTestUIContentHelperEx.SetContentAndWait(SUT);
// Ensure the container is a `ContentPresenter` and not a `TabBarItem`
var container = SUT.ContainerFromItem(SUT.SelectedItem);
Assert.IsInstanceOfType(container, typeof(ContentPresenter));
// Ensure the inner container is a `TabBarItem`
var selectedItem = SUT.GetInnerContainer(container); // see comment on GetInnerContainer
Assert.IsInstanceOfType(selectedItem, typeof(TabBarItem));
}
[TestMethod]
public async Task Verify_ItemTemplate_Disabled_Not_Selectable()
{
var source = new[]
{
new TestRecord("True", true),
new TestRecord("False", false),
new TestRecord("True", true)
};
var dt = XamlHelper.LoadXaml<DataTemplate>("""
<DataTemplate>
<utu:TabBarItem Content="{Binding Name}" IsSelectable="{Binding IsSelectable}" />
</DataTemplate>
""");
var SUT = new TabBar
{
Style = (Style)Application.Current.Resources["TopTabBarStyle"],
ItemsSource = source,
ItemTemplate = dt,
};
await UnitTestUIContentHelperEx.SetContentAndWait(SUT);
Assert.IsNull(SUT.SelectedItem);
// Make sure the first item is selectable
SUT.SelectedIndex = 0;
await UnitTestsUIContentHelper.WaitForIdle();
Assert.AreSame(SUT.SelectedItem, source[0]);
SUT.SelectedIndex = 1;
await UnitTestsUIContentHelper.WaitForIdle();
// Assert the second item is not selected
Assert.AreNotSame(SUT.SelectedItem, source[1]);
}
[TestMethod]
public async Task Initial_Selection()
{
var setup = XamlHelper.LoadXaml<TabBar>("""
<utu:TabBar>
<utu:TabBar.Items>
<utu:TabBarItem Content="Tab Uno" IsSelected="True"/>
<utu:TabBarItem Content="Tab Deux" />
<utu:TabBarItem Content="Tab Three" />
</utu:TabBar.Items>
</utu:TabBar>
""");
await UnitTestUIContentHelperEx.SetContentAndWait(setup);
var selected = setup.ContainerFromIndex(0) as TabBarItem ?? throw new Exception("Container#0 not found");
Assert.AreEqual(0, setup.SelectedIndex, "SelectedIndex is expected to be 0");
Assert.AreEqual(selected, setup.SelectedItem, "SelectedItem is expected to be container#0");
Assert.AreEqual(true, selected.IsSelected, "Container#0 should be selected");
}
}
internal partial class TabBarTests // supporting classes/methods
{
private class SelectedIndexTestViewModel : INotifyPropertyChanged
{
private int _p;
public int P
{
get => _p; set
{
_p = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(P)));
}
}
public event PropertyChangedEventHandler? PropertyChanged;
}
public record TestRecord(string Name, bool IsSelectable);
}
}