Skip to content

Commit

Permalink
New Facade UnitTests
Browse files Browse the repository at this point in the history
Code coverage: 100%
  • Loading branch information
NicolasConstant committed Feb 28, 2016
1 parent 0480685 commit 331c78c
Show file tree
Hide file tree
Showing 2 changed files with 157 additions and 1 deletion.
2 changes: 1 addition & 1 deletion SpotifySleepModeStopper/SpotifySaveModeStopperFacade.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public void StopListening()
_preventSleepScreen.EnableConstantDisplayAndPower(false, false);
}

public void ResetListening()
private void ResetListening()
{
_messageDisplayer.OutputMessage("Reset listening");

Expand Down
156 changes: 156 additions & 0 deletions UnitTestProject/FacadeUnitTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,5 +150,161 @@ public void ListeningTestMethod()
preventSleepScreenMock.VerifyAllExpectations();
soundAnalyserMock.VerifyAllExpectations();
}

[TestMethod]
public void StopListeningTestMethod()
{
const string spotifyProcessName = "Spotify";

var messageDisplayerStub = MockRepository.GenerateStub<IMessageDisplayer>();
messageDisplayerStub.Stub(x => x.OutputMessage(Arg<string>.Is.Anything)).Repeat.Any();

var settingsManagerStub = MockRepository.GenerateStub<ISettingsManager>();
settingsManagerStub.Stub(x => x.GetConfig()).Return(new AppSettings { IsScreenSleepEnabled = true });

var processAnalyserStub = MockRepository.GenerateStub<IProcessAnalyser>();
processAnalyserStub.Stub(x => x.IsAppRunning(spotifyProcessName))
.Repeat.Any()
.Return(true);

var appStateStub = MockRepository.GenerateStub<IAppStatusReporting>();
appStateStub.Stub(x => x.NotifyAntiSleepingModeIsActivated());
appStateStub.Stub(x => x.NotifyAntiSleepingModeIsDisabled());

var constantDisplayEnabled = new ManualResetEvent(false);
var constantDisplayDisabled = new ManualResetEvent(false);

var preventSleepScreenMock = MockRepository.GenerateMock<IPreventSleepScreen>();
preventSleepScreenMock.Expect(x => x.EnableConstantDisplayAndPower(true, false))
.Do((Action<bool, bool>)((contantPower, constantScreen) =>
{
constantDisplayDisabled.Reset();
constantDisplayEnabled.Set();
}));

preventSleepScreenMock.Expect(x => x.EnableConstantDisplayAndPower(false, false))
.Do((Action<bool, bool>)((contantPower, constantScreen) =>
{
constantDisplayDisabled.Set();
}));

//Sequence:
// false - false - true always
var calledTimes = 0;
var soundAnalyserMock = MockRepository.GenerateMock<ISoundAnalyser>();

soundAnalyserMock.Expect(x => x.IsProcessNameOutputingSound(Arg<string>.Is.Equal(spotifyProcessName)))
.Return(false)
.WhenCalled(invk =>
{
Interlocked.Increment(ref calledTimes);
if (calledTimes < 3)
invk.ReturnValue = false;
else
invk.ReturnValue = true;
});

var facade = new SpotifySaveModeStopperFacade(messageDisplayerStub, preventSleepScreenMock, soundAnalyserMock, processAnalyserStub, appStateStub, null, settingsManagerStub, 1);
facade.StartListening();

Assert.IsTrue(constantDisplayEnabled.WaitOne(2 * 2 * 1500));
Assert.IsFalse(constantDisplayDisabled.WaitOne(1000));

facade.StopListening();

Assert.IsTrue(constantDisplayDisabled.WaitOne(2 * 3 * 1500));

preventSleepScreenMock.VerifyAllExpectations();
soundAnalyserMock.VerifyAllExpectations();
}

