Finish off having conductors with collections respond property to that collection being manipulated

This commit is contained in:
Antony Male 2014-04-15 12:22:05 +01:00
parent d0ce17ec17
commit 6a1e8e688e
3 changed files with 53 additions and 8 deletions

View File

@ -31,25 +31,37 @@ namespace Stylet
switch (e.Action)
{
case NotifyCollectionChangedAction.Add:
this.SetParent(e.NewItems, true);
this.ActivateAndSetParent(e.NewItems);
break;
case NotifyCollectionChangedAction.Remove:
this.SetParent(e.OldItems, false);
this.CloseAndCleanUp(e.OldItems);
break;
case NotifyCollectionChangedAction.Replace:
this.SetParent(e.NewItems, true);
this.SetParent(e.OldItems, false);
this.ActivateAndSetParent(e.NewItems);
this.CloseAndCleanUp(e.OldItems);
break;
case NotifyCollectionChangedAction.Reset:
this.SetParent(this.items, true);
this.ActivateAndSetParent(this.items);
break;
}
};
}
protected virtual void ActivateAndSetParent(IEnumerable items)
{
this.SetParent(items, true);
if (this.IsActive)
{
foreach (var item in items.OfType<IActivate>())
{
item.Activate();
}
}
}
protected override void OnActivate()
{
foreach (var item in this.items.OfType<IActivate>())

View File

@ -99,21 +99,24 @@ namespace StyletUnitTests
}
[Test]
public void AddingItemSetsParent()
public void AddingItemActivatesAndSetsParent()
{
((IActivate)this.conductor).Activate();
var screen = new Mock<IScreen>();
this.conductor.Items.Add(screen.Object);
screen.VerifySet(x => x.Parent = this.conductor);
screen.Verify(x => x.Activate());
}
[Test]
public void RemovingItemRemovesParent()
public void RemovingItemClosesAndRemovesParent()
{
var screen = new Mock<IScreen>();
screen.SetupGet(x => x.Parent).Returns(this.conductor);
this.conductor.Items.Add(screen.Object);
this.conductor.Items.Remove(screen.Object);
screen.VerifySet(x => x.Parent = null);
screen.Verify(x => x.Close());
}
[Test]

View File

@ -164,13 +164,43 @@ namespace StyletUnitTests
}
[Test]
public void RemovingItemRemovesParent()
public void AddingItemDoesNotChangeActiveItem()
{
var screen1 = new Mock<IScreen>();
var screen2 = new Mock<IScreen>();
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()
{
var screen = new Mock<IScreen>();
screen.SetupGet(x => x.Parent).Returns(this.conductor);
this.conductor.Items.Add(screen.Object);
this.conductor.Items.Remove(screen.Object);
screen.VerifySet(x => x.Parent = null);
screen.Verify(x => x.Close());
}
[Test]
public void RemovingActiveItemActivatesAnotherItem()
{
((IActivate)this.conductor).Activate();
var screen1 = new Mock<IScreen>();
var screen2 = new Mock<IScreen>();
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());
}
[Test]