Files
Artemis/src/Artemis.UI.Shared.Tests/ProfileEditor/Commands/KeyframeCommandTests.cs
T
RobertBeekman 295038f338 Profile editor - Fix undo and editor service bugs found by the tests (#913)
* Profile editor - Fix undoing profile editor commands

- Undoing a change to an existing keyframe's value restores the value instead of deleting the keyframe
- Undoing "remove effect" puts the effect back in its place instead of below the next one
- Undoing "toggle keyframes" restores what it was before
- Undoing a brush change on a layer that had no brush removes the brush again, using the new Layer.RemoveLayerBrush
- Undoing "add element" disables the element again
- Undoing an event path change back to no path goes back to an event start node, like a new condition
- MoveProfileElement says "Move folder" for folders
- UpdateLayerProperty without a time no longer calls itself "Add ... keyframe"

* Profile editor - Fix auto-saving, failing commands, suspended editing and smaller editor issues

- A failed auto-save no longer stops auto-saving for the rest of the session
- A failing command is no longer also reported to ReactiveUI as unhandled, on top of the error dialog
- Resuming editing applies the editor's time again right away
- Nothing is focused while editing is suspended
- A layer created on another layer gets a name that isn't in use next to it
- CanUndo and CanRedo start out false, so new subscribers know the state right away
- An empty command scope doesn't add an undo step
- New subscribers to History get the current history once
- Expanding the keyframe selection with an already selected keyframe doesn't add it twice
- Snapping can snap to a snap time of zero
2026-10-24 16:17:05 +02:00

234 lines
7.8 KiB
C#

using Artemis.UI.Shared.Services.ProfileEditor.Commands;
using Artemis.UI.Shared.Tests.Infrastructure;
using RGB.NET.Core;
namespace Artemis.UI.Shared.Tests.ProfileEditor.Commands;
/// <summary>
/// Deleting, duplicating, moving and easing keyframes, and turning keyframes on and off
/// </summary>
public class KeyframeCommandTests : ProfileEditorTestBase
{
private readonly FloatLayerProperty _opacity;
private readonly LayerPropertyKeyframe<float> _first;
private readonly LayerPropertyKeyframe<float> _second;
public KeyframeCommandTests()
{
Layer layer = AddLayer("Layer", null, LedId.Keyboard_A);
_opacity = layer.Transform.Opacity;
_opacity.KeyframesEnabled = true;
_first = new LayerPropertyKeyframe<float>(20, TimeSpan.FromSeconds(1), Easings.Functions.Linear, _opacity);
_second = new LayerPropertyKeyframe<float>(80, TimeSpan.FromSeconds(2), Easings.Functions.Linear, _opacity);
_opacity.AddKeyframe(_first);
_opacity.AddKeyframe(_second);
}
[Fact]
public void DeleteKeyframe_RemovesTheKeyframe()
{
DeleteKeyframe command = new(_first);
command.Execute();
command.DisplayName.ShouldBe("Delete keyframe");
_opacity.Keyframes.ShouldBe([_second]);
}
[Fact]
public void DeleteKeyframe_Undo_PutsTheSameKeyframeBack()
{
string before = Snapshot();
DeleteKeyframe command = new(_first);
command.Execute();
command.Undo();
_opacity.Keyframes.ShouldBe([_first, _second]);
Snapshot().ShouldBe(before);
}
[Fact]
public void DeleteKeyframe_Undo_OfTheLastKeyframe_TurnsKeyframesBackOn()
{
DeleteKeyframe deleteFirst = new(_first);
DeleteKeyframe deleteSecond = new(_second);
deleteFirst.Execute();
deleteSecond.Execute();
_opacity.KeyframesEnabled = false;
deleteSecond.Undo();
// Adding a keyframe always turns keyframes on
_opacity.KeyframesEnabled.ShouldBeTrue();
}
[Fact]
public void DuplicateKeyframe_AddsACopyAtThePosition()
{
_first.EasingFunction = Easings.Functions.CubicEaseIn;
DuplicateKeyframe command = new(_first, TimeSpan.FromSeconds(3));
command.Execute();
command.DisplayName.ShouldBe("Duplicate keyframe");
LayerPropertyKeyframe<float> copy = command.Duplication.ShouldBeOfType<LayerPropertyKeyframe<float>>();
_opacity.Keyframes.ShouldBe([_first, _second, copy]);
copy.Value.ShouldBe(20);
copy.Position.ShouldBe(TimeSpan.FromSeconds(3));
copy.EasingFunction.ShouldBe(Easings.Functions.CubicEaseIn);
}
[Fact]
public void DuplicateKeyframe_UndoAndRedo_ReuseTheSameCopy()
{
string before = Snapshot();
DuplicateKeyframe command = new(_first, TimeSpan.FromSeconds(3));
command.Execute();
ILayerPropertyKeyframe copy = command.Duplication!;
command.Undo();
Snapshot().ShouldBe(before);
command.Execute();
command.Duplication.ShouldBe(copy);
_opacity.Keyframes.ShouldContain((LayerPropertyKeyframe<float>) copy);
}
[Fact]
public void DuplicateKeyframe_Undo_BeforeExecute_DoesNothing()
{
new DuplicateKeyframe(_first, TimeSpan.FromSeconds(3)).Undo();
_opacity.Keyframes.ShouldBe([_first, _second]);
}
[Fact]
public void MoveKeyframe_MovesAndUndoRestoresThePosition()
{
string before = Snapshot();
MoveKeyframe command = new(_first, TimeSpan.FromSeconds(3));
command.Execute();
command.DisplayName.ShouldBe("Move keyframe");
_first.Position.ShouldBe(TimeSpan.FromSeconds(3));
// Keyframes stay sorted by position
_opacity.Keyframes.ShouldBe([_second, _first]);
command.Undo();
_first.Position.ShouldBe(TimeSpan.FromSeconds(1));
_opacity.Keyframes.ShouldBe([_first, _second]);
Snapshot().ShouldBe(before);
}
[Fact]
public void MoveKeyframe_WithAnOriginalPosition_UndoesToThatPosition()
{
// The timeline moves keyframes while dragging and passes where the drag started
_first.Position = TimeSpan.FromSeconds(1.5);
MoveKeyframe command = new(_first, TimeSpan.FromSeconds(1.75), TimeSpan.FromSeconds(1));
command.Execute();
_first.Position.ShouldBe(TimeSpan.FromSeconds(1.75));
command.Undo();
_first.Position.ShouldBe(TimeSpan.FromSeconds(1));
}
[Fact]
public void ChangeKeyframeEasing_ChangesAndUndoRestoresTheEasing()
{
string before = Snapshot();
ChangeKeyframeEasing command = new(_first, Easings.Functions.CubicEaseInOut);
command.Execute();
command.DisplayName.ShouldBe("Change easing to cubic ease in out");
_first.EasingFunction.ShouldBe(Easings.Functions.CubicEaseInOut);
command.Undo();
_first.EasingFunction.ShouldBe(Easings.Functions.Linear);
Snapshot().ShouldBe(before);
}
[Fact]
public void ToggleLayerPropertyKeyframes_Enable_AddsAKeyframeWithTheCurrentValue()
{
FloatLayerProperty rotation = ((Layer) _opacity.ProfileElement).Transform.Rotation;
rotation.SetCurrentValue(45);
ToggleLayerPropertyKeyframes<float> command = new(rotation, true, TimeSpan.FromSeconds(2));
command.Execute();
command.DisplayName.ShouldBe("Enable keyframes");
rotation.KeyframesEnabled.ShouldBeTrue();
LayerPropertyKeyframe<float> keyframe = rotation.Keyframes.ShouldHaveSingleItem();
keyframe.Value.ShouldBe(45);
keyframe.Position.ShouldBe(TimeSpan.FromSeconds(2));
keyframe.EasingFunction.ShouldBe(Easings.Functions.Linear);
}
[Fact]
public void ToggleLayerPropertyKeyframes_Enable_UndoAndRedo()
{
FloatLayerProperty rotation = ((Layer) _opacity.ProfileElement).Transform.Rotation;
string before = Snapshot();
ToggleLayerPropertyKeyframes<float> command = new(rotation, true, TimeSpan.FromSeconds(2));
command.Execute();
LayerPropertyKeyframe<float> keyframe = rotation.Keyframes.Single();
command.Undo();
rotation.KeyframesEnabled.ShouldBeFalse();
rotation.Keyframes.ShouldBeEmpty();
Snapshot().ShouldBe(before);
command.Execute();
rotation.Keyframes.ShouldBe([keyframe]);
}
[Fact]
public void ToggleLayerPropertyKeyframes_Enable_WithKeyframesAlreadyThere_AddsNone()
{
_opacity.KeyframesEnabled = false;
string before = Snapshot();
ToggleLayerPropertyKeyframes<float> command = new(_opacity, true, TimeSpan.FromSeconds(3));
command.Execute();
_opacity.KeyframesEnabled.ShouldBeTrue();
_opacity.Keyframes.ShouldBe([_first, _second]);
command.Undo();
_opacity.KeyframesEnabled.ShouldBeFalse();
Snapshot().ShouldBe(before);
}
[Fact]
public void ToggleLayerPropertyKeyframes_Disable_UndoTurnsThemBackOn()
{
string before = Snapshot();
ToggleLayerPropertyKeyframes<float> command = new(_opacity, false, TimeSpan.Zero);
command.Execute();
command.DisplayName.ShouldBe("Disable keyframes");
_opacity.KeyframesEnabled.ShouldBeFalse();
// The keyframes themselves are kept
_opacity.Keyframes.ShouldBe([_first, _second]);
command.Undo();
_opacity.KeyframesEnabled.ShouldBeTrue();
Snapshot().ShouldBe(before);
}
[Fact]
public void ToggleLayerPropertyKeyframes_Undo_RestoresWhatItWasBefore()
{
ToggleLayerPropertyKeyframes<float> enable = new(_opacity, true, TimeSpan.Zero);
enable.Execute();
enable.Undo();
// "Enabling" keyframes that were already enabled leaves them enabled when undone
_opacity.KeyframesEnabled.ShouldBeTrue();
_opacity.Keyframes.ShouldBe([_first, _second]);
}
}