1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-27 00:13:00 +08:00
osu-lazer/osu.Game.Tests/Visual/TestCaseKeyCounter.cs

104 lines
3.4 KiB
C#
Raw Normal View History

2018-09-15 15:25:37 +08:00
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
2018-04-13 17:19:50 +08:00
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
2018-09-15 15:25:37 +08:00
using System;
using System.Collections.Generic;
using System.Linq;
2018-04-13 17:19:50 +08:00
using NUnit.Framework;
using osu.Framework.Graphics;
using osu.Framework.MathUtils;
2018-07-29 04:24:03 +08:00
using osu.Framework.Timing;
2018-04-13 17:19:50 +08:00
using osu.Game.Screens.Play;
using OpenTK.Input;
namespace osu.Game.Tests.Visual
{
[TestFixture]
public class TestCaseKeyCounter : ManualInputManagerTestCase
2018-04-13 17:19:50 +08:00
{
2018-09-15 15:24:06 +08:00
public override IReadOnlyList<Type> RequiredTypes => new[]
{
typeof(KeyCounterKeyboard),
typeof(KeyCounterMouse),
typeof(KeyCounterCollection)
};
2018-07-29 04:24:03 +08:00
2018-04-13 17:19:50 +08:00
public TestCaseKeyCounter()
{
2018-07-29 04:24:03 +08:00
KeyCounterKeyboard rewindTestKeyCounterKeyboard;
2018-04-13 17:19:50 +08:00
KeyCounterCollection kc = new KeyCounterCollection
{
Origin = Anchor.Centre,
Anchor = Anchor.Centre,
Children = new KeyCounter[]
{
2018-09-15 15:25:37 +08:00
rewindTestKeyCounterKeyboard = new KeyCounterKeyboard(Key.X),
2018-04-13 17:19:50 +08:00
new KeyCounterKeyboard(Key.X),
new KeyCounterMouse(MouseButton.Left),
new KeyCounterMouse(MouseButton.Right),
},
};
2018-09-15 15:25:37 +08:00
2018-04-13 17:19:50 +08:00
AddStep("Add random", () =>
{
Key key = (Key)((int)Key.A + RNG.Next(26));
kc.Add(new KeyCounterKeyboard(key));
});
AddSliderStep("Fade time", 0, 200, 50, v => kc.FadeTime = v);
2018-09-15 15:25:37 +08:00
Key testKey = ((KeyCounterKeyboard)kc.Children.First()).Key;
double time1 = 0;
AddStep($"Press {testKey} key", () =>
2018-07-29 04:24:03 +08:00
{
InputManager.PressKey(testKey);
InputManager.ReleaseKey(testKey);
2018-07-29 04:24:03 +08:00
});
2018-09-15 15:25:37 +08:00
AddAssert($"Check {testKey} counter after keypress", () => rewindTestKeyCounterKeyboard.CountPresses == 1);
2018-07-29 04:24:03 +08:00
2018-09-15 15:25:37 +08:00
AddStep($"Press {testKey} key", () =>
2018-07-29 04:24:03 +08:00
{
InputManager.PressKey(testKey);
InputManager.ReleaseKey(testKey);
2018-09-15 15:25:37 +08:00
time1 = Clock.CurrentTime;
2018-07-29 04:24:03 +08:00
});
2018-09-15 15:25:37 +08:00
AddAssert($"Check {testKey} counter after keypress", () => rewindTestKeyCounterKeyboard.CountPresses == 2);
IFrameBasedClock oldClock = null;
AddStep($"Rewind {testKey} counter once", () =>
2018-07-29 04:24:03 +08:00
{
2018-09-15 15:25:37 +08:00
oldClock = rewindTestKeyCounterKeyboard.Clock;
rewindTestKeyCounterKeyboard.Clock = new FramedOffsetClock(new FixedClock(time1 - 10));
2018-07-29 04:24:03 +08:00
});
2018-09-15 15:25:37 +08:00
AddAssert($"Check {testKey} counter after rewind", () => rewindTestKeyCounterKeyboard.CountPresses == 1);
AddStep($"Rewind {testKey} counter to zero", () => rewindTestKeyCounterKeyboard.Clock = new FramedOffsetClock(new FixedClock(0)));
AddAssert($"Check {testKey} counter after rewind", () => rewindTestKeyCounterKeyboard.CountPresses == 0);
AddStep("Restore clock", () => rewindTestKeyCounterKeyboard.Clock = oldClock);
2018-04-13 17:19:50 +08:00
Add(kc);
}
2018-09-15 15:25:37 +08:00
private class FixedClock : IClock
{
private readonly double time;
public FixedClock(double time)
{
this.time = time;
}
public double CurrentTime => time;
public double Rate => 1;
public bool IsRunning => false;
}
2018-04-13 17:19:50 +08:00
}
}