@@ -439,6 +439,103 @@ await App.DispatcherQueue.EnqueueAsync(async () =>
439
439
} ) ;
440
440
}
441
441
442
+ [ TestCategory ( "VisualTree" ) ]
443
+ [ TestMethod ]
444
+ public async Task Test_VisualTree_FindFirstLevelDescendants_Exists ( )
445
+ {
446
+ await App . DispatcherQueue . EnqueueAsync ( async ( ) =>
447
+ {
448
+ var treeRoot = XamlReader . Load ( @"<Page
449
+ xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
450
+ xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""> <!-- Starting Point -->
451
+ <Grid>
452
+ <Border x:Name=""A""/>
453
+ <TextBlock x:Name=""B""/>
454
+ <StackPanel x:Name=""C"">
455
+ <TextBox/>
456
+ <TextBlock/>
457
+ </StackPanel>
458
+ <Grid x:Name=""D"">
459
+ <TextBlock/>
460
+ <StackPanel>
461
+ <TextBox/>
462
+ <TextBlock/>
463
+ </StackPanel>
464
+ <TextBlock/>
465
+ </Grid>
466
+ <TextBlock x:Name=""E""/>
467
+ </Grid>
468
+ </Page>" ) as Page ;
469
+
470
+ // Test Setup
471
+ Assert . IsNotNull ( treeRoot , "XAML Failed to Load" ) ;
472
+
473
+ // Initialize Visual Tree
474
+ await SetTestContentAsync ( treeRoot ) ;
475
+
476
+ // Main Test
477
+ var rootGrid = treeRoot . FindDescendant < Grid > ( ) ;
478
+ var children = rootGrid . FindFirstLevelDescendants ( ) . ToArray ( ) ;
479
+
480
+ Assert . AreEqual ( 5 , children . Length , "Expected to find 5 children." ) ;
481
+
482
+ Assert . IsTrue ( children . Any ( c => ( ( FrameworkElement ) c ) . Name == "A" ) , "Couldn't find child 'A'" ) ;
483
+ Assert . IsTrue ( children . Any ( c => ( ( FrameworkElement ) c ) . Name == "A" ) , "Couldn't find child 'B'" ) ;
484
+ Assert . IsTrue ( children . Any ( c => ( ( FrameworkElement ) c ) . Name == "A" ) , "Couldn't find child 'C'" ) ;
485
+ Assert . IsTrue ( children . Any ( c => ( ( FrameworkElement ) c ) . Name == "A" ) , "Couldn't find child 'D'" ) ;
486
+ Assert . IsTrue ( children . Any ( c => ( ( FrameworkElement ) c ) . Name == "A" ) , "Couldn't find child 'E'" ) ;
487
+ } ) ;
488
+ }
489
+
490
+ [ TestCategory ( "VisualTree" ) ]
491
+ [ TestMethod ]
492
+ public async Task Test_VisualTree_FindFirstLevelDescendantsOrSelf_Exists ( )
493
+ {
494
+ await App . DispatcherQueue . EnqueueAsync ( async ( ) =>
495
+ {
496
+ var treeRoot = XamlReader . Load ( @"<Page
497
+ xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
498
+ xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""> <!-- Starting Point -->
499
+ <Grid x:Name=""RootGrid"">
500
+ <Border x:Name=""A""/>
501
+ <TextBlock x:Name=""B""/>
502
+ <StackPanel x:Name=""C"">
503
+ <TextBox/>
504
+ <TextBlock/>
505
+ </StackPanel>
506
+ <Grid x:Name=""D"">
507
+ <TextBlock/>
508
+ <StackPanel>
509
+ <TextBox/>
510
+ <TextBlock/>
511
+ </StackPanel>
512
+ <TextBlock/>
513
+ </Grid>
514
+ <TextBlock x:Name=""E""/>
515
+ </Grid>
516
+ </Page>" ) as Page ;
517
+
518
+ // Test Setup
519
+ Assert . IsNotNull ( treeRoot , "XAML Failed to Load" ) ;
520
+
521
+ // Initialize Visual Tree
522
+ await SetTestContentAsync ( treeRoot ) ;
523
+
524
+ // Main Test
525
+ var rootGrid = treeRoot . FindDescendant < Grid > ( ) ;
526
+ var childrenOrSelf = rootGrid . FindFirstLevelDescendantsOrSelf ( ) . ToArray ( ) ;
527
+
528
+ Assert . AreEqual ( 6 , childrenOrSelf . Length , "Expected to find 6 children or self." ) ;
529
+
530
+ Assert . IsTrue ( childrenOrSelf . Any ( c => ( ( FrameworkElement ) c ) . Name == "RootGrid" ) , "Couldn't find self" ) ;
531
+ Assert . IsTrue ( childrenOrSelf . Any ( c => ( ( FrameworkElement ) c ) . Name == "A" ) , "Couldn't find child 'A'" ) ;
532
+ Assert . IsTrue ( childrenOrSelf . Any ( c => ( ( FrameworkElement ) c ) . Name == "A" ) , "Couldn't find child 'B'" ) ;
533
+ Assert . IsTrue ( childrenOrSelf . Any ( c => ( ( FrameworkElement ) c ) . Name == "A" ) , "Couldn't find child 'C'" ) ;
534
+ Assert . IsTrue ( childrenOrSelf . Any ( c => ( ( FrameworkElement ) c ) . Name == "A" ) , "Couldn't find child 'D'" ) ;
535
+ Assert . IsTrue ( childrenOrSelf . Any ( c => ( ( FrameworkElement ) c ) . Name == "A" ) , "Couldn't find child 'E'" ) ;
536
+ } ) ;
537
+ }
538
+
442
539
[ TestCategory ( "VisualTree" ) ]
443
540
[ TestMethod ]
444
541
public async Task Test_VisualTree_FindAscendant_Exists ( )
0 commit comments