Skip to content

Commit

Permalink
chore: fix tabbar indicator tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Xiaoy312 committed Jan 6, 2025
1 parent 8f6073d commit 1815d9b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 22 deletions.
48 changes: 29 additions & 19 deletions src/Uno.Toolkit.RuntimeTests/Tests/TabBarTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,15 +157,23 @@ public async Task SetSelectedIndex()
public async Task Verify_Indicator_Max_Size()
{
var source = Enumerable.Range(0, 3).Select(x => new TabBarItem { Content = x }).ToArray();
var indicator = new Border() { Height = 5, Background = new SolidColorBrush(Colors.Red) };
var SUT = new TabBar
{
ItemsSource = source,
SelectionIndicatorContent = indicator,
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();

Expand All @@ -183,37 +191,39 @@ public async Task Verify_Indicator_Max_Size()
}

[TestMethod]
[DataRow(Orientation.Horizontal, IndicatorTransitionMode.Snap, DisplayName = "Horizontal Snap")]
[DataRow(Orientation.Horizontal, IndicatorTransitionMode.Slide, DisplayName = "Horizontal Slide")]
[DataRow(Orientation.Vertical, IndicatorTransitionMode.Snap, DisplayName = "Vertical Snap")]
[DataRow(Orientation.Vertical, IndicatorTransitionMode.Slide, DisplayName = "Vertical Slide")]
[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 indicator = new Border() { Background = new SolidColorBrush(Colors.Red) };
var SUT = new TabBar
{
Orientation = orientation,
ItemsSource = source,
SelectionIndicatorContent = indicator,
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,
};

if (orientation == Orientation.Horizontal)
{
SUT.Width = ItemSize * NumItems;
indicator.Height = 5;
}
else
{
SUT.Height = ItemSize * NumItems;
indicator.Width = 5;
}

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;
Expand Down
9 changes: 6 additions & 3 deletions src/Uno.Toolkit.UI/Helpers/VisualTreeHelperEx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,10 @@ void Print(object o, int depth)
.OfType<T>()
.FirstOrDefault();

public static T? GetFirstDescendant<T>(this DependencyObject reference, string name) where T : FrameworkElement => GetDescendants(reference)
.OfType<T>()
.FirstOrDefault(x => x.Name == name);

/// <summary>
/// Returns the first descendant of a specified type that satisfies the <paramref name="predicate"/>.
/// </summary>
Expand All @@ -146,9 +150,8 @@ void Print(object o, int depth)
.OfType<T>()
.FirstOrDefault(predicate);

public static T GetFirstDescendantOrThrow<T>(this DependencyObject reference, string name) where T : FrameworkElement => GetDescendants(reference)
.OfType<T>()
.FirstOrDefault(x => x.Name == name) ??
public static T GetFirstDescendantOrThrow<T>(this DependencyObject reference, string name) where T : FrameworkElement =>
GetFirstDescendant<T>(reference, name) ??
throw new Exception($"Unable to find element: {typeof(T).Name}#{name}");

/// <summary>
Expand Down

0 comments on commit 1815d9b

Please sign in to comment.