2020-04-13 11:34:18 +00:00
|
|
|
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
|
|
|
|
// See the LICENCE file in the repository root for full licence text.
|
|
|
|
|
|
|
|
using NUnit.Framework;
|
2022-01-12 14:17:35 +00:00
|
|
|
using osu.Game.Rulesets.Osu;
|
|
|
|
using osu.Game.Rulesets.Osu.Beatmaps;
|
2020-09-28 06:30:18 +00:00
|
|
|
using osu.Game.Rulesets.Osu.Objects;
|
2020-04-13 11:34:18 +00:00
|
|
|
using osu.Game.Screens.Edit;
|
|
|
|
|
2020-04-23 08:07:55 +00:00
|
|
|
namespace osu.Game.Tests.Editing
|
2020-04-13 11:34:18 +00:00
|
|
|
{
|
|
|
|
[TestFixture]
|
|
|
|
public class EditorChangeHandlerTest
|
|
|
|
{
|
2020-10-02 06:44:32 +00:00
|
|
|
private int stateChangedFired;
|
|
|
|
|
|
|
|
[SetUp]
|
|
|
|
public void SetUp()
|
|
|
|
{
|
|
|
|
stateChangedFired = 0;
|
|
|
|
}
|
|
|
|
|
2020-04-13 11:34:18 +00:00
|
|
|
[Test]
|
|
|
|
public void TestSaveRestoreState()
|
|
|
|
{
|
2020-09-28 06:30:18 +00:00
|
|
|
var (handler, beatmap) = createChangeHandler();
|
2020-04-13 11:34:18 +00:00
|
|
|
|
2020-04-15 06:50:43 +00:00
|
|
|
Assert.That(handler.CanUndo.Value, Is.False);
|
|
|
|
Assert.That(handler.CanRedo.Value, Is.False);
|
2020-04-13 11:34:18 +00:00
|
|
|
|
2020-09-28 06:30:18 +00:00
|
|
|
addArbitraryChange(beatmap);
|
2020-04-13 11:34:18 +00:00
|
|
|
handler.SaveState();
|
|
|
|
|
2020-10-02 06:44:32 +00:00
|
|
|
Assert.That(stateChangedFired, Is.EqualTo(1));
|
|
|
|
|
2020-04-15 06:50:43 +00:00
|
|
|
Assert.That(handler.CanUndo.Value, Is.True);
|
|
|
|
Assert.That(handler.CanRedo.Value, Is.False);
|
2020-04-13 11:34:18 +00:00
|
|
|
|
|
|
|
handler.RestoreState(-1);
|
|
|
|
|
2020-04-15 06:50:43 +00:00
|
|
|
Assert.That(handler.CanUndo.Value, Is.False);
|
|
|
|
Assert.That(handler.CanRedo.Value, Is.True);
|
2020-10-02 06:44:32 +00:00
|
|
|
|
|
|
|
Assert.That(stateChangedFired, Is.EqualTo(2));
|
2020-04-13 11:34:18 +00:00
|
|
|
}
|
|
|
|
|
2020-11-27 07:56:26 +00:00
|
|
|
[Test]
|
|
|
|
public void TestApplyThenUndoThenApplySameChange()
|
|
|
|
{
|
|
|
|
var (handler, beatmap) = createChangeHandler();
|
|
|
|
|
|
|
|
Assert.That(handler.CanUndo.Value, Is.False);
|
|
|
|
Assert.That(handler.CanRedo.Value, Is.False);
|
|
|
|
|
|
|
|
string originalHash = handler.CurrentStateHash;
|
|
|
|
|
|
|
|
addArbitraryChange(beatmap);
|
|
|
|
handler.SaveState();
|
|
|
|
|
|
|
|
Assert.That(handler.CanUndo.Value, Is.True);
|
|
|
|
Assert.That(handler.CanRedo.Value, Is.False);
|
|
|
|
Assert.That(stateChangedFired, Is.EqualTo(1));
|
|
|
|
|
|
|
|
string hash = handler.CurrentStateHash;
|
|
|
|
|
2020-11-30 04:33:29 +00:00
|
|
|
// undo a change without saving
|
2020-11-27 07:56:26 +00:00
|
|
|
handler.RestoreState(-1);
|
|
|
|
|
|
|
|
Assert.That(originalHash, Is.EqualTo(handler.CurrentStateHash));
|
|
|
|
Assert.That(stateChangedFired, Is.EqualTo(2));
|
|
|
|
|
|
|
|
addArbitraryChange(beatmap);
|
|
|
|
handler.SaveState();
|
|
|
|
Assert.That(hash, Is.EqualTo(handler.CurrentStateHash));
|
|
|
|
}
|
|
|
|
|
2020-09-28 06:30:18 +00:00
|
|
|
[Test]
|
|
|
|
public void TestSaveSameStateDoesNotSave()
|
|
|
|
{
|
|
|
|
var (handler, beatmap) = createChangeHandler();
|
|
|
|
|
|
|
|
Assert.That(handler.CanUndo.Value, Is.False);
|
|
|
|
Assert.That(handler.CanRedo.Value, Is.False);
|
|
|
|
|
|
|
|
addArbitraryChange(beatmap);
|
|
|
|
handler.SaveState();
|
|
|
|
|
|
|
|
Assert.That(handler.CanUndo.Value, Is.True);
|
|
|
|
Assert.That(handler.CanRedo.Value, Is.False);
|
2020-10-02 06:44:32 +00:00
|
|
|
Assert.That(stateChangedFired, Is.EqualTo(1));
|
2020-09-28 06:30:18 +00:00
|
|
|
|
|
|
|
string hash = handler.CurrentStateHash;
|
|
|
|
|
|
|
|
// save a save without making any changes
|
|
|
|
handler.SaveState();
|
|
|
|
|
|
|
|
Assert.That(hash, Is.EqualTo(handler.CurrentStateHash));
|
2020-10-02 06:44:32 +00:00
|
|
|
Assert.That(stateChangedFired, Is.EqualTo(1));
|
2020-09-28 06:30:18 +00:00
|
|
|
|
|
|
|
handler.RestoreState(-1);
|
|
|
|
|
|
|
|
Assert.That(hash, Is.Not.EqualTo(handler.CurrentStateHash));
|
|
|
|
|
|
|
|
// we should only be able to restore once even though we saved twice.
|
|
|
|
Assert.That(handler.CanUndo.Value, Is.False);
|
|
|
|
Assert.That(handler.CanRedo.Value, Is.True);
|
2020-10-02 06:44:32 +00:00
|
|
|
Assert.That(stateChangedFired, Is.EqualTo(2));
|
2020-09-28 06:30:18 +00:00
|
|
|
}
|
|
|
|
|
2020-04-13 11:34:18 +00:00
|
|
|
[Test]
|
|
|
|
public void TestMaxStatesSaved()
|
|
|
|
{
|
2020-09-28 06:30:18 +00:00
|
|
|
var (handler, beatmap) = createChangeHandler();
|
2020-04-13 11:34:18 +00:00
|
|
|
|
2020-04-15 06:50:43 +00:00
|
|
|
Assert.That(handler.CanUndo.Value, Is.False);
|
2020-04-13 11:34:18 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < EditorChangeHandler.MAX_SAVED_STATES; i++)
|
2020-09-28 06:30:18 +00:00
|
|
|
{
|
2020-10-02 06:44:32 +00:00
|
|
|
Assert.That(stateChangedFired, Is.EqualTo(i));
|
|
|
|
|
2020-09-28 06:30:18 +00:00
|
|
|
addArbitraryChange(beatmap);
|
2020-04-13 11:34:18 +00:00
|
|
|
handler.SaveState();
|
2020-09-28 06:30:18 +00:00
|
|
|
}
|
2020-04-13 11:34:18 +00:00
|
|
|
|
2020-04-15 06:50:43 +00:00
|
|
|
Assert.That(handler.CanUndo.Value, Is.True);
|
2020-04-13 11:34:18 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < EditorChangeHandler.MAX_SAVED_STATES; i++)
|
|
|
|
{
|
2020-04-15 06:50:43 +00:00
|
|
|
Assert.That(handler.CanUndo.Value, Is.True);
|
2020-04-13 11:34:18 +00:00
|
|
|
handler.RestoreState(-1);
|
|
|
|
}
|
|
|
|
|
2020-04-15 06:50:43 +00:00
|
|
|
Assert.That(handler.CanUndo.Value, Is.False);
|
2020-04-13 11:34:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void TestMaxStatesExceeded()
|
|
|
|
{
|
2020-09-28 06:30:18 +00:00
|
|
|
var (handler, beatmap) = createChangeHandler();
|
2020-04-13 11:34:18 +00:00
|
|
|
|
2020-04-15 06:50:43 +00:00
|
|
|
Assert.That(handler.CanUndo.Value, Is.False);
|
2020-04-13 11:34:18 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < EditorChangeHandler.MAX_SAVED_STATES * 2; i++)
|
2020-09-28 06:30:18 +00:00
|
|
|
{
|
|
|
|
addArbitraryChange(beatmap);
|
2020-04-13 11:34:18 +00:00
|
|
|
handler.SaveState();
|
2020-09-28 06:30:18 +00:00
|
|
|
}
|
2020-04-13 11:34:18 +00:00
|
|
|
|
2020-04-15 06:50:43 +00:00
|
|
|
Assert.That(handler.CanUndo.Value, Is.True);
|
2020-04-13 11:34:18 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < EditorChangeHandler.MAX_SAVED_STATES; i++)
|
|
|
|
{
|
2020-04-15 06:50:43 +00:00
|
|
|
Assert.That(handler.CanUndo.Value, Is.True);
|
2020-04-13 11:34:18 +00:00
|
|
|
handler.RestoreState(-1);
|
|
|
|
}
|
|
|
|
|
2020-04-15 06:50:43 +00:00
|
|
|
Assert.That(handler.CanUndo.Value, Is.False);
|
2020-04-13 11:34:18 +00:00
|
|
|
}
|
2020-09-28 06:30:18 +00:00
|
|
|
|
|
|
|
private (EditorChangeHandler, EditorBeatmap) createChangeHandler()
|
|
|
|
{
|
2022-01-12 14:17:35 +00:00
|
|
|
var beatmap = new EditorBeatmap(new OsuBeatmap
|
|
|
|
{
|
|
|
|
BeatmapInfo =
|
|
|
|
{
|
|
|
|
Ruleset = new OsuRuleset().RulesetInfo,
|
|
|
|
},
|
|
|
|
});
|
2020-09-28 06:30:18 +00:00
|
|
|
|
2020-10-02 06:44:32 +00:00
|
|
|
var changeHandler = new EditorChangeHandler(beatmap);
|
|
|
|
|
|
|
|
changeHandler.OnStateChange += () => stateChangedFired++;
|
|
|
|
return (changeHandler, beatmap);
|
2020-09-28 06:30:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void addArbitraryChange(EditorBeatmap beatmap)
|
|
|
|
{
|
2020-11-27 07:56:26 +00:00
|
|
|
beatmap.Add(new HitCircle { StartTime = 2760 });
|
2020-09-28 06:30:18 +00:00
|
|
|
}
|
2020-04-13 11:34:18 +00:00
|
|
|
}
|
|
|
|
}
|