Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Element.ChildRemoved event sender #11741

Merged
merged 3 commits into from
Nov 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Controls/src/Core/Element.cs
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ protected virtual void OnChildRemoved(Element child, int oldLogicalIndex)
{
child.Parent = null;

ChildRemoved?.Invoke(child, new ElementEventArgs(child));
ChildRemoved?.Invoke(this, new ElementEventArgs(child));

VisualDiagnostics.OnChildRemoved(this, child, oldLogicalIndex);

Expand Down
59 changes: 55 additions & 4 deletions src/Controls/tests/Core.UnitTests/ElementTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,13 @@ public void DescendantAddedLevel1()
bool added = false;
root.DescendantAdded += (sender, args) =>
{
Assert.Same(args.Element, child);
Assert.Same(root, sender);
Assert.Same(child, args.Element);
added = true;
};

root.Children.Add(child);
Assert.True(added);
}

[Fact]
Expand All @@ -75,11 +77,13 @@ public void DescendantAddedLevel2()
bool added = false;
root.DescendantAdded += (sender, args) =>
{
Assert.Same(args.Element, child2);
Assert.Same(root, sender);
Assert.Same(child2, args.Element);
added = true;
};

child.Children.Add(child2);
Assert.True(added);
}

[Fact]
Expand All @@ -100,6 +104,8 @@ public void DescendantAddedExistingChildren()
bool tier2added = false;
root.DescendantAdded += (sender, args) =>
{
Assert.Same(root, sender);

if (!tier1added)
tier1added = ReferenceEquals(child, args.Element);
if (!tier2added)
Expand All @@ -123,11 +129,13 @@ public void DescendantRemovedLevel1()
bool removed = false;
root.DescendantRemoved += (sender, args) =>
{
Assert.Same(args.Element, child);
Assert.Same(root, sender);
Assert.Same(child, args.Element);
removed = true;
};

root.Children.Remove(child);
Assert.True(removed);
}

[Fact]
Expand All @@ -144,11 +152,13 @@ public void DescendantRemovedLevel2()
bool removed = false;
root.DescendantRemoved += (sender, args) =>
{
Assert.Same(args.Element, child2);
Assert.Same(root, sender);
Assert.Same(child2, args.Element);
removed = true;
};

child.Children.Remove(child2);
Assert.True(removed);
}

[Fact]
Expand All @@ -171,6 +181,8 @@ public void DescendantRemovedWithChildren()
bool tier2removed = false;
root.DescendantRemoved += (sender, args) =>
{
Assert.Same(root, sender);

if (!tier1removed)
tier1removed = ReferenceEquals(child, args.Element);
if (!tier2removed)
Expand All @@ -182,5 +194,44 @@ public void DescendantRemovedWithChildren()
Assert.True(tier1removed);
Assert.True(tier2removed);
}

[Fact]
public void ChildAdded()
{
var root = new TestElement();

var child = new TestElement();

bool added = false;
root.ChildAdded += (sender, args) =>
{
Assert.Same(root, sender);
Assert.Same(child, args.Element);
added = true;
};

root.Children.Add(child);
Assert.True(added);
}

[Fact]
public void ChildRemoved()
{
var root = new TestElement();

var child = new TestElement();
root.Children.Add(child);

bool removed = false;
root.ChildRemoved += (sender, args) =>
{
Assert.Same(root, sender);
Assert.Same(child, args.Element);
removed = true;
};

root.Children.Remove(child);
Assert.True(removed);
}
}
}