A cross-platform UI framework for .NET
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

77 lines
2.3 KiB

using Avalonia.Controls.Presenters;
using Avalonia.Controls.Primitives;
using Avalonia.Controls.Templates;
using Avalonia.LogicalTree;
using Avalonia.UnitTests;
using Xunit;
namespace Avalonia.Controls.UnitTests
{
public class HeaderedItemsControlTests : ScopedTestBase
{
[Fact]
public void Control_Header_Should_Be_Logical_Child_Before_ApplyTemplate()
{
var target = new HeaderedItemsControl
{
Template = GetTemplate(),
};
var child = new Control();
target.Header = child;
Assert.Equal(child.Parent, target);
Assert.Equal(child.GetLogicalParent(), target);
Assert.Equal(new[] { child }, target.GetLogicalChildren());
}
[Fact]
public void DataTemplate_Created_Control_Should_Be_Logical_Child_After_ApplyTemplate()
{
var target = new HeaderedItemsControl
{
Template = GetTemplate(),
};
target.Header = "Foo";
target.ApplyTemplate();
target.HeaderPresenter.UpdateChild();
var child = target.HeaderPresenter.Child;
Assert.NotNull(child);
Assert.Equal(target, child.Parent);
Assert.Equal(target, child.GetLogicalParent());
Assert.Equal(new[] { child }, target.GetLogicalChildren());
}
[Fact]
public void Clearing_Content_Should_Clear_Logical_Child()
{
var target = new HeaderedItemsControl();
var child = new Control();
target.Header = child;
target.Header = null;
Assert.Null(child.Parent);
Assert.Null(child.GetLogicalParent());
Assert.Empty(target.GetLogicalChildren());
}
private static FuncControlTemplate GetTemplate()
{
return new FuncControlTemplate<HeaderedItemsControl>((parent, scope) =>
{
return new Border
{
Child = new ContentPresenter
{
Name = "PART_HeaderPresenter",
[~ContentPresenter.ContentProperty] = parent[~HeaderedItemsControl.HeaderProperty],
}.RegisterInNameScope(scope)
};
});
}
}
}