|
| 1 | +using Microsoft.UI.Xaml.Controls; |
| 2 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 3 | +using Microsoft.VisualStudio.TestTools.UnitTesting.AppContainer; |
| 4 | +using System.Linq; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using WinUI.TableView.Extensions; |
| 7 | + |
| 8 | +namespace WinUI.TableView.Tests; |
| 9 | + |
| 10 | +[TestClass] |
| 11 | +public class DependencyObjectExtensionsTests |
| 12 | +{ |
| 13 | + private static Grid BuildVisualTree() |
| 14 | + { |
| 15 | + var root = new Grid(); |
| 16 | + var child1 = new StackPanel(); |
| 17 | + var child2 = new Button(); |
| 18 | + var grandChild = new TextBlock(); |
| 19 | + child1.Children.Add(grandChild); |
| 20 | + root.Children.Add(child1); |
| 21 | + root.Children.Add(child2); |
| 22 | + return root; |
| 23 | + } |
| 24 | + |
| 25 | + [UITestMethod] |
| 26 | + public void FindDescendant_FindsFirstMatchingType() |
| 27 | + { |
| 28 | + var root = BuildVisualTree(); |
| 29 | + var result = root.FindDescendant<TextBlock>(); |
| 30 | + Assert.IsNotNull(result); |
| 31 | + Assert.IsInstanceOfType<TextBlock>(result); |
| 32 | + } |
| 33 | + |
| 34 | + [UITestMethod] |
| 35 | + public void FindDescendant_WithPredicate_FindsCorrectDescendant() |
| 36 | + { |
| 37 | + var root = BuildVisualTree(); |
| 38 | + var result = root.FindDescendant<StackPanel>(sp => sp.Children.Count == 1); |
| 39 | + Assert.IsNotNull(result); |
| 40 | + Assert.IsInstanceOfType<StackPanel>(result); |
| 41 | + } |
| 42 | + |
| 43 | + [UITestMethod] |
| 44 | + public void FindDescendants_EnumeratesAllDescendants() |
| 45 | + { |
| 46 | + var root = BuildVisualTree(); |
| 47 | + var descendants = root.FindDescendants().ToList(); |
| 48 | + |
| 49 | + Assert.IsTrue(descendants.OfType<StackPanel>().Any()); |
| 50 | + Assert.IsTrue(descendants.OfType<Button>().Any()); |
| 51 | + Assert.IsTrue(descendants.OfType<TextBlock>().Any()); |
| 52 | + } |
| 53 | + |
| 54 | + [UITestMethod] |
| 55 | + public async Task FindAscendant_FindsFirstMatchingAscendant() |
| 56 | + { |
| 57 | + var root = BuildVisualTree(); |
| 58 | + await UnitTestApp.Current.MainWindow.LoadTestContentAsync(root); |
| 59 | + |
| 60 | + var grandChild = root.FindDescendant<TextBlock>(); |
| 61 | + var ascendant = grandChild?.FindAscendant<Grid>(); |
| 62 | + Assert.AreEqual(root, ascendant); |
| 63 | + |
| 64 | + await UnitTestApp.Current.MainWindow.UnloadTestContentAsync(root); |
| 65 | + } |
| 66 | + |
| 67 | + [UITestMethod] |
| 68 | + public async Task FindAscendants_EnumeratesAllAscendants() |
| 69 | + { |
| 70 | + var root = BuildVisualTree(); |
| 71 | + await UnitTestApp.Current.MainWindow.LoadTestContentAsync(root); |
| 72 | + |
| 73 | + var grandChild = root.FindDescendant<TextBlock>(); |
| 74 | + var ascendants = grandChild?.FindAscendants().ToList(); |
| 75 | + Assert.IsTrue(ascendants?.OfType<StackPanel>().Any() ?? false); |
| 76 | + Assert.IsTrue(ascendants?.OfType<Grid>().Any() ?? false); |
| 77 | + |
| 78 | + await UnitTestApp.Current.MainWindow.UnloadTestContentAsync(root); |
| 79 | + } |
| 80 | +} |
0 commit comments