osu/osu.Game.Tests/Visual/Gameplay/TestSceneBreakTracker.cs

197 lines
6.2 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-04-13 09:19:50 +00:00
using System.Collections.Generic;
2019-07-27 12:51:14 +00:00
using System.Linq;
2018-04-13 09:19:50 +00:00
using NUnit.Framework;
using osu.Framework.Graphics;
2019-07-27 12:51:14 +00:00
using osu.Framework.Timing;
2019-03-24 16:02:36 +00:00
using osu.Game.Beatmaps.Timing;
2018-04-13 09:19:50 +00:00
using osu.Game.Screens.Play;
2019-03-24 16:02:36 +00:00
namespace osu.Game.Tests.Visual.Gameplay
2018-04-13 09:19:50 +00:00
{
[TestFixture]
public class TestSceneBreakTracker : OsuTestScene
2018-04-13 09:19:50 +00:00
{
private readonly BreakOverlay breakOverlay;
private readonly TestBreakTracker breakTracker;
2019-07-27 12:51:14 +00:00
private readonly IReadOnlyList<BreakPeriod> testBreaks = new List<BreakPeriod>
{
new BreakPeriod(1000, 5000),
new BreakPeriod(6000, 13500),
2019-07-27 12:51:14 +00:00
};
2018-04-13 09:19:50 +00:00
public TestSceneBreakTracker()
{
AddRange(new Drawable[]
{
breakTracker = new TestBreakTracker(),
2020-03-28 03:18:28 +00:00
breakOverlay = new BreakOverlay(true, null)
{
ProcessCustomClock = false,
}
});
}
protected override void Update()
2018-04-13 09:19:50 +00:00
{
base.Update();
breakOverlay.Clock = breakTracker.Clock;
2019-07-27 12:51:14 +00:00
}
2019-07-25 13:24:05 +00:00
2019-07-27 12:51:14 +00:00
[Test]
public void TestShowBreaks()
{
2019-07-30 09:31:57 +00:00
setClock(false);
2018-04-13 09:19:50 +00:00
2019-07-27 12:51:14 +00:00
addShowBreakStep(2);
addShowBreakStep(5);
addShowBreakStep(15);
2018-04-13 09:19:50 +00:00
}
2019-07-27 12:51:14 +00:00
[Test]
public void TestNoEffectsBreak()
2018-04-13 09:19:50 +00:00
{
var shortBreak = new BreakPeriod(0, 500);
2019-07-27 12:51:14 +00:00
2019-07-30 09:31:57 +00:00
setClock(true);
loadBreaksStep("short break", new[] { shortBreak });
2019-07-27 12:51:14 +00:00
addBreakSeeks(shortBreak, false);
2018-04-13 09:19:50 +00:00
}
2019-07-27 12:51:14 +00:00
[Test]
public void TestMultipleBreaks()
2018-04-13 09:19:50 +00:00
{
2019-07-30 09:31:57 +00:00
setClock(true);
loadBreaksStep("multiple breaks", testBreaks);
2019-07-27 12:51:14 +00:00
foreach (var b in testBreaks)
addBreakSeeks(b, false);
}
2018-04-13 09:19:50 +00:00
2019-07-27 12:51:14 +00:00
[Test]
public void TestRewindBreaks()
{
2019-07-30 09:31:57 +00:00
setClock(true);
loadBreaksStep("multiple breaks", testBreaks);
2019-07-27 12:51:14 +00:00
foreach (var b in testBreaks.Reverse())
addBreakSeeks(b, true);
}
[Test]
public void TestSkipBreaks()
{
2019-07-30 09:31:57 +00:00
setClock(true);
loadBreaksStep("multiple breaks", testBreaks);
2019-07-27 12:51:14 +00:00
seekAndAssertBreak("seek to break start", testBreaks[1].StartTime, true);
seekAndAssertBreak("seek to break middle", testBreaks[1].StartTime + testBreaks[1].Duration / 2, true);
seekAndAssertBreak("seek to break end", testBreaks[1].EndTime, false);
seekAndAssertBreak("seek to break after end", testBreaks[1].EndTime + 500, false);
2019-07-27 12:51:14 +00:00
}
[TestCase(true)]
[TestCase(false)]
public void TestBeforeGameplayStart(bool withBreaks)
{
setClock(true);
if (withBreaks)
loadBreaksStep("multiple breaks", testBreaks);
seekAndAssertBreak("seek to break intro time", -100, true);
seekAndAssertBreak("seek to break intro time", 0, false);
}
2019-07-27 12:51:14 +00:00
private void addShowBreakStep(double seconds)
{
AddStep($"show '{seconds}s' break", () =>
2018-04-13 09:19:50 +00:00
{
breakOverlay.Breaks = breakTracker.Breaks = new List<BreakPeriod>
2018-04-13 09:19:50 +00:00
{
new BreakPeriod(Clock.CurrentTime, Clock.CurrentTime + seconds * 1000)
};
2019-07-27 12:51:14 +00:00
});
2018-04-13 09:19:50 +00:00
}
2019-07-30 09:31:57 +00:00
private void setClock(bool useManual)
2018-04-13 09:19:50 +00:00
{
AddStep($"set {(useManual ? "manual" : "realtime")} clock", () => breakTracker.SwitchClock(useManual));
2019-07-27 12:51:14 +00:00
}
2018-04-13 09:19:50 +00:00
private void loadBreaksStep(string breakDescription, IReadOnlyList<BreakPeriod> breaks)
{
AddStep($"load {breakDescription}", () => breakOverlay.Breaks = breakTracker.Breaks = breaks);
2019-07-30 09:31:57 +00:00
seekAndAssertBreak("seek back to 0", 0, false);
}
2019-07-27 12:51:14 +00:00
private void addBreakSeeks(BreakPeriod b, bool isReversed)
{
if (isReversed)
2018-04-13 09:19:50 +00:00
{
2019-07-30 09:31:57 +00:00
seekAndAssertBreak("seek to break after end", b.EndTime + 500, false);
seekAndAssertBreak("seek to break end", b.EndTime, false);
seekAndAssertBreak("seek to break middle", b.StartTime + b.Duration / 2, b.HasEffect);
seekAndAssertBreak("seek to break start", b.StartTime, b.HasEffect);
2019-07-27 12:51:14 +00:00
}
else
{
2019-07-30 09:31:57 +00:00
seekAndAssertBreak("seek to break start", b.StartTime, b.HasEffect);
seekAndAssertBreak("seek to break middle", b.StartTime + b.Duration / 2, b.HasEffect);
seekAndAssertBreak("seek to break end", b.EndTime, false);
seekAndAssertBreak("seek to break after end", b.EndTime + 500, false);
2019-07-27 12:51:14 +00:00
}
}
2019-07-30 09:31:57 +00:00
private void seekAndAssertBreak(string seekStepDescription, double time, bool shouldBeBreak)
2019-07-27 12:51:14 +00:00
{
AddStep(seekStepDescription, () => breakTracker.ManualClockTime = time);
2019-07-30 09:31:57 +00:00
AddAssert($"is{(!shouldBeBreak ? " not" : string.Empty)} break time", () =>
{
breakTracker.ProgressTime();
return breakTracker.IsBreakTime.Value == shouldBeBreak;
2019-07-30 09:31:57 +00:00
});
2018-04-13 09:19:50 +00:00
}
private class TestBreakTracker : BreakTracker
{
public readonly FramedClock FramedManualClock;
private readonly ManualClock manualClock;
2019-07-30 09:31:57 +00:00
private IFrameBasedClock originalClock;
public double ManualClockTime
{
get => manualClock.CurrentTime;
set => manualClock.CurrentTime = value;
}
public TestBreakTracker()
{
FramedManualClock = new FramedClock(manualClock = new ManualClock());
2019-07-30 09:31:57 +00:00
ProcessCustomClock = false;
}
public void ProgressTime()
{
FramedManualClock.ProcessFrame();
2019-07-30 09:31:57 +00:00
Update();
}
public void SwitchClock(bool setManual) => Clock = setManual ? FramedManualClock : originalClock;
protected override void LoadComplete()
{
base.LoadComplete();
2019-07-30 09:31:57 +00:00
originalClock = Clock;
}
}
2018-04-13 09:19:50 +00:00
}
}