Skip to content

Commit

Permalink
Start adding settings tests
Browse files Browse the repository at this point in the history
  • Loading branch information
danielchalmers committed Jul 17, 2024
1 parent ddeab63 commit cdfbf7d
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 3 deletions.
87 changes: 87 additions & 0 deletions DesktopClock.Tests/SettingsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
using System;
using System.IO;
using System.Threading.Tasks;
using DesktopClock.Properties;

namespace DesktopClock.Tests;

public class SettingsTests : IAsyncLifetime
{
public Task InitializeAsync()
{
// Ensure the settings file does not exist before each test.
if (File.Exists(Settings.FilePath))
{
File.Delete(Settings.FilePath);
}

return Task.CompletedTask;
}

public Task DisposeAsync()
{
// Clean up.
if (File.Exists(Settings.FilePath))
{
File.Delete(Settings.FilePath);
}

return Task.CompletedTask;
}

[Fact(Skip = "File path is unauthorized")]
public void Save_ShouldWriteSettingsToFile()
{
var settings = Settings.Default;
settings.FontFamily = "My custom font";

var result = settings.Save();

Assert.True(result);
Assert.True(File.Exists(Settings.FilePath));

var savedSettings = File.ReadAllText(Settings.FilePath);
Assert.Contains("My custom font", savedSettings);
}

[Fact(Skip = "File path is unauthorized")]
public void LoadFromFile_ShouldPopulateSettings()
{
var json = "{\"FontFamily\": \"Consolas\"}";
File.WriteAllText(Settings.FilePath, json);
var settings = Settings.Default;
Assert.Equal("My custom font", settings.FontFamily);
}

[Fact]
public void ScaleHeight_ShouldAdjustHeightByExpectedAmount()
{
var settings = Settings.Default;

Assert.Equal(48, settings.Height);

settings.ScaleHeight(2);

Assert.Equal(64, settings.Height);

settings.ScaleHeight(-2);

Assert.Equal(47, settings.Height);
}

[Fact]
public void GetTimeZoneInfo_ShouldReturnExpectedTimeZoneInfo()
{
var settings = Settings.Default;

// Default TimeZone should return Local
var localTimeZone = TimeZoneInfo.Local;
var timeZoneInfo = settings.GetTimeZoneInfo();
Assert.Equal(localTimeZone, timeZoneInfo);

// Set to a specific time zone
settings.TimeZone = "Pacific Standard Time";
timeZoneInfo = settings.GetTimeZoneInfo();
Assert.Equal("Pacific Standard Time", timeZoneInfo.Id);
}
}
9 changes: 6 additions & 3 deletions DesktopClock/Properties/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,18 @@ public sealed class Settings : INotifyPropertyChanged, IDisposable
public static readonly double MaxSizeLog = 6.5;
public static readonly double MinSizeLog = 2.7;

// Private constructor to enforce singleton pattern.
private Settings()
static Settings()
{
// Settings file path from the same directory as the executable.
var settingsFileName = Path.GetFileNameWithoutExtension(App.MainFileInfo.FullName) + ".settings";
FilePath = Path.Combine(App.MainFileInfo.DirectoryName, settingsFileName);
}

// Private constructor to enforce singleton pattern.
private Settings()
{
// Watch for changes in the settings file.
_watcher = new(App.MainFileInfo.DirectoryName, settingsFileName)
_watcher = new(App.MainFileInfo.DirectoryName, Path.GetFileName(FilePath))
{
EnableRaisingEvents = true,
};
Expand Down

0 comments on commit cdfbf7d

Please sign in to comment.