SettingsTests.cs 1.8 KB

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