1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-06 12:17:46 +08:00
osu-lazer/osu.Game.Rulesets.Osu.Tests/TestSceneSliderSnaking.cs

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

273 lines
11 KiB
C#
Raw Normal View History

// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
2020-03-29 22:00:26 +08:00
// See the LICENCE file in the repository root for full licence text.
2022-06-17 15:37:17 +08:00
#nullable disable
2020-04-05 21:26:32 +08:00
using System;
2020-03-29 22:00:26 +08:00
using System.Collections.Generic;
using System.Linq;
2020-04-04 20:10:54 +08:00
using Humanizer;
2020-03-29 22:00:26 +08:00
using NUnit.Framework;
using osu.Framework.Allocation;
using osu.Framework.Audio;
using osu.Framework.Bindables;
using osu.Framework.Extensions.ObjectExtensions;
2020-03-29 22:00:26 +08:00
using osu.Framework.Graphics.Containers;
using osu.Framework.Testing;
using osu.Framework.Timing;
using osu.Framework.Utils;
using osu.Game.Beatmaps;
using osu.Game.Rulesets.Objects;
using osu.Game.Rulesets.Objects.Types;
using osu.Game.Rulesets.Osu.Configuration;
using osu.Game.Rulesets.Osu.Objects;
using osu.Game.Rulesets.Osu.Objects.Drawables;
2020-12-04 19:21:53 +08:00
using osu.Game.Rulesets.Osu.Skinning.Default;
2020-03-29 22:00:26 +08:00
using osu.Game.Storyboards;
using osu.Game.Tests;
2020-03-29 22:00:26 +08:00
using osuTK;
namespace osu.Game.Rulesets.Osu.Tests
{
[TestFixture]
public class TestSceneSliderSnaking : TestSceneOsuPlayer
{
[Resolved]
private AudioManager audioManager { get; set; }
2020-03-29 22:43:18 +08:00
protected override bool Autoplay => autoplay;
private bool autoplay;
2020-03-29 22:00:26 +08:00
2020-04-04 20:35:35 +08:00
private readonly BindableBool snakingIn = new BindableBool();
private readonly BindableBool snakingOut = new BindableBool();
2020-03-29 22:00:26 +08:00
2021-06-30 18:43:34 +08:00
private IBeatmap beatmap;
2020-04-04 20:10:54 +08:00
private const double duration_of_span = 3605;
private const double fade_in_modifier = -1200;
2020-03-29 22:00:26 +08:00
protected override WorkingBeatmap CreateWorkingBeatmap(IBeatmap beatmap, Storyboard storyboard = null)
2021-06-30 18:43:34 +08:00
=> new ClockBackedTestWorkingBeatmap(this.beatmap = beatmap, storyboard, new FramedClock(new ManualClock { Rate = 1 }), audioManager);
2020-03-29 22:00:26 +08:00
[BackgroundDependencyLoader]
private void load()
2020-03-29 22:00:26 +08:00
{
var config = (OsuRulesetConfigManager)RulesetConfigs.GetConfigFor(Ruleset.Value.CreateInstance()).AsNonNull();
2020-03-29 22:00:26 +08:00
config.BindWith(OsuRulesetSetting.SnakingInSliders, snakingIn);
config.BindWith(OsuRulesetSetting.SnakingOutSliders, snakingOut);
}
2021-06-30 18:43:34 +08:00
private Slider slider;
2020-11-13 22:03:44 +08:00
private DrawableSlider drawableSlider;
2020-03-29 22:00:26 +08:00
2021-06-30 18:43:34 +08:00
[SetUp]
public void Setup() => Schedule(() =>
{
slider = null;
drawableSlider = null;
});
protected override bool HasCustomSteps => true;
2020-03-29 22:00:26 +08:00
2020-04-05 16:22:52 +08:00
[TestCase(0)]
[TestCase(1)]
[TestCase(2)]
[FlakyTest]
/*
* Fail rate around 0.15%
*
* TearDown : System.TimeoutException : "wait for seek to finish" timed out
* --TearDown
* at osu.Framework.Testing.Drawables.Steps.UntilStepButton.<>c__DisplayClass11_0.<.ctor>b__0()
* at osu.Framework.Testing.Drawables.Steps.StepButton.PerformStep(Boolean userTriggered)
* at osu.Framework.Testing.TestScene.runNextStep(Action onCompletion, Action`1 onError, Func`2 stopCondition)
*/
2020-04-05 21:26:32 +08:00
public void TestSnakingEnabled(int sliderIndex)
2020-03-29 22:00:26 +08:00
{
2020-04-05 21:26:32 +08:00
AddStep("enable autoplay", () => autoplay = true);
CreateTest();
2020-08-26 23:21:50 +08:00
AddUntilStep("wait for track to start running", () => Beatmap.Value.Track.IsRunning);
2020-03-29 22:43:18 +08:00
2021-06-30 18:43:34 +08:00
retrieveSlider(sliderIndex);
2020-04-05 21:26:32 +08:00
setSnaking(true);
2021-06-30 18:43:34 +08:00
addEnsureSnakingInSteps(() => slider.StartTime + fade_in_modifier);
2020-04-05 21:26:32 +08:00
for (int i = 0; i < sliderIndex; i++)
{
// non-final repeats should not snake out
2021-06-30 18:43:34 +08:00
addEnsureNoSnakingOutStep(() => slider.StartTime, i);
2020-04-05 21:26:32 +08:00
}
// final repeat should snake out
2021-06-30 18:43:34 +08:00
addEnsureSnakingOutSteps(() => slider.StartTime, sliderIndex);
2020-04-05 16:22:52 +08:00
}
[TestCase(0)]
[TestCase(1)]
[TestCase(2)]
[FlakyTest]
/*
* Fail rate around 0.15%
*
* TearDown : System.TimeoutException : "wait for seek to finish" timed out
* --TearDown
* at osu.Framework.Testing.Drawables.Steps.UntilStepButton.<>c__DisplayClass11_0.<.ctor>b__0()
* at osu.Framework.Testing.Drawables.Steps.StepButton.PerformStep(Boolean userTriggered)
* at osu.Framework.Testing.TestScene.runNextStep(Action onCompletion, Action`1 onError, Func`2 stopCondition)
*/
2020-04-05 21:26:32 +08:00
public void TestSnakingDisabled(int sliderIndex)
2020-04-05 16:22:52 +08:00
{
AddStep("have autoplay", () => autoplay = true);
CreateTest();
2020-08-26 23:21:50 +08:00
AddUntilStep("wait for track to start running", () => Beatmap.Value.Track.IsRunning);
2020-04-05 16:22:52 +08:00
2021-06-30 18:43:34 +08:00
retrieveSlider(sliderIndex);
2020-04-05 21:26:32 +08:00
setSnaking(false);
2021-06-30 18:43:34 +08:00
addEnsureNoSnakingInSteps(() => slider.StartTime + fade_in_modifier);
2020-04-05 21:26:32 +08:00
for (int i = 0; i <= sliderIndex; i++)
{
// no snaking out ever, including final repeat
2021-06-30 18:43:34 +08:00
addEnsureNoSnakingOutStep(() => slider.StartTime, i);
2020-04-05 21:26:32 +08:00
}
2020-03-29 22:43:18 +08:00
}
2020-04-05 21:37:31 +08:00
[Test]
public void TestRepeatArrowDoesNotMoveWhenHit()
2020-03-29 22:43:18 +08:00
{
2020-04-05 21:37:31 +08:00
AddStep("enable autoplay", () => autoplay = true);
2020-03-29 22:00:26 +08:00
setSnaking(true);
CreateTest();
2020-04-05 21:53:56 +08:00
// repeat might have a chance to update its position depending on where in the frame its hit,
// so some leniency is allowed here instead of checking strict equality
2021-06-30 18:43:34 +08:00
addCheckPositionChangeSteps(() => 16600, getSliderRepeat, positionAlmostSame);
2020-04-05 21:37:31 +08:00
}
2020-04-04 20:35:35 +08:00
2020-04-05 21:37:31 +08:00
[Test]
public void TestRepeatArrowMovesWhenNotHit()
{
AddStep("disable autoplay", () => autoplay = false);
setSnaking(true);
CreateTest();
2021-06-30 18:43:34 +08:00
addCheckPositionChangeSteps(() => 16600, getSliderRepeat, positionDecreased);
2020-03-29 22:00:26 +08:00
}
2021-06-30 18:43:34 +08:00
private void retrieveSlider(int index)
{
AddStep("retrieve slider at index", () => slider = (Slider)beatmap.HitObjects[index]);
addSeekStep(() => slider.StartTime);
2021-06-30 18:43:34 +08:00
AddUntilStep("retrieve drawable slider", () =>
2021-06-30 18:52:25 +08:00
(drawableSlider = (DrawableSlider)Player.DrawableRuleset.Playfield.AllHitObjects.SingleOrDefault(d => d.HitObject == slider)) != null);
2021-06-30 18:43:34 +08:00
}
2020-04-05 21:26:32 +08:00
2021-06-30 18:43:34 +08:00
private void addEnsureSnakingInSteps(Func<double> startTime) => addCheckPositionChangeSteps(startTime, getSliderEnd, positionIncreased);
private void addEnsureNoSnakingInSteps(Func<double> startTime) => addCheckPositionChangeSteps(startTime, getSliderEnd, positionRemainsSame);
private void addEnsureSnakingOutSteps(Func<double> startTime, int repeatIndex)
{
2020-04-05 21:26:32 +08:00
if (repeatIndex % 2 == 0)
2021-06-30 18:43:34 +08:00
addCheckPositionChangeSteps(timeAtRepeat(startTime, repeatIndex), getSliderStart, positionIncreased);
2020-04-05 21:26:32 +08:00
else
2021-06-30 18:43:34 +08:00
addCheckPositionChangeSteps(timeAtRepeat(startTime, repeatIndex), getSliderEnd, positionDecreased);
2020-03-29 22:00:26 +08:00
}
2021-06-30 18:43:34 +08:00
private void addEnsureNoSnakingOutStep(Func<double> startTime, int repeatIndex)
=> addCheckPositionChangeSteps(timeAtRepeat(startTime, repeatIndex), positionAtRepeat(repeatIndex), positionRemainsSame);
2020-04-04 20:35:35 +08:00
2021-06-30 18:43:34 +08:00
private Func<double> timeAtRepeat(Func<double> startTime, int repeatIndex) => () => startTime() + 100 + duration_of_span * repeatIndex;
2022-06-24 13:48:43 +08:00
private Func<Vector2> positionAtRepeat(int repeatIndex) => repeatIndex % 2 == 0 ? getSliderStart : getSliderEnd;
2020-04-04 20:35:35 +08:00
2021-06-30 18:43:34 +08:00
private List<Vector2> getSliderCurve() => ((PlaySliderBody)drawableSlider.Body.Drawable).CurrentCurve;
private Vector2 getSliderStart() => getSliderCurve().First();
private Vector2 getSliderEnd() => getSliderCurve().Last();
2020-04-05 21:39:32 +08:00
2021-06-30 18:43:34 +08:00
private Vector2 getSliderRepeat()
2020-04-05 21:37:31 +08:00
{
2021-06-30 18:43:34 +08:00
var drawable = Player.DrawableRuleset.Playfield.AllHitObjects.SingleOrDefault(d => d.HitObject == beatmap.HitObjects[1]);
2020-04-05 21:37:31 +08:00
var repeat = drawable.ChildrenOfType<Container<DrawableSliderRepeat>>().First().Children.First();
return repeat.Position;
}
2020-04-04 20:35:35 +08:00
2020-04-05 21:26:32 +08:00
private bool positionRemainsSame(Vector2 previous, Vector2 current) => previous == current;
private bool positionIncreased(Vector2 previous, Vector2 current) => current.X > previous.X && current.Y > previous.Y;
private bool positionDecreased(Vector2 previous, Vector2 current) => current.X < previous.X && current.Y < previous.Y;
2020-04-05 21:37:31 +08:00
private bool positionAlmostSame(Vector2 previous, Vector2 current) => Precision.AlmostEquals(previous, current, 1);
2020-03-29 22:00:26 +08:00
2021-06-30 18:43:34 +08:00
private void addCheckPositionChangeSteps(Func<double> startTime, Func<Vector2> positionToCheck, Func<Vector2, Vector2, bool> positionAssertion)
2020-04-04 20:10:54 +08:00
{
2020-04-05 21:26:32 +08:00
Vector2 previousPosition = Vector2.Zero;
string positionDescription = positionToCheck.Method.Name.Humanize(LetterCasing.LowerCase);
string assertionDescription = positionAssertion.Method.Name.Humanize(LetterCasing.LowerCase);
addSeekStep(startTime);
AddStep($"save {positionDescription} position", () => previousPosition = positionToCheck.Invoke());
2021-06-30 18:43:34 +08:00
addSeekStep(() => startTime() + 100);
2020-04-05 21:26:32 +08:00
AddAssert($"{positionDescription} {assertionDescription}", () =>
{
var currentPosition = positionToCheck.Invoke();
return positionAssertion.Invoke(previousPosition, currentPosition);
});
2020-04-04 20:10:54 +08:00
}
2020-03-29 22:00:26 +08:00
private void setSnaking(bool value)
{
2020-04-05 21:39:32 +08:00
AddStep($"{(value ? "enable" : "disable")} snaking", () =>
2020-03-29 22:00:26 +08:00
{
snakingIn.Value = value;
snakingOut.Value = value;
});
}
private void addSeekStep(Func<double> getTime)
2020-03-29 22:00:26 +08:00
{
AddStep("seek to time", () => Player.GameplayClockContainer.Seek(getTime()));
AddUntilStep("wait for seek to finish", () => Precision.AlmostEquals(getTime(), Player.DrawableRuleset.FrameStableClock.CurrentTime, 100));
2021-06-30 18:43:34 +08:00
}
protected override IBeatmap CreateBeatmap(RulesetInfo ruleset) => new Beatmap { HitObjects = createHitObjects() };
private static List<HitObject> createHitObjects() => new List<HitObject>
{
new Slider
2020-03-29 22:00:26 +08:00
{
StartTime = 3000,
Position = new Vector2(100, 100),
Path = new SliderPath(PathType.PerfectCurve, new[]
2020-03-29 22:00:26 +08:00
{
Vector2.Zero,
new Vector2(300, 200)
}),
},
new Slider
{
StartTime = 13000,
Position = new Vector2(100, 100),
Path = new SliderPath(PathType.PerfectCurve, new[]
2020-03-29 22:00:26 +08:00
{
Vector2.Zero,
new Vector2(300, 200)
}),
RepeatCount = 1,
},
new Slider
{
StartTime = 23000,
Position = new Vector2(100, 100),
Path = new SliderPath(PathType.PerfectCurve, new[]
2020-03-29 22:00:26 +08:00
{
Vector2.Zero,
new Vector2(300, 200)
}),
RepeatCount = 2,
},
new HitCircle
{
StartTime = 199999,
2020-03-29 22:00:26 +08:00
}
};
}
}