changed settings saving (#3)

Reviewed-on: #3
Co-authored-by: Aleksandr Neychev <alexnejchev73@gmail.com>
This commit was merged in pull request #3.
This commit is contained in:
2026-08-12 17:15:12 +00:00
committed by alex
parent 68b3d544d2
commit 5e11b16758
19 changed files with 1169 additions and 150 deletions
@@ -71,7 +71,7 @@ public sealed class CapsLockSwitchCoordinatorTests
int startsBefore = hotkey.StartCalls;
int stopsBefore = hotkey.StopCalls;
settings.FontSize = 44;
settings.Current.FontSize = 44;
settings.CapsLockHoldMilliseconds = 700;
Assert.Equal(startsBefore, hotkey.StartCalls);
@@ -30,7 +30,7 @@ public sealed class SettingsServiceTests
});
Assert.Equal(AppTheme.System, settings.Theme);
Assert.Equal(20, settings.FontSize);
Assert.Equal(20, settings.Current.FontSize);
}
[Theory]
@@ -72,10 +72,10 @@ public sealed class SettingsServiceTests
using SettingsService service = Create(folder);
AppSettings settings = service.Load();
settings.FontSize = 42;
settings.Theme = AppTheme.Dark;
settings.PlacementMode = PopupPlacementMode.AtCaret;
settings.BackgroundColor = Color.FromArgb(0x11, 0x22, 0x33);
settings.Current.FontSize = 42;
settings.Current.BackgroundColor = Color.FromArgb(0x11, 0x22, 0x33);
settings.UseCapsLockHotkey = true;
service.Save();
@@ -87,10 +87,10 @@ public sealed class SettingsServiceTests
return service.Load();
});
Assert.Equal(42, restored.FontSize);
Assert.Equal(42, restored.Current.FontSize);
Assert.Equal(AppTheme.Dark, restored.Theme);
Assert.Equal(PopupPlacementMode.AtCaret, restored.PlacementMode);
Assert.Equal(Color.FromArgb(0x11, 0x22, 0x33), restored.BackgroundColor);
Assert.Equal(Color.FromArgb(0x11, 0x22, 0x33), restored.Current.BackgroundColor);
Assert.True(restored.UseCapsLockHotkey);
}
@@ -104,7 +104,7 @@ public sealed class SettingsServiceTests
using SettingsService service = Create(folder);
AppSettings settings = service.Load();
settings.Theme = AppTheme.Dark;
settings.BackgroundColor = Color.FromArgb(0x20, 0x20, 0x20);
settings.Current.BackgroundColor = Color.FromArgb(0x20, 0x20, 0x20);
service.Save();
});
@@ -130,7 +130,7 @@ public sealed class SettingsServiceTests
AppSettings settings = service.Load();
service.TrackChanges();
settings.FontSize = 33;
settings.Current.FontSize = 33;
// Right after the edit there is nothing on disk yet: the write is deferred
Assert.False(File.Exists(path));
@@ -156,7 +156,7 @@ public sealed class SettingsServiceTests
for (int i = 0; i < 10; i++)
{
settings.Opacity = 0.5 + (i * 0.01);
settings.Current.Opacity = 0.5 + (i * 0.01);
Assert.False(File.Exists(path));
Pump.Pause(TimeSpan.FromMilliseconds(10));
}
@@ -188,7 +188,7 @@ public sealed class SettingsServiceTests
service.TrackChanges();
AppSettings settings = service.Load();
settings.FontSize = 29;
settings.Current.FontSize = 29;
Pump.WaitFor(() => File.Exists(path), "the settings were written by the timer");
});
@@ -230,11 +230,11 @@ public sealed class SettingsServiceTests
using SettingsService service = Create(folder);
AppSettings settings = service.Load();
File.WriteAllText(path, """{"FontSize": 31, "BackgroundColor": "#FF102030"}""");
File.WriteAllText(path, """{"AtCursor": {"FontSize": 31, "BackgroundColor": "#FF102030"}}""");
service.Reload();
Assert.Equal(31, settings.FontSize);
Assert.Equal(Color.FromArgb(0x10, 0x20, 0x30), settings.BackgroundColor);
Assert.Equal(31, settings.Current.FontSize);
Assert.Equal(Color.FromArgb(0x10, 0x20, 0x30), settings.Current.BackgroundColor);
});
}
@@ -250,12 +250,12 @@ public sealed class SettingsServiceTests
{
using SettingsService service = Create(folder);
AppSettings settings = service.Load();
settings.FontSize = 44;
settings.Current.FontSize = 44;
File.WriteAllText(path, "not json at all");
service.Reload();
Assert.Equal(44, settings.FontSize);
Assert.Equal(44, settings.Current.FontSize);
});
}
@@ -269,7 +269,7 @@ public sealed class SettingsServiceTests
SettingsService service = Create(folder);
AppSettings settings = service.Load();
service.TrackChanges();
settings.FontSize = 27;
settings.Current.FontSize = 27;
service.Dispose();
});
@@ -295,7 +295,7 @@ public sealed class SettingsServiceTests
string afterDispose = File.ReadAllText(path);
settings.FontSize = 99;
settings.Current.FontSize = 99;
Pump.Pause(TimeSpan.FromMilliseconds(60));
Assert.Equal(afterDispose, File.ReadAllText(path));
@@ -309,7 +309,7 @@ public sealed class SettingsServiceTests
string inherited = folder.File("inherited.json");
string own = folder.File("settings.json");
File.WriteAllText(inherited, """{"FontSize": 31, "Language": "ru"}""");
File.WriteAllText(inherited, """{"AtCursor": {"FontSize": 31}, "Language": "ru"}""");
AppSettings settings = Pump.Run(() =>
{
@@ -317,7 +317,7 @@ public sealed class SettingsServiceTests
return service.Load();
});
Assert.Equal(31, settings.FontSize);
Assert.Equal(31, settings.Current.FontSize);
Assert.Equal("ru", settings.Language);
// What was taken over is pinned to its new place at once rather than on the first edit
@@ -331,7 +331,7 @@ public sealed class SettingsServiceTests
{
using var folder = new TempFolder();
string inherited = folder.File("inherited.json");
string original = """{"FontSize": 31}""";
string original = """{"AtCursor": {"FontSize": 31}}""";
File.WriteAllText(inherited, original);
@@ -351,8 +351,8 @@ public sealed class SettingsServiceTests
string own = folder.File("settings.json");
string inherited = folder.File("inherited.json");
File.WriteAllText(own, """{"FontSize": 12}""");
File.WriteAllText(inherited, """{"FontSize": 31}""");
File.WriteAllText(own, """{"AtCursor": {"FontSize": 12}}""");
File.WriteAllText(inherited, """{"AtCursor": {"FontSize": 31}}""");
AppSettings settings = Pump.Run(() =>
{
@@ -360,7 +360,7 @@ public sealed class SettingsServiceTests
return service.Load();
});
Assert.Equal(12, settings.FontSize);
Assert.Equal(12, settings.Current.FontSize);
}
// Outside a package both paths are the same, so there is nothing to take over
@@ -392,14 +392,14 @@ public sealed class SettingsServiceTests
return service.Load();
});
Assert.Equal(20, settings.FontSize);
Assert.Equal(20, settings.Current.FontSize);
}
[Fact]
public void Settings_with_unknown_fields_are_still_read()
{
using var folder = new TempFolder();
File.WriteAllText(folder.File("settings.json"), """{"FontSize": 15, "SomethingNew": true}""");
File.WriteAllText(folder.File("settings.json"), """{"AtCursor": {"FontSize": 15}, "SomethingNew": true}""");
AppSettings settings = Pump.Run(() =>
{
@@ -407,7 +407,7 @@ public sealed class SettingsServiceTests
return service.Load();
});
Assert.Equal(15, settings.FontSize);
Assert.Equal(15, settings.Current.FontSize);
}
[Theory]
@@ -417,7 +417,7 @@ public sealed class SettingsServiceTests
public void A_colour_is_read_from_its_usual_notation(string stored, byte r, byte g, byte b)
{
using var folder = new TempFolder();
File.WriteAllText(folder.File("settings.json"), $$"""{"BackgroundColor": {{stored}}}""");
File.WriteAllText(folder.File("settings.json"), $$"""{"AtCursor": {"BackgroundColor": {{stored}} } }""");
AppSettings settings = Pump.Run(() =>
{
@@ -425,7 +425,7 @@ public sealed class SettingsServiceTests
return service.Load();
});
Assert.Equal(Color.FromArgb(r, g, b), settings.BackgroundColor);
Assert.Equal(Color.FromArgb(r, g, b), settings.Current.BackgroundColor);
}
[Theory]
@@ -435,7 +435,7 @@ public sealed class SettingsServiceTests
public void An_unintelligible_colour_becomes_black(string stored)
{
using var folder = new TempFolder();
File.WriteAllText(folder.File("settings.json"), $$"""{"BackgroundColor": {{stored}}}""");
File.WriteAllText(folder.File("settings.json"), $$"""{"AtCursor": {"BackgroundColor": {{stored}} } }""");
AppSettings settings = Pump.Run(() =>
{
@@ -443,7 +443,7 @@ public sealed class SettingsServiceTests
return service.Load();
});
Assert.Equal(Color.Black, settings.BackgroundColor);
Assert.Equal(Color.Black, settings.Current.BackgroundColor);
}
// The service creates the settings folder itself
@@ -478,7 +478,7 @@ public sealed class SettingsServiceTests
using SettingsService service = new(path, folder.File("inherited.json"), SaveDelay);
AppSettings settings = service.Load();
settings.FontSize = 18;
settings.Current.FontSize = 18;
service.Save();
});
@@ -534,6 +534,107 @@ public sealed class SettingsServiceTests
}
}
// The modes are sections of their own, and every setting of a mode has to reach the
// file inside its own section
[Theory]
[InlineData(nameof(AppSettings.AtCursor), typeof(CursorModeSettings))]
[InlineData(nameof(AppSettings.AtCaret), typeof(CaretModeSettings))]
[InlineData(nameof(AppSettings.FixedPoint), typeof(FixedPointModeSettings))]
public void Every_setting_of_a_mode_reaches_its_section_of_the_file(string section, Type mode)
{
using var folder = new TempFolder();
Pump.Run(() =>
{
using SettingsService service = Create(folder);
_ = service.Load();
service.Save();
});
using JsonDocument document = JsonDocument.Parse(File.ReadAllText(folder.File("settings.json")));
Assert.True(document.RootElement.TryGetProperty(section, out JsonElement stored));
foreach (string name in PopupModeSettingsTests.WritablePropertyNames(mode))
{
Assert.True(stored.TryGetProperty(name, out _), $"{section}.{name}");
}
}
/// <summary>
/// The modes are stored apart: what is set up in one is still there after a trip
/// through the file and the other two.
/// </summary>
[Fact]
public void Each_mode_keeps_its_own_settings()
{
using var folder = new TempFolder();
Pump.Run(() =>
{
using SettingsService service = Create(folder);
AppSettings settings = service.Load();
settings.AtCursor.Side = AnchorSide.TopLeft;
settings.AtCursor.FontSize = 14;
settings.AtCaret.Side = CaretSide.Left;
settings.AtCaret.FontSize = 28;
settings.AtCaret.ForegroundColor = Color.FromArgb(0x0A, 0x0B, 0x0C);
settings.FixedPoint.Position = ScreenPosition.Top;
settings.FixedPoint.Offset = 96;
settings.FixedPoint.Opacity = 0.4;
service.Save();
});
AppSettings restored = Pump.Run(() =>
{
using SettingsService service = Create(folder);
return service.Load();
});
Assert.Equal(AnchorSide.TopLeft, restored.AtCursor.Side);
Assert.Equal(14, restored.AtCursor.FontSize);
Assert.Equal(CaretSide.Left, restored.AtCaret.Side);
Assert.Equal(28, restored.AtCaret.FontSize);
Assert.Equal(Color.FromArgb(0x0A, 0x0B, 0x0C), restored.AtCaret.ForegroundColor);
Assert.Equal(ScreenPosition.Top, restored.FixedPoint.Position);
Assert.Equal(96, restored.FixedPoint.Offset);
Assert.Equal(0.4, restored.FixedPoint.Opacity);
// What was left alone stays at its default rather than following a neighbour
Assert.Equal(0.9, restored.AtCursor.Opacity);
}
// A change inside a mode is a change of the settings: the file has to follow the
// sliders of the look as well as the ones above them
[Fact]
public void A_change_inside_a_mode_saves_itself()
{
using var folder = new TempFolder();
string path = folder.File("settings.json");
Pump.Run(() =>
{
using SettingsService service = Create(folder);
AppSettings settings = service.Load();
service.TrackChanges();
settings.AtCaret.FontSize = 37;
Pump.WaitFor(() => File.Exists(path), "the settings were written by the timer");
});
using JsonDocument document = JsonDocument.Parse(File.ReadAllText(path));
Assert.Equal(
37,
document.RootElement
.GetProperty(nameof(AppSettings.AtCaret))
.GetProperty(nameof(PopupModeSettings.FontSize))
.GetDouble());
}
// An ordinary run picks the storage place itself: a package keeps settings
// of its own, a separate install keeps them in the user profile
[Fact]