Skip to content

Commit

Permalink
Made SettingsManager generic
Browse files Browse the repository at this point in the history
  • Loading branch information
NicolasConstant committed Feb 28, 2016
1 parent 331c78c commit ef8ae01
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 30 deletions.
6 changes: 3 additions & 3 deletions SpotifySleepModeStopper/Contracts/ISettingsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace SpotifyTools.Contracts
{
public interface ISettingsManager
public interface ISettingsManager<T> where T : class
{
AppSettings GetConfig();
void SaveConfig(AppSettings settings);
T GetConfig();
void SaveConfig(T settings);
}
}
5 changes: 3 additions & 2 deletions SpotifySleepModeStopper/SpotifySaveModeStopperFacade.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Threading.Tasks;
using SpotifyTools.Contracts;
using SpotifyTools.Tools;
using SpotifyTools.Tools.Model;

namespace SpotifyTools
{
Expand All @@ -17,7 +18,7 @@ public class SpotifySaveModeStopperFacade : ISpotifySaveModeStopperFacade
private readonly ISoundAnalyser _soundAnalyser;
private readonly IAppStatusReporting _appState;
private readonly IAutoStartManager _autoStartManager;
private readonly ISettingsManager _settingsManager;
private readonly ISettingsManager<AppSettings> _settingsManager;
private readonly IProcessAnalyser _processAnalyser;

private bool _spotifyRunning;
Expand All @@ -37,7 +38,7 @@ public class SpotifySaveModeStopperFacade : ISpotifySaveModeStopperFacade

#region Ctor
public SpotifySaveModeStopperFacade(IMessageDisplayer messageDisplayer, IPreventSleepScreen preventSleepScreen,
ISoundAnalyser soundAnalyser, IProcessAnalyser processAnalyser, IAppStatusReporting appState, IAutoStartManager autoStartManager, ISettingsManager settingsManager, int refreshRate = -1)
ISoundAnalyser soundAnalyser, IProcessAnalyser processAnalyser, IAppStatusReporting appState, IAutoStartManager autoStartManager, ISettingsManager<AppSettings> settingsManager, int refreshRate = -1)
{
_messageDisplayer = messageDisplayer;
_preventSleepScreen = preventSleepScreen;
Expand Down
20 changes: 10 additions & 10 deletions SpotifySleepModeStopper/Tools/SettingsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,24 @@

namespace SpotifyTools.Tools
{
public class SettingsManager : ISettingsManager
public class SettingsManager<T> : ISettingsManager<T> where T : class
{
private readonly string _fullPathSettingsFile;
private readonly T _defaultValue;

#region Ctor
public SettingsManager(string appName, string fileName = "settings.config")
public SettingsManager(string appName, T defaultValue, string fileName = "settings.config")
{
var userAppDataFolder = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\" + appName;

if (!Directory.Exists(userAppDataFolder)) Directory.CreateDirectory(userAppDataFolder);

_fullPathSettingsFile = userAppDataFolder + @"\" + fileName;
_defaultValue = defaultValue;
}
#endregion

public AppSettings GetConfig()
public T GetConfig()
{
string configFile;

Expand All @@ -30,17 +32,17 @@ public AppSettings GetConfig()
try
{
configFile = File.ReadAllText(_fullPathSettingsFile);
return JsonSerializerHelper.Deserialize<AppSettings>(configFile);
return JsonSerializerHelper.Deserialize<T>(configFile);
}
catch (Exception)
{
configFile = ResetConfigFile();
}

return JsonSerializerHelper.Deserialize<AppSettings>(configFile);
return JsonSerializerHelper.Deserialize<T>(configFile);
}

public void SaveConfig(AppSettings settings)
public void SaveConfig(T settings)
{
var serializedConfig = JsonSerializerHelper.Serialize(settings);

Expand All @@ -49,10 +51,8 @@ public void SaveConfig(AppSettings settings)

private string ResetConfigFile()
{
var newSettings = new AppSettings() { IsScreenSleepEnabled = false };

SaveConfig(newSettings);
return JsonSerializerHelper.Serialize(newSettings);
SaveConfig(_defaultValue);
return JsonSerializerHelper.Serialize(_defaultValue);
}
}
}
10 changes: 6 additions & 4 deletions SpotifySleepModeStopperGui/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using SpotifyTools.DomainLayer.MessageManagement;
using SpotifyTools.DomainLayer.PowerManagement;
using SpotifyTools.Tools;
using SpotifyTools.Tools.Model;

namespace SpotifySleepModeStopperGui
{
Expand All @@ -30,11 +31,12 @@ public partial class MainWindow : Window
private const string AppNotStartingOnStartupMess = "Auto-Start: Off";
private const string ScreenSleepEnabledMess = "Screen Sleep: On";
private const string ScreenSleepDisabledMess = "Screen Sleep: Off";
private const string ExitMess = "Exit";

private readonly MenuItem _autoStartMenuItem;
private readonly MenuItem _screenBehaviorMenuItem;

//private string _mess;
private const string AppName = "SpotifySleepModeStopper";

public MainWindow()
{
Expand All @@ -48,9 +50,9 @@ public MainWindow()
var processAnalyser = new ProcessAnalyser();

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

var settingsManager = new SettingsManager("SpotifySleepModeStopper");
var settingsManager = new SettingsManager<AppSettings>(AppName, new AppSettings {IsScreenSleepEnabled = false});
#endregion

_facade = new SpotifySaveModeStopperFacade(messageDisplayer, powerHandler, soundAnalyser, processAnalyser, iconChanger, autoStartManager, settingsManager);
Expand Down Expand Up @@ -79,7 +81,7 @@ public MainWindow()
var contextMenu = new ContextMenu();
var exitMenuItem = new MenuItem();
exitMenuItem.Index = 2;
exitMenuItem.Text = "Exit";
exitMenuItem.Text = ExitMess;
exitMenuItem.Click += exit_Click;

_screenBehaviorMenuItem = new MenuItem();
Expand Down
14 changes: 7 additions & 7 deletions UnitTestProject/FacadeUnitTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class FacadeUnitTest
[TestMethod]
public void CheckIfScreenSleepEnabledTestMethod()
{
var settingsManagerStub = MockRepository.GenerateMock<ISettingsManager>();
var settingsManagerStub = MockRepository.GenerateMock<ISettingsManager<AppSettings>>();
settingsManagerStub.Expect(x => x.GetConfig()).Return(new AppSettings { IsScreenSleepEnabled = true });

var facade = new SpotifySaveModeStopperFacade(null, null, null, null, null, null, settingsManagerStub);
Expand All @@ -37,7 +37,7 @@ public void CheckIfScreenSleepEnabledTestMethod()
[TestMethod]
public void CheckIfAutoStartEnabledTestMethod()
{
var settingsManagerStub = MockRepository.GenerateStub<ISettingsManager>();
var settingsManagerStub = MockRepository.GenerateStub<ISettingsManager<AppSettings>>();
settingsManagerStub.Stub(x => x.GetConfig()).Return(new AppSettings { IsScreenSleepEnabled = true });

var autoStartManagerStub = MockRepository.GenerateMock<IAutoStartManager>();
Expand All @@ -54,7 +54,7 @@ public void ChangeScreenSleepSettingsTestMethod()
{
var saveConfigFired = new ManualResetEvent(false);

var settingsManagerStub = MockRepository.GenerateMock<ISettingsManager>();
var settingsManagerStub = MockRepository.GenerateMock<ISettingsManager<AppSettings>>();
settingsManagerStub.Stub(x => x.GetConfig()).Return(new AppSettings { IsScreenSleepEnabled = true });
settingsManagerStub.Expect(x => x.SaveConfig(new AppSettings()))
.Constraints(Property.Value("IsScreenSleepEnabled", true))
Expand All @@ -72,7 +72,7 @@ public void ChangeAutoStartSettingsTestMethod()
{
var autoStartChangeFired = new ManualResetEvent(false);

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

var autoStartManagerStub = MockRepository.GenerateMock<IAutoStartManager>();
Expand All @@ -94,7 +94,7 @@ public void ListeningTestMethod()
var messageDisplayerStub = MockRepository.GenerateStub<IMessageDisplayer>();
messageDisplayerStub.Stub(x => x.OutputMessage(Arg<string>.Is.Anything)).Repeat.Any();

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

var processAnalyserStub = MockRepository.GenerateStub<IProcessAnalyser>();
Expand Down Expand Up @@ -159,7 +159,7 @@ public void StopListeningTestMethod()
var messageDisplayerStub = MockRepository.GenerateStub<IMessageDisplayer>();
messageDisplayerStub.Stub(x => x.OutputMessage(Arg<string>.Is.Anything)).Repeat.Any();

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

var processAnalyserStub = MockRepository.GenerateStub<IProcessAnalyser>();
Expand Down Expand Up @@ -226,7 +226,7 @@ public void ChangeListeningTestMethod()
var messageDisplayerStub = MockRepository.GenerateStub<IMessageDisplayer>();
messageDisplayerStub.Stub(x => x.OutputMessage(Arg<string>.Is.Anything)).Repeat.Any();

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

var processAnalyserStub = MockRepository.GenerateStub<IProcessAnalyser>();
Expand Down
8 changes: 4 additions & 4 deletions UnitTestProject/SettingsUnitTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public void FirstSettingsDeployementTestMethod()

Assert.IsFalse(File.Exists(fullFileName));

var settingsManager = new SettingsManager(AppName);
var settingsManager = new SettingsManager<AppSettings>(AppName, new AppSettings {IsScreenSleepEnabled = false});
settingsManager.GetConfig(); //First get => Reset config

Assert.IsTrue(File.Exists(fullFileName));
Expand All @@ -40,7 +40,7 @@ public void SaveSettingsTestMethod()

var appSettings = new AppSettings() {IsScreenSleepEnabled = true};

var settingsManager = new SettingsManager(AppName, configName);
var settingsManager = new SettingsManager<AppSettings>(AppName, new AppSettings {IsScreenSleepEnabled = false}, configName);
settingsManager.SaveConfig(appSettings);

Assert.IsTrue(File.Exists(fullFileName));
Expand Down Expand Up @@ -70,7 +70,7 @@ public void LoadSettingsTestMethod()

File.WriteAllText(fullFileName, configContentFalse);

var settingsManager = new SettingsManager(AppName, configName);
var settingsManager = new SettingsManager<AppSettings>(AppName, new AppSettings { IsScreenSleepEnabled = false }, configName);
var config = settingsManager.GetConfig();

Assert.IsFalse(config.IsScreenSleepEnabled);
Expand All @@ -94,7 +94,7 @@ public void LoadCorruptSettingsTestMethod()

File.WriteAllText(fullFileName, corruptConfigContent);

var settingsManager = new SettingsManager(AppName, configName);
var settingsManager = new SettingsManager<AppSettings>(AppName, new AppSettings { IsScreenSleepEnabled = false }, configName);
var config = settingsManager.GetConfig();

Assert.IsFalse(config.IsScreenSleepEnabled);
Expand Down

0 comments on commit ef8ae01

Please sign in to comment.