Skip to content

Commit

Permalink
Added unit tests for new APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergio0694 committed Dec 20, 2021
1 parent ea83489 commit c1625f2
Showing 1 changed file with 211 additions and 0 deletions.
211 changes: 211 additions & 0 deletions UnitTests/UnitTests.UWP/Extensions/Test_VisualTreeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Toolkit.Uwp;
Expand Down Expand Up @@ -52,6 +53,43 @@ await App.DispatcherQueue.EnqueueAsync(async () =>
});
}

[TestCategory("VisualTree")]
[TestMethod]
[DataRow(SearchType.DepthFirst)]
[DataRow(SearchType.BreadthFirst)]
public async Task Test_VisualTree_FindDescendantByName_Exists(SearchType searchType)
{
await App.DispatcherQueue.EnqueueAsync(async () =>
{
var treeRoot = XamlReader.Load(@"<Page
xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""> <!-- Starting Point -->
<Grid>
<Grid>
<Border/>
<StackPanel>
<TextBox/>
<TextBlock x:Name=""TargetElement""/> <!-- Target -->
</StackPanel>
</Grid>
</Grid>
</Page>") as Page;
// Test Setup
Assert.IsNotNull(treeRoot, "XAML Failed to Load");
// Initialize Visual Tree
await SetTestContentAsync(treeRoot);
// Main Test
var textBlock = treeRoot.FindDescendant("TargetElement", StringComparison.Ordinal, searchType);
Assert.IsNotNull(textBlock, "Expected to find something.");
Assert.IsInstanceOfType(textBlock, typeof(TextBlock), "Didn't find expected typed element.");
Assert.AreEqual("TargetElement", textBlock.Name, "Didn't find named element.");
});
}

[TestCategory("VisualTree")]
[TestMethod]
public async Task Test_VisualTree_FindDescendantByName_NotFound()
Expand Down Expand Up @@ -119,6 +157,42 @@ await App.DispatcherQueue.EnqueueAsync(async () =>
});
}

[TestCategory("VisualTree")]
[TestMethod]
[DataRow(SearchType.DepthFirst)]
[DataRow(SearchType.BreadthFirst)]
public async Task Test_VisualTree_FindDescendant_Exists(SearchType searchType)
{
await App.DispatcherQueue.EnqueueAsync(async () =>
{
var treeRoot = XamlReader.Load(@"<Page
xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""> <!-- Starting Point -->
<Grid>
<Grid>
<Border/>
<StackPanel>
<TextBox/>
<TextBlock/> <!-- Target -->
</StackPanel>
</Grid>
</Grid>
</Page>") as Page;
// Test Setup
Assert.IsNotNull(treeRoot, "XAML Failed to Load");
// Initialize Visual Tree
await SetTestContentAsync(treeRoot);
// Main Test
var textBlock = treeRoot.FindDescendant<TextBlock>(searchType);
Assert.IsNotNull(textBlock, "Expected to find something.");
Assert.IsInstanceOfType(textBlock, typeof(TextBlock), "Didn't find expected typed element.");
});
}

[TestCategory("VisualTree")]
[TestMethod]
public async Task Test_VisualTree_FindDescendant_ItemsControl_Exists()
Expand Down Expand Up @@ -157,6 +231,46 @@ await App.DispatcherQueue.EnqueueAsync(async () =>
});
}

[TestCategory("VisualTree")]
[TestMethod]
[DataRow(SearchType.DepthFirst)]
[DataRow(SearchType.BreadthFirst)]
public async Task Test_VisualTree_FindDescendant_ItemsControl_Exists(SearchType searchType)
{
await App.DispatcherQueue.EnqueueAsync(async () =>
{
var treeRoot = XamlReader.Load(@"<Page
xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""> <!-- Starting Point -->
<Grid>
<Grid>
<Border/>
<Pivot>
<PivotItem>
<TextBox/>
</PivotItem>
<PivotItem>
<TextBlock/> <!-- Target -->
</PivotItem>
</Pivot>
</Grid>
</Grid>
</Page>") as Page;
// Test Setup
Assert.IsNotNull(treeRoot, "XAML Failed to Load");
// Initialize Visual Tree
await SetTestContentAsync(treeRoot);
// Main Test
var textBlock = treeRoot.FindDescendant<TextBlock>(searchType);
Assert.IsNotNull(textBlock, "Expected to find something.");
Assert.IsInstanceOfType(textBlock, typeof(TextBlock), "Didn't find expected typed element.");
});
}

[TestCategory("VisualTree")]
[TestMethod]
public async Task Test_VisualTree_FindDescendant_NotFound()
Expand Down Expand Up @@ -237,6 +351,55 @@ await App.DispatcherQueue.EnqueueAsync(async () =>
});
}

