-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d0df8e6
commit 0480685
Showing
8 changed files
with
202 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace SpotifyTools.Contracts | ||
{ | ||
public interface IProcessAnalyser | ||
{ | ||
bool IsAppRunning(string processName); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |