|
|
@@ -109,6 +109,20 @@ namespace Avalonia.Controls.UnitTests.Presenters
|
|
|
Assert.Same(target, content.Parent);
|
|
|
}
|
|
|
|
|
|
+ [Fact]
|
|
|
+ public void Should_Add_Child_To_Own_LogicalChildren_Outside_Template()
|
|
|
+ {
|
|
|
+ var content = new Border();
|
|
|
+ var target = new ContentPresenter { Content = content };
|
|
|
+
|
|
|
+ target.UpdateChild();
|
|
|
+
|
|
|
+ var logicalChildren = target.GetLogicalChildren();
|
|
|
+
|
|
|
+ Assert.Equal(1, logicalChildren.Count());
|
|
|
+ Assert.Equal(content, logicalChildren.First());
|
|
|
+ }
|
|
|
+
|
|
|
[Fact]
|
|
|
public void Adding_To_Logical_Tree_Should_Reevaluate_DataTemplates()
|
|
|
{
|
|
|
@@ -181,7 +195,7 @@ namespace Avalonia.Controls.UnitTests.Presenters
|
|
|
}
|
|
|
|
|
|
[Fact]
|
|
|
- public void Should_Raise_DetachedFromLogicalTree_On_Content_Changed()
|
|
|
+ public void Should_Raise_DetachedFromLogicalTree_On_Content_Changed_OutsideTemplate()
|
|
|
{
|
|
|
var target = new ContentPresenter
|
|
|
{
|
|
|
@@ -193,8 +207,6 @@ namespace Avalonia.Controls.UnitTests.Presenters
|
|
|
parentMock.As<IContentPresenterHost>();
|
|
|
parentMock.As<IStyleRoot>();
|
|
|
|
|
|
- // target.SetValue(ContentPresenter.TemplatedParentProperty, parentMock.Object);
|
|
|
-
|
|
|
(target as ISetLogicalParent).SetParent(parentMock.Object);
|
|
|
|
|
|
target.Content = "foo";
|
|
|
@@ -222,7 +234,7 @@ namespace Avalonia.Controls.UnitTests.Presenters
|
|
|
}
|
|
|
|
|
|
[Fact]
|
|
|
- public void Should_Raise_DetachedFromLogicalTree_In_ContentControl_On_Content_Changed()
|
|
|
+ public void Should_Raise_DetachedFromLogicalTree_In_ContentControl_On_Content_Changed_OutsideTemplate()
|
|
|
{
|
|
|
var contentControl = new ContentControl
|
|
|
{
|
|
|
@@ -269,6 +281,72 @@ namespace Avalonia.Controls.UnitTests.Presenters
|
|
|
Assert.True(foodetached);
|
|
|
}
|
|
|
|
|
|
+ [Fact]
|
|
|
+ public void Should_Raise_DetachedFromLogicalTree_On_Detached_OutsideTemplate()
|
|
|
+ {
|
|
|
+ var target = new ContentPresenter
|
|
|
+ {
|
|
|
+ ContentTemplate =
|
|
|
+ new FuncDataTemplate<string>(t => new ContentControl() { Content = t }, false)
|
|
|
+ };
|
|
|
+
|
|
|
+ var parentMock = new Mock<Control>();
|
|
|
+ parentMock.As<IContentPresenterHost>();
|
|
|
+ parentMock.As<IStyleRoot>();
|
|
|
+
|
|
|
+ (target as ISetLogicalParent).SetParent(parentMock.Object);
|
|
|
+
|
|
|
+ target.Content = "foo";
|
|
|
+
|
|
|
+ target.UpdateChild();
|
|
|
+
|
|
|
+ var foo = target.Child as ContentControl;
|
|
|
+
|
|
|
+ bool foodetached = false;
|
|
|
+
|
|
|
+ Assert.NotNull(foo);
|
|
|
+ Assert.Equal("foo", foo.Content);
|
|
|
+
|
|
|
+ foo.DetachedFromLogicalTree += delegate { foodetached = true; };
|
|
|
+
|
|
|
+ (target as ISetLogicalParent).SetParent(null);
|
|
|
+
|
|
|
+ Assert.False((foo as IControl).IsAttachedToLogicalTree);
|
|
|
+ Assert.True(foodetached);
|
|
|
+ }
|
|
|
+
|
|
|
+ [Fact]
|
|
|
+ public void Should_Remove_Old_Child_From_LogicalChildren_On_ContentChanged_OutsideTemplate()
|
|
|
+ {
|
|
|
+ var target = new ContentPresenter
|
|
|
+ {
|
|
|
+ ContentTemplate =
|
|
|
+ new FuncDataTemplate<string>(t => new ContentControl() { Content = t }, false)
|
|
|
+ };
|
|
|
+
|
|
|
+ target.Content = "foo";
|
|
|
+
|
|
|
+ target.UpdateChild();
|
|
|
+
|
|
|
+ var foo = target.Child as ContentControl;
|
|
|
+
|
|
|
+ Assert.NotNull(foo);
|
|
|
+
|
|
|
+ var logicalChildren = target.GetLogicalChildren();
|
|
|
+
|
|
|
+ Assert.Equal(1, logicalChildren.Count());
|
|
|
+
|
|
|
+ target.Content = "bar";
|
|
|
+ target.UpdateChild();
|
|
|
+
|
|
|
+ Assert.Equal(null, foo.Parent);
|
|
|
+
|
|
|
+ logicalChildren = target.GetLogicalChildren();
|
|
|
+
|
|
|
+ Assert.Equal(1, logicalChildren.Count());
|
|
|
+ Assert.NotEqual(foo, logicalChildren.First());
|
|
|
+ }
|
|
|
+
|
|
|
private class TestContentControl : ContentControl
|
|
|
{
|
|
|
public IControl Child { get; set; }
|