osu/osu.Game/Tests/Visual/ModSandboxTestScene.cs

110 lines
3.3 KiB
C#
Raw Normal View History

2020-03-01 05:16:28 +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 System;
using System.Collections.Generic;
using System.Linq;
2020-03-02 01:50:41 +00:00
using JetBrains.Annotations;
using osu.Game.Beatmaps;
2020-03-02 01:50:41 +00:00
using osu.Game.Replays;
2020-03-01 05:16:28 +00:00
using osu.Game.Rulesets;
using osu.Game.Rulesets.Mods;
2020-03-02 01:50:41 +00:00
using osu.Game.Scoring;
2020-03-01 05:16:28 +00:00
using osu.Game.Screens.Play;
namespace osu.Game.Tests.Visual
{
2020-03-02 01:06:49 +00:00
public abstract class ModSandboxTestScene : PlayerTestScene
2020-03-01 05:16:28 +00:00
{
public override IReadOnlyList<Type> RequiredTypes => new[]
{
2020-03-02 01:06:49 +00:00
typeof(ModSandboxTestScene)
2020-03-01 05:16:28 +00:00
};
protected ModSandboxTestScene(Ruleset ruleset)
2020-03-01 05:16:28 +00:00
: base(ruleset)
{
}
private ModTestCaseData currentTest;
2020-03-01 05:16:28 +00:00
public override void SetUpSteps()
{
foreach (var testCase in CreateTestCases())
{
2020-03-02 01:50:41 +00:00
AddStep(testCase.Name, () => currentTest = testCase);
base.SetUpSteps();
2020-03-02 01:50:41 +00:00
AddUntilStep("test passed", () => testCase.PassCondition?.Invoke() ?? true);
}
2020-03-01 05:16:28 +00:00
}
2020-03-02 01:50:41 +00:00
protected sealed override IBeatmap CreateBeatmap(RulesetInfo ruleset) => currentTest?.Beatmap ?? base.CreateBeatmap(ruleset);
2020-03-01 05:16:28 +00:00
2020-03-02 01:50:41 +00:00
protected sealed override Player CreatePlayer(Ruleset ruleset)
2020-03-01 05:16:28 +00:00
{
SelectedMods.Value = SelectedMods.Value.Append(currentTest.Mod).ToArray();
2020-03-02 01:50:41 +00:00
var score = currentTest.Autoplay
? ruleset.GetAutoplayMod().CreateReplayScore(Beatmap.Value.GetPlayableBeatmap(ruleset.RulesetInfo, SelectedMods.Value))
: new Score { Replay = new Replay() };
2020-03-01 05:16:28 +00:00
2020-03-02 01:50:41 +00:00
return CreateReplayPlayer(score);
2020-03-01 05:16:28 +00:00
}
2020-03-02 01:50:41 +00:00
/// <summary>
/// Creates the test cases for this test scene.
/// </summary>
protected abstract ModTestCaseData[] CreateTestCases();
2020-03-01 05:16:28 +00:00
2020-03-02 01:50:41 +00:00
/// <summary>
/// Creates the <see cref="TestPlayer"/> for a test case.
/// </summary>
/// <param name="score">The <see cref="Score"/>.</param>
protected virtual TestPlayer CreateReplayPlayer(Score score) => new TestPlayer(score);
protected class TestPlayer : TestReplayPlayer
{
public TestPlayer(Score score)
: base(score, false, false)
{
}
}
protected class ModTestCaseData
{
2020-03-02 01:50:41 +00:00
/// <summary>
/// Whether to use a replay to simulate an auto-play. True by default.
/// </summary>
public bool Autoplay = true;
/// <summary>
/// The beatmap for this test case.
/// </summary>
[CanBeNull]
public IBeatmap Beatmap;
2020-03-02 01:50:41 +00:00
/// <summary>
/// The conditions that cause this test case to pass.
/// </summary>
[CanBeNull]
public Func<bool> PassCondition;
/// <summary>
/// The name of this test case, displayed in the test browser.
/// </summary>
public readonly string Name;
/// <summary>
/// The <see cref="Mod"/> this test case tests.
/// </summary>
public readonly Mod Mod;
public ModTestCaseData(string name, Mod mod)
{
Name = name;
Mod = mod;
}
2020-03-01 05:16:28 +00:00
}
}
}