Skip to content

Commit

Permalink
First set of Facade UnitTests
Browse files Browse the repository at this point in the history
  • Loading branch information
NicolasConstant committed Feb 28, 2016
1 parent d0df8e6 commit 0480685
Show file tree
Hide file tree
Showing 8 changed files with 202 additions and 3 deletions.
7 changes: 7 additions & 0 deletions SpotifySleepModeStopper/Contracts/IProcessAnalyser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace SpotifyTools.Contracts
{
public interface IProcessAnalyser
{
bool IsAppRunning(string processName);
}
}
9 changes: 7 additions & 2 deletions SpotifySleepModeStopper/SpotifySaveModeStopperFacade.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class SpotifySaveModeStopperFacade : ISpotifySaveModeStopperFacade
private readonly IAppStatusReporting _appState;
private readonly IAutoStartManager _autoStartManager;
private readonly ISettingsManager _settingsManager;
private readonly IProcessAnalyser _processAnalyser;

private bool _spotifyRunning;
private bool _spotifyPlaying;
Expand All @@ -36,16 +37,19 @@ public class SpotifySaveModeStopperFacade : ISpotifySaveModeStopperFacade

#region Ctor
public SpotifySaveModeStopperFacade(IMessageDisplayer messageDisplayer, IPreventSleepScreen preventSleepScreen,
ISoundAnalyser soundAnalyser, IAppStatusReporting appState, IAutoStartManager autoStartManager, ISettingsManager settingsManager)
ISoundAnalyser soundAnalyser, IProcessAnalyser processAnalyser, IAppStatusReporting appState, IAutoStartManager autoStartManager, ISettingsManager settingsManager, int refreshRate = -1)
{
_messageDisplayer = messageDisplayer;
_preventSleepScreen = preventSleepScreen;
_soundAnalyser = soundAnalyser;
_appState = appState;
_autoStartManager = autoStartManager;
_settingsManager = settingsManager;
_processAnalyser = processAnalyser;

_screenSleepEnabled = IsScreenSleepEnabled();
if(refreshRate > 0)
_checkInterval = TimeSpan.FromSeconds(refreshRate);
}
#endregion

Expand Down Expand Up @@ -164,7 +168,8 @@ private void AnalyseSpotifyStatus(CancellationToken token)

private bool IsSpotifyRunning()
{
return Process.GetProcessesByName(SpotifyProcessName).Any();
var isSpotifyRunning = _processAnalyser.IsAppRunning(SpotifyProcessName);
return isSpotifyRunning;
}

private bool IsSoundStreaming()
Expand Down
2 changes: 2 additions & 0 deletions SpotifySleepModeStopper/SpotifySleepModeStopper.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
<Compile Include="Contracts\IAppStateReporting.cs" />
<Compile Include="Contracts\IMessageDisplayer.cs" />
<Compile Include="Contracts\IPreventSleepScreen.cs" />
<Compile Include="Contracts\IProcessAnalyser.cs" />
<Compile Include="Contracts\ISoundAnalyser.cs" />
<Compile Include="DomainLayer\AppStatesManagement\AppStateReporting.cs" />
<Compile Include="Contracts\ISpotifySaveModeStopperFacade.cs" />
Expand All @@ -56,6 +57,7 @@
<Compile Include="Contracts\ISettingsManager.cs" />
<Compile Include="Tools\JsonSerializerHelper.cs" />
<Compile Include="Tools\Model\AppSettings.cs" />
<Compile Include="Tools\ProcessAnalyser.cs" />
<Compile Include="Tools\Repeat.cs" />
<Compile Include="SpotifySaveModeStopperFacade.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
Expand Down
18 changes: 18 additions & 0 deletions SpotifySleepModeStopper/Tools/ProcessAnalyser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SpotifyTools.Contracts;

namespace SpotifyTools.Tools
{
public class ProcessAnalyser : IProcessAnalyser
{
public bool IsAppRunning(string processName)
{
return Process.GetProcessesByName(processName).Any();
}
}
}
3 changes: 2 additions & 1 deletion SpotifySleepModeStopperGui/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,15 @@ public MainWindow()
var messageDisplayer = new DummyMessageDisplayer();
var powerHandler = new PowerRequestContextHandler();
var soundAnalyser = new CsCoreSoundAnalyser(messageDisplayer);
var processAnalyser = new ProcessAnalyser();

var fullPath = Assembly.GetExecutingAssembly().Location;
var autoStartManager = new AutoStartManager("SpotifySleepModeStopper", fullPath);

var settingsManager = new SettingsManager("SpotifySleepModeStopper");
#endregion

_facade = new SpotifySaveModeStopperFacade(messageDisplayer, powerHandler, soundAnalyser, iconChanger, autoStartManager, settingsManager);
_facade = new SpotifySaveModeStopperFacade(messageDisplayer, powerHandler, soundAnalyser, processAnalyser, iconChanger, autoStartManager, settingsManager);

