Skip to content
Open
1 change: 1 addition & 0 deletions tests/Beutl.UnitTests/Beutl.UnitTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
<ProjectReference Include="..\..\src\Beutl.Engine\Beutl.Engine.csproj" />
<ProjectReference Include="..\..\src\Beutl.ProjectSystem\Beutl.ProjectSystem.csproj" />
<ProjectReference Include="..\..\src\Beutl.Operators\Beutl.Operators.csproj" />
<ProjectReference Include="..\..\src\Beutl.Configuration\Beutl.Configuration.csproj" />
</ItemGroup>

<ItemGroup>
Expand Down
93 changes: 93 additions & 0 deletions tests/Beutl.UnitTests/Configuration/DefaultPreferencesTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
using System;
using System.IO;
using Beutl.Configuration;

namespace Beutl.UnitTests.Configuration;

public class DefaultPreferencesTests
{
private static string NewPrefsFile()
{
return Path.Combine(ArtifactProvider.GetArtifactDirectory(), "prefs.json");
}

[Test]
public void SetGet_PrimitiveTypes()
{
string file = NewPrefsFile();
if (File.Exists(file)) File.Delete(file);
var prefs = new DefaultPreferences(file);

prefs.Set("i", 42);
prefs.Set("d", 3.14159);
prefs.Set("b", true);
var dt = new DateTime(2023, 1, 2, 3, 4, 5, DateTimeKind.Utc);
prefs.Set("t", dt);
prefs.Set("s", "hello");

Assert.That(prefs.Get("i", 0), Is.EqualTo(42));
Assert.That(prefs.Get("d", 0.0), Is.EqualTo(3.14159));
Assert.That(prefs.Get("b", false), Is.True);
Assert.That(prefs.Get("t", DateTime.MinValue), Is.EqualTo(dt));
Assert.That(prefs.Get("s", string.Empty), Is.EqualTo("hello"));
}

[Test]
public void RemoveAndClear_Works()
{
string file = NewPrefsFile();
if (File.Exists(file)) File.Delete(file);
var prefs = new DefaultPreferences(file);

prefs.Set("x", 1);
Assert.That(prefs.ContainsKey("x"), Is.True);
prefs.Remove("x");
Assert.That(prefs.ContainsKey("x"), Is.False);
Assert.That(prefs.Get("x", -1), Is.EqualTo(-1));

prefs.Set("a", 10);
prefs.Set("b", 20);
prefs.Clear();
Assert.That(prefs.ContainsKey("a"), Is.False);
Assert.That(prefs.ContainsKey("b"), Is.False);
}

[Test]
public void Persistence_LoadsSavedValues()
{
string file = NewPrefsFile();
if (File.Exists(file)) File.Delete(file);
var prefs = new DefaultPreferences(file);

prefs.Set("i", 7);
prefs.Set("s", "foo");

var prefs2 = new DefaultPreferences(file);
Assert.That(prefs2.Get("i", 0), Is.EqualTo(7));
Assert.That(prefs2.Get("s", string.Empty), Is.EqualTo("foo"));
}

[Test]
public void Load_InvalidJson_UsesEmpty()
{
string file = NewPrefsFile();
Directory.CreateDirectory(Path.GetDirectoryName(file)!);
File.WriteAllText(file, "{invalid json}");

var prefs = new DefaultPreferences(file);
Assert.That(prefs.ContainsKey("anything"), Is.False);
Assert.That(prefs.Get("x", 123), Is.EqualTo(123));
}

[Test]
public void UnsupportedType_Throws()
{
string file = NewPrefsFile();
if (File.Exists(file)) File.Delete(file);
var prefs = new DefaultPreferences(file);

Assert.Throws<NotSupportedException>(() => prefs.Set<Guid>("g", Guid.NewGuid()));
Assert.Throws<NotSupportedException>(() => prefs.Get<Guid>("g", Guid.Empty));
}
}

