2
2
// The .NET Foundation licenses this file to you under the MIT license.
3
3
// See the LICENSE file in the project root for more information.
4
4
5
+ using System ;
5
6
using System . Linq ;
6
7
using System . Threading . Tasks ;
7
8
using Microsoft . Toolkit . Uwp ;
@@ -52,6 +53,43 @@ await App.DispatcherQueue.EnqueueAsync(async () =>
52
53
} ) ;
53
54
}
54
55
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
+
55
93
[ TestCategory ( "VisualTree" ) ]
56
94
[ TestMethod ]
57
95
public async Task Test_VisualTree_FindDescendantByName_NotFound ( )
@@ -119,6 +157,42 @@ await App.DispatcherQueue.EnqueueAsync(async () =>
119
157
} ) ;
120
158
}
121
159
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
+
122
196
[ TestCategory ( "VisualTree" ) ]
123
197
[ TestMethod ]
124
198
public async Task Test_VisualTree_FindDescendant_ItemsControl_Exists ( )
@@ -157,6 +231,46 @@ await App.DispatcherQueue.EnqueueAsync(async () =>
157
231
} ) ;
158
232
}
159
233
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
+
160
274
[ TestCategory ( "VisualTree" ) ]
161
275
[ TestMethod ]
162
276
public async Task Test_VisualTree_FindDescendant_NotFound ( )
@@ -237,6 +351,55 @@ await App.DispatcherQueue.EnqueueAsync(async () =>
237
351
} ) ;
238
352
}
239
353
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
+
240
403
[ TestCategory ( "VisualTree" ) ]
241
404
[ TestMethod ]
242
405
public async Task Test_VisualTree_FindDescendants_NotFound ( )
@@ -520,6 +683,54 @@ await App.DispatcherQueue.EnqueueAsync(async () =>
520
683
} ) ;
521
684
}
522
685
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
+
523
734
// TODO: Add another Ascendants test where we have something like a ListView which creates a container.
524
735
}
525
736
}
0 commit comments