mirror of
https://github.com/ppy/osu.git
synced 2024-11-12 04:49:40 +08:00
132 lines
4.4 KiB
C#
132 lines
4.4 KiB
C#
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
|
|
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
|
|
|
|
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;
|
|
|
|
namespace osu.Game.Tests.Visual
|
|
{
|
|
public class TestCasePreviewTrackManager : OsuTestCase, IPreviewTrackOwner
|
|
{
|
|
private readonly PreviewTrackManager trackManager = new TestPreviewTrackManager();
|
|
|
|
protected override IReadOnlyDependencyContainer CreateLocalDependencies(IReadOnlyDependencyContainer parent)
|
|
{
|
|
var dependencies = new DependencyContainer(base.CreateLocalDependencies(parent));
|
|
dependencies.CacheAs(trackManager);
|
|
dependencies.CacheAs<IPreviewTrackOwner>(this);
|
|
return dependencies;
|
|
}
|
|
|
|
[BackgroundDependencyLoader]
|
|
private void load()
|
|
{
|
|
AddInternal(trackManager);
|
|
}
|
|
|
|
[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.Stop(this));
|
|
AddAssert("stopped", () => !track.IsRunning);
|
|
}
|
|
|
|
[Test]
|
|
public void TestCancelFromNonOwner()
|
|
{
|
|
TestTrackOwner owner = null;
|
|
PreviewTrack track = null;
|
|
|
|
AddStep("get track", () => AddInternal(owner = new TestTrackOwner(track = getTrack())));
|
|
AddStep("start", () => track.Start());
|
|
AddStep("attempt stop", () => trackManager.Stop(this));
|
|
AddAssert("not stopped", () => track.IsRunning);
|
|
AddStep("stop by true owner", () => trackManager.Stop(owner));
|
|
AddAssert("stopped", () => !track.IsRunning);
|
|
}
|
|
|
|
private PreviewTrack getTrack() => trackManager.Get(null);
|
|
|
|
private PreviewTrack getOwnedTrack()
|
|
{
|
|
var track = getTrack();
|
|
|
|
AddInternal(track);
|
|
|
|
return track;
|
|
}
|
|
|
|
private class TestTrackOwner : CompositeDrawable, IPreviewTrackOwner
|
|
{
|
|
private readonly PreviewTrack track;
|
|
|
|
public TestTrackOwner(PreviewTrack track)
|
|
{
|
|
this.track = track;
|
|
|
|
AddInternal(track);
|
|
}
|
|
|
|
protected override IReadOnlyDependencyContainer CreateLocalDependencies(IReadOnlyDependencyContainer parent)
|
|
{
|
|
var dependencies = new DependencyContainer(base.CreateLocalDependencies(parent));
|
|
dependencies.CacheAs<IPreviewTrackOwner>(this);
|
|
return dependencies;
|
|
}
|
|
}
|
|
|
|
private class TestPreviewTrackManager : PreviewTrackManager
|
|
{
|
|
protected override TrackManagerPreviewTrack CreatePreviewTrack(BeatmapSetInfo beatmapSetInfo, TrackManager trackManager) => new TestPreviewTrack(beatmapSetInfo, trackManager);
|
|
|
|
protected class TestPreviewTrack : TrackManagerPreviewTrack
|
|
{
|
|
public TestPreviewTrack(BeatmapSetInfo beatmapSetInfo, TrackManager trackManager)
|
|
: base(beatmapSetInfo, trackManager)
|
|
{
|
|
}
|
|
|
|
protected override Track GetTrack() => new TrackVirtual { Length = 100000 };
|
|
}
|
|
}
|
|
}
|
|
}
|