Skip to content

Commit c1625f2

Browse files
committed
Added unit tests for new APIs
1 parent ea83489 commit c1625f2

File tree

1 file changed

+211
-0
lines changed

1 file changed

+211
-0
lines changed

UnitTests/UnitTests.UWP/Extensions/Test_VisualTreeExtensions.cs

Lines changed: 211 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
using System;
56
using System.Linq;
67
using System.Threading.Tasks;
78
using Microsoft.Toolkit.Uwp;
@@ -52,6 +53,43 @@ await App.DispatcherQueue.EnqueueAsync(async () =>
5253
});
5354
}
5455

56+
[TestCategory("VisualTree")]
57+
[TestMethod]
58+
[DataRow(SearchType.DepthFirst)]
59+
[DataRow(SearchType.BreadthFirst)]
60+
public async Task Test_VisualTree_FindDescendantByName_Exists(SearchType searchType)
61+
{
62+
await App.DispatcherQueue.EnqueueAsync(async () =>
63+
{
64+
var treeRoot = XamlReader.Load(@"<Page
65+
xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
66+
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""> <!-- Starting Point -->
67+
<Grid>
68+
<Grid>
69+
<Border/>
70+
<StackPanel>
71+
<TextBox/>
72+
<TextBlock x:Name=""TargetElement""/> <!-- Target -->
73+
</StackPanel>
74+
</Grid>
75+
</Grid>
76+
</Page>") as Page;
77+
78+
// Test Setup
79+
Assert.IsNotNull(treeRoot, "XAML Failed to Load");
80+
81+
// Initialize Visual Tree
82+
await SetTestContentAsync(treeRoot);
83+
84+
// Main Test
85+
var textBlock = treeRoot.FindDescendant("TargetElement", StringComparison.Ordinal, searchType);
86+
87+
Assert.IsNotNull(textBlock, "Expected to find something.");
88+
Assert.IsInstanceOfType(textBlock, typeof(TextBlock), "Didn't find expected typed element.");
89+
Assert.AreEqual("TargetElement", textBlock.Name, "Didn't find named element.");
90+
});
91+
}
92+
5593
[TestCategory("VisualTree")]
5694
[TestMethod]
5795
public async Task Test_VisualTree_FindDescendantByName_NotFound()
@@ -119,6 +157,42 @@ await App.DispatcherQueue.EnqueueAsync(async () =>
119157
});
120158
}
121159

160+
[TestCategory("VisualTree")]
161+
[TestMethod]
162+
[DataRow(SearchType.DepthFirst)]
163+
[DataRow(SearchType.BreadthFirst)]
164+
public async Task Test_VisualTree_FindDescendant_Exists(SearchType searchType)
165+
{
166+
await App.DispatcherQueue.EnqueueAsync(async () =>
167+
{
168+
var treeRoot = XamlReader.Load(@"<Page
169+
xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
170+
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""> <!-- Starting Point -->
171+
<Grid>
172+
<Grid>
173+
<Border/>
174+
<StackPanel>
175+
<TextBox/>
176+
<TextBlock/> <!-- Target -->
177+
</StackPanel>
178+
</Grid>
179+
</Grid>
180+
</Page>") as Page;
181+
182+
// Test Setup
183+
Assert.IsNotNull(treeRoot, "XAML Failed to Load");
184+
185+
// Initialize Visual Tree
186+
await SetTestContentAsync(treeRoot);
187+
188+
// Main Test
189+
var textBlock = treeRoot.FindDescendant<TextBlock>(searchType);
190+
191+
Assert.IsNotNull(textBlock, "Expected to find something.");
192+
Assert.IsInstanceOfType(textBlock, typeof(TextBlock), "Didn't find expected typed element.");
193+
});
194+
}
195+
122196
[TestCategory("VisualTree")]
123197
[TestMethod]
124198
public async Task Test_VisualTree_FindDescendant_ItemsControl_Exists()
@@ -157,6 +231,46 @@ await App.DispatcherQueue.EnqueueAsync(async () =>
157231
});
158232
}
159233

234+
[TestCategory("VisualTree")]
235+
[TestMethod]
236+
[DataRow(SearchType.DepthFirst)]
237+
[DataRow(SearchType.BreadthFirst)]
238+
public async Task Test_VisualTree_FindDescendant_ItemsControl_Exists(SearchType searchType)
239+
{
240+
await App.DispatcherQueue.EnqueueAsync(async () =>
241+
{
242+
var treeRoot = XamlReader.Load(@"<Page
243+
xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
244+
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""> <!-- Starting Point -->
245+
<Grid>
246+
<Grid>
247+
<Border/>
248+
<Pivot>
249+
<PivotItem>
250+
<TextBox/>
251+
</PivotItem>
252+
<PivotItem>
253+
<TextBlock/> <!-- Target -->
254+
</PivotItem>
255+
</Pivot>
256+
</Grid>
257+
</Grid>
258+
</Page>") as Page;
259+
260+
// Test Setup
261+
Assert.IsNotNull(treeRoot, "XAML Failed to Load");
262+
263+
// Initialize Visual Tree
264+
await SetTestContentAsync(treeRoot);
265+
266+
// Main Test
267+
var textBlock = treeRoot.FindDescendant<TextBlock>(searchType);
268+
269+
Assert.IsNotNull(textBlock, "Expected to find something.");
270+
Assert.IsInstanceOfType(textBlock, typeof(TextBlock), "Didn't find expected typed element.");
271+
});
272+
}
273+
160274
[TestCategory("VisualTree")]
161275
[TestMethod]
162276
public async Task Test_VisualTree_FindDescendant_NotFound()
@@ -237,6 +351,55 @@ await App.DispatcherQueue.EnqueueAsync(async () =>
237351
});
238352
}
239353

