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.
 
 
 

266 lines
8.4 KiB

// -----------------------------------------------------------------------
// <copyright file="ItemsControlTests.cs" company="Steven Kirk">
// Copyright 2014 MIT Licence. See licence.md for more information.
// </copyright>
// -----------------------------------------------------------------------
namespace Perspex.Controls.UnitTests
{
using System;
using System.Collections.Specialized;
using System.Linq;
using Perspex.Collections;
using Perspex.Controls;
using Perspex.Controls.Presenters;
using Perspex.Controls.Templates;
using Perspex.Platform;
using Perspex.Styling;
using Perspex.VisualTree;
using Ploeh.AutoFixture;
using Ploeh.AutoFixture.AutoMoq;
using Splat;
using Xunit;
public class ItemsControlTests
{
[Fact]
public void Panel_Should_Have_TemplatedParent_Set_To_ItemsPresenter()
{
var target = new ItemsControl();
target.Template = this.GetTemplate();
target.Items = new[] { "Foo" };
target.ApplyTemplate();
var presenter = target.GetTemplateChildren().OfType<ItemsPresenter>().Single();
var panel = presenter.GetTemplateChildren().OfType<StackPanel>().Single();
Assert.Equal(presenter, panel.TemplatedParent);
}
[Fact]
public void Item_Should_Have_TemplatedParent_Set_To_Null()
{
var target = new ItemsControl();
target.Template = this.GetTemplate();
target.Items = new[] { "Foo" };
target.ApplyTemplate();
var presenter = target.GetTemplateChildren().OfType<ItemsPresenter>().Single();
var panel = presenter.GetTemplateChildren().OfType<StackPanel>().Single();
var item = (TextBlock)panel.GetVisualChildren().First();
Assert.Null(item.TemplatedParent);
}
[Fact]
public void Control_Item_Should_Have_Parent_Set()
{
var target = new ItemsControl();
var child = new Control();
target.Template = this.GetTemplate();
target.Items = new[] { child };
target.ApplyTemplate();
Assert.Equal(target, child.Parent);
Assert.Equal(target, ((ILogical)child).LogicalParent);
}
[Fact]
public void Clearing_Control_Item_Should_Clear_Child_Controls_Parent()
{
var target = new ItemsControl();
var child = new Control();
target.Template = this.GetTemplate();
target.Items = new[] { child };
target.ApplyTemplate();
target.Items = null;
Assert.Null(child.Parent);
Assert.Null(((ILogical)child).LogicalParent);
}
[Fact]
public void Adding_Control_Item_Should_Make_Control_Appear_In_LogicalChildren()
{
var target = new ItemsControl();
var child = new Control();
target.Template = this.GetTemplate();
target.Items = new[] { child };
target.ApplyTemplate();
Assert.Equal(new[] { child }, ((ILogical)target).LogicalChildren.ToList());
}
[Fact]
public void Adding_String_Item_Should_Make_TextBlock_Appear_In_LogicalChildren()
{
var target = new ItemsControl();
var child = new Control();
target.Template = this.GetTemplate();
target.Items = new[] { "Foo" };
target.ApplyTemplate();
var logical = (ILogical)target;
Assert.Equal(1, logical.LogicalChildren.Count);
Assert.IsType<TextBlock>(logical.LogicalChildren[0]);
}
[Fact]
public void Setting_Items_To_Null_Should_Remove_LogicalChildren()
{
var target = new ItemsControl();
var child = new Control();
target.Template = this.GetTemplate();
target.Items = new[] { "Foo" };
target.ApplyTemplate();
target.Items = null;
Assert.Equal(new ILogical[0], ((ILogical)target).LogicalChildren.ToList());
}
[Fact]
public void Setting_Items_Should_Fire_LogicalChildren_CollectionChanged()
{
var target = new ItemsControl();
var child = new Control();
var called = false;
target.Template = this.GetTemplate();
target.ApplyTemplate();
((ILogical)target).LogicalChildren.CollectionChanged += (s, e) =>
called = e.Action == NotifyCollectionChangedAction.Add;
target.Items = new[] { child };
Assert.True(called);
}
[Fact]
public void Setting_Items_To_Null_Should_Fire_LogicalChildren_CollectionChanged()
{
var target = new ItemsControl();
var child = new Control();
var called = false;
target.Template = this.GetTemplate();
target.Items = new[] { child };
target.ApplyTemplate();
((ILogical)target).LogicalChildren.CollectionChanged += (s, e) =>
called = e.Action == NotifyCollectionChangedAction.Remove;
target.Items = null;
Assert.True(called);
}
[Fact]
public void Changing_Items_Should_Fire_LogicalChildren_CollectionChanged()
{
var target = new ItemsControl();
var child = new Control();
var called = false;
target.Template = this.GetTemplate();
target.Items = new[] { child };
target.ApplyTemplate();
((ILogical)target).LogicalChildren.CollectionChanged += (s, e) => called = true;
target.Items = new[] { "Foo" };
Assert.True(called);
}
[Fact]
public void Adding_Items_Should_Fire_LogicalChildren_CollectionChanged()
{
var target = new ItemsControl();
var items = new PerspexList<string> { "Foo" };
var called = false;
target.Template = this.GetTemplate();
target.Items = items;
target.ApplyTemplate();
((ILogical)target).LogicalChildren.CollectionChanged += (s, e) =>
called = e.Action == NotifyCollectionChangedAction.Add;
items.Add("Bar");
Assert.True(called);
}
[Fact]
public void Removing_Items_Should_Fire_LogicalChildren_CollectionChanged()
{
var target = new ItemsControl();
var items = new PerspexList<string> { "Foo", "Bar" };
var called = false;
target.Template = this.GetTemplate();
target.Items = items;
target.ApplyTemplate();
((ILogical)target).LogicalChildren.CollectionChanged += (s, e) =>
called = e.Action == NotifyCollectionChangedAction.Remove;
items.Remove("Bar");
Assert.True(called);
}
[Fact]
public void LogicalChildren_Should_Not_Change_Instance_When_Template_Changed()
{
var target = new ItemsControl()
{
Template = this.GetTemplate(),
};
var before = ((ILogical)target).LogicalChildren;
target.Template = null;
target.Template = this.GetTemplate();
var after = ((ILogical)target).LogicalChildren;
Assert.NotNull(before);
Assert.NotNull(after);
Assert.Same(before, after);
}
private ControlTemplate GetTemplate()
{
return ControlTemplate.Create<ItemsControl>(parent =>
{
return new Border
{
Background = new Perspex.Media.SolidColorBrush(0xffffffff),
Content = new ItemsPresenter
{
Id = "itemsPresenter",
[~ItemsPresenter.ItemsProperty] = parent[~ItemsControl.ItemsProperty],
}
};
});
}
private IDisposable RegisterServices()
{
var result = Locator.CurrentMutable.WithResolver();
var fixture = new Fixture().Customize(new AutoMoqCustomization());
var renderInterface = fixture.Create<IPlatformRenderInterface>();
Locator.CurrentMutable.RegisterConstant(renderInterface, typeof(IPlatformRenderInterface));
return result;
}
}
}