35 changes: 35 additions & 0 deletions tests/Beutl.UnitTests/Configuration/EditorConfigTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System.Linq;
using System.Text.Json.Nodes;
using Beutl.Configuration;
using Beutl.Serialization;

namespace Beutl.UnitTests.Configuration;

public class EditorConfigTests
{
[Test]
public void SerializeDeserialize_LibraryTabDisplayModes()
{
var cfg = new EditorConfig();
cfg.LibraryTabDisplayModes.Clear();
cfg.LibraryTabDisplayModes["Custom1"] = LibraryTabDisplayMode.Hide;
cfg.LibraryTabDisplayModes["Custom2"] = LibraryTabDisplayMode.Show;

var json = new JsonObject();
var ctx = new JsonSerializationContext(typeof(EditorConfig), NullSerializationErrorNotifier.Instance, json: json);
using (ThreadLocalSerializationContext.Enter(ctx))
{
cfg.Serialize(ctx);
}

// Clear and restore into a new instance
var cfg2 = new EditorConfig();
var ctx2 = new JsonSerializationContext(typeof(EditorConfig), NullSerializationErrorNotifier.Instance, json: json);
using (ThreadLocalSerializationContext.Enter(ctx2))
{
cfg2.Deserialize(ctx2);
}

Assert.That(cfg2.LibraryTabDisplayModes.ToArray(), Is.EquivalentTo(cfg.LibraryTabDisplayModes.ToArray()));
}
}
39 changes: 39 additions & 0 deletions tests/Beutl.UnitTests/Configuration/ExtensionConfigTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System.Linq;
using System.Text.Json.Nodes;
using Beutl.Configuration;
using Beutl.Serialization;

namespace Beutl.UnitTests.Configuration;

public class ExtensionConfigTests
{
[Test]
public void SerializeDeserialize_RoundTrip()
{
var cfg = new ExtensionConfig();
cfg.EditorExtensions[".txt"] = new Beutl.Collections.CoreList<ExtensionConfig.TypeLazy>(
new[] { new ExtensionConfig.TypeLazy("[System]System:Int32"), new ExtensionConfig.TypeLazy("[System]System:String") });
cfg.DecoderPriority.Add(new ExtensionConfig.TypeLazy("[System]System:Double"));

var json = new JsonObject();
var ctx = new JsonSerializationContext(typeof(ExtensionConfig), NullSerializationErrorNotifier.Instance, json: json);
using (ThreadLocalSerializationContext.Enter(ctx))
{
cfg.Serialize(ctx);
}

var cfg2 = new ExtensionConfig();
var ctx2 = new JsonSerializationContext(typeof(ExtensionConfig), NullSerializationErrorNotifier.Instance, json: json);
using (ThreadLocalSerializationContext.Enter(ctx2))
{
cfg2.Deserialize(ctx2);
}

Assert.That(cfg2.EditorExtensions.ContainsKey(".txt"), Is.True);
Assert.That(cfg2.EditorExtensions[".txt"].Select(x => x.FormattedTypeName).ToArray(),
Is.EqualTo(cfg.EditorExtensions[".txt"].Select(x => x.FormattedTypeName).ToArray()));

Assert.That(cfg2.DecoderPriority.Select(x => x.FormattedTypeName).ToArray(),
Is.EqualTo(cfg.DecoderPriority.Select(x => x.FormattedTypeName).ToArray()));
}
}
34 changes: 34 additions & 0 deletions tests/Beutl.UnitTests/Configuration/FontConfigTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System.IO;
using System.Linq;
using System.Text.Json.Nodes;
using Beutl.Configuration;
using Beutl.Serialization;

namespace Beutl.UnitTests.Configuration;

