1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-16 09:17:48 +08:00
osu-lazer/osu.Game.Tests/Beatmaps/TestSceneEditorBeatmap.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

295 lines
10 KiB
C#
Raw Normal View History

2019-10-03 13:27:40 +08: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.
2022-06-17 15:37:17 +08:00
#nullable disable
using System;
2020-04-10 12:29:49 +08:00
using System.Collections.Generic;
2019-10-03 13:27:40 +08:00
using System.Linq;
using NUnit.Framework;
2020-04-09 20:00:23 +08:00
using osu.Framework.Testing;
2019-10-03 13:27:40 +08:00
using osu.Game.Rulesets.Objects;
2022-01-12 21:34:07 +08:00
using osu.Game.Rulesets.Osu;
2019-10-03 13:27:40 +08:00
using osu.Game.Rulesets.Osu.Beatmaps;
using osu.Game.Rulesets.Osu.Objects;
using osu.Game.Screens.Edit;
2020-04-09 20:00:23 +08:00
using osu.Game.Tests.Visual;
2019-10-03 13:27:40 +08:00
namespace osu.Game.Tests.Beatmaps
{
2020-04-09 20:00:23 +08:00
[HeadlessTest]
public partial class TestSceneEditorBeatmap : EditorClockTestScene
2019-10-03 13:27:40 +08:00
{
/// <summary>
/// Tests that the addition event is correctly invoked after a hitobject is added.
/// </summary>
[Test]
public void TestHitObjectAddEvent()
{
var hitCircle = new HitCircle();
2019-10-03 13:27:40 +08:00
HitObject addedObject = null;
EditorBeatmap editorBeatmap = null;
2019-10-03 13:27:40 +08:00
AddStep("add beatmap", () =>
{
2022-01-12 21:34:07 +08:00
Child = editorBeatmap = new EditorBeatmap(new OsuBeatmap
{
BeatmapInfo =
{
Ruleset = new OsuRuleset().RulesetInfo,
},
});
editorBeatmap.HitObjectAdded += h => addedObject = h;
});
2019-10-03 13:27:40 +08:00
AddStep("add hitobject", () => editorBeatmap.Add(hitCircle));
AddAssert("received add event", () => addedObject == hitCircle);
2019-10-03 13:27:40 +08:00
}
/// <summary>
/// Tests that the removal event is correctly invoked after a hitobject is removed.
/// </summary>
[Test]
public void HitObjectRemoveEvent()
{
var hitCircle = new HitCircle();
HitObject removedObject = null;
EditorBeatmap editorBeatmap = null;
AddStep("add beatmap", () =>
{
2022-01-12 21:34:07 +08:00
Child = editorBeatmap = new EditorBeatmap(new OsuBeatmap
{
BeatmapInfo =
{
Ruleset = new OsuRuleset().RulesetInfo,
},
HitObjects = { hitCircle }
});
editorBeatmap.HitObjectRemoved += h => removedObject = h;
});
AddStep("remove hitobject", () => editorBeatmap.Remove(editorBeatmap.HitObjects.First()));
AddAssert("received remove event", () => removedObject == hitCircle);
2019-10-03 13:27:40 +08:00
}
/// <summary>
/// Tests that the changed event is correctly invoked after the start time of a hitobject is changed.
/// This tests for hitobjects which were already present before the editor beatmap was constructed.
/// </summary>
[Test]
public void TestInitialHitObjectStartTimeChangeEvent()
{
var hitCircle = new HitCircle();
2020-09-14 16:52:59 +08:00
2019-10-03 13:27:40 +08:00
HitObject changedObject = null;
2020-09-14 16:52:59 +08:00
2020-04-09 20:00:23 +08:00
AddStep("add beatmap", () =>
{
EditorBeatmap editorBeatmap;
2022-01-12 21:34:07 +08:00
Child = editorBeatmap = new EditorBeatmap(new OsuBeatmap
{
BeatmapInfo =
{
Ruleset = new OsuRuleset().RulesetInfo,
},
HitObjects = { hitCircle }
});
2020-04-09 20:00:23 +08:00
editorBeatmap.HitObjectUpdated += h => changedObject = h;
});
2020-09-14 16:52:59 +08:00
2020-04-09 20:00:23 +08:00
AddStep("change start time", () => hitCircle.StartTime = 1000);
AddAssert("received change event", () => changedObject == hitCircle);
2019-10-03 13:27:40 +08:00
}
/// <summary>
/// Tests that the changed event is correctly invoked after the start time of a hitobject is changed.
/// This tests for hitobjects which were added to an existing editor beatmap.
/// </summary>
[Test]
public void TestAddedHitObjectStartTimeChangeEvent()
{
2020-04-09 20:00:23 +08:00
EditorBeatmap editorBeatmap = null;
2019-10-03 13:27:40 +08:00
HitObject changedObject = null;
2020-09-14 16:52:59 +08:00
2020-04-09 20:00:23 +08:00
AddStep("add beatmap", () =>
{
2022-01-12 21:34:07 +08:00
Child = editorBeatmap = new EditorBeatmap(new OsuBeatmap
{
BeatmapInfo =
{
Ruleset = new OsuRuleset().RulesetInfo,
},
});
2020-04-09 20:00:23 +08:00
editorBeatmap.HitObjectUpdated += h => changedObject = h;
});
2020-09-14 16:52:59 +08:00
2019-10-03 13:27:40 +08:00
var hitCircle = new HitCircle();
2020-09-14 16:52:59 +08:00
2020-04-09 20:00:23 +08:00
AddStep("add object", () => editorBeatmap.Add(hitCircle));
AddAssert("event not received", () => changedObject == null);
2020-09-14 16:52:59 +08:00
2020-04-09 20:00:23 +08:00
AddStep("change start time", () => hitCircle.StartTime = 1000);
AddAssert("event received", () => changedObject == hitCircle);
2019-10-03 13:27:40 +08:00
}
/// <summary>
/// Tests that the channged event is not invoked after a hitobject is removed from the beatmap/
/// </summary>
[Test]
public void TestRemovedHitObjectStartTimeChangeEvent()
{
var hitCircle = new HitCircle();
2022-01-12 21:34:07 +08:00
var editorBeatmap = new EditorBeatmap(new OsuBeatmap
{
BeatmapInfo =
{
Ruleset = new OsuRuleset().RulesetInfo,
},
HitObjects = { hitCircle }
});
2020-09-14 16:52:59 +08:00
2019-10-03 13:27:40 +08:00
HitObject changedObject = null;
2020-04-09 18:54:58 +08:00
editorBeatmap.HitObjectUpdated += h => changedObject = h;
2020-09-14 16:52:59 +08:00
2019-10-03 13:27:40 +08:00
editorBeatmap.Remove(hitCircle);
Assert.That(changedObject, Is.Null);
2020-09-14 16:52:59 +08:00
2019-10-03 13:27:40 +08:00
hitCircle.StartTime = 1000;
Assert.That(changedObject, Is.Null);
}
/// <summary>
/// Tests that an added hitobject is correctly inserted to preserve the sorting order of the beatmap.
/// </summary>
[Test]
public void TestAddHitObjectInMiddle()
{
2019-12-27 18:39:30 +08:00
var editorBeatmap = new EditorBeatmap(new OsuBeatmap
2019-10-03 13:27:40 +08:00
{
2022-01-12 21:34:07 +08:00
BeatmapInfo =
{
Ruleset = new OsuRuleset().RulesetInfo,
},
2019-10-03 13:27:40 +08:00
HitObjects =
{
new HitCircle(),
new HitCircle { StartTime = 1000 },
new HitCircle { StartTime = 1000 },
new HitCircle { StartTime = 2000 },
}
});
var hitCircle = new HitCircle { StartTime = 1000 };
editorBeatmap.Add(hitCircle);
Assert.That(editorBeatmap.HitObjects.Count(h => h == hitCircle), Is.EqualTo(1));
Assert.That(Array.IndexOf(editorBeatmap.HitObjects.ToArray(), hitCircle), Is.EqualTo(3));
2019-10-03 13:27:40 +08:00
}
/// <summary>
/// Tests that the beatmap remains correctly sorted after the start time of a hitobject is changed.
/// </summary>
[Test]
public void TestResortWhenStartTimeChanged()
{
var hitCircle = new HitCircle { StartTime = 1000 };
2019-12-27 18:39:30 +08:00
var editorBeatmap = new EditorBeatmap(new OsuBeatmap
2019-10-03 13:27:40 +08:00
{
2022-01-12 21:34:07 +08:00
BeatmapInfo =
{
Ruleset = new OsuRuleset().RulesetInfo,
},
2019-10-03 13:27:40 +08:00
HitObjects =
{
new HitCircle(),
new HitCircle { StartTime = 1000 },
new HitCircle { StartTime = 1000 },
hitCircle,
new HitCircle { StartTime = 2000 },
}
});
hitCircle.StartTime = 0;
Assert.That(editorBeatmap.HitObjects.Count(h => h == hitCircle), Is.EqualTo(1));
Assert.That(Array.IndexOf(editorBeatmap.HitObjects.ToArray(), hitCircle), Is.EqualTo(1));
2019-10-03 13:27:40 +08:00
}
2020-04-10 12:29:49 +08:00
/// <summary>
/// Tests that multiple hitobjects are updated simultaneously.
/// </summary>
[Test]
public void TestMultipleHitObjectUpdate()
{
var updatedObjects = new List<HitObject>();
var allHitObjects = new List<HitObject>();
EditorBeatmap editorBeatmap = null;
2020-09-14 16:52:59 +08:00
2020-04-10 12:29:49 +08:00
AddStep("add beatmap", () =>
{
updatedObjects.Clear();
2022-01-12 21:34:07 +08:00
Child = editorBeatmap = new EditorBeatmap(new OsuBeatmap
{
BeatmapInfo =
{
Ruleset = new OsuRuleset().RulesetInfo,
},
});
2020-04-10 12:29:49 +08:00
for (int i = 0; i < 10; i++)
{
var h = new HitCircle();
editorBeatmap.Add(h);
allHitObjects.Add(h);
}
});
2020-09-14 16:52:59 +08:00
2020-04-10 12:29:49 +08:00
AddStep("change all start times", () =>
{
editorBeatmap.HitObjectUpdated += h => updatedObjects.Add(h);
2020-09-14 16:52:59 +08:00
2020-04-10 12:29:49 +08:00
for (int i = 0; i < 10; i++)
allHitObjects[i].StartTime += 10;
});
// Distinct ensures that all hitobjects have been updated once, debounce is tested below.
AddAssert("all hitobjects updated", () => updatedObjects.Distinct().Count() == 10);
}
/// <summary>
/// Tests that hitobject updates are debounced when they happen too soon.
/// </summary>
[Test]
public void TestDebouncedUpdate()
{
var updatedObjects = new List<HitObject>();
EditorBeatmap editorBeatmap = null;
2020-09-14 16:52:59 +08:00
2020-04-10 12:29:49 +08:00
AddStep("add beatmap", () =>
{
updatedObjects.Clear();
2022-01-12 21:34:07 +08:00
Child = editorBeatmap = new EditorBeatmap(new OsuBeatmap
{
BeatmapInfo =
{
Ruleset = new OsuRuleset().RulesetInfo,
},
});
2020-04-10 12:29:49 +08:00
editorBeatmap.Add(new HitCircle());
});
2020-09-14 16:52:59 +08:00
2020-04-10 12:29:49 +08:00
AddStep("change start time twice", () =>
{
editorBeatmap.HitObjectUpdated += h => updatedObjects.Add(h);
2020-09-14 16:52:59 +08:00
2020-04-10 12:29:49 +08:00
editorBeatmap.HitObjects[0].StartTime = 10;
editorBeatmap.HitObjects[0].StartTime = 20;
});
2020-09-14 16:52:59 +08:00
2020-04-10 12:29:49 +08:00
AddAssert("only updated once", () => updatedObjects.Count == 1);
}
2019-10-03 13:27:40 +08:00
}
}