1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-07 19:22:58 +08:00

Merge pull request #8066 from voidedWarranties/mod-testscene

Add base test scene allowing different player initialisation per test method
This commit is contained in:
Dan Balasescu 2020-03-05 17:40:08 +09:00 committed by GitHub
commit 07a54a10b5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 207 additions and 3 deletions

View File

@ -0,0 +1,86 @@
// 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.Linq;
using NUnit.Framework;
using osu.Framework.Graphics.Containers;
using osu.Framework.Testing;
using osu.Framework.Utils;
using osu.Game.Graphics.Containers;
using osu.Game.Rulesets.Osu.Mods;
using osu.Game.Rulesets.Osu.Objects.Drawables;
using osu.Game.Tests.Visual;
namespace osu.Game.Rulesets.Osu.Tests.Mods
{
public class TestSceneOsuModDifficultyAdjust : ModTestScene
{
public TestSceneOsuModDifficultyAdjust()
: base(new OsuRuleset())
{
}
[Test]
public void TestNoAdjustment() => CreateModTest(new ModTestData
{
Mod = new OsuModDifficultyAdjust(),
Autoplay = true,
PassCondition = checkSomeHit
});
[Test]
public void TestCircleSize1() => CreateModTest(new ModTestData
{
Mod = new OsuModDifficultyAdjust { CircleSize = { Value = 1 } },
Autoplay = true,
PassCondition = () => checkSomeHit() && checkObjectsScale(0.78f)
});
[Test]
public void TestCircleSize10() => CreateModTest(new ModTestData
{
Mod = new OsuModDifficultyAdjust { CircleSize = { Value = 10 } },
Autoplay = true,
PassCondition = () => checkSomeHit() && checkObjectsScale(0.15f)
});
[Test]
public void TestApproachRate1() => CreateModTest(new ModTestData
{
Mod = new OsuModDifficultyAdjust { ApproachRate = { Value = 1 } },
Autoplay = true,
PassCondition = () => checkSomeHit() && checkObjectsPreempt(1680)
});
[Test]
public void TestApproachRate10() => CreateModTest(new ModTestData
{
Mod = new OsuModDifficultyAdjust { ApproachRate = { Value = 10 } },
Autoplay = true,
PassCondition = () => checkSomeHit() && checkObjectsPreempt(450)
});
private bool checkObjectsPreempt(double target)
{
var objects = Player.ChildrenOfType<DrawableHitCircle>();
if (!objects.Any())
return false;
return objects.All(o => o.HitObject.TimePreempt == target);
}
private bool checkObjectsScale(float target)
{
var objects = Player.ChildrenOfType<DrawableHitCircle>();
if (!objects.Any())
return false;
return objects.All(o => Precision.AlmostEquals(o.ChildrenOfType<ShakeContainer>().First().Children.OfType<Container>().Single().Scale.X, target));
}
private bool checkSomeHit()
{
return Player.ScoreProcessor.JudgedHits >= 2;
}
}
}

View File

@ -0,0 +1,99 @@
// 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 JetBrains.Annotations;
using osu.Game.Beatmaps;
using osu.Game.Rulesets;
using osu.Game.Rulesets.Mods;
namespace osu.Game.Tests.Visual
{
public abstract class ModTestScene : PlayerTestScene
{
protected sealed override bool HasCustomSteps => true;
public override IReadOnlyList<Type> RequiredTypes => new[]
{
typeof(ModTestScene)
};
protected ModTestScene(Ruleset ruleset)
: base(ruleset)
{
}
private ModTestData currentTestData;
protected void CreateModTest(ModTestData testData) => CreateTest(() =>
{
AddStep("set test data", () => currentTestData = testData);
});
public override void TearDownSteps()
{
AddUntilStep("test passed", () =>
{
if (currentTestData == null)
return true;
return currentTestData.PassCondition?.Invoke() ?? false;
});
base.TearDownSteps();
}
protected sealed override IBeatmap CreateBeatmap(RulesetInfo ruleset) => currentTestData?.Beatmap ?? base.CreateBeatmap(ruleset);
protected sealed override TestPlayer CreatePlayer(Ruleset ruleset)
{
var mods = new List<Mod>(SelectedMods.Value);
if (currentTestData.Mod != null)
mods.Add(currentTestData.Mod);
if (currentTestData.Autoplay)
mods.Add(ruleset.GetAutoplayMod());
SelectedMods.Value = mods;
return new ModTestPlayer(AllowFail);
}
protected class ModTestPlayer : TestPlayer
{
protected override bool AllowFail { get; }
public ModTestPlayer(bool allowFail)
: base(false, false)
{
AllowFail = allowFail;
}
}
protected class ModTestData
{
/// <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;
/// <summary>
/// The conditions that cause this test case to pass.
/// </summary>
[CanBeNull]
public Func<bool> PassCondition;
/// <summary>
/// The <see cref="Mod"/> this test case tests.
/// </summary>
public Mod Mod;
}
}
}

View File

@ -1,6 +1,7 @@
// 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.Linq;
using osu.Framework.Allocation;
using osu.Framework.Extensions.IEnumerableExtensions;
@ -13,6 +14,11 @@ namespace osu.Game.Tests.Visual
{
public abstract class PlayerTestScene : RateAdjustedBeatmapTestScene
{
/// <summary>
/// Whether custom test steps are provided. Custom tests should invoke <see cref="CreateTest"/> to create the test steps.
/// </summary>
protected virtual bool HasCustomSteps { get; } = false;
private readonly Ruleset ruleset;
protected TestPlayer Player;
@ -36,7 +42,18 @@ namespace osu.Game.Tests.Visual
{
base.SetUpSteps();
AddStep(ruleset.RulesetInfo.Name, loadPlayer);
if (!HasCustomSteps)
CreateTest(null);
}
protected void CreateTest(Action action)
{
if (action != null && !HasCustomSteps)
throw new InvalidOperationException($"Cannot add custom test steps without {nameof(HasCustomSteps)} being set.");
action?.Invoke();
AddStep(ruleset.RulesetInfo.Name, LoadPlayer);
AddUntilStep("player loaded", () => Player.IsLoaded && Player.Alpha == 1);
}
@ -44,12 +61,14 @@ namespace osu.Game.Tests.Visual
protected virtual bool Autoplay => false;
private void loadPlayer()
protected void LoadPlayer()
{
var beatmap = CreateBeatmap(ruleset.RulesetInfo);
Beatmap.Value = CreateWorkingBeatmap(beatmap);
SelectedMods.Value = Array.Empty<Mod>();
if (!AllowFail)
{
var noFailMod = ruleset.GetAllMods().FirstOrDefault(m => m is ModNoFail);

View File

@ -34,7 +34,7 @@ namespace osu.Game.Tests.Visual
public virtual void SetUpSteps() => addExitAllScreensStep();
[TearDownSteps]
public void TearDownSteps() => addExitAllScreensStep();
public virtual void TearDownSteps() => addExitAllScreensStep();
private void addExitAllScreensStep()
{