354+
[TestCategory("VisualTree")]
355+
[TestMethod]
356+
[DataRow(SearchType.DepthFirst)]
357+
[DataRow(SearchType.BreadthFirst)]
358+
public async Task Test_VisualTree_FindDescendants_Exists(SearchType searchType)
359+
{
360+
await App.DispatcherQueue.EnqueueAsync(async () =>
361+
{
362+
var treeRoot = XamlReader.Load(@"<Page
363+
xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
364+
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""> <!-- Starting Point -->
365+
<Grid>
366+
<Grid>
367+
<Border/>
368+
<TextBlock x:Name=""One""/> <!-- Target -->
369+
<StackPanel>
370+
<TextBox/> <!-- Hidden Target -->
371+
<TextBlock x:Name=""Two""/> <!-- Target -->
372+
</StackPanel>
373+
</Grid>
374+
<TextBlock x:Name=""Three""/> <!-- Target -->
375+
</Grid>
376+
</Page>") as Page;
377+
378+
// Test Setup
379+
Assert.IsNotNull(treeRoot, "XAML Failed to Load");
380+
381+
// Initialize Visual Tree
382+
await SetTestContentAsync(treeRoot);
383+
384+
// Main Test
385+
var textBlocks = treeRoot.FindDescendants(searchType).OfType<TextBlock>();
386+
387+
Assert.IsNotNull(textBlocks, "Expected to find something.");
388+
389+
var array = textBlocks.ToArray();
390+
391+
Assert.AreEqual(4, array.Length, "Expected to find 4 TextBlock elements.");
392+
393+
// I don't think we want to guarantee order here, so just care that we can find each one.
394+
Assert.IsTrue(array.Any((tb) => tb.Name == "One"), "Couldn't find TextBlock 'One'");
395+
Assert.IsTrue(array.Any((tb) => tb.Name == "Two"), "Couldn't find TextBlock 'Two'");
396+
Assert.IsTrue(array.Any((tb) => tb.Name == "Three"), "Couldn't find TextBlock 'Three'");
397+
398+
// TextBox has one in its template!
399+
Assert.IsTrue(array.Any((tb) => tb.Name == "PlaceholderTextContentPresenter"), "Couldn't find hidden TextBlock from TextBox.");
400+
});
401+
}
402+
240403
[TestCategory("VisualTree")]
241404
[TestMethod]
242405
public async Task Test_VisualTree_FindDescendants_NotFound()
@@ -520,6 +683,54 @@ await App.DispatcherQueue.EnqueueAsync(async () =>
520683
});
521684
}
522685

686+
[TestCategory("VisualTree")]
687+
[TestMethod]
688+
public async Task Test_VisualTree_IsAscendantOrDescendantOf()
689+
{
690+
await App.DispatcherQueue.EnqueueAsync(async () =>
691+
{
692+
var treeRoot = XamlReader.Load(@"<Page
693+
xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
694+
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""> <!-- Starting Point -->
695+
<Grid x:Name=""RootGrid"">
696+
<Grid>
697+
<Border x:Name=""NestedBorder""/>
698+
<StackPanel>
699+
<TextBox/>
700+
<TextBlock x:Name=""TargetElement""/> <!-- Target -->
701+
</StackPanel>
702+
</Grid>
703+
</Grid>
704+
</Page>") as Page;
705+
706+
// Test Setup
707+
Assert.IsNotNull(treeRoot, "XAML Failed to Load");
708+
709+
// Initialize Visual Tree
710+
await SetTestContentAsync(treeRoot);
711+
712+
// Get the root and target elements
713+
var rootGrid = treeRoot.FindDescendant("RootGrid");
714+
var nestedBorder = treeRoot.FindDescendant("NestedBorder");
715+
var textBlock = treeRoot.FindDescendant("TargetElement");
716+
717+
Assert.IsTrue(rootGrid.IsAscendantOf(nestedBorder));
718+
Assert.IsTrue(rootGrid.IsAscendantOf(textBlock));
719+
Assert.IsFalse(rootGrid.IsDescendantOf(nestedBorder));
720+
Assert.IsFalse(rootGrid.IsDescendantOf(textBlock));
721+
722+
Assert.IsTrue(nestedBorder.IsDescendantOf(rootGrid));
723+
Assert.IsFalse(nestedBorder.IsDescendantOf(textBlock));
724+
Assert.IsFalse(nestedBorder.IsAscendantOf(rootGrid));
725+
Assert.IsFalse(nestedBorder.IsAscendantOf(textBlock));
726+
727+
Assert.IsTrue(textBlock.IsDescendantOf(rootGrid));
728+
Assert.IsFalse(textBlock.IsDescendantOf(nestedBorder));
729+
Assert.IsFalse(textBlock.IsAscendantOf(rootGrid));
730+
Assert.IsFalse(textBlock.IsAscendantOf(nestedBorder));
731+
});
732+
}
733+
523734
// TODO: Add another Ascendants test where we have something like a ListView which creates a container.
524735
}
525736
}

0 commit comments

Comments
 (0)