[TestCategory("VisualTree")]
[TestMethod]
[DataRow(SearchType.DepthFirst)]
[DataRow(SearchType.BreadthFirst)]
public async Task Test_VisualTree_FindDescendants_Exists(SearchType searchType)
{
await App.DispatcherQueue.EnqueueAsync(async () =>
{
var treeRoot = XamlReader.Load(@"<Page
xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""> <!-- Starting Point -->
<Grid>
<Grid>
<Border/>
<TextBlock x:Name=""One""/> <!-- Target -->
<StackPanel>
<TextBox/> <!-- Hidden Target -->
<TextBlock x:Name=""Two""/> <!-- Target -->
</StackPanel>
</Grid>
<TextBlock x:Name=""Three""/> <!-- Target -->
</Grid>
</Page>") as Page;
// Test Setup
Assert.IsNotNull(treeRoot, "XAML Failed to Load");
// Initialize Visual Tree
await SetTestContentAsync(treeRoot);
// Main Test
var textBlocks = treeRoot.FindDescendants(searchType).OfType<TextBlock>();
Assert.IsNotNull(textBlocks, "Expected to find something.");
var array = textBlocks.ToArray();
Assert.AreEqual(4, array.Length, "Expected to find 4 TextBlock elements.");
// I don't think we want to guarantee order here, so just care that we can find each one.
Assert.IsTrue(array.Any((tb) => tb.Name == "One"), "Couldn't find TextBlock 'One'");
Assert.IsTrue(array.Any((tb) => tb.Name == "Two"), "Couldn't find TextBlock 'Two'");
Assert.IsTrue(array.Any((tb) => tb.Name == "Three"), "Couldn't find TextBlock 'Three'");
// TextBox has one in its template!
Assert.IsTrue(array.Any((tb) => tb.Name == "PlaceholderTextContentPresenter"), "Couldn't find hidden TextBlock from TextBox.");
});
}

[TestCategory("VisualTree")]
[TestMethod]
public async Task Test_VisualTree_FindDescendants_NotFound()
Expand Down Expand Up @@ -520,6 +683,54 @@ await App.DispatcherQueue.EnqueueAsync(async () =>
});
}

[TestCategory("VisualTree")]
[TestMethod]
public async Task Test_VisualTree_IsAscendantOrDescendantOf()
{
await App.DispatcherQueue.EnqueueAsync(async () =>
{
var treeRoot = XamlReader.Load(@"<Page
xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""> <!-- Starting Point -->
<Grid x:Name=""RootGrid"">
<Grid>
<Border x:Name=""NestedBorder""/>
<StackPanel>
<TextBox/>
<TextBlock x:Name=""TargetElement""/> <!-- Target -->
</StackPanel>
</Grid>
</Grid>
</Page>") as Page;
// Test Setup
Assert.IsNotNull(treeRoot, "XAML Failed to Load");
// Initialize Visual Tree
await SetTestContentAsync(treeRoot);
// Get the root and target elements
var rootGrid = treeRoot.FindDescendant("RootGrid");
var nestedBorder = treeRoot.FindDescendant("NestedBorder");
var textBlock = treeRoot.FindDescendant("TargetElement");
Assert.IsTrue(rootGrid.IsAscendantOf(nestedBorder));
Assert.IsTrue(rootGrid.IsAscendantOf(textBlock));
Assert.IsFalse(rootGrid.IsDescendantOf(nestedBorder));
Assert.IsFalse(rootGrid.IsDescendantOf(textBlock));
Assert.IsTrue(nestedBorder.IsDescendantOf(rootGrid));
Assert.IsFalse(nestedBorder.IsDescendantOf(textBlock));
Assert.IsFalse(nestedBorder.IsAscendantOf(rootGrid));
Assert.IsFalse(nestedBorder.IsAscendantOf(textBlock));
Assert.IsTrue(textBlock.IsDescendantOf(rootGrid));
Assert.IsFalse(textBlock.IsDescendantOf(nestedBorder));
Assert.IsFalse(textBlock.IsAscendantOf(rootGrid));
Assert.IsFalse(textBlock.IsAscendantOf(nestedBorder));
});
}

// TODO: Add another Ascendants test where we have something like a ListView which creates a container.
}
}

0 comments on commit c1625f2

Please sign in to comment.