Skip to content

Commit

Permalink
Fix Element.ChildRemoved event sender (#11741) Fixes #11720
Browse files Browse the repository at this point in the history
* Pass correct event source

* Update ElementTests.cs

* Update ElementTests.cs
  • Loading branch information
mattjohnsonpint authored Nov 30, 2022
1 parent 4eddd7f commit f9c7e4d
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 5 deletions.
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);
}
}
}

0 comments on commit f9c7e4d

Please sign in to comment.