mirror of
https://github.com/osukey/osukey.git
synced 2025-08-08 00:53:56 +09:00
Use less custom classes for CPS tests
This commit is contained in:
@ -4,6 +4,7 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
|
using System.Diagnostics.CodeAnalysis;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using NUnit.Framework;
|
using NUnit.Framework;
|
||||||
using osu.Framework.Bindables;
|
using osu.Framework.Bindables;
|
||||||
@ -13,9 +14,9 @@ using osu.Framework.Testing;
|
|||||||
using osu.Framework.Timing;
|
using osu.Framework.Timing;
|
||||||
using osu.Game.Rulesets;
|
using osu.Game.Rulesets;
|
||||||
using osu.Game.Rulesets.Judgements;
|
using osu.Game.Rulesets.Judgements;
|
||||||
using osu.Game.Rulesets.Mania;
|
|
||||||
using osu.Game.Rulesets.Mods;
|
using osu.Game.Rulesets.Mods;
|
||||||
using osu.Game.Rulesets.Objects;
|
using osu.Game.Rulesets.Objects;
|
||||||
|
using osu.Game.Rulesets.Osu;
|
||||||
using osu.Game.Rulesets.UI;
|
using osu.Game.Rulesets.UI;
|
||||||
using osu.Game.Scoring;
|
using osu.Game.Scoring;
|
||||||
using osu.Game.Screens.Play;
|
using osu.Game.Screens.Play;
|
||||||
@ -27,9 +28,12 @@ namespace osu.Game.Tests.Visual.Gameplay
|
|||||||
public class TestSceneClicksPerSecond : OsuTestScene
|
public class TestSceneClicksPerSecond : OsuTestScene
|
||||||
{
|
{
|
||||||
private DependencyProvidingContainer? dependencyContainer;
|
private DependencyProvidingContainer? dependencyContainer;
|
||||||
private MockFrameStableClock? mainClock;
|
|
||||||
private ClicksPerSecondCalculator? calculator;
|
private ClicksPerSecondCalculator? calculator;
|
||||||
private ManualInputListener? listener;
|
private ManualInputListener? listener;
|
||||||
|
private GameplayClockContainer? gameplayClockContainer;
|
||||||
|
private ManualClock? manualClock;
|
||||||
|
private DrawableRuleset? drawableRuleset;
|
||||||
|
private IFrameStableClock? frameStableClock;
|
||||||
|
|
||||||
[SetUpSteps]
|
[SetUpSteps]
|
||||||
public void SetUpSteps()
|
public void SetUpSteps()
|
||||||
@ -40,41 +44,20 @@ namespace osu.Game.Tests.Visual.Gameplay
|
|||||||
|
|
||||||
Debug.Assert(ruleset != null);
|
Debug.Assert(ruleset != null);
|
||||||
|
|
||||||
Children = new Drawable[]
|
Child = gameplayClockContainer = new GameplayClockContainer(manualClock = new ManualClock());
|
||||||
|
gameplayClockContainer.AddRange(new Drawable[]
|
||||||
{
|
{
|
||||||
|
drawableRuleset = new TestDrawableRuleset(frameStableClock = new TestFrameStableClock(manualClock)),
|
||||||
dependencyContainer = new DependencyProvidingContainer
|
dependencyContainer = new DependencyProvidingContainer
|
||||||
{
|
{
|
||||||
RelativeSizeAxes = Axes.Both,
|
RelativeSizeAxes = Axes.Both,
|
||||||
CachedDependencies = new (Type, object)[]
|
CachedDependencies = new (Type, object)[]
|
||||||
{
|
{
|
||||||
(typeof(IGameplayClock), mainClock = new MockFrameStableClock(new MockFrameBasedClock())),
|
(typeof(DrawableRuleset), drawableRuleset),
|
||||||
(typeof(DrawableRuleset), new MockDrawableRuleset(ruleset, mainClock))
|
(typeof(IGameplayClock), gameplayClockContainer)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},
|
|
||||||
};
|
|
||||||
});
|
});
|
||||||
}
|
|
||||||
|
|
||||||
private void createCalculator()
|
|
||||||
{
|
|
||||||
AddStep("create calculator", () =>
|
|
||||||
{
|
|
||||||
dependencyContainer!.Children = new Drawable[]
|
|
||||||
{
|
|
||||||
calculator = new ClicksPerSecondCalculator(),
|
|
||||||
new DependencyProvidingContainer
|
|
||||||
{
|
|
||||||
RelativeSizeAxes = Axes.Both,
|
|
||||||
CachedDependencies = new (Type, object)[] { (typeof(ClicksPerSecondCalculator), calculator) },
|
|
||||||
Child = new ClicksPerSecondCounter // For visual debugging, has no real purpose in the tests
|
|
||||||
{
|
|
||||||
Anchor = Anchor.Centre,
|
|
||||||
Origin = Anchor.Centre,
|
|
||||||
Scale = new Vector2(5),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
calculator!.Listener = listener = new ManualInputListener(calculator!);
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -82,6 +65,7 @@ namespace osu.Game.Tests.Visual.Gameplay
|
|||||||
public void TestBasicConsistency()
|
public void TestBasicConsistency()
|
||||||
{
|
{
|
||||||
createCalculator();
|
createCalculator();
|
||||||
|
startClock();
|
||||||
|
|
||||||
AddStep("Create gradually increasing KPS inputs", () =>
|
AddStep("Create gradually increasing KPS inputs", () =>
|
||||||
{
|
{
|
||||||
@ -101,6 +85,7 @@ namespace osu.Game.Tests.Visual.Gameplay
|
|||||||
public void TestRateAdjustConsistency()
|
public void TestRateAdjustConsistency()
|
||||||
{
|
{
|
||||||
createCalculator();
|
createCalculator();
|
||||||
|
startClock();
|
||||||
|
|
||||||
AddStep("Create consistent KPS inputs", () => addInputs(generateConsistentKps(10)));
|
AddStep("Create consistent KPS inputs", () => addInputs(generateConsistentKps(10)));
|
||||||
|
|
||||||
@ -125,6 +110,7 @@ namespace osu.Game.Tests.Visual.Gameplay
|
|||||||
public void TestInputsDiscardedOnRewind()
|
public void TestInputsDiscardedOnRewind()
|
||||||
{
|
{
|
||||||
createCalculator();
|
createCalculator();
|
||||||
|
startClock();
|
||||||
|
|
||||||
AddStep("Create consistent KPS inputs", () => addInputs(generateConsistentKps(10)));
|
AddStep("Create consistent KPS inputs", () => addInputs(generateConsistentKps(10)));
|
||||||
seek(1000);
|
seek(1000);
|
||||||
@ -136,38 +122,79 @@ namespace osu.Game.Tests.Visual.Gameplay
|
|||||||
AddAssert("KPS didn't changed", () => calculator!.Value == 10);
|
AddAssert("KPS didn't changed", () => calculator!.Value == 10);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void seek(double time) => AddStep($"Seek main clock to {time}ms", () => mainClock?.Seek(time));
|
private void seekAllClocks(double time)
|
||||||
|
|
||||||
private void changeRate(double rate) => AddStep($"Change rate to x{rate}", () =>
|
|
||||||
(mainClock?.UnderlyingClock as MockFrameBasedClock)!.Rate = rate);
|
|
||||||
|
|
||||||
private void advanceForwards(int frames = 1) => AddStep($"Advance main clock {frames} frame(s) forward.", () =>
|
|
||||||
{
|
{
|
||||||
if (mainClock == null) return;
|
gameplayClockContainer?.Seek(time);
|
||||||
|
manualClock!.CurrentTime = time;
|
||||||
|
}
|
||||||
|
|
||||||
MockFrameBasedClock underlyingClock = (MockFrameBasedClock)mainClock.UnderlyingClock;
|
protected override Ruleset CreateRuleset() => new OsuRuleset();
|
||||||
underlyingClock.Backwards = false;
|
|
||||||
|
|
||||||
for (int i = 0; i < frames; i++)
|
#region Quick steps methods
|
||||||
|
|
||||||
|
private void createCalculator()
|
||||||
{
|
{
|
||||||
underlyingClock.ProcessFrame();
|
AddStep("create calculator", () =>
|
||||||
|
{
|
||||||
|
Debug.Assert(dependencyContainer?.Dependencies.Get(typeof(DrawableRuleset)) is DrawableRuleset);
|
||||||
|
dependencyContainer!.Children = new Drawable[]
|
||||||
|
{
|
||||||
|
calculator = new ClicksPerSecondCalculator(),
|
||||||
|
new DependencyProvidingContainer
|
||||||
|
{
|
||||||
|
RelativeSizeAxes = Axes.Both,
|
||||||
|
CachedDependencies = new (Type, object)[] { (typeof(ClicksPerSecondCalculator), calculator) },
|
||||||
|
Child = new ClicksPerSecondCounter // For visual debugging, has no real purpose in the tests
|
||||||
|
{
|
||||||
|
Anchor = Anchor.Centre,
|
||||||
|
Origin = Anchor.Centre,
|
||||||
|
Scale = new Vector2(5),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
calculator!.Listener = listener = new ManualInputListener(calculator!);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private void seek(double time) => AddStep($"Seek clocks to {time}ms", () => seekAllClocks(time));
|
||||||
|
|
||||||
|
private void changeRate(double rate) => AddStep($"Change rate to x{rate}", () => manualClock!.Rate = rate);
|
||||||
|
|
||||||
|
private void advanceForwards(double time) =>
|
||||||
|
AddStep($"Advance clocks {time} seconds forward.", () =>
|
||||||
|
{
|
||||||
|
gameplayClockContainer!.Seek(gameplayClockContainer.CurrentTime + time * manualClock!.Rate);
|
||||||
|
|
||||||
|
for (int i = 0; i < time; i++)
|
||||||
|
{
|
||||||
|
frameStableClock?.ProcessFrame();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
private void startClock() => AddStep("Start clocks", () =>
|
||||||
|
{
|
||||||
|
gameplayClockContainer?.Start();
|
||||||
|
manualClock!.Rate = 1;
|
||||||
|
});
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region Input generation
|
||||||
|
|
||||||
private void addInputs(IEnumerable<double> inputs)
|
private void addInputs(IEnumerable<double> inputs)
|
||||||
{
|
{
|
||||||
Debug.Assert(mainClock != null && listener != null);
|
Debug.Assert(manualClock != null && listener != null && gameplayClockContainer != null);
|
||||||
if (!inputs.Any()) return;
|
if (!inputs.Any()) return;
|
||||||
|
|
||||||
double baseTime = mainClock.CurrentTime;
|
double baseTime = gameplayClockContainer.CurrentTime;
|
||||||
|
|
||||||
foreach (double timestamp in inputs)
|
foreach (double timestamp in inputs)
|
||||||
{
|
{
|
||||||
mainClock.Seek(timestamp);
|
seekAllClocks(timestamp);
|
||||||
listener.AddInput();
|
listener.AddInput();
|
||||||
}
|
}
|
||||||
|
|
||||||
mainClock.Seek(baseTime);
|
seekAllClocks(baseTime);
|
||||||
}
|
}
|
||||||
|
|
||||||
private IEnumerable<double> generateGraduallyIncreasingKps()
|
private IEnumerable<double> generateGraduallyIncreasingKps()
|
||||||
@ -200,9 +227,52 @@ namespace osu.Game.Tests.Visual.Gameplay
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override Ruleset CreateRuleset() => new ManiaRuleset();
|
#endregion
|
||||||
|
|
||||||
#region Mock classes
|
#region Test classes
|
||||||
|
|
||||||
|
private class TestFrameStableClock : IFrameStableClock
|
||||||
|
{
|
||||||
|
public TestFrameStableClock(IClock source, double startTime = 0)
|
||||||
|
{
|
||||||
|
this.source = source;
|
||||||
|
StartTime = startTime;
|
||||||
|
|
||||||
|
if (source is ManualClock manualClock)
|
||||||
|
{
|
||||||
|
manualClock.CurrentTime = startTime;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public double CurrentTime => source.CurrentTime;
|
||||||
|
public double Rate => source.Rate;
|
||||||
|
public bool IsRunning => source.IsRunning;
|
||||||
|
|
||||||
|
private IClock source;
|
||||||
|
|
||||||
|
public void ProcessFrame()
|
||||||
|
{
|
||||||
|
if (source is ManualClock manualClock)
|
||||||
|
{
|
||||||
|
manualClock.CurrentTime += 1000 * Rate;
|
||||||
|
}
|
||||||
|
|
||||||
|
TimeInfo = new FrameTimeInfo
|
||||||
|
{
|
||||||
|
Elapsed = 1000 * Rate,
|
||||||
|
Current = CurrentTime
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public double ElapsedFrameTime => TimeInfo.Elapsed;
|
||||||
|
public double FramesPerSecond => 1 / ElapsedFrameTime * 1000;
|
||||||
|
public FrameTimeInfo TimeInfo { get; private set; }
|
||||||
|
|
||||||
|
public double? StartTime { get; }
|
||||||
|
public IEnumerable<double> NonGameplayAdjustments => Enumerable.Empty<double>();
|
||||||
|
public IBindable<bool> IsCatchingUp => new Bindable<bool>();
|
||||||
|
public IBindable<bool> WaitingOnFrames => new Bindable<bool>();
|
||||||
|
}
|
||||||
|
|
||||||
private class ManualInputListener : ClicksPerSecondCalculator.InputListener
|
private class ManualInputListener : ClicksPerSecondCalculator.InputListener
|
||||||
{
|
{
|
||||||
@ -214,108 +284,54 @@ namespace osu.Game.Tests.Visual.Gameplay
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private class MockFrameBasedClock : ManualClock, IFrameBasedClock
|
#nullable disable
|
||||||
{
|
|
||||||
public const double FRAME_INTERVAL = 1000;
|
|
||||||
public bool Backwards;
|
|
||||||
|
|
||||||
public MockFrameBasedClock()
|
[SuppressMessage("ReSharper", "UnassignedGetOnlyAutoProperty")]
|
||||||
|
private class TestDrawableRuleset : DrawableRuleset
|
||||||
{
|
{
|
||||||
Rate = 1;
|
public override IEnumerable<HitObject> Objects => Enumerable.Empty<HitObject>();
|
||||||
IsRunning = true;
|
|
||||||
|
public override event Action<JudgementResult> NewResult
|
||||||
|
{
|
||||||
|
add => throw new InvalidOperationException($"{nameof(NewResult)} operations not supported in test context");
|
||||||
|
remove => throw new InvalidOperationException($"{nameof(NewResult)} operations not supported in test context");
|
||||||
}
|
}
|
||||||
|
|
||||||
public void ProcessFrame()
|
public override event Action<JudgementResult> RevertResult
|
||||||
{
|
{
|
||||||
CurrentTime += FRAME_INTERVAL * Rate * (Backwards ? -1 : 1);
|
add => throw new InvalidOperationException($"{nameof(RevertResult)} operations not supported in test context");
|
||||||
TimeInfo = new FrameTimeInfo
|
remove => throw new InvalidOperationException($"{nameof(RevertResult)} operations not supported in test context");
|
||||||
{
|
|
||||||
Current = CurrentTime,
|
|
||||||
Elapsed = FRAME_INTERVAL * Rate * (Backwards ? -1 : 1)
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Seek(double time)
|
public override Playfield Playfield => null;
|
||||||
{
|
public override Container Overlays => null;
|
||||||
TimeInfo = new FrameTimeInfo
|
public override Container FrameStableComponents => null;
|
||||||
{
|
|
||||||
Elapsed = time - CurrentTime,
|
|
||||||
Current = CurrentTime = time
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
public double ElapsedFrameTime => TimeInfo.Elapsed;
|
|
||||||
public double FramesPerSecond => 1 / FRAME_INTERVAL;
|
|
||||||
public FrameTimeInfo TimeInfo { get; private set; }
|
|
||||||
}
|
|
||||||
|
|
||||||
private class MockFrameStableClock : IGameplayClock, IFrameStableClock
|
|
||||||
{
|
|
||||||
internal readonly IFrameBasedClock UnderlyingClock;
|
|
||||||
|
|
||||||
public readonly BindableBool IsPaused = new BindableBool();
|
|
||||||
|
|
||||||
public MockFrameStableClock(MockFrameBasedClock underlyingClock)
|
|
||||||
{
|
|
||||||
UnderlyingClock = underlyingClock;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Seek(double time) => (UnderlyingClock as MockFrameBasedClock)?.Seek(time);
|
|
||||||
|
|
||||||
public IBindable<bool> IsCatchingUp => new Bindable<bool>();
|
|
||||||
public IBindable<bool> WaitingOnFrames => new Bindable<bool>();
|
|
||||||
public double CurrentTime => UnderlyingClock.CurrentTime;
|
|
||||||
public double Rate => UnderlyingClock.Rate;
|
|
||||||
public bool IsRunning => UnderlyingClock.IsRunning;
|
|
||||||
public void ProcessFrame() => UnderlyingClock.ProcessFrame();
|
|
||||||
|
|
||||||
public double ElapsedFrameTime => UnderlyingClock.ElapsedFrameTime;
|
|
||||||
public double FramesPerSecond => UnderlyingClock.FramesPerSecond;
|
|
||||||
public FrameTimeInfo TimeInfo => UnderlyingClock.TimeInfo;
|
|
||||||
public double TrueGameplayRate => UnderlyingClock.Rate;
|
|
||||||
public double? StartTime => 0;
|
|
||||||
public IEnumerable<double> NonGameplayAdjustments => Enumerable.Empty<double>();
|
|
||||||
IBindable<bool> IGameplayClock.IsPaused => IsPaused;
|
|
||||||
}
|
|
||||||
|
|
||||||
private class MockDrawableRuleset : DrawableRuleset
|
|
||||||
{
|
|
||||||
public MockDrawableRuleset(Ruleset ruleset, IFrameStableClock clock)
|
|
||||||
: base(ruleset)
|
|
||||||
{
|
|
||||||
FrameStableClock = clock;
|
|
||||||
}
|
|
||||||
|
|
||||||
#pragma warning disable CS0067
|
|
||||||
public override event Action<JudgementResult>? NewResult;
|
|
||||||
public override event Action<JudgementResult>? RevertResult;
|
|
||||||
#pragma warning restore CS0067
|
|
||||||
public override Playfield? Playfield => null;
|
|
||||||
public override Container? Overlays => null;
|
|
||||||
public override Container? FrameStableComponents => null;
|
|
||||||
public override IFrameStableClock FrameStableClock { get; }
|
public override IFrameStableClock FrameStableClock { get; }
|
||||||
|
|
||||||
internal override bool FrameStablePlayback { get; set; }
|
internal override bool FrameStablePlayback { get; set; }
|
||||||
public override IReadOnlyList<Mod> Mods => Array.Empty<Mod>();
|
public override IReadOnlyList<Mod> Mods => Array.Empty<Mod>();
|
||||||
public override IEnumerable<HitObject> Objects => Array.Empty<HitObject>();
|
|
||||||
public override double GameplayStartTime => 0;
|
public override double GameplayStartTime => 0;
|
||||||
public override GameplayCursorContainer? Cursor => null;
|
public override GameplayCursorContainer Cursor => null;
|
||||||
|
|
||||||
public override void SetReplayScore(Score replayScore)
|
public TestDrawableRuleset()
|
||||||
|
: base(new OsuRuleset())
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void SetRecordTarget(Score score)
|
public TestDrawableRuleset(IFrameStableClock frameStableClock)
|
||||||
|
: this()
|
||||||
{
|
{
|
||||||
|
FrameStableClock = frameStableClock;
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void RequestResume(Action continueResume)
|
public override void SetReplayScore(Score replayScore) => throw new NotImplementedException();
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
public override void CancelResume()
|
public override void SetRecordTarget(Score score) => throw new NotImplementedException();
|
||||||
{
|
|
||||||
}
|
public override void RequestResume(Action continueResume) => throw new NotImplementedException();
|
||||||
|
|
||||||
|
public override void CancelResume() => throw new NotImplementedException();
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
Reference in New Issue
Block a user