public class FontConfigTests
{
[Test]
public void Deserialize_SynchronizesFontDirectories()
{
var cfg = new FontConfig();
string[] newDirs =
{
Path.Combine(ArtifactProvider.GetArtifactDirectory(), "Fonts1"),
Path.Combine(ArtifactProvider.GetArtifactDirectory(), "Fonts2"),
};

var json = new JsonObject
{
["FontDirectories"] = new JsonArray(newDirs.Select(d => (JsonNode)d).ToArray())
};

var ctx = new JsonSerializationContext(typeof(FontConfig), NullSerializationErrorNotifier.Instance, json: json);
using (ThreadLocalSerializationContext.Enter(ctx))
{
cfg.Deserialize(ctx);
}

Assert.That(cfg.FontDirectories.ToArray(), Is.EqualTo(newDirs));
}
}
81 changes: 81 additions & 0 deletions tests/Beutl.UnitTests/Configuration/GlobalConfigurationTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
using System;
using System.IO;
using System.Text.Json.Nodes;
using Beutl.Configuration;

namespace Beutl.UnitTests.Configuration;

public class GlobalConfigurationTests
{
private static string NewSettingsFile()
{
return Path.Combine(ArtifactProvider.GetArtifactDirectory(), "settings.json");
}

private static bool? ReadBackupSetting(string file)
{
if (JsonHelper.JsonRestore(file) is JsonObject obj && obj["Backup"] is JsonObject backup)
{
return backup.TryGetPropertyValueAsJsonValue("BackupSettings", out bool value) ? value : null;
}
return null;
}

private static bool? ReadEditorSwapTimeline(string file)
{
if (JsonHelper.JsonRestore(file) is JsonObject obj && obj["Editor"] is JsonObject editor)
{
return editor.TryGetPropertyValueAsJsonValue("SwapTimelineScrollDirection", out bool value) ? value : null;
}
return null;
}

[Test]
public void Save_WritesExpectedEditorSection()
{
var gc = GlobalConfiguration.Instance;
string file = NewSettingsFile();

bool original = gc.BackupConfig.BackupSettings;
bool originalEditor = gc.EditorConfig.SwapTimelineScrollDirection;
try
{
gc.EditorConfig.SwapTimelineScrollDirection = true;
gc.Save(file);

bool? written = ReadEditorSwapTimeline(file);
Assert.That(written, Is.True);
}
finally
{
gc.BackupConfig.BackupSettings = original;
gc.EditorConfig.SwapTimelineScrollDirection = originalEditor;
}
}

[Test]
public void AutoSave_OnSubConfigChange_WritesFile()
{
var gc = GlobalConfiguration.Instance;
string file = Path.Combine(ArtifactProvider.GetArtifactDirectory(), "autosave.settings.json");

bool original = gc.EditorConfig.SwapTimelineScrollDirection;
try
{
gc.Save(file);
bool? before = ReadEditorSwapTimeline(file);
bool newVal = !gc.EditorConfig.SwapTimelineScrollDirection;
gc.EditorConfig.SwapTimelineScrollDirection = newVal;

bool? after = ReadEditorSwapTimeline(file);
Assert.That(after, Is.EqualTo(newVal));

// Sanity: before may be null if first write, but after should be defined
Assert.That(after.HasValue, Is.True);
}
finally
{
gc.EditorConfig.SwapTimelineScrollDirection = original;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using Beutl.Configuration;

namespace Beutl.UnitTests.Configuration;

public class TelemetryAndBackupConfigTests
{
[Test]
public void Telemetry_PropertyChange_RaisesConfigurationChanged()
{
var cfg = new TelemetryConfig();
int changed = 0;
cfg.ConfigurationChanged += (_, _) => changed++;

cfg.Beutl_Logging = true;
Assert.That(changed, Is.GreaterThanOrEqualTo(1));
}

[Test]
public void Backup_PropertyChange_RaisesConfigurationChanged()
{
var cfg = new BackupConfig();
int changed = 0;
cfg.ConfigurationChanged += (_, _) => changed++;

cfg.BackupSettings = !cfg.BackupSettings;
Assert.That(changed, Is.GreaterThanOrEqualTo(1));
}
}

Loading
Loading