Browse Source

Added failing test for #2821.

`ContentPresenter` doesn't set the logical parent of its child control after it has been removed from the logical tree and re-added.
Steven Kirk 6 years ago
parent
commit
5c407f966c
1 changed files with 38 additions and 0 deletions
  1. 38 0
      tests/Avalonia.Controls.UnitTests/ContentControlTests.cs

+ 38 - 0
tests/Avalonia.Controls.UnitTests/ContentControlTests.cs

@@ -331,6 +331,44 @@ namespace Avalonia.Controls.UnitTests
             Assert.Null(textBlock.GetLogicalParent());
         }
 
+        [Fact]
+        public void Should_Set_Child_LogicalParent_After_Removing_And_Adding_Back_To_Logical_Tree()
+        {
+            using (UnitTestApplication.Start(TestServices.RealStyler))
+            {
+                var target = new ContentControl();
+                var root = new TestRoot
+                {
+                    Styles =
+                    {
+                        new Style(x => x.OfType<ContentControl>())
+                        {
+                            Setters =
+                            {
+                                new Setter(ContentControl.TemplateProperty, GetTemplate()),
+                            }
+                        }
+                    },
+                    Child = target
+                };
+
+                target.Content = "Foo";
+                target.ApplyTemplate();
+
+                Assert.Equal(target, target.Presenter.Child.LogicalParent);
+
+                root.Child = null;
+
+                Assert.Null(target.Template);
+
+                target.Content = null;
+                root.Child = target;
+                target.Content = "Bar";
+
+                Assert.Equal(target, target.Presenter.Child.LogicalParent);
+            }
+        }
+
         private FuncControlTemplate GetTemplate()
         {
             return new FuncControlTemplate<ContentControl>((parent, scope) =>