private static void CompareVisualTree(VisualTreeObject actual, VisualTreeObject expected, bool compareProperty = true) { // compare actual.Name.ShouldBeEquivalentTo(expected.Name); if (compareProperty) { var filteredActual = actual.Properties.Where(p => SupportedWpfProperties.IsSupported(p.Name)); var filteredExpected = expected.Properties.Where(p => SupportedWpfProperties.IsSupported(p.Name)); var onlyInActual = filteredActual.Except(filteredExpected); var onlyInExpected = filteredExpected.Except(filteredActual); onlyInActual.Count().ShouldBeEquivalentTo(0); onlyInExpected.Count().ShouldBeEquivalentTo(0); } actual.Children.Count.ShouldBeEquivalentTo(expected.Children.Count); var sortedActualChildren = actual.Children.OrderBy(c => c.Name); var sortedExpectedChildren = expected.Children.OrderBy(c => c.Name); for (int i = 0; i < actual.Children.Count; i++) { CompareVisualTree(sortedActualChildren.ElementAt(i), sortedExpectedChildren.ElementAt(i), compareProperty); } }
private static void CompareVisualTree(VisualTreeObject actual, VisualTreeObject expected, bool compareProperty = true) { bool visible = true; // compare actual.Name.Should().Be(expected.Name); var visibility = actual.Properties.FirstOrDefault(p => p.Name == "Visibility"); if (visibility != null) { visible = (visibility.Value != "Collapsed"); } if (compareProperty && visible) { var filteredActual = actual.Properties.Where(p => SupportedWpfProperties.IsSupported(p.Name)); var filteredExpected = expected.Properties.Where(p => SupportedWpfProperties.IsSupported(p.Name)); filteredActual.Should().BeEquivalentTo(filteredExpected); } actual.Children.Count.ShouldBeEquivalentTo(expected.Children.Count); if (visible) { var sortedActualChildren = actual.Children.OrderBy(c => c.Name).ToList(); var sortedExpectedChildren = expected.Children.OrderBy(c => c.Name).ToList(); sortedActualChildren.Count.Should().Be(sortedExpectedChildren.Count); for (int i = 0; i < actual.Children.Count; i++) { CompareVisualTree(sortedActualChildren[i], sortedExpectedChildren[i], compareProperty); } } }
public static VisualTreeObject Create(DependencyObject o) { VisualTreeObject visualTreeObj = null; UIThreadHelper.Instance.Invoke(() => { visualTreeObj = new VisualTreeObject(); visualTreeObj.Name = o.GetType().Name; visualTreeObj.Properties = VisualTreeProperty.GetProperties(o).Where(p => SupportedWpfProperties.IsSupported(p.Name)).ToList(); visualTreeObj.Children = GetChildren(o); }); return(visualTreeObj); }