Skip to content

Commit

Permalink
chore: wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Xiaoy312 committed Jul 23, 2024
1 parent 1251899 commit 55cbd8a
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 6 deletions.
72 changes: 71 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,12 +162,18 @@ public async Task When_SetChildTemplateUsingVisualState()

[TestMethod]
[RunsOnUIThread]
public async Task When_Refresh_Setter_BindingOnInvocation()
public async Task When_Refresh_Setter_BindingOnInvocation_AsdAsd10()
{
// wip: tp binding part is fixed
// ^ however, the binding converter is not properly inject
// ^ somehow it is resolving from the global level instead of local level...

var SUT = new When_Refresh_Setter_BindingOnInvocation();
TestServices.WindowHelper.WindowContent = SUT;
await TestServices.WindowHelper.WaitForIdle();

var tree = Uno.UI.Extensions.ViewExtensions.TreeGraph(SUT, DebugVT_Custom, TraverseVSG);

SUT.root.Content = 42;

var testTransform = (SUT.root.FindName("ContentElement") as FrameworkElement).RenderTransform as CompositeTransform;
Expand All @@ -189,6 +197,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
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 55cbd8a

Please sign in to comment.