From 1815d9b129ebbe978baeeab9bd1ee3cbeb02e694 Mon Sep 17 00:00:00 2001 From: xiaoy312 Date: Mon, 6 Jan 2025 13:43:12 -0500 Subject: [PATCH] chore: fix tabbar indicator tests --- .../Tests/TabBarTests.cs | 48 +++++++++++-------- .../Helpers/VisualTreeHelperEx.cs | 9 ++-- 2 files changed, 35 insertions(+), 22 deletions(-) diff --git a/src/Uno.Toolkit.RuntimeTests/Tests/TabBarTests.cs b/src/Uno.Toolkit.RuntimeTests/Tests/TabBarTests.cs index 6f58fbcc3..0a2ef84be 100644 --- a/src/Uno.Toolkit.RuntimeTests/Tests/TabBarTests.cs +++ b/src/Uno.Toolkit.RuntimeTests/Tests/TabBarTests.cs @@ -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(""" + + + + """), }; await UnitTestUIContentHelperEx.SetContentAndWait(SUT); + var presenter = SUT.GetFirstDescendant(x => x.Visibility == Visibility.Visible); + var indicator = presenter?.GetFirstDescendant("SutIndicator")!; + Assert.IsNotNull(indicator, "Failed to find Border#SutIndicator"); + source[0].IsSelected = true; await UnitTestsUIContentHelper.WaitForIdle(); @@ -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($""" + + + + """), 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(x => x.Visibility == Visibility.Visible); + var indicator = presenter?.GetFirstDescendant("SutIndicator")!; + Assert.IsNotNull(indicator, "Failed to find Border#SutIndicator"); + for (int i = 0; i < NumItems; i++) { SUT.SelectedIndex = i; diff --git a/src/Uno.Toolkit.UI/Helpers/VisualTreeHelperEx.cs b/src/Uno.Toolkit.UI/Helpers/VisualTreeHelperEx.cs index cf9a2a7b6..d8c0b8d70 100644 --- a/src/Uno.Toolkit.UI/Helpers/VisualTreeHelperEx.cs +++ b/src/Uno.Toolkit.UI/Helpers/VisualTreeHelperEx.cs @@ -135,6 +135,10 @@ void Print(object o, int depth) .OfType() .FirstOrDefault(); + public static T? GetFirstDescendant(this DependencyObject reference, string name) where T : FrameworkElement => GetDescendants(reference) + .OfType() + .FirstOrDefault(x => x.Name == name); + /// /// Returns the first descendant of a specified type that satisfies the . /// @@ -146,9 +150,8 @@ void Print(object o, int depth) .OfType() .FirstOrDefault(predicate); - public static T GetFirstDescendantOrThrow(this DependencyObject reference, string name) where T : FrameworkElement => GetDescendants(reference) - .OfType() - .FirstOrDefault(x => x.Name == name) ?? + public static T GetFirstDescendantOrThrow(this DependencyObject reference, string name) where T : FrameworkElement => + GetFirstDescendant(reference, name) ?? throw new Exception($"Unable to find element: {typeof(T).Name}#{name}"); ///