Prevent double closure of a Screen

This commit is contained in:
Antony Male 2014-07-24 09:45:20 +01:00
parent d51ef4a0a4
commit 3e4e09a598
3 changed files with 33 additions and 5 deletions

View File

@ -65,6 +65,7 @@ namespace Stylet
return;
this.IsActive = true;
this.isClosed = false;
if (!this.hasBeenActivatedEver)
this.OnInitialActivate();
@ -103,6 +104,7 @@ namespace Stylet
return;
this.IsActive = false;
this.isClosed = false;
this.OnDeactivate();
@ -120,6 +122,8 @@ namespace Stylet
#region IClose
private bool isClosed = false;
/// <summary>
/// Called whenever this Screen is closed
/// </summary>
@ -128,10 +132,14 @@ namespace Stylet
[SuppressMessage("Microsoft.Design", "CA1033:InterfaceMethodsShouldBeCallableByChildTypes", Justification = "As this is a framework type, don't want to make it too easy for users to call this method")]
void IClose.Close()
{
if (this.isClosed)
return;
// This will early-exit if it's already deactive
((IDeactivate)this).Deactivate();
this.View = null;
this.isClosed = true;
this.OnClose();

View File

@ -200,6 +200,25 @@ namespace StyletUnitTests
Assert.IsTrue(this.screen.OnCloseCalled);
}
[Test]
public void DoubleCloseDoesNotClose()
{
((IClose)this.screen).Close();
this.screen.OnCloseCalled = false;
((IClose)this.screen).Close();
Assert.IsFalse(this.screen.OnCloseCalled);
}
[Test]
public void ActivatingAllowsScreenToBeClosedAgain()
{
((IClose)this.screen).Close();
this.screen.OnCloseCalled = false;
((IActivate)this.screen).Activate();
((IClose)this.screen).Close();
Assert.IsTrue(this.screen.OnCloseCalled);
}
[Test]
public void AttachViewAttachesView()
{
@ -213,7 +232,7 @@ namespace StyletUnitTests
{
var view = new UIElement();
((IViewAware)this.screen).AttachView(view);
Assert.Throws<Exception>(() => ((IViewAware)this.screen).AttachView(view));
Assert.Throws<InvalidOperationException>(() => ((IViewAware)this.screen).AttachView(view));
}
[Test]

View File

@ -79,6 +79,7 @@ namespace StyletUnitTests
[TestFixtureSetUp]
public void FixtureSetUp()
{
Execute.TestExecuteSynchronously = true;
AssemblySource.Assemblies.Clear();
}
@ -137,7 +138,7 @@ namespace StyletUnitTests
[Test]
public void LocateViewForModelThrowsIfViewNotFound()
{
Assert.Throws<Exception>(() => this.viewManager.LocateViewForModel(typeof(C1)));
Assert.Throws<StyletViewLocationException>(() => this.viewManager.LocateViewForModel(typeof(C1)));
}
[Test]
@ -155,13 +156,13 @@ namespace StyletUnitTests
var viewManager = new LocatingViewManager();
viewManager.LocatedViewType = typeof(I1);
Assert.Throws<Exception>(() => viewManager.CreateAndSetupViewForModel(new object()));
Assert.Throws<StyletViewLocationException>(() => viewManager.CreateAndSetupViewForModel(new object()));
viewManager.LocatedViewType = typeof(AC1);
Assert.Throws<Exception>(() => viewManager.CreateAndSetupViewForModel(new object()));
Assert.Throws<StyletViewLocationException>(() => viewManager.CreateAndSetupViewForModel(new object()));
viewManager.LocatedViewType = typeof(C1);
Assert.Throws<Exception>(() => viewManager.CreateAndSetupViewForModel(new object()));
Assert.Throws<StyletViewLocationException>(() => viewManager.CreateAndSetupViewForModel(new object()));
}
[Test]