2019-05-29 15:43:27 +08:00
|
|
|
|
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
|
2019-01-24 16:43:03 +08:00
|
|
|
|
// 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;
|
2019-11-15 06:08:40 +08:00
|
|
|
|
using osu.Framework.Audio;
|
2018-06-21 18:31:07 +08:00
|
|
|
|
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
|
|
|
|
{
|
2019-05-15 03:37:25 +08:00
|
|
|
|
public class TestScenePreviewTrackManager : OsuTestScene, IPreviewTrackOwner
|
2018-06-21 18:31:07 +08:00
|
|
|
|
{
|
2019-11-14 19:23:52 +08:00
|
|
|
|
private readonly TestPreviewTrackManager trackManager = new TestPreviewTrackManager();
|
2018-06-21 18:31:07 +08:00
|
|
|
|
|
2019-11-15 06:08:40 +08:00
|
|
|
|
private AudioManager audio;
|
|
|
|
|
|
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]
|
2019-11-15 06:08:40 +08:00
|
|
|
|
private void load(AudioManager audio)
|
2018-06-21 18:31:07 +08:00
|
|
|
|
{
|
2019-11-15 06:08:40 +08:00
|
|
|
|
this.audio = audio;
|
|
|
|
|
|
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());
|
2019-11-06 18:06:24 +08:00
|
|
|
|
AddUntilStep("wait loaded", () => track.IsLoaded);
|
2018-06-21 18:31:07 +08:00
|
|
|
|
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();
|
|
|
|
|
});
|
|
|
|
|
|
2019-11-06 18:06:24 +08:00
|
|
|
|
AddUntilStep("wait loaded", () => track1.IsLoaded && track2.IsLoaded);
|
|
|
|
|
|
2018-06-21 18:31:07 +08:00
|
|
|
|
AddStep("start track 1", () => track1.Start());
|
|
|
|
|
AddStep("start track 2", () => track2.Start());
|
|
|
|
|
AddAssert("track 1 stopped", () => !track1.IsRunning);
|
|
|
|
|
AddAssert("track 2 started", () => track2.IsRunning);
|
2019-11-08 18:20:02 +08:00
|
|
|
|
AddStep("start track 1", () => track1.Start());
|
|
|
|
|
AddAssert("track 2 stopped", () => !track2.IsRunning);
|
|
|
|
|
AddAssert("track 1 started", () => track1.IsRunning);
|
2018-06-21 18:31:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void TestCancelFromOwner()
|
|
|
|
|
{
|
|
|
|
|
PreviewTrack track = null;
|
|
|
|
|
|
|
|
|
|
AddStep("get track", () => track = getOwnedTrack());
|
2019-11-06 18:06:24 +08:00
|
|
|
|
AddUntilStep("wait loaded", () => track.IsLoaded);
|
2018-06-21 18:31:07 +08:00
|
|
|
|
AddStep("start", () => track.Start());
|
2018-06-22 11:35:43 +08:00
|
|
|
|
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())));
|
2019-11-06 18:06:24 +08:00
|
|
|
|
AddUntilStep("wait loaded", () => track.IsLoaded);
|
2018-06-21 18:31:07 +08:00
|
|
|
|
AddStep("start", () => track.Start());
|
2018-06-22 11:35:43 +08:00
|
|
|
|
AddStep("attempt stop", () => trackManager.StopAnyPlaying(this));
|
2018-06-21 18:31:07 +08:00
|
|
|
|
AddAssert("not stopped", () => track.IsRunning);
|
2018-06-22 11:35:43 +08:00
|
|
|
|
AddStep("stop by true owner", () => trackManager.StopAnyPlaying(owner));
|
2018-06-21 18:31:07 +08:00
|
|
|
|
AddAssert("stopped", () => !track.IsRunning);
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-11 04:21:46 +08:00
|
|
|
|
[Test]
|
|
|
|
|
public void TestNonPresentTrack()
|
|
|
|
|
{
|
2021-01-02 19:25:16 +08:00
|
|
|
|
TestPreviewTrackManager.TestPreviewTrack track = null;
|
2019-11-11 04:21:46 +08:00
|
|
|
|
|
|
|
|
|
AddStep("get non-present track", () =>
|
|
|
|
|
{
|
|
|
|
|
Add(new TestTrackOwner(track = getTrack()));
|
|
|
|
|
track.Alpha = 0;
|
|
|
|
|
});
|
|
|
|
|
AddUntilStep("wait loaded", () => track.IsLoaded);
|
|
|
|
|
AddStep("start", () => track.Start());
|
|
|
|
|
AddStep("seek to end", () => track.Track.Seek(track.Track.Length));
|
|
|
|
|
AddAssert("track stopped", () => !track.IsRunning);
|
|
|
|
|
}
|
2018-06-21 18:31:07 +08:00
|
|
|
|
|
2019-11-14 19:23:52 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Ensures that <see cref="PreviewTrackManager.CurrentTrack"/> changes correctly.
|
|
|
|
|
/// </summary>
|
|
|
|
|
[Test]
|
|
|
|
|
public void TestCurrentTrackChanges()
|
|
|
|
|
{
|
|
|
|
|
PreviewTrack track = null;
|
|
|
|
|
TestTrackOwner owner = null;
|
|
|
|
|
|
|
|
|
|
AddStep("get track", () => Add(owner = new TestTrackOwner(track = getTrack())));
|
|
|
|
|
AddUntilStep("wait loaded", () => track.IsLoaded);
|
|
|
|
|
AddStep("start track", () => track.Start());
|
|
|
|
|
AddAssert("current is track", () => trackManager.CurrentTrack == track);
|
|
|
|
|
AddStep("pause manager updates", () => trackManager.AllowUpdate = false);
|
|
|
|
|
AddStep("stop any playing", () => trackManager.StopAnyPlaying(owner));
|
|
|
|
|
AddAssert("current not changed", () => trackManager.CurrentTrack == track);
|
|
|
|
|
AddStep("resume manager updates", () => trackManager.AllowUpdate = true);
|
|
|
|
|
AddAssert("current is null", () => trackManager.CurrentTrack == null);
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-15 06:08:40 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Ensures that <see cref="PreviewTrackManager"/> mutes game-wide audio tracks correctly.
|
|
|
|
|
/// </summary>
|
|
|
|
|
[TestCase(false)]
|
|
|
|
|
[TestCase(true)]
|
|
|
|
|
public void TestEnsureMutingCorrectly(bool stopAnyPlaying)
|
|
|
|
|
{
|
|
|
|
|
PreviewTrack track = null;
|
|
|
|
|
TestTrackOwner owner = null;
|
|
|
|
|
|
2019-11-21 09:35:10 +08:00
|
|
|
|
AddStep("ensure volume not zero", () =>
|
|
|
|
|
{
|
|
|
|
|
if (audio.Volume.Value == 0)
|
|
|
|
|
audio.Volume.Value = 1;
|
|
|
|
|
|
|
|
|
|
if (audio.VolumeTrack.Value == 0)
|
|
|
|
|
audio.VolumeTrack.Value = 1;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
AddAssert("game not muted", () => audio.Tracks.AggregateVolume.Value != 0);
|
|
|
|
|
|
2019-11-15 06:08:40 +08:00
|
|
|
|
AddStep("get track", () => Add(owner = new TestTrackOwner(track = getTrack())));
|
|
|
|
|
AddUntilStep("wait loaded", () => track.IsLoaded);
|
|
|
|
|
AddStep("start track", () => track.Start());
|
|
|
|
|
AddAssert("game is muted", () => audio.Tracks.AggregateVolume.Value == 0);
|
|
|
|
|
|
|
|
|
|
if (stopAnyPlaying)
|
|
|
|
|
AddStep("stop any playing", () => trackManager.StopAnyPlaying(owner));
|
|
|
|
|
else
|
|
|
|
|
AddStep("stop track", () => track.Stop());
|
|
|
|
|
|
|
|
|
|
AddAssert("game not muted", () => audio.Tracks.AggregateVolume.Value != 0);
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-15 23:38:25 +08:00
|
|
|
|
[Test]
|
|
|
|
|
public void TestOwnerNotRegistered()
|
|
|
|
|
{
|
|
|
|
|
PreviewTrack track = null;
|
|
|
|
|
|
|
|
|
|
AddStep("get track", () => Add(new TestTrackOwner(track = getTrack(), registerAsOwner: false)));
|
|
|
|
|
AddUntilStep("wait for loaded", () => track.IsLoaded);
|
|
|
|
|
|
|
|
|
|
AddStep("start track", () => track.Start());
|
|
|
|
|
AddUntilStep("track is running", () => track.IsRunning);
|
|
|
|
|
|
|
|
|
|
AddStep("cancel from anyone", () => trackManager.StopAnyPlaying(this));
|
|
|
|
|
AddAssert("track stopped", () => !track.IsRunning);
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-02 19:25:16 +08:00
|
|
|
|
private TestPreviewTrackManager.TestPreviewTrack getTrack() => (TestPreviewTrackManager.TestPreviewTrack)trackManager.Get(null);
|
2019-11-11 04:21:46 +08:00
|
|
|
|
|
2021-01-02 19:25:16 +08:00
|
|
|
|
private TestPreviewTrackManager.TestPreviewTrack getOwnedTrack()
|
2018-06-21 18:31:07 +08:00
|
|
|
|
{
|
|
|
|
|
var track = getTrack();
|
|
|
|
|
|
2019-11-06 18:06:24 +08:00
|
|
|
|
LoadComponentAsync(track, Add);
|
2018-06-21 18:31:07 +08:00
|
|
|
|
|
|
|
|
|
return track;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private class TestTrackOwner : CompositeDrawable, IPreviewTrackOwner
|
|
|
|
|
{
|
2019-11-06 18:06:24 +08:00
|
|
|
|
private readonly PreviewTrack track;
|
2020-11-15 23:38:25 +08:00
|
|
|
|
private readonly bool registerAsOwner;
|
2019-11-06 18:06:24 +08:00
|
|
|
|
|
2020-11-15 23:38:25 +08:00
|
|
|
|
public TestTrackOwner(PreviewTrack track, bool registerAsOwner = true)
|
2018-06-21 18:31:07 +08:00
|
|
|
|
{
|
2019-11-06 18:06:24 +08:00
|
|
|
|
this.track = track;
|
2020-11-15 23:38:25 +08:00
|
|
|
|
this.registerAsOwner = registerAsOwner;
|
2019-11-06 18:06:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
|
private void load()
|
|
|
|
|
{
|
|
|
|
|
LoadComponentAsync(track, AddInternal);
|
2018-06-21 18:31:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
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));
|
2020-11-15 23:38:25 +08:00
|
|
|
|
if (registerAsOwner)
|
|
|
|
|
dependencies.CacheAs<IPreviewTrackOwner>(this);
|
2018-06-21 18:31:07 +08:00
|
|
|
|
return dependencies;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-11 04:21:46 +08:00
|
|
|
|
public class TestPreviewTrackManager : PreviewTrackManager
|
2018-06-21 18:31:07 +08:00
|
|
|
|
{
|
2019-11-14 19:23:52 +08:00
|
|
|
|
public bool AllowUpdate = true;
|
|
|
|
|
|
|
|
|
|
public new PreviewTrack CurrentTrack => base.CurrentTrack;
|
|
|
|
|
|
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
|
|
|
|
|
2019-11-14 19:23:52 +08:00
|
|
|
|
public override bool UpdateSubTree()
|
|
|
|
|
{
|
|
|
|
|
if (!AllowUpdate)
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
return base.UpdateSubTree();
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-11 04:21:46 +08:00
|
|
|
|
public class TestPreviewTrack : TrackManagerPreviewTrack
|
2018-06-21 18:31:07 +08:00
|
|
|
|
{
|
2019-05-29 15:43:27 +08:00
|
|
|
|
private readonly ITrackStore trackManager;
|
|
|
|
|
|
2019-11-11 04:21:46 +08:00
|
|
|
|
public new Track Track => base.Track;
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|