Finish up unit tests for Conductor

This commit is contained in:
Antony Male 2014-03-13 13:05:12 +00:00
parent 7ef6d6521a
commit 9a3a8ba4f6
4 changed files with 102 additions and 2 deletions

View File

@ -26,8 +26,15 @@ namespace Stylet
if (item == null || !item.Equals(this.ActiveItem))
return;
if (await this.CanCloseItem(item))
this.ChangeActiveItem(default(T), close);
if (close)
{
if (await this.CanCloseItem(item))
this.ChangeActiveItem(default(T), close);
}
else
{
ScreenExtensions.TryDeactivate(this.ActiveItem, false);
}
}
public override Task<bool> CanCloseAsync()

View File

@ -22,6 +22,13 @@ namespace Stylet
return newItem;
}
protected virtual void CleanUpAfterClose(T item)
{
var itemAsChild = item as IChild;
if (itemAsChild != null && itemAsChild.Parent == this)
itemAsChild.Parent = null;
}
protected virtual async Task<bool> CanAllItemsCloseAsync(IEnumerable<T> toClose)
{
var results = await Task.WhenAll(toClose.Select(x => this.CanCloseItem(x)));

View File

@ -23,6 +23,8 @@ namespace Stylet
protected virtual void ChangeActiveItem(T newItem, bool closePrevious)
{
ScreenExtensions.TryDeactivate(this.ActiveItem, closePrevious);
if (closePrevious)
this.CleanUpAfterClose(this.ActiveItem);
newItem = this.EnsureItem(newItem);

View File

@ -84,5 +84,89 @@ namespace StyletUnitTests
this.conductor.ActivateItem(screen2.Object);
screen1.Verify(x => x.Deactivate(true));
}
[Test]
public void ActivateDoesNothingIfPreviousItemCanNotClose()
{
var screen1 = new Mock<IScreen>();
var screen2 = new Mock<IScreen>();
((IActivate)this.conductor).Activate();
this.conductor.ActivateItem(screen1.Object);
screen1.Setup(x => x.CanCloseAsync()).Returns(Task.FromResult(false));
this.conductor.ActivateItem(screen2.Object);
screen1.Verify(x => x.Deactivate(true), Times.Never);
screen2.Verify(x => x.Activate(), Times.Never);
}
[Test]
public void ActivatingCurrentScreenReactivatesScreen()
{
var screen = new Mock<IScreen>();
((IActivate)this.conductor).Activate();
this.conductor.ActivateItem(screen.Object);
this.conductor.ActivateItem(screen.Object);
screen.Verify(x => x.Activate(), Times.Exactly(2));
screen.Verify(x => x.Deactivate(true), Times.Never);
}
[Test]
public void DeactiveDoesNothingIfToldToDeactiveInactiveItem()
{
var screen1 = new Mock<IScreen>();
var screen2 = new Mock<IScreen>();
((IActivate)this.conductor).Activate();
this.conductor.ActivateItem(screen1.Object);
this.conductor.DeactivateItem(screen2.Object, true);
screen1.Verify(x => x.Deactivate(true), Times.Never);
screen2.Verify(x => x.Activate(), Times.Never);
}
[Test]
public void DeactiveFalseDoesNotChangeActiveItem()
{
var screen = new Mock<IScreen>();
((IActivate)this.conductor).Activate();
this.conductor.ActivateItem(screen.Object);
this.conductor.DeactivateItem(screen.Object, false);
screen.Verify(x => x.Deactivate(false));
Assert.AreEqual(this.conductor.ActiveItem, screen.Object);
}
[Test]
public void ActivateSetsConductorAsItemsParent()
{
var screen = new Mock<IScreen>();
this.conductor.ActivateItem(screen.Object);
screen.VerifySet(x => x.Parent = this.conductor);
}
[Test]
public void DeactivateTrueRemovesItemsParent()
{
var screen = new Mock<IScreen>();
screen.Setup(x => x.CanCloseAsync()).Returns(Task.FromResult(true));
screen.Setup(x => x.Parent).Returns(this.conductor);
this.conductor.ActivateItem(screen.Object);
this.conductor.DeactivateItem(screen.Object, true);
screen.VerifySet(x => x.Parent = null);
}
[Test]
public void CanCloseReturnsTrueIfNoActiveItem()
{
Assert.IsTrue(this.conductor.CanCloseAsync().Result);
}
[Test]
public void CanCloseReturnsActiveItemsCanClose()
{
var screen1 = new Mock<IScreen>();
this.conductor.ActivateItem(screen1.Object);
screen1.Setup(x => x.CanCloseAsync()).Returns(Task.FromResult(false));
Assert.IsFalse(this.conductor.CanCloseAsync().Result);
}
}
}