123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337 |
- using Avalonia.Controls.Presenters;
- using Avalonia.Controls.Primitives;
- using Avalonia.Controls.Templates;
- using Avalonia.LogicalTree;
- using Avalonia.Styling;
- using Avalonia.UnitTests;
- using Avalonia.VisualTree;
- using Moq;
- using System;
- using System.Linq;
- using Xunit;
- using Avalonia.Rendering;
- using Avalonia.Media;
- using Avalonia.Data;
- namespace Avalonia.Controls.UnitTests.Presenters
- {
- /// <summary>
- /// Tests for ContentControls that aren't hosted in a control template.
- /// </summary>
- public class ContentPresenterTests_Standalone : ScopedTestBase
- {
- [Fact]
- public void Should_Set_Childs_Parent_To_Itself_Standalone()
- {
- var content = new Border();
- var target = new ContentPresenter { Content = content };
- target.UpdateChild();
- Assert.Same(target, content.Parent);
- }
- [Fact]
- public void Should_Add_Child_To_Own_LogicalChildren_Standalone()
- {
- var content = new Border();
- var target = new ContentPresenter { Content = content };
- target.UpdateChild();
- var logicalChildren = target.GetLogicalChildren();
- Assert.Single(logicalChildren);
- Assert.Equal(content, logicalChildren.First());
- }
- [Fact]
- public void Should_Raise_DetachedFromLogicalTree_On_Content_Changed_Standalone()
- {
- var target = new ContentPresenter
- {
- ContentTemplate =
- new FuncDataTemplate<string>((t, _) => new ContentControl() { Content = t }, false)
- };
- var parentMock = new Mock<Control>();
- parentMock.As<IContentPresenterHost>();
- parentMock.As<IRenderRoot>();
- parentMock.As<ILogicalRoot>();
- (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.Content = "bar";
- target.UpdateChild();
- var bar = target.Child as ContentControl;
- Assert.NotNull(bar);
- Assert.True(bar != foo);
- Assert.False((foo as ILogical).IsAttachedToLogicalTree);
- Assert.True(foodetached);
- }
- [Fact]
- public void Should_Raise_DetachedFromLogicalTree_In_ContentControl_On_Content_Changed_Standalone()
- {
- var contentControl = new ContentControl
- {
- Template = new FuncControlTemplate<ContentControl>((c, scope) => new ContentPresenter()
- {
- Name = "PART_ContentPresenter",
- [~ContentPresenter.ContentProperty] = c[~ContentControl.ContentProperty],
- [~ContentPresenter.ContentTemplateProperty] = c[~ContentControl.ContentTemplateProperty]
- }.RegisterInNameScope(scope)),
- ContentTemplate =
- new FuncDataTemplate<string>((t, _) => new ContentControl() { Content = t }, false)
- };
- var parentMock = new Mock<Control>();
- parentMock.As<IRenderRoot>();
- parentMock.As<ILogicalRoot>();
- parentMock.As<ILogical>().SetupGet(l => l.IsAttachedToLogicalTree).Returns(true);
- (contentControl as ISetLogicalParent).SetParent(parentMock.Object);
- contentControl.ApplyTemplate();
- var target = contentControl.Presenter as ContentPresenter;
- contentControl.Content = "foo";
- target.UpdateChild();
- var tbfoo = target.Child as ContentControl;
- bool foodetached = false;
- Assert.NotNull(tbfoo);
- Assert.Equal("foo", tbfoo.Content);
- tbfoo.DetachedFromLogicalTree += delegate { foodetached = true; };
- contentControl.Content = "bar";
- target.UpdateChild();
- var tbbar = target.Child as ContentControl;
- Assert.NotNull(tbbar);
- Assert.True(tbbar != tbfoo);
- Assert.False((tbfoo as ILogical).IsAttachedToLogicalTree);
- Assert.True(foodetached);
- }
- [Fact]
- public void Should_Raise_DetachedFromLogicalTree_On_Detached_Standalone()
- {
- var target = new ContentPresenter
- {
- ContentTemplate =
- new FuncDataTemplate<string>((t, _) => new ContentControl() { Content = t }, false)
- };
- var parentMock = new Mock<Control>();
- parentMock.As<IContentPresenterHost>();
- parentMock.As<IRenderRoot>();
- parentMock.As<ILogicalRoot>();
- (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 ILogical).IsAttachedToLogicalTree);
- Assert.True(foodetached);
- }
- [Fact]
- public void Should_Remove_Old_Child_From_LogicalChildren_On_ContentChanged_Standalone()
- {
- 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.Single(logicalChildren);
- target.Content = "bar";
- target.UpdateChild();
- Assert.Null(foo.Parent);
- logicalChildren = target.GetLogicalChildren();
- Assert.Single(logicalChildren);
- Assert.NotEqual(foo, logicalChildren.First());
- }
- [Fact]
- public void Should_Not_Bind_Old_Child_To_New_DataContext()
- {
- // Test for issue #1099.
- var textBlock = new TextBlock
- {
- [!TextBlock.TextProperty] = new Binding(),
- };
- var target = new ContentPresenter()
- {
- DataTemplates =
- {
- new FuncDataTemplate<string>((x, _) => textBlock),
- new FuncDataTemplate<int>((x, _) => new Canvas()),
- },
- };
- var root = new TestRoot(target);
- target.Content = "foo";
- Assert.Same(textBlock, target.Child);
- textBlock.PropertyChanged += (s, e) =>
- {
- Assert.NotEqual(e.NewValue, "42");
- };
- target.Content = 42;
- }
- [Fact]
- public void Should_Reset_InheritanceParent_When_Child_Removed()
- {
- var logicalParent = new Canvas();
- var child = new TextBlock();
- var target = new ContentPresenter();
- var root = new TestRoot(target);
- ((ISetLogicalParent)child).SetParent(logicalParent);
- target.Content = child;
- target.Content = null;
- // InheritanceParent is exposed via StylingParent.
- Assert.Same(logicalParent, ((IStyleHost)child).StylingParent);
- }
- [Fact]
- public void Should_Create_Child_Even_With_Null_Content_When_ContentTemplate_Is_Set()
- {
- var target = new ContentPresenter
- {
- ContentTemplate = new FuncDataTemplate<object>(_ => true, (_, __) => new TextBlock
- {
- Text = "Hello World"
- }),
- Content = null
- };
- target.UpdateChild();
- var textBlock = Assert.IsType<TextBlock>(target.Child);
- Assert.Equal("Hello World", textBlock.Text);
- }
- [Fact]
- public void Should_Not_Create_Child_Even_With_Null_Content_And_DataTemplates_InsteadOf_ContentTemplate()
- {
- var target = new ContentPresenter
- {
- DataTemplates =
- {
- new FuncDataTemplate<object>(_ => true, (_, __) => new TextBlock
- {
- Text = "Hello World"
- })
- },
- Content = null
- };
- target.UpdateChild();
- Assert.Null(target.Child);
- }
- [Fact]
- public void Should_Not_Create_Child_When_Content_And_Template_Are_Null()
- {
- var target = new ContentPresenter
- {
- ContentTemplate = null,
- Content = null
- };
- target.UpdateChild();
- Assert.Null(target.Child);
- }
- [Fact]
- public void Should_Not_Create_When_Child_Content_Is_Null_But_Expected_ValueType_With_FuncDataTemplate()
- {
- var target = new ContentPresenter
- {
- ContentTemplate = new FuncDataTemplate<int>(_ => true, (_, __) => new TextBlock
- {
- Text = "Hello World"
- }),
- Content = null
- };
- target.UpdateChild();
- Assert.Null(target.Child);
- }
- [Fact]
- public void Should_Create_Child_When_Content_Is_Null_And_Expected_NullableValueType_With_FuncDataTemplate()
- {
- var target = new ContentPresenter
- {
- ContentTemplate = new FuncDataTemplate<int?>(_ => true, (_, __) => new TextBlock
- {
- Text = "Hello World"
- }),
- Content = null
- };
- target.UpdateChild();
- Assert.NotNull(target.Child);
- }
- }
- }
|