Skip to content

Commit

Permalink
DO NOT MERGE: debug code
Browse files Browse the repository at this point in the history
  • Loading branch information
Xiaoy312 committed Jul 24, 2024
1 parent 5fbbbfe commit ace5799
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 11 deletions.
66 changes: 65 additions & 1 deletion src/Uno.UI.RuntimeTests/Tests/Windows_UI_Xaml/Given_Control.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
using Windows.UI;
using Windows.Foundation;
using Uno.UI.RuntimeTests.Helpers;
using System.Linq;
using Uno.Extensions;

namespace Uno.UI.RuntimeTests.Tests.Windows_UI_Xaml
{
Expand Down Expand Up @@ -160,7 +162,7 @@ public async Task When_SetChildTemplateUsingVisualState()

[TestMethod]
[RunsOnUIThread]
public async Task When_Refresh_Setter_BindingOnInvocation()
public async Task When_Refresh_Setter_BindingOnInvocation_AsdAsd10()
{
var SUT = new When_Refresh_Setter_BindingOnInvocation();
TestServices.WindowHelper.WindowContent = SUT;
Expand Down Expand Up @@ -189,6 +191,68 @@ public async Task When_Refresh_Setter_BindingOnInvocation()
Assert.AreEqual(43, testTransform.TranslateY);
}

private static IEnumerable<object> TraverseVSG(object o)
{
if (o is Control c && GetChildren(c).FirstOrDefault() is FrameworkElement root)
{
foreach (var item in VisualStateManager.GetVisualStateGroups(root).Safe())
{
yield return item;
}
}
if (o is VisualStateGroup vsg)
{
foreach (var item in vsg.States.Safe())
{
yield return item;
}
}
if (o is VisualState vs)
{
foreach (var item in vs.Setters.Safe())
{
yield return item;
}
}
}

private static IEnumerable<string> DebugVT_Custom(object x)
{
if (x is FrameworkElement fe)
{
yield return $"TP={DescribeObject(fe.GetTemplatedParent())}";
}
if (x is VisualStateGroup vsg)
{
yield return $"TP={DescribeObject(vsg.GetTemplatedParent())}";
yield return $"Name={vsg.Name}";
}
if (x is VisualState vs)
{
yield return $"TP={DescribeObject(vs.GetTemplatedParent())}";
yield return $"Name={vs.Name}";
}
if (x is Setter s)
{
yield return $"TP={DescribeObject(s.GetTemplatedParent())}";
}
}

public static IEnumerable<DependencyObject> GetChildren(DependencyObject reference)
{
return Enumerable
.Range(0, VisualTreeHelper.GetChildrenCount(reference))
.Select(x => VisualTreeHelper.GetChild(reference, x));
}


private static string DescribeObject(object x)
{
if (x == null) return "<null>";
if (x is FrameworkElement { Name: { Length: > 0 } name }) return $"{x.GetType().Name}#{name}";
return x.GetType().Name;
}

#if HAS_UNO
[TestMethod]
[RunsOnUIThread]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4389,6 +4389,12 @@ public async Task When_GridView_Header_Orientation()

await UITestHelper.Load(SUT);

var tree = SUT.TreeGraph(DebugVT);
IEnumerable<string> DebugVT(object x)
{
if (x is FrameworkElement fe) yield return $"Abs={fe.GetAbsoluteBounds().ToDebugString()}";
}

var item1 = SUT.ContainerFromIndex(0).FindVisualChildByType<TextBlock>();
Assert.AreEqual("1", item1.Text);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,12 @@ public async Task When_CreateVerticalScroller_Then_DoNotLoadAllTemplate()

await WindowHelper.WaitForIdle();

var buttons = sut
var buttonCount = sut
.EnumerateAllChildren(maxDepth: 256)
.OfType<RepeatButton>()
.Count();

Assert.IsTrue(buttons > 0); // We make sure that we really loaded the right template
Assert.IsTrue(buttons <= 4);
buttonCount.Should().BeInRange(1, 4); // We make sure that we really loaded the right template
}


Expand All @@ -84,12 +83,12 @@ public async Task When_NonScrollableScroller_Then_DoNotLoadAllTemplate()

await WindowHelper.WaitForIdle();

var buttons = sut
var buttonCount = sut
.EnumerateAllChildren(maxDepth: 256)
.OfType<RepeatButton>()
.Count();

Assert.IsTrue(buttons == 0);
buttonCount.Should().Be(0);
}

[TestMethod]
Expand Down
24 changes: 19 additions & 5 deletions src/Uno.UI/Extensions/ViewExtensions.visual-tree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,27 +52,41 @@ static partial class ViewExtensions
public static string TreeGraph(this _View reference, Func<object, IEnumerable<string>> describeProperties) =>
TreeGraph(reference, x => DescribeVTNode(x, describeProperties));

public static string TreeGraph(this object reference, Func<object, IEnumerable<string>> describeProperties, Func<object, IEnumerable<object>>? getMembers = null) =>
TreeGraph(reference, x => DescribeVTNode(x, describeProperties), getMembers);

/// <summary>
/// Produces a text representation of the visual tree, using the provided method of description.
/// </summary>
/// <param name="reference">Any node of the visual tree</param>
/// <param name="describe">A function to describe a visual tree node in a single line.</param>
/// <returns></returns>
public static string TreeGraph(this _View reference, Func<_View, string> describe)
public static string TreeGraph(this object reference, Func<object, string> describe, Func<object, IEnumerable<object>>? getMembers = null)
{
var buffer = new StringBuilder();
Walk(reference);
return buffer.ToString();

void Walk(_View o, int depth = 0)
void Walk(object o, int depth = 0)
{
Print(o, depth);
foreach (var child in EnumerateChildren(o))

if (o is _View view)
{
foreach (var child in EnumerateChildren(view))
{
Walk(child, depth + 1);
}
}
if (getMembers is { })
{
Walk(child, depth + 1);
foreach (var member in getMembers(o))
{
Walk(member, depth + 1);
}
}
}
void Print(_View o, int depth)
void Print(object o, int depth)
{
buffer
.Append(new string(' ', depth * 4))
Expand Down

0 comments on commit ace5799

Please sign in to comment.