1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-27 15:32:57 +08:00
osu-lazer/osu.Game.Tests/Visual/Components/TestScenePollingComponent.cs

152 lines
4.4 KiB
C#
Raw Normal View History

// 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.
2018-12-10 20:08:14 +08:00
2022-06-17 15:37:17 +08:00
#nullable disable
2018-12-10 20:08:14 +08:00
using System;
using System.Threading.Tasks;
2018-12-11 19:12:30 +08:00
using NUnit.Framework;
2018-12-10 20:08:14 +08:00
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Shapes;
2018-12-11 19:12:30 +08:00
using osu.Framework.Logging;
using osu.Framework.Testing;
2018-12-10 20:08:14 +08:00
using osu.Game.Graphics.Sprites;
using osu.Game.Online;
using osuTK;
using osuTK.Graphics;
2019-03-25 00:02:36 +08:00
namespace osu.Game.Tests.Visual.Components
2018-12-10 20:08:14 +08:00
{
[HeadlessTest]
public class TestScenePollingComponent : OsuTestScene
2018-12-10 20:08:14 +08:00
{
private Container pollBox;
private TestPoller poller;
2018-12-11 19:12:30 +08:00
private const float safety_adjust = 1f;
private int count;
[SetUp]
public void SetUp() => Schedule(() =>
2018-12-10 20:08:14 +08:00
{
2018-12-11 19:12:30 +08:00
count = 0;
2018-12-10 20:08:14 +08:00
Children = new Drawable[]
{
pollBox = new Container
{
Alpha = 0,
RelativeSizeAxes = Axes.Both,
Children = new Drawable[]
{
new Box
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Scale = new Vector2(0.4f),
Colour = Color4.LimeGreen,
RelativeSizeAxes = Axes.Both,
},
new OsuSpriteText
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Text = "Poll!",
}
}
}
};
2018-12-11 19:12:30 +08:00
});
2018-12-10 20:08:14 +08:00
2018-12-14 16:42:11 +08:00
[Test]
2019-02-22 15:59:52 +08:00
[Ignore("polling is threaded, and it's very hard to hook into it correctly")]
2018-12-11 19:12:30 +08:00
public void TestInstantPolling()
{
createPoller(true);
2018-12-10 20:08:14 +08:00
2020-12-20 17:34:54 +08:00
AddStep("set poll interval to 1", () => poller.TimeBetweenPolls.Value = TimePerAction * safety_adjust);
2018-12-10 20:08:14 +08:00
checkCount(1);
checkCount(2);
checkCount(3);
2020-12-20 17:34:54 +08:00
AddStep("set poll interval to 5", () => poller.TimeBetweenPolls.Value = TimePerAction * safety_adjust * 5);
2018-12-10 20:08:14 +08:00
checkCount(4);
checkCount(4);
checkCount(4);
2018-12-11 19:12:30 +08:00
skip();
2018-12-10 20:08:14 +08:00
checkCount(5);
checkCount(5);
2020-12-20 17:34:54 +08:00
AddStep("set poll interval to 1", () => poller.TimeBetweenPolls.Value = TimePerAction * safety_adjust);
2018-12-11 19:12:30 +08:00
checkCount(6);
checkCount(7);
}
[Test]
2018-12-14 16:42:11 +08:00
[Ignore("i have no idea how to fix the timing of this one")]
2018-12-11 19:12:30 +08:00
public void TestSlowPolling()
{
createPoller(false);
2020-12-20 17:34:54 +08:00
AddStep("set poll interval to 1", () => poller.TimeBetweenPolls.Value = TimePerAction * safety_adjust * 5);
2018-12-11 19:12:30 +08:00
checkCount(0);
skip();
checkCount(0);
skip();
skip();
checkCount(0);
skip();
skip();
checkCount(0);
}
2018-12-10 20:08:14 +08:00
2018-12-11 19:12:30 +08:00
private void skip() => AddStep("skip", () =>
{
// could be 4 or 5 at this point due to timing discrepancies (safety_adjust @ 0.2 * 5 ~= 1)
// easiest to just ignore the value at this point and move on.
});
2018-12-10 20:08:14 +08:00
2018-12-11 19:12:30 +08:00
private void checkCount(int checkValue)
{
2019-02-22 15:59:52 +08:00
AddAssert($"count is {checkValue}", () =>
{
Logger.Log($"value is {count}");
return count == checkValue;
});
2018-12-10 20:08:14 +08:00
}
2018-12-11 19:12:30 +08:00
private void createPoller(bool instant) => AddStep("create poller", () =>
{
poller?.Expire();
Add(poller = instant ? new TestPoller() : new TestSlowPoller());
poller.OnPoll += () =>
{
pollBox.FadeOutFromOne(500);
count++;
};
});
2018-12-14 16:42:11 +08:00
protected override double TimePerAction => 500;
2018-12-10 20:08:14 +08:00
public class TestPoller : PollingComponent
{
public event Action OnPoll;
protected override Task Poll()
{
2018-12-11 19:12:30 +08:00
Schedule(() => OnPoll?.Invoke());
2018-12-10 20:08:14 +08:00
return base.Poll();
}
}
2018-12-11 19:12:30 +08:00
public class TestSlowPoller : TestPoller
{
2020-12-20 17:34:54 +08:00
protected override Task Poll() => Task.Delay((int)(TimeBetweenPolls.Value / 2f / Clock.Rate)).ContinueWith(_ => base.Poll());
2018-12-11 19:12:30 +08:00
}
2018-12-10 20:08:14 +08:00
}
}