2014-03-15 07:51:52 -07:00
|
|
|
|
using Moq;
|
|
|
|
|
using NUnit.Framework;
|
|
|
|
|
using Stylet;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace StyletUnitTests
|
|
|
|
|
{
|
|
|
|
|
[TestFixture]
|
|
|
|
|
public class ConductorOneActiveTests
|
|
|
|
|
{
|
2014-08-04 09:35:16 -07:00
|
|
|
|
public interface IMyScreen : IScreen, IDisposable
|
|
|
|
|
{ }
|
|
|
|
|
|
2014-04-24 00:11:21 -07:00
|
|
|
|
private Conductor<IScreen>.Collection.OneActive conductor;
|
2014-03-15 07:51:52 -07:00
|
|
|
|
|
|
|
|
|
[SetUp]
|
|
|
|
|
public void SetUp()
|
|
|
|
|
{
|
2014-04-24 00:11:21 -07:00
|
|
|
|
this.conductor = new Conductor<IScreen>.Collection.OneActive();
|
2014-03-15 07:51:52 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void NoActiveItemsBeforeAnyItemsActivated()
|
|
|
|
|
{
|
|
|
|
|
Assert.IsEmpty(this.conductor.Items);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void ActivatingItemActivatesAndAddsToItems()
|
|
|
|
|
{
|
|
|
|
|
var screen = new Mock<IScreen>();
|
|
|
|
|
((IActivate)this.conductor).Activate();
|
|
|
|
|
this.conductor.ActivateItem(screen.Object);
|
|
|
|
|
|
|
|
|
|
screen.Verify(x => x.Activate());
|
|
|
|
|
Assert.AreEqual(screen.Object, this.conductor.ActiveItem);
|
|
|
|
|
Assert.That(this.conductor.Items, Is.EquivalentTo(new[] { screen.Object }));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void ActivatingItemDeactivatesPreviousItem()
|
|
|
|
|
{
|
|
|
|
|
var screen1 = new Mock<IScreen>();
|
|
|
|
|
var screen2 = new Mock<IScreen>();
|
|
|
|
|
((IActivate)this.conductor).Activate();
|
|
|
|
|
|
|
|
|
|
this.conductor.ActivateItem(screen1.Object);
|
|
|
|
|
this.conductor.ActivateItem(screen2.Object);
|
|
|
|
|
|
2014-03-26 01:36:19 -07:00
|
|
|
|
screen1.Verify(x => x.Deactivate());
|
2014-03-15 07:51:52 -07:00
|
|
|
|
screen2.Verify(x => x.Activate());
|
|
|
|
|
|
|
|
|
|
Assert.AreEqual(screen2.Object, this.conductor.ActiveItem);
|
|
|
|
|
Assert.AreEqual(new[] { screen1.Object, screen2.Object }, this.conductor.Items);
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-06 05:11:51 -07:00
|
|
|
|
[Test]
|
|
|
|
|
public void SettingActiveItemActivatesItem()
|
|
|
|
|
{
|
|
|
|
|
var screen = new Mock<IScreen>();
|
|
|
|
|
((IActivate)this.conductor).Activate();
|
|
|
|
|
this.conductor.ActiveItem = screen.Object;
|
|
|
|
|
screen.Verify(x => x.Activate());
|
|
|
|
|
Assert.AreEqual(this.conductor.ActiveItem, screen.Object);
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-15 07:51:52 -07:00
|
|
|
|
[Test]
|
|
|
|
|
public void ClosingActiveItemChoosesPreviousItemIfAvailable()
|
|
|
|
|
{
|
|
|
|
|
var screen1 = new Mock<IScreen>();
|
|
|
|
|
var screen2 = new Mock<IScreen>();
|
|
|
|
|
var screen3 = new Mock<IScreen>();
|
|
|
|
|
((IActivate)this.conductor).Activate();
|
|
|
|
|
|
|
|
|
|
this.conductor.Items.AddRange(new[] { screen1.Object, screen2.Object, screen3.Object });
|
|
|
|
|
this.conductor.ActivateItem(screen2.Object);
|
|
|
|
|
|
|
|
|
|
screen2.Setup(x => x.CanCloseAsync()).Returns(Task.FromResult(true));
|
|
|
|
|
|
2014-03-26 01:36:19 -07:00
|
|
|
|
this.conductor.CloseItem(screen2.Object);
|
2014-03-15 07:51:52 -07:00
|
|
|
|
Assert.AreEqual(screen1.Object, this.conductor.ActiveItem);
|
|
|
|
|
Assert.AreEqual(new[] { screen1.Object, screen3.Object }, this.conductor.Items);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void ClosingActiveItemChoosesNextItemIfNoPreviousItem()
|
|
|
|
|
{
|
|
|
|
|
var screen1 = new Mock<IScreen>();
|
|
|
|
|
var screen2 = new Mock<IScreen>();
|
|
|
|
|
var screen3 = new Mock<IScreen>();
|
|
|
|
|
((IActivate)this.conductor).Activate();
|
|
|
|
|
|
|
|
|
|
this.conductor.Items.AddRange(new[] { screen1.Object, screen2.Object, screen3.Object });
|
|
|
|
|
this.conductor.ActivateItem(screen3.Object);
|
|
|
|
|
|
|
|
|
|
screen3.Setup(x => x.CanCloseAsync()).Returns(Task.FromResult(true));
|
|
|
|
|
|
2014-03-26 01:36:19 -07:00
|
|
|
|
this.conductor.CloseItem(screen3.Object);
|
2014-03-15 07:51:52 -07:00
|
|
|
|
Assert.AreEqual(screen2.Object, this.conductor.ActiveItem);
|
|
|
|
|
Assert.AreEqual(new[] { screen1.Object, screen2.Object }, this.conductor.Items);
|
|
|
|
|
}
|
2014-03-25 06:20:31 -07:00
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void ActivateItemDoesNotActivateIfConductorIsNotActive()
|
|
|
|
|
{
|
|
|
|
|
var screen = new Mock<IScreen>();
|
|
|
|
|
this.conductor.ActivateItem(screen.Object);
|
|
|
|
|
screen.Verify(x => x.Activate(), Times.Never);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void ActivateItemDoesActiveIfConductorIsActive()
|
|
|
|
|
{
|
|
|
|
|
var screen = new Mock<IScreen>();
|
|
|
|
|
((IActivate)this.conductor).Activate();
|
|
|
|
|
this.conductor.ActivateItem(screen.Object);
|
|
|
|
|
screen.Verify(x => x.Activate());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void DeactiveDeactivatesItems()
|
|
|
|
|
{
|
|
|
|
|
var screen = new Mock<IScreen>();
|
|
|
|
|
((IActivate)this.conductor).Activate();
|
|
|
|
|
this.conductor.ActivateItem(screen.Object);
|
2014-03-26 01:36:19 -07:00
|
|
|
|
((IDeactivate)this.conductor).Deactivate();
|
|
|
|
|
screen.Verify(x => x.Deactivate());
|
2014-03-25 06:20:31 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void ClosingClosesAllItems()
|
|
|
|
|
{
|
2014-08-04 09:35:16 -07:00
|
|
|
|
var screen = new Mock<IMyScreen>();
|
2014-03-25 06:20:31 -07:00
|
|
|
|
((IActivate)this.conductor).Activate();
|
|
|
|
|
this.conductor.ActivateItem(screen.Object);
|
2014-03-26 01:36:19 -07:00
|
|
|
|
((IClose)this.conductor).Close();
|
|
|
|
|
screen.Verify(x => x.Close());
|
2014-08-04 09:35:16 -07:00
|
|
|
|
screen.Verify(x => x.Dispose());
|
2014-03-25 06:20:31 -07:00
|
|
|
|
Assert.AreEqual(0, this.conductor.Items.Count);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void ConductorCanCloseIfAllItemsCanClose()
|
|
|
|
|
{
|
|
|
|
|
var screen1 = new Mock<IScreen>();
|
|
|
|
|
var screen2 = new Mock<IScreen>();
|
|
|
|
|
|
|
|
|
|
screen1.Setup(x => x.CanCloseAsync()).Returns(Task.FromResult(true));
|
|
|
|
|
screen2.Setup(x => x.CanCloseAsync()).Returns(Task.FromResult(true));
|
|
|
|
|
|
|
|
|
|
this.conductor.Items.AddRange(new[] { screen1.Object, screen2.Object });
|
|
|
|
|
Assert.IsTrue(this.conductor.CanCloseAsync().Result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void ConductorCanNotCloseIfAnyItemCanNotClose()
|
|
|
|
|
{
|
|
|
|
|
var screen1 = new Mock<IScreen>();
|
|
|
|
|
var screen2 = new Mock<IScreen>();
|
|
|
|
|
|
|
|
|
|
screen1.Setup(x => x.CanCloseAsync()).Returns(Task.FromResult(true));
|
|
|
|
|
screen2.Setup(x => x.CanCloseAsync()).Returns(Task.FromResult(false));
|
|
|
|
|
|
|
|
|
|
this.conductor.Items.AddRange(new[] { screen1.Object, screen2.Object });
|
|
|
|
|
Assert.IsFalse(this.conductor.CanCloseAsync().Result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void AddingItemSetsParent()
|
|
|
|
|
{
|
|
|
|
|
var screen = new Mock<IScreen>();
|
|
|
|
|
this.conductor.Items.Add(screen.Object);
|
|
|
|
|
screen.VerifySet(x => x.Parent = this.conductor);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
2014-04-15 04:22:05 -07:00
|
|
|
|
public void AddingItemDoesNotChangeActiveItem()
|
|
|
|
|
{
|
|
|
|
|
var screen1 = new Mock<IScreen>();
|
|
|
|
|
var screen2 = new Mock<IScreen>();
|
2015-01-15 01:54:05 -08:00
|
|
|
|
((IActivate)this.conductor).Activate();
|
2014-04-15 04:22:05 -07:00
|
|
|
|
this.conductor.ActivateItem(screen1.Object);
|
|
|
|
|
this.conductor.Items.Add(screen2.Object);
|
|
|
|
|
|
|
|
|
|
Assert.AreEqual(this.conductor.ActiveItem, screen1.Object);
|
|
|
|
|
screen2.Verify(x => x.Activate(), Times.Never);
|
|
|
|
|
screen1.Verify(x => x.Deactivate(), Times.Never);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void RemovingItemClosesAndRemovesParent()
|
2014-03-25 06:20:31 -07:00
|
|
|
|
{
|
2014-08-04 09:35:16 -07:00
|
|
|
|
var screen = new Mock<IMyScreen>();
|
2014-03-26 06:16:26 -07:00
|
|
|
|
screen.SetupGet(x => x.Parent).Returns(this.conductor);
|
2014-03-25 06:20:31 -07:00
|
|
|
|
this.conductor.Items.Add(screen.Object);
|
|
|
|
|
this.conductor.Items.Remove(screen.Object);
|
|
|
|
|
screen.VerifySet(x => x.Parent = null);
|
2014-04-15 04:22:05 -07:00
|
|
|
|
screen.Verify(x => x.Close());
|
2015-01-15 01:54:05 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void RemovingItemDisposesIfDisposeChildrenIsTrue()
|
|
|
|
|
{
|
|
|
|
|
var screen = new Mock<IMyScreen>();
|
|
|
|
|
this.conductor.Items.Add(screen.Object);
|
|
|
|
|
this.conductor.Items.Remove(screen.Object);
|
2014-08-04 09:35:16 -07:00
|
|
|
|
screen.Verify(x => x.Dispose());
|
2014-04-15 04:22:05 -07:00
|
|
|
|
}
|
|
|
|
|
|
2015-01-15 01:54:05 -08:00
|
|
|
|
[Test]
|
|
|
|
|
public void RemovingItemDoesNotDisposeIfDisposeChildrenIsFalse()
|
|
|
|
|
{
|
|
|
|
|
var screen = new Mock<IMyScreen>();
|
|
|
|
|
this.conductor.DisposeChildren = false;
|
|
|
|
|
this.conductor.Items.Add(screen.Object);
|
|
|
|
|
this.conductor.Items.Remove(screen.Object);
|
|
|
|
|
screen.Verify(x => x.Dispose(), Times.Never);
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-15 04:22:05 -07:00
|
|
|
|
[Test]
|
|
|
|
|
public void RemovingActiveItemActivatesAnotherItem()
|
|
|
|
|
{
|
|
|
|
|
((IActivate)this.conductor).Activate();
|
2014-08-04 09:35:16 -07:00
|
|
|
|
var screen1 = new Mock<IMyScreen>();
|
|
|
|
|
var screen2 = new Mock<IMyScreen>();
|
2014-04-15 04:22:05 -07:00
|
|
|
|
this.conductor.ActivateItem(screen1.Object);
|
|
|
|
|
this.conductor.Items.Add(screen2.Object);
|
|
|
|
|
|
|
|
|
|
this.conductor.Items.Remove(screen1.Object);
|
|
|
|
|
|
|
|
|
|
Assert.AreEqual(this.conductor.ActiveItem, screen2.Object);
|
|
|
|
|
screen2.Verify(x => x.Activate());
|
|
|
|
|
screen1.Verify(x => x.Close());
|
2014-03-25 06:20:31 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void AddingItemTwiceDoesNotResultInDuplicates()
|
|
|
|
|
{
|
|
|
|
|
var screen = new Mock<IScreen>();
|
|
|
|
|
this.conductor.ActivateItem(screen.Object);
|
|
|
|
|
this.conductor.ActivateItem(screen.Object);
|
|
|
|
|
Assert.AreEqual(1, this.conductor.Items.Count);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void DeactivateItemDeactivatesItemButDoesNotRemoveFromItems()
|
|
|
|
|
{
|
|
|
|
|
var screen = new Mock<IScreen>();
|
|
|
|
|
this.conductor.ActivateItem(screen.Object);
|
2014-03-26 01:36:19 -07:00
|
|
|
|
this.conductor.DeactivateItem(screen.Object);
|
|
|
|
|
screen.Verify(x => x.Deactivate());
|
2014-03-25 06:20:31 -07:00
|
|
|
|
Assert.That(this.conductor.Items, Is.EquivalentTo(new[] { screen.Object }));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void CloseItemDoesNothingIfItemCanNotClose()
|
|
|
|
|
{
|
2014-08-04 09:35:16 -07:00
|
|
|
|
var screen = new Mock<IMyScreen>();
|
2014-03-25 06:20:31 -07:00
|
|
|
|
this.conductor.ActivateItem(screen.Object);
|
2014-03-26 01:36:19 -07:00
|
|
|
|
this.conductor.CloseItem(screen.Object);
|
|
|
|
|
screen.Verify(x => x.Close(), Times.Never);
|
2014-08-04 09:35:16 -07:00
|
|
|
|
screen.Verify(x => x.Dispose(), Times.Never);
|
2014-03-25 06:20:31 -07:00
|
|
|
|
Assert.That(this.conductor.Items, Is.EquivalentTo(new[] { screen.Object }));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void CloseItemDeactivatesItemAndRemovesFromItemsIfItemCanClose()
|
|
|
|
|
{
|
2014-08-04 09:35:16 -07:00
|
|
|
|
var screen = new Mock<IMyScreen>();
|
2014-03-25 06:20:31 -07:00
|
|
|
|
screen.Setup(x => x.CanCloseAsync()).Returns(Task.FromResult(true));
|
|
|
|
|
this.conductor.ActivateItem(screen.Object);
|
2014-03-26 01:36:19 -07:00
|
|
|
|
this.conductor.CloseItem(screen.Object);
|
|
|
|
|
screen.Verify(x => x.Close());
|
2014-03-25 06:20:31 -07:00
|
|
|
|
Assert.AreEqual(0, this.conductor.Items.Count);
|
|
|
|
|
}
|
2014-05-06 05:11:51 -07:00
|
|
|
|
|
2015-01-15 01:54:05 -08:00
|
|
|
|
[Test]
|
|
|
|
|
public void CloseItemDisposesIfDisposeChildrenIsTrue()
|
|
|
|
|
{
|
|
|
|
|
var screen = new Mock<IMyScreen>();
|
|
|
|
|
screen.Setup(x => x.CanCloseAsync()).Returns(Task.FromResult(true));
|
|
|
|
|
this.conductor.ActivateItem(screen.Object);
|
|
|
|
|
this.conductor.CloseItem(screen.Object);
|
|
|
|
|
screen.Verify(x => x.Dispose());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void CloseItemDoesNotDisposeIfDisposeChildrenIsFalse()
|
|
|
|
|
{
|
|
|
|
|
var screen = new Mock<IMyScreen>();
|
|
|
|
|
this.conductor.DisposeChildren = false;
|
|
|
|
|
screen.Setup(x => x.CanCloseAsync()).Returns(Task.FromResult(true));
|
|
|
|
|
this.conductor.ActivateItem(screen.Object);
|
|
|
|
|
this.conductor.CloseItem(screen.Object);
|
|
|
|
|
screen.Verify(x => x.Dispose(), Times.Never);
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-06 05:11:51 -07:00
|
|
|
|
[Test]
|
|
|
|
|
public void ClosingConductorClosesActiveItem()
|
|
|
|
|
{
|
2014-08-04 09:35:16 -07:00
|
|
|
|
var screen1 = new Mock<IMyScreen>();
|
2014-05-06 05:11:51 -07:00
|
|
|
|
screen1.SetupGet(x => x.Parent).Returns(this.conductor);
|
|
|
|
|
this.conductor.ActivateItem(screen1.Object);
|
|
|
|
|
((IClose)this.conductor).Close();
|
|
|
|
|
screen1.Verify(x => x.Close());
|
2014-08-04 09:35:16 -07:00
|
|
|
|
screen1.Verify(x => x.Dispose());
|
2014-05-06 05:11:51 -07:00
|
|
|
|
screen1.VerifySet(x => x.Parent = null);
|
|
|
|
|
}
|
2014-05-06 09:42:55 -07:00
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void ClosesItemIfItemRequestsClose()
|
|
|
|
|
{
|
2014-08-04 09:35:16 -07:00
|
|
|
|
var screen = new Mock<IMyScreen>();
|
2014-05-06 09:42:55 -07:00
|
|
|
|
this.conductor.ActivateItem(screen.Object);
|
|
|
|
|
screen.Setup(x => x.CanCloseAsync()).Returns(Task.FromResult(true));
|
|
|
|
|
((IChildDelegate)this.conductor).CloseItem(screen.Object);
|
|
|
|
|
|
|
|
|
|
screen.Verify(x => x.Close());
|
2014-08-04 09:35:16 -07:00
|
|
|
|
screen.Verify(x => x.Dispose());
|
2014-05-06 09:42:55 -07:00
|
|
|
|
Assert.Null(this.conductor.ActiveItem);
|
|
|
|
|
}
|
2014-03-15 07:51:52 -07:00
|
|
|
|
}
|
|
|
|
|
}
|