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

181 lines
5.5 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
2019-07-25 13:24:05 +00:00
using System;
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;
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 TestSceneBreakOverlay : OsuTestScene
2018-04-13 09:19:50 +00:00
{
2019-07-25 13:24:05 +00:00
public override IReadOnlyList<Type> RequiredTypes => new[]
{
typeof(BreakOverlay),
};
private readonly TestBreakOverlay breakOverlay;
2019-07-27 12:51:14 +00:00
private readonly IReadOnlyList<BreakPeriod> testBreaks = new List<BreakPeriod>
{
new BreakPeriod
{
StartTime = 1000,
EndTime = 5000,
},
new BreakPeriod
{
StartTime = 6000,
EndTime = 13500,
},
};
2018-04-13 09:19:50 +00:00
public TestSceneBreakOverlay()
2018-04-13 09:19:50 +00:00
{
Add(breakOverlay = new TestBreakOverlay(true));
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
{
2019-07-27 12:51:14 +00:00
var shortBreak = new BreakPeriod { EndTime = 500 };
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
2019-07-28 02:22:09 +00:00
addBreakSeeks(testBreaks.Last(), false);
2019-07-27 12:51:14 +00:00
}
private void addShowBreakStep(double seconds)
{
AddStep($"show '{seconds}s' break", () => breakOverlay.Breaks = new List<BreakPeriod>
2018-04-13 09:19:50 +00:00
{
new BreakPeriod
{
2019-07-27 12:51:14 +00:00
StartTime = Clock.CurrentTime,
EndTime = Clock.CurrentTime + seconds * 1000,
2018-04-13 09:19:50 +00:00
}
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
{
2019-07-30 09:31:57 +00:00
AddStep($"set {(useManual ? "manual" : "realtime")} clock", () => breakOverlay.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 = 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, () => breakOverlay.ManualClockTime = time);
2019-07-30 09:31:57 +00:00
AddAssert($"is{(!shouldBeBreak ? " not" : string.Empty)} break time", () =>
{
breakOverlay.ProgressTime();
return breakOverlay.IsBreakTime.Value == shouldBeBreak;
});
2018-04-13 09:19:50 +00:00
}
private class TestBreakOverlay : BreakOverlay
{
private readonly FramedClock framedManualClock;
private readonly ManualClock manualClock;
2019-07-30 09:31:57 +00:00
private IFrameBasedClock originalClock;
2019-07-30 10:29:41 +00:00
public new int CurrentBreakIndex => base.CurrentBreakIndex;
public double ManualClockTime
{
get => manualClock.CurrentTime;
set => manualClock.CurrentTime = value;
}
public TestBreakOverlay(bool letterboxing)
: base(letterboxing)
{
framedManualClock = new FramedClock(manualClock = new ManualClock());
2019-07-30 09:31:57 +00:00
ProcessCustomClock = false;
}
public void ProgressTime()
{
framedManualClock.ProcessFrame();
Update();
}
2019-07-30 09:31:57 +00:00
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
}
}