SettingsTests.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. using System;
  2. using System.IO;
  3. using System.Threading.Tasks;
  4. using DesktopClock.Properties;
  5. namespace DesktopClock.Tests;
  6. public class SettingsTests : IAsyncLifetime
  7. {
  8. public Task InitializeAsync()
  9. {
  10. // Ensure the settings file does not exist before each test.
  11. if (File.Exists(Settings.FilePath))
  12. {
  13. File.Delete(Settings.FilePath);
  14. }
  15. return Task.CompletedTask;
  16. }
  17. public Task DisposeAsync()
  18. {
  19. // Clean up.
  20. if (File.Exists(Settings.FilePath))
  21. {
  22. File.Delete(Settings.FilePath);
  23. }
  24. return Task.CompletedTask;
  25. }
  26. [Fact(Skip = "File path is unauthorized")]
  27. public void Save_ShouldWriteSettingsToFile()
  28. {
  29. var settings = Settings.Default;
  30. settings.FontFamily = "My custom font";
  31. var result = settings.Save();
  32. Assert.True(result);
  33. Assert.True(File.Exists(Settings.FilePath));
  34. var savedSettings = File.ReadAllText(Settings.FilePath);
  35. Assert.Contains("My custom font", savedSettings);
  36. }
  37. [Fact(Skip = "File path is unauthorized")]
  38. public void LoadFromFile_ShouldPopulateSettings()
  39. {
  40. var json = "{\"FontFamily\": \"Consolas\"}";
  41. File.WriteAllText(Settings.FilePath, json);
  42. var settings = Settings.Default;
  43. Assert.Equal("My custom font", settings.FontFamily);
  44. }
  45. [Fact(Skip = "The process cannot access the file [...] because it is being used by another process.")]
  46. public void ScaleHeight_ShouldAdjustHeightByExpectedAmount()
  47. {
  48. var settings = Settings.Default;
  49. Assert.Equal(48, settings.Height);
  50. settings.ScaleHeight(2);
  51. Assert.Equal(64, settings.Height);
  52. settings.ScaleHeight(-2);
  53. Assert.Equal(47, settings.Height);
  54. }
  55. [Fact(Skip = "The process cannot access the file [...] because it is being used by another process.")]
  56. public void GetTimeZoneInfo_ShouldReturnExpectedTimeZoneInfo()
  57. {
  58. var settings = Settings.Default;
  59. // Default TimeZone should return Local
  60. var localTimeZone = TimeZoneInfo.Local;
  61. var timeZoneInfo = settings.GetTimeZoneInfo();
  62. Assert.Equal(localTimeZone, timeZoneInfo);
  63. // Set to a specific time zone
  64. settings.TimeZone = "Pacific Standard Time";
  65. timeZoneInfo = settings.GetTimeZoneInfo();
  66. Assert.Equal("Pacific Standard Time", timeZoneInfo.Id);
  67. }
  68. }