[TestMethod]
public void ChangeListeningTestMethod()
{
const string spotifyProcessName = "Spotify";

var messageDisplayerStub = MockRepository.GenerateStub<IMessageDisplayer>();
messageDisplayerStub.Stub(x => x.OutputMessage(Arg<string>.Is.Anything)).Repeat.Any();

var settingsManagerStub = MockRepository.GenerateStub<ISettingsManager>();
settingsManagerStub.Stub(x => x.GetConfig()).Return(new AppSettings { IsScreenSleepEnabled = true });

var processAnalyserStub = MockRepository.GenerateStub<IProcessAnalyser>();
processAnalyserStub.Stub(x => x.IsAppRunning(spotifyProcessName))
.Repeat.Any()
.Return(true);

var appStateStub = MockRepository.GenerateStub<IAppStatusReporting>();
appStateStub.Stub(x => x.NotifyAntiSleepingModeIsActivated());
appStateStub.Stub(x => x.NotifyAntiSleepingModeIsDisabled());

var constantDisplaySleepScreenEnabled = new ManualResetEvent(false);
var constantDisplayNoSleepScreenEnabled = new ManualResetEvent(false);
var constantDisplayDisabled = new ManualResetEvent(false);

var preventSleepScreenMock = MockRepository.GenerateMock<IPreventSleepScreen>();
preventSleepScreenMock.Expect(x => x.EnableConstantDisplayAndPower(true, false))
.Do((Action<bool, bool>)((contantPower, constantScreen) =>
{
constantDisplayDisabled.Reset();
constantDisplayNoSleepScreenEnabled.Reset();
constantDisplaySleepScreenEnabled.Set();
}));

preventSleepScreenMock.Expect(x => x.EnableConstantDisplayAndPower(true, true))
.Do((Action<bool, bool>)((contantPower, constantScreen) =>
{
constantDisplaySleepScreenEnabled.Reset();
constantDisplayDisabled.Reset();
constantDisplayNoSleepScreenEnabled.Set();
}));

preventSleepScreenMock.Expect(x => x.EnableConstantDisplayAndPower(false, false))
.Do((Action<bool, bool>)((contantPower, constantScreen) =>
{
constantDisplayNoSleepScreenEnabled.Reset();
constantDisplaySleepScreenEnabled.Reset();
constantDisplayDisabled.Set();
}));

//Sequence:
// false - false - true always
var calledTimes = 0;
var soundAnalyserMock = MockRepository.GenerateMock<ISoundAnalyser>();

soundAnalyserMock.Expect(x => x.IsProcessNameOutputingSound(Arg<string>.Is.Equal(spotifyProcessName)))
.Return(false)
.WhenCalled(invk =>
{
Interlocked.Increment(ref calledTimes);
if (calledTimes < 3)
invk.ReturnValue = false;
else
invk.ReturnValue = true;
});

var facade = new SpotifySaveModeStopperFacade(messageDisplayerStub, preventSleepScreenMock, soundAnalyserMock, processAnalyserStub, appStateStub, null, settingsManagerStub, 1);
facade.StartListening();

var debugCoef = 1;

#if DEBUG
debugCoef = 60;
#endif

Assert.IsTrue(constantDisplaySleepScreenEnabled.WaitOne(debugCoef * 2 * 2 * 1500));
Assert.IsFalse(constantDisplayDisabled.WaitOne(1000));

facade.ChangeScreenSleep(false);

Assert.IsTrue(constantDisplayDisabled.WaitOne(debugCoef * 2 * 1 * 1500));
Assert.IsTrue(constantDisplayNoSleepScreenEnabled.WaitOne(debugCoef * 2 * 1 * 1500));

Assert.IsFalse(constantDisplayDisabled.WaitOne(500));
Assert.IsFalse(constantDisplaySleepScreenEnabled.WaitOne(500));

preventSleepScreenMock.VerifyAllExpectations();
soundAnalyserMock.VerifyAllExpectations();
}
}
}

0 comments on commit 331c78c

Please sign in to comment.