diff --git a/osu.Game.Tests/Visual/TestCaseKeyCounter.cs b/osu.Game.Tests/Visual/TestCaseKeyCounter.cs index b98875cd6a..931c62a64a 100644 --- a/osu.Game.Tests/Visual/TestCaseKeyCounter.cs +++ b/osu.Game.Tests/Visual/TestCaseKeyCounter.cs @@ -3,7 +3,9 @@ using NUnit.Framework; using osu.Framework.Graphics; +using osu.Framework.Input.EventArgs; using osu.Framework.MathUtils; +using osu.Framework.Timing; using osu.Game.Screens.Play; using OpenTK.Input; @@ -12,15 +14,18 @@ namespace osu.Game.Tests.Visual [TestFixture] public class TestCaseKeyCounter : OsuTestCase { + private const Key rewind_test_key = Key.Z; + public TestCaseKeyCounter() { + KeyCounterKeyboard rewindTestKeyCounterKeyboard; KeyCounterCollection kc = new KeyCounterCollection { Origin = Anchor.Centre, Anchor = Anchor.Centre, Children = new KeyCounter[] { - new KeyCounterKeyboard(Key.Z), + rewindTestKeyCounterKeyboard = new KeyCounterKeyboard(rewind_test_key), new KeyCounterKeyboard(Key.X), new KeyCounterMouse(MouseButton.Left), new KeyCounterMouse(MouseButton.Right), @@ -34,6 +39,28 @@ namespace osu.Game.Tests.Visual }); AddSliderStep("Fade time", 0, 200, 50, v => kc.FadeTime = v); + var expectedCountPresses = rewindTestKeyCounterKeyboard.CountPresses + 1; + AddStep($"Press {rewind_test_key} key", () => + { + rewindTestKeyCounterKeyboard.TriggerOnKeyDown(null, new KeyDownEventArgs { Key = rewind_test_key, Repeat = false }); + rewindTestKeyCounterKeyboard.TriggerOnKeyUp(null, new KeyUpEventArgs { Key = rewind_test_key }); + }); + + AddAssert($"Check {rewind_test_key} counter after keypress", () => rewindTestKeyCounterKeyboard.CountPresses == expectedCountPresses); + + IFrameBasedClock counterClock = null; + AddStep($"Rewind {rewind_test_key} counter", () => + { + counterClock = rewindTestKeyCounterKeyboard.Clock; + rewindTestKeyCounterKeyboard.Clock = new DecoupleableInterpolatingFramedClock(); + }); + + AddAssert($"Check {rewind_test_key} counter after rewind", () => + { + rewindTestKeyCounterKeyboard.Clock = counterClock; + return rewindTestKeyCounterKeyboard.CountPresses == 0; + }); + Add(kc); } } diff --git a/osu.Game/Screens/Play/KeyCounter.cs b/osu.Game/Screens/Play/KeyCounter.cs index 031fe11f88..cb9ff28aff 100644 --- a/osu.Game/Screens/Play/KeyCounter.cs +++ b/osu.Game/Screens/Play/KeyCounter.cs @@ -152,11 +152,6 @@ namespace osu.Game.Screens.Play RestoreState(Clock.CurrentTime); } - public void RestoreState(double time) - { - var targetState = states.LastOrDefault(state => state.Time <= time) ?? states.LastOrDefault(); - var targetCount = targetState?.Count ?? 0; - CountPresses = targetCount; - } + public void RestoreState(double time) => CountPresses = states.LastOrDefault(state => state.Time <= time)?.Count ?? 0; } }