Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions Keybinding.Test/DefaultProfileReactivationTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// Copyright (c) 2023-2026 ktsu-dev contributors

namespace ktsu.Keybinding.Test;

using ktsu.Keybinding.Core;
using ktsu.Keybinding.Core.Models;

[TestClass]
public class DefaultProfileReactivationTests
{
private string _testDataDirectory = null!;

[TestInitialize]
public void Setup()
{
_testDataDirectory = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
Directory.CreateDirectory(_testDataDirectory);
}

[TestCleanup]
public void Cleanup()
{
if (Directory.Exists(_testDataDirectory))
{
Directory.Delete(_testDataDirectory, recursive: true);
}
}

[TestMethod]
public async Task CreateDefaultProfile_AfterActiveProfileWasDeleted_ActivatesExistingDefaultOnNextLaunch()
{
{
using KeybindingManager manager = new(_testDataDirectory);
await manager.InitializeAsync().ConfigureAwait(false);
manager.CreateDefaultProfile();
manager.Profiles.CreateProfile("vim", "Vim");
Assert.IsTrue(manager.Profiles.SetActiveProfile("vim"));
Assert.IsTrue(manager.Profiles.DeleteProfile("vim"));
await manager.SaveAsync().ConfigureAwait(false);
}

{
using KeybindingManager manager = new(_testDataDirectory);
await manager.InitializeAsync().ConfigureAwait(false);
manager.CreateDefaultProfile();
manager.RegisterCommands([new Command("file.save", "Save")]);

Assert.AreEqual("default", manager.Profiles.GetActiveProfile()?.Id);
Assert.IsTrue(manager.Keybindings.BindChord("file.save", Chord.Parse("Ctrl+S")));
Assert.AreEqual("file.save", manager.Keybindings.ExecuteChord(Chord.Parse("Ctrl+S")));
}
}

[TestMethod]
public void CreateDefaultProfile_ExistingDefaultWhileAnotherIsActive_LeavesActiveProfileAlone()
{
using KeybindingManager manager = new(_testDataDirectory);
manager.CreateDefaultProfile();
manager.Profiles.CreateProfile("vim", "Vim");
Assert.IsTrue(manager.Profiles.SetActiveProfile("vim"));

Assert.IsNull(manager.CreateDefaultProfile());
Assert.AreEqual("vim", manager.Profiles.GetActiveProfile()?.Id);
}

[TestMethod]
public void CreateDefaultProfile_ExistingDefaultWithDoNotActivate_LeavesNoActiveProfile()
{
using KeybindingManager manager = new(_testDataDirectory);
manager.CreateDefaultProfile();
manager.Profiles.ClearActiveProfile();

Assert.IsNull(manager.CreateDefaultProfile(activation: ProfileActivation.DoNotActivate));
Assert.IsNull(manager.Profiles.GetActiveProfile());
}
}
11 changes: 10 additions & 1 deletion Keybinding/KeybindingManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,14 +150,23 @@
/// </summary>
/// <param name="profileId">The ID for the default profile</param>
/// <param name="profileName">The name for the default profile</param>
/// <param name="activation">Whether to set as the active profile</param>
/// <param name="activation">
/// Whether to set as the active profile. When the profile already exists, it is activated only if no
/// profile is active, so a startup call recovers from a deleted or unset active profile without
/// overriding the one the user chose.
/// </param>
/// <returns>The created profile, or null if a profile already exists with the given ID</returns>
public Profile? CreateDefaultProfile(string profileId = "default", string profileName = "Default", ProfileActivation activation = ProfileActivation.Activate)
{
DisposalHelper.ThrowIfDisposed(_disposed, this);

if (Profiles.ProfileExists(profileId))
{
if (activation == ProfileActivation.Activate && Profiles.GetActiveProfile() is null)
{
Profiles.SetActiveProfile(profileId);
}

return null;
}

Expand Down Expand Up @@ -199,7 +208,7 @@
DisposalHelper.ThrowIfDisposed(_disposed, this);
Ensure.NotNull(chords);

Profile activeProfile = Profiles.GetActiveProfile() ?? throw new InvalidOperationException("No active profile is set");

Check warning on line 211 in Keybinding/KeybindingManager.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Remove the unused local variable 'activeProfile'.

Check warning on line 211 in Keybinding/KeybindingManager.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Remove the unused local variable 'activeProfile'.

Check warning on line 211 in Keybinding/KeybindingManager.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Remove the unused local variable 'activeProfile'.

Check warning on line 211 in Keybinding/KeybindingManager.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Remove the unused local variable 'activeProfile'.

Check warning on line 211 in Keybinding/KeybindingManager.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Remove the unused local variable 'activeProfile'.

Check warning on line 211 in Keybinding/KeybindingManager.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Remove the unused local variable 'activeProfile'.

Check warning on line 211 in Keybinding/KeybindingManager.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Remove the unused local variable 'activeProfile'.

Check warning on line 211 in Keybinding/KeybindingManager.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Remove the unused local variable 'activeProfile'.

Check warning on line 211 in Keybinding/KeybindingManager.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Remove the unused local variable 'activeProfile'.

Check warning on line 211 in Keybinding/KeybindingManager.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Remove the unused local variable 'activeProfile'.

return OperationHelper.ExecuteWithCount(chords, Keybindings.BindChord);
}
Expand Down
Loading