From b279305958cd5d7abe5159af06ff973d6d723bbc Mon Sep 17 00:00:00 2001 From: Antony Male Date: Sat, 15 Mar 2014 13:50:17 +0000 Subject: [PATCH] Add tests for Conductor.Collections.AllActive --- StyletUnitTests/ConductorAllActiveTests.cs | 158 +++++++++++++++++++++ StyletUnitTests/StyletUnitTests.csproj | 1 + 2 files changed, 159 insertions(+) create mode 100644 StyletUnitTests/ConductorAllActiveTests.cs diff --git a/StyletUnitTests/ConductorAllActiveTests.cs b/StyletUnitTests/ConductorAllActiveTests.cs new file mode 100644 index 0000000..abce2d0 --- /dev/null +++ b/StyletUnitTests/ConductorAllActiveTests.cs @@ -0,0 +1,158 @@ +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 ConductorAllActiveTests + { + private Conductor.Collections.AllActive conductor; + + [SetUp] + public void SetUp() + { + this.conductor = new Conductor.Collections.AllActive(); + } + + [Test] + public void NoActiveItemsBeforeAnyItemsActivated() + { + Assert.IsEmpty(this.conductor.Items); + } + + [Test] + public void ActivateItemAddsItemToItems() + { + var screen = new Mock(); + this.conductor.ActivateItem(screen.Object); + Assert.That(this.conductor.Items, Is.EquivalentTo(new[] { screen.Object })); + } + + [Test] + public void ActivateItemDoesNotActivateIfConductorIsNotActive() + { + var screen = new Mock(); + this.conductor.ActivateItem(screen.Object); + screen.Verify(x => x.Activate(), Times.Never); + } + + [Test] + public void ActivateItemDoesActiveIfConductorIsActive() + { + var screen = new Mock(); + ((IActivate)this.conductor).Activate(); + this.conductor.ActivateItem(screen.Object); + screen.Verify(x => x.Activate()); + } + + [Test] + public void DeactiveDeactivatesItems() + { + var screen = new Mock(); + ((IActivate)this.conductor).Activate(); + this.conductor.ActivateItem(screen.Object); + ((IDeactivate)this.conductor).Deactivate(false); + screen.Verify(x => x.Deactivate(false)); + } + + [Test] + public void ClosingClosesAllItems() + { + var screen = new Mock(); + ((IActivate)this.conductor).Activate(); + this.conductor.ActivateItem(screen.Object); + ((IDeactivate)this.conductor).Deactivate(true); + screen.Verify(x => x.Deactivate(true)); + Assert.AreEqual(0, this.conductor.Items.Count); + } + + [Test] + public void ConductorCanCloseIfAllItemsCanClose() + { + var screen1 = new Mock(); + var screen2 = new Mock(); + + 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(); + var screen2 = new Mock(); + + 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(); + this.conductor.Items.Add(screen.Object); + screen.VerifySet(x => x.Parent = this.conductor); + } + + [Test] + public void RemovingItemRemovesParent() + { + var screen = new Mock(); + this.conductor.Items.Add(screen.Object); + this.conductor.Items.Remove(screen.Object); + screen.VerifySet(x => x.Parent = null); + } + + [Test] + public void AddingItemTwiceDoesNotResultInDuplicates() + { + var screen = new Mock(); + 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(); + this.conductor.ActivateItem(screen.Object); + this.conductor.DeactivateItem(screen.Object, false); + screen.Verify(x => x.Deactivate(false)); + Assert.That(this.conductor.Items, Is.EquivalentTo(new[] { screen.Object })); + } + + [Test] + public void CloseItemDoesNothingIfItemCanNotClose() + { + var screen = new Mock(); + this.conductor.ActivateItem(screen.Object); + this.conductor.DeactivateItem(screen.Object, true); + screen.Verify(x => x.Deactivate(true), Times.Never); + Assert.That(this.conductor.Items, Is.EquivalentTo(new[] { screen.Object })); + } + + [Test] + public void CloseItemDeactivatesItemAndRemovesFromItemsIfItemCanClose() + { + var screen = new Mock(); + screen.Setup(x => x.CanCloseAsync()).Returns(Task.FromResult(true)); + this.conductor.ActivateItem(screen.Object); + this.conductor.DeactivateItem(screen.Object, true); + screen.Verify(x => x.Deactivate(true)); + Assert.AreEqual(0, this.conductor.Items.Count); + } + } +} diff --git a/StyletUnitTests/StyletUnitTests.csproj b/StyletUnitTests/StyletUnitTests.csproj index 6beaf9d..453e390 100644 --- a/StyletUnitTests/StyletUnitTests.csproj +++ b/StyletUnitTests/StyletUnitTests.csproj @@ -53,6 +53,7 @@ +