Stylet/StyletUnitTests/ScreenExtensionTests.cs

201 lines
5.3 KiB
C#
Raw Normal View History

2014-05-07 05:20:36 -07:00
using Moq;
using NUnit.Framework;
using Stylet;
using System;
namespace StyletUnitTests
{
[TestFixture]
public class ScreenExtensionTests
{
public interface IMyScreen : IScreen, IDisposable
{ }
2014-05-07 05:20:36 -07:00
private Screen parent;
private Mock<IMyScreen> child;
2014-05-07 05:20:36 -07:00
[SetUp]
public void SetUp()
{
this.parent = new Screen();
this.child = new Mock<IMyScreen>();
2014-05-07 05:20:36 -07:00
}
[Test]
public void TryActivateActivatesIScreenState()
2014-05-07 05:20:36 -07:00
{
var screen = new Mock<IScreenState>();
2014-05-07 05:20:36 -07:00
ScreenExtensions.TryActivate(screen.Object);
screen.Verify(x => x.Activate());
}
[Test]
public void TryActivateDoesNothingToNonIScreenState()
2014-05-07 05:20:36 -07:00
{
var screen = new Mock<IGuardClose>(MockBehavior.Strict);
2014-05-07 05:20:36 -07:00
ScreenExtensions.TryActivate(screen.Object);
}
[Test]
public void TryDeactivateDeactivatesIScreenState()
2014-05-07 05:20:36 -07:00
{
var screen = new Mock<IScreenState>();
2014-05-07 05:20:36 -07:00
ScreenExtensions.TryDeactivate(screen.Object);
screen.Verify(x => x.Deactivate());
}
[Test]
public void TryDeactivateDoesNothingToNonIScreenState()
2014-05-07 05:20:36 -07:00
{
var screen = new Mock<IGuardClose>(MockBehavior.Strict);
2014-05-07 05:20:36 -07:00
ScreenExtensions.TryDeactivate(screen.Object);
}
[Test]
public void TryCloseClosesIScreenState()
2014-05-07 05:20:36 -07:00
{
var screen = new Mock<IScreenState>();
ScreenExtensions.TryClose(screen.Object);
2014-05-07 05:20:36 -07:00
screen.Verify(x => x.Close());
}
[Test]
public void TryDisposeDisposesIDisposable()
{
var screen = new Mock<IDisposable>();
ScreenExtensions.TryDispose(screen.Object);
screen.Verify(x => x.Dispose());
}
[Test]
public void TryCloseDoesNothingToNonIScreenState()
2014-05-07 05:20:36 -07:00
{
var screen = new Mock<IGuardClose>(MockBehavior.Strict);
ScreenExtensions.TryClose(screen.Object);
2014-05-07 05:20:36 -07:00
}
2015-02-09 04:48:18 -08:00
[Test]
public void ActivateWithActivates()
{
this.child.Object.ActivateWith(this.parent);
((IScreenState)this.parent).Activate();
this.child.Verify(x => x.Activate());
}
[Test]
public void ActivateWithDoesNotRetainChild()
{
2019-09-22 07:07:38 -07:00
WeakReference Test()
{
var child = new Screen();
child.ActivateWith(this.parent);
return new WeakReference(child);
}
var weakChild = Test();
2015-02-09 04:48:18 -08:00
GC.Collect();
((IScreenState)this.parent).Activate();
Assert.Null(weakChild.Target);
}
2014-05-07 05:20:36 -07:00
[Test]
public void ConductWithActivates()
{
this.child.Object.ConductWith(this.parent);
((IScreenState)this.parent).Activate();
2014-05-07 05:20:36 -07:00
this.child.Verify(x => x.Activate());
}
2015-02-09 04:48:18 -08:00
[Test]
public void DeactivateWithDeactivates()
{
// Needs to be active....
((IScreenState)this.parent).Activate();
this.child.Object.DeactivateWith(this.parent);
((IScreenState)this.parent).Deactivate();
this.child.Verify(x => x.Deactivate());
}
[Test]
public void DeactivateDoesNotRetainChild()
{
2019-09-22 07:07:38 -07:00
WeakReference Test()
{
var child = new Screen();
child.DeactivateWith(this.parent);
return new WeakReference(child);
}
var weakChild = Test();
2015-02-09 04:48:18 -08:00
GC.Collect();
((IScreenState)this.parent).Deactivate();
Assert.Null(weakChild.Target);
}
2014-05-07 05:20:36 -07:00
[Test]
public void ConductWithDeactivates()
{
// Needs to be active....
((IScreenState)this.parent).Activate();
2014-05-07 05:20:36 -07:00
this.child.Object.ConductWith(this.parent);
((IScreenState)this.parent).Deactivate();
2014-05-07 05:20:36 -07:00
this.child.Verify(x => x.Deactivate());
}
2015-02-09 04:48:18 -08:00
[Test]
public void CloseWithCloses()
{
this.child.Object.CloseWith(this.parent);
((IScreenState)this.parent).Close();
this.child.Verify(x => x.Close());
}
[Test]
public void CloseWithDoesNotRetain()
{
2019-09-22 07:07:38 -07:00
WeakReference Test()
{
var child = new Screen();
child.CloseWith(this.parent);
return new WeakReference(child);
}
var weakChild = Test();
2015-02-09 04:48:18 -08:00
GC.Collect();
((IScreenState)this.parent).Close();
Assert.Null(weakChild.Target);
}
2014-05-07 05:20:36 -07:00
[Test]
public void ConductWithCloses()
{
this.child.Object.ConductWith(this.parent);
((IScreenState)this.parent).Close();
2014-05-07 05:20:36 -07:00
this.child.Verify(x => x.Close());
}
[Test]
public void ConductWithDoesNotRetain()
{
2019-09-22 07:07:38 -07:00
WeakReference Test()
{
var child = new Screen();
child.ConductWith(this.parent);
return new WeakReference(child);
}
var weakChild = Test();
2014-05-07 05:20:36 -07:00
GC.Collect();
Assert.Null(weakChild.Target);
}
}
}