mirror of
https://github.com/ppy/osu.git
synced 2025-02-01 21:22:57 +08:00
Add more tests
This commit is contained in:
parent
ea4dce8454
commit
ebd9375780
@ -3,10 +3,11 @@
|
|||||||
|
|
||||||
using System;
|
using System;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using osu.Framework.Allocation;
|
using NUnit.Framework;
|
||||||
using osu.Framework.Graphics;
|
using osu.Framework.Graphics;
|
||||||
using osu.Framework.Graphics.Containers;
|
using osu.Framework.Graphics.Containers;
|
||||||
using osu.Framework.Graphics.Shapes;
|
using osu.Framework.Graphics.Shapes;
|
||||||
|
using osu.Framework.Logging;
|
||||||
using osu.Game.Graphics.Sprites;
|
using osu.Game.Graphics.Sprites;
|
||||||
using osu.Game.Online;
|
using osu.Game.Online;
|
||||||
using osuTK;
|
using osuTK;
|
||||||
@ -19,12 +20,16 @@ namespace osu.Game.Tests.Visual
|
|||||||
private Container pollBox;
|
private Container pollBox;
|
||||||
private TestPoller poller;
|
private TestPoller poller;
|
||||||
|
|
||||||
[BackgroundDependencyLoader]
|
private const float safety_adjust = 1f;
|
||||||
private void load()
|
private int count;
|
||||||
|
|
||||||
|
[SetUp]
|
||||||
|
public void SetUp() => Schedule(() =>
|
||||||
{
|
{
|
||||||
|
count = 0;
|
||||||
|
|
||||||
Children = new Drawable[]
|
Children = new Drawable[]
|
||||||
{
|
{
|
||||||
poller = new TestPoller(),
|
|
||||||
pollBox = new Container
|
pollBox = new Container
|
||||||
{
|
{
|
||||||
Alpha = 0,
|
Alpha = 0,
|
||||||
@ -48,41 +53,75 @@ namespace osu.Game.Tests.Visual
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
});
|
||||||
|
|
||||||
int count = 0;
|
//[Test]
|
||||||
|
public void TestInstantPolling()
|
||||||
|
{
|
||||||
|
createPoller(true);
|
||||||
|
|
||||||
|
AddStep("set poll interval to 1", () => poller.TimeBetweenPolls = TimePerAction * safety_adjust);
|
||||||
|
checkCount(1);
|
||||||
|
checkCount(2);
|
||||||
|
checkCount(3);
|
||||||
|
|
||||||
|
AddStep("set poll interval to 5", () => poller.TimeBetweenPolls = TimePerAction * safety_adjust * 5);
|
||||||
|
checkCount(4);
|
||||||
|
checkCount(4);
|
||||||
|
checkCount(4);
|
||||||
|
|
||||||
|
skip();
|
||||||
|
|
||||||
|
checkCount(5);
|
||||||
|
checkCount(5);
|
||||||
|
|
||||||
|
AddStep("set poll interval to 1", () => poller.TimeBetweenPolls = TimePerAction * safety_adjust);
|
||||||
|
checkCount(6);
|
||||||
|
checkCount(7);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void TestSlowPolling()
|
||||||
|
{
|
||||||
|
createPoller(false);
|
||||||
|
|
||||||
|
AddStep("set poll interval to 1", () => poller.TimeBetweenPolls = TimePerAction * safety_adjust * 5);
|
||||||
|
checkCount(0);
|
||||||
|
skip();
|
||||||
|
checkCount(0);
|
||||||
|
skip();
|
||||||
|
skip();
|
||||||
|
checkCount(0);
|
||||||
|
skip();
|
||||||
|
skip();
|
||||||
|
checkCount(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
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.
|
||||||
|
});
|
||||||
|
|
||||||
|
private void checkCount(int checkValue)
|
||||||
|
{
|
||||||
|
Logger.Log($"value is {count}");
|
||||||
|
AddAssert($"count is {checkValue}", () => count == checkValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void createPoller(bool instant) => AddStep("create poller", () =>
|
||||||
|
{
|
||||||
|
poller?.Expire();
|
||||||
|
|
||||||
|
Add(poller = instant ? new TestPoller() : new TestSlowPoller());
|
||||||
poller.OnPoll += () =>
|
poller.OnPoll += () =>
|
||||||
{
|
{
|
||||||
pollBox.FadeOutFromOne(500);
|
pollBox.FadeOutFromOne(500);
|
||||||
count++;
|
count++;
|
||||||
};
|
};
|
||||||
|
});
|
||||||
|
|
||||||
AddStep("set poll to 1 second", () => poller.TimeBetweenPolls = TimePerAction);
|
protected override double TimePerAction => 500000;
|
||||||
|
|
||||||
void checkCount(int checkValue) => AddAssert($"count is {checkValue}", () => count == checkValue);
|
|
||||||
|
|
||||||
checkCount(1);
|
|
||||||
checkCount(2);
|
|
||||||
checkCount(3);
|
|
||||||
|
|
||||||
AddStep("set poll to 5 second", () => poller.TimeBetweenPolls = TimePerAction * 5);
|
|
||||||
|
|
||||||
checkCount(4);
|
|
||||||
checkCount(4);
|
|
||||||
checkCount(4);
|
|
||||||
checkCount(4);
|
|
||||||
|
|
||||||
checkCount(5);
|
|
||||||
checkCount(5);
|
|
||||||
checkCount(5);
|
|
||||||
|
|
||||||
AddStep("set poll to 5 second", () => poller.TimeBetweenPolls = TimePerAction);
|
|
||||||
|
|
||||||
AddAssert("count is 6", () => count == 6);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
protected override double TimePerAction => 500;
|
|
||||||
|
|
||||||
public class TestPoller : PollingComponent
|
public class TestPoller : PollingComponent
|
||||||
{
|
{
|
||||||
@ -90,9 +129,14 @@ namespace osu.Game.Tests.Visual
|
|||||||
|
|
||||||
protected override Task Poll()
|
protected override Task Poll()
|
||||||
{
|
{
|
||||||
OnPoll?.Invoke();
|
Schedule(() => OnPoll?.Invoke());
|
||||||
return base.Poll();
|
return base.Poll();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public class TestSlowPoller : TestPoller
|
||||||
|
{
|
||||||
|
protected override Task Poll() => Task.Delay((int)(TimeBetweenPolls / 2f / Clock.Rate)).ContinueWith(_ => base.Poll());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user