1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-12 01:47:24 +08:00
osu-lazer/osu.Game.Tests/Visual/Components/TestScenePreviewTrackManager.cs

131 lines
4.4 KiB
C#
Raw Normal View History

2019-05-29 15:43:27 +08:00
// 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-06-21 18:31:07 +08:00
using NUnit.Framework;
using osu.Framework.Allocation;
using osu.Framework.Audio.Track;
using osu.Framework.Graphics.Containers;
using osu.Game.Audio;
using osu.Game.Beatmaps;
2019-03-25 00:02:36 +08:00
namespace osu.Game.Tests.Visual.Components
2018-06-21 18:31:07 +08:00
{
public class TestScenePreviewTrackManager : OsuTestScene, IPreviewTrackOwner
2018-06-21 18:31:07 +08:00
{
private readonly PreviewTrackManager trackManager = new TestPreviewTrackManager();
2018-07-11 16:07:14 +08:00
protected override IReadOnlyDependencyContainer CreateChildDependencies(IReadOnlyDependencyContainer parent)
2018-06-21 18:31:07 +08:00
{
2018-07-11 16:07:14 +08:00
var dependencies = new DependencyContainer(base.CreateChildDependencies(parent));
2018-06-21 18:31:07 +08:00
dependencies.CacheAs(trackManager);
dependencies.CacheAs<IPreviewTrackOwner>(this);
return dependencies;
}
[BackgroundDependencyLoader]
private void load()
{
2018-08-24 14:50:39 +08:00
Add(trackManager);
2018-06-21 18:31:07 +08:00
}
[Test]
public void TestStartStop()
{
PreviewTrack track = null;
AddStep("get track", () => track = getOwnedTrack());
AddStep("start", () => track.Start());
AddAssert("started", () => track.IsRunning);
AddStep("stop", () => track.Stop());
AddAssert("stopped", () => !track.IsRunning);
}
[Test]
public void TestStartMultipleTracks()
{
PreviewTrack track1 = null;
PreviewTrack track2 = null;
AddStep("get tracks", () =>
{
track1 = getOwnedTrack();
track2 = getOwnedTrack();
});
AddStep("start track 1", () => track1.Start());
AddStep("start track 2", () => track2.Start());
AddAssert("track 1 stopped", () => !track1.IsRunning);
AddAssert("track 2 started", () => track2.IsRunning);
}
[Test]
public void TestCancelFromOwner()
{
PreviewTrack track = null;
AddStep("get track", () => track = getOwnedTrack());
AddStep("start", () => track.Start());
AddStep("stop by owner", () => trackManager.StopAnyPlaying(this));
2018-06-21 18:31:07 +08:00
AddAssert("stopped", () => !track.IsRunning);
}
[Test]
public void TestCancelFromNonOwner()
{
TestTrackOwner owner = null;
PreviewTrack track = null;
2018-08-24 14:50:39 +08:00
AddStep("get track", () => Add(owner = new TestTrackOwner(track = getTrack())));
2018-06-21 18:31:07 +08:00
AddStep("start", () => track.Start());
AddStep("attempt stop", () => trackManager.StopAnyPlaying(this));
2018-06-21 18:31:07 +08:00
AddAssert("not stopped", () => track.IsRunning);
AddStep("stop by true owner", () => trackManager.StopAnyPlaying(owner));
2018-06-21 18:31:07 +08:00
AddAssert("stopped", () => !track.IsRunning);
}
private PreviewTrack getTrack() => trackManager.Get(null);
private PreviewTrack getOwnedTrack()
{
var track = getTrack();
2018-08-24 14:50:39 +08:00
Add(track);
2018-06-21 18:31:07 +08:00
return track;
}
private class TestTrackOwner : CompositeDrawable, IPreviewTrackOwner
{
public TestTrackOwner(PreviewTrack track)
{
AddInternal(track);
}
2018-07-11 16:07:14 +08:00
protected override IReadOnlyDependencyContainer CreateChildDependencies(IReadOnlyDependencyContainer parent)
2018-06-21 18:31:07 +08:00
{
2018-07-11 16:07:14 +08:00
var dependencies = new DependencyContainer(base.CreateChildDependencies(parent));
2018-06-21 18:31:07 +08:00
dependencies.CacheAs<IPreviewTrackOwner>(this);
return dependencies;
}
}
private class TestPreviewTrackManager : PreviewTrackManager
{
2019-05-29 15:43:15 +08:00
protected override TrackManagerPreviewTrack CreatePreviewTrack(BeatmapSetInfo beatmapSetInfo, ITrackStore trackStore) => new TestPreviewTrack(beatmapSetInfo, trackStore);
2018-06-21 18:31:07 +08:00
protected class TestPreviewTrack : TrackManagerPreviewTrack
{
2019-05-29 15:43:27 +08:00
private readonly ITrackStore trackManager;
2019-05-29 15:43:15 +08:00
public TestPreviewTrack(BeatmapSetInfo beatmapSetInfo, ITrackStore trackManager)
2018-06-21 18:31:07 +08:00
: base(beatmapSetInfo, trackManager)
{
2019-05-29 15:43:27 +08:00
this.trackManager = trackManager;
2018-06-21 18:31:07 +08:00
}
2019-05-29 15:43:27 +08:00
protected override Track GetTrack() => trackManager.GetVirtual(100000);
2018-06-21 18:31:07 +08:00
}
}
}
}