#region Events Subscription
Closing += OnClosing;
Expand Down
154 changes: 154 additions & 0 deletions UnitTestProject/FacadeUnitTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
using System;
using System.IO;
using System.Security.Policy;
using System.Threading;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Rhino.Mocks;
using Rhino.Mocks.Constraints;
using Rhino.Mocks.Impl.Invocation.Specifications;
using SpotifyTools;
using SpotifyTools.Contracts;
using SpotifyTools.Tools;
using SpotifyTools.Tools.Model;

namespace UnitTestProject
{
/// <summary>
/// Use RhinoMocks
///
/// Quick Guide:
/// http://www.wrightfully.com/using-rhino-mocks-quick-guide-to-generating-mocks-and-stubs/
/// </summary>
[TestClass]
public class FacadeUnitTest
{
[TestMethod]
public void CheckIfScreenSleepEnabledTestMethod()
{
var settingsManagerStub = MockRepository.GenerateMock<ISettingsManager>();
settingsManagerStub.Expect(x => x.GetConfig()).Return(new AppSettings { IsScreenSleepEnabled = true });

var facade = new SpotifySaveModeStopperFacade(null, null, null, null, null, null, settingsManagerStub);

Assert.IsTrue(facade.IsScreenSleepEnabled());
settingsManagerStub.VerifyAllExpectations();
}

[TestMethod]
public void CheckIfAutoStartEnabledTestMethod()
{
var settingsManagerStub = MockRepository.GenerateStub<ISettingsManager>();
settingsManagerStub.Stub(x => x.GetConfig()).Return(new AppSettings { IsScreenSleepEnabled = true });

var autoStartManagerStub = MockRepository.GenerateMock<IAutoStartManager>();
autoStartManagerStub.Expect(x => x.IsAutoStartSet()).Return(true);

var facade = new SpotifySaveModeStopperFacade(null, null, null, null, null, autoStartManagerStub, settingsManagerStub);

Assert.IsTrue(facade.IsAutoStartEnabled());
autoStartManagerStub.VerifyAllExpectations();
}

[TestMethod]
public void ChangeScreenSleepSettingsTestMethod()
{
var saveConfigFired = new ManualResetEvent(false);

var settingsManagerStub = MockRepository.GenerateMock<ISettingsManager>();
settingsManagerStub.Stub(x => x.GetConfig()).Return(new AppSettings { IsScreenSleepEnabled = true });
settingsManagerStub.Expect(x => x.SaveConfig(new AppSettings()))
.Constraints(Property.Value("IsScreenSleepEnabled", true))
.Do((Action<AppSettings>)(ant => { saveConfigFired.Set(); }));

var facade = new SpotifySaveModeStopperFacade(null, null, null, null, null, null, settingsManagerStub);
facade.ChangeScreenSleep(true);

Assert.IsTrue(saveConfigFired.WaitOne(10));
settingsManagerStub.VerifyAllExpectations();
}

[TestMethod]
public void ChangeAutoStartSettingsTestMethod()
{
var autoStartChangeFired = new ManualResetEvent(false);

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

var autoStartManagerStub = MockRepository.GenerateMock<IAutoStartManager>();
autoStartManagerStub.Expect(x => x.SetAutoStart(Arg<bool>.Is.Equal(true)))
.Do((Action<bool>)(ant => { autoStartChangeFired.Set(); }));

var facade = new SpotifySaveModeStopperFacade(null, null, null, null, null, autoStartManagerStub, settingsManagerStub);
facade.ChangeAutoStart(true);

Assert.IsTrue(autoStartChangeFired.WaitOne(10));
autoStartManagerStub.VerifyAllExpectations();
}

[TestMethod]
public void ListeningTestMethod()
{
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 - true - false 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 if(calledTimes < 5)
invk.ReturnValue = true;
else
invk.ReturnValue = false;
});

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

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

preventSleepScreenMock.VerifyAllExpectations();
soundAnalyserMock.VerifyAllExpectations();
}
}
}
8 changes: 8 additions & 0 deletions UnitTestProject/UnitTestProject.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Rhino.Mocks, Version=3.6.0.0, Culture=neutral, PublicKeyToken=0b3305902db7183f, processorArchitecture=MSIL">
<HintPath>..\packages\RhinoMocks.3.6.1\lib\net\Rhino.Mocks.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
</ItemGroup>
<Choose>
Expand All @@ -50,6 +54,7 @@
</Otherwise>
</Choose>
<ItemGroup>
<Compile Include="FacadeUnitTest.cs" />
<Compile Include="SettingsUnitTest.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
Expand All @@ -59,6 +64,9 @@
<Name>SpotifySleepModeStopper</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<Choose>
<When Condition="'$(VisualStudioVersion)' == '10.0' And '$(IsCodedUITest)' == 'True'">
<ItemGroup>
Expand Down
4 changes: 4 additions & 0 deletions UnitTestProject/packages.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="RhinoMocks" version="3.6.1" targetFramework="net452" />
</packages>

0 comments on commit 0480685

Please sign in to comment.