浏览代码

Start adding settings tests

Daniel Chalmers 1 年之前
父节点
当前提交
cdfbf7d56d
共有 2 个文件被更改,包括 93 次插入3 次删除
  1. 87 0
      DesktopClock.Tests/SettingsTests.cs
  2. 6 3
      DesktopClock/Properties/Settings.cs

+ 87 - 0
DesktopClock.Tests/SettingsTests.cs

@@ -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);
+    }
+}

+ 6 - 3
DesktopClock/Properties/Settings.cs

@@ -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,
         };