2020-07-27 16:46:10 +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.
|
|
|
|
|
2020-09-16 06:59:41 +08:00
|
|
|
using System;
|
2021-06-22 15:19:55 +08:00
|
|
|
using System.Collections.Generic;
|
2020-07-27 16:46:10 +08:00
|
|
|
using System.Linq;
|
|
|
|
using NUnit.Framework;
|
|
|
|
using osu.Framework.Allocation;
|
2020-09-16 06:59:41 +08:00
|
|
|
using osu.Framework.Audio.Sample;
|
|
|
|
using osu.Framework.Bindables;
|
2020-07-27 16:46:10 +08:00
|
|
|
using osu.Framework.Graphics;
|
|
|
|
using osu.Framework.Graphics.Audio;
|
|
|
|
using osu.Framework.Graphics.Containers;
|
2020-09-16 06:59:41 +08:00
|
|
|
using osu.Framework.Graphics.Textures;
|
2020-07-27 16:46:10 +08:00
|
|
|
using osu.Framework.Testing;
|
|
|
|
using osu.Game.Audio;
|
|
|
|
using osu.Game.Skinning;
|
|
|
|
|
|
|
|
namespace osu.Game.Tests.Visual.Gameplay
|
|
|
|
{
|
|
|
|
public partial class TestSceneSkinnableSound : OsuTestScene
|
|
|
|
{
|
2023-03-16 14:10:35 +08:00
|
|
|
private TestSkinSourceContainer skinSource = null!;
|
|
|
|
private PausableSkinnableSound skinnableSound = null!;
|
2020-07-27 16:46:10 +08:00
|
|
|
|
2024-03-10 02:55:00 +08:00
|
|
|
private const string sample_lookup = "Gameplay/Argon/normal-sliderslide";
|
2023-03-16 14:41:05 +08:00
|
|
|
|
2022-04-21 15:49:21 +08:00
|
|
|
[SetUpSteps]
|
2020-10-14 20:40:49 +08:00
|
|
|
public void SetUpSteps()
|
2020-07-27 16:46:10 +08:00
|
|
|
{
|
2020-10-15 05:24:16 +08:00
|
|
|
AddStep("setup hierarchy", () =>
|
2020-07-27 16:46:10 +08:00
|
|
|
{
|
2021-05-27 14:39:35 +08:00
|
|
|
Child = skinSource = new TestSkinSourceContainer
|
2020-07-27 16:46:10 +08:00
|
|
|
{
|
2021-05-27 14:39:35 +08:00
|
|
|
RelativeSizeAxes = Axes.Both,
|
2020-10-14 20:40:49 +08:00
|
|
|
};
|
2021-05-27 14:39:35 +08:00
|
|
|
|
|
|
|
// has to be added after the hierarchy above else the `ISkinSource` dependency won't be cached.
|
2023-03-16 14:41:05 +08:00
|
|
|
skinSource.Add(skinnableSound = new PausableSkinnableSound(new SampleInfo(sample_lookup)));
|
2020-10-14 20:40:49 +08:00
|
|
|
});
|
|
|
|
}
|
2020-07-27 16:46:10 +08:00
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void TestStoppedSoundDoesntResumeAfterPause()
|
|
|
|
{
|
2021-01-20 13:05:35 +08:00
|
|
|
AddStep("start sample with looping", () =>
|
|
|
|
{
|
|
|
|
skinnableSound.Looping = true;
|
|
|
|
skinnableSound.Play();
|
|
|
|
});
|
2020-07-28 16:09:38 +08:00
|
|
|
|
2021-01-19 16:11:40 +08:00
|
|
|
AddUntilStep("wait for sample to start playing", () => skinnableSound.IsPlaying);
|
2020-07-27 16:46:10 +08:00
|
|
|
|
2020-07-27 16:46:44 +08:00
|
|
|
AddStep("stop sample", () => skinnableSound.Stop());
|
2020-07-27 16:46:10 +08:00
|
|
|
|
2021-01-19 16:11:40 +08:00
|
|
|
AddUntilStep("wait for sample to stop playing", () => !skinnableSound.IsPlaying);
|
2020-07-27 16:46:10 +08:00
|
|
|
|
2020-10-15 05:24:16 +08:00
|
|
|
AddStep("disable sample playback", () => skinSource.SamplePlaybackDisabled.Value = true);
|
2020-10-14 20:40:49 +08:00
|
|
|
|
2020-10-15 05:24:16 +08:00
|
|
|
AddStep("enable sample playback", () => skinSource.SamplePlaybackDisabled.Value = false);
|
2020-07-27 16:46:10 +08:00
|
|
|
|
2020-07-27 18:05:31 +08:00
|
|
|
AddWaitStep("wait a bit", 5);
|
2021-01-19 16:11:40 +08:00
|
|
|
AddAssert("sample not playing", () => !skinnableSound.IsPlaying);
|
2020-07-27 16:46:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void TestLoopingSoundResumesAfterPause()
|
|
|
|
{
|
2021-01-20 13:05:35 +08:00
|
|
|
AddStep("start sample with looping", () =>
|
|
|
|
{
|
|
|
|
skinnableSound.Looping = true;
|
|
|
|
skinnableSound.Play();
|
|
|
|
});
|
2020-07-27 16:46:10 +08:00
|
|
|
|
2021-01-19 16:11:40 +08:00
|
|
|
AddUntilStep("wait for sample to start playing", () => skinnableSound.IsPlaying);
|
2020-07-27 16:46:10 +08:00
|
|
|
|
2020-10-15 05:24:16 +08:00
|
|
|
AddStep("disable sample playback", () => skinSource.SamplePlaybackDisabled.Value = true);
|
2021-01-19 16:11:40 +08:00
|
|
|
AddUntilStep("wait for sample to stop playing", () => !skinnableSound.IsPlaying);
|
2020-10-14 20:40:49 +08:00
|
|
|
|
2020-10-15 05:24:16 +08:00
|
|
|
AddStep("enable sample playback", () => skinSource.SamplePlaybackDisabled.Value = false);
|
2021-01-19 16:11:40 +08:00
|
|
|
AddUntilStep("wait for sample to start playing", () => skinnableSound.IsPlaying);
|
2020-07-27 16:46:10 +08:00
|
|
|
}
|
2020-07-27 17:02:14 +08:00
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void TestNonLoopingStopsWithPause()
|
|
|
|
{
|
2021-01-19 16:11:40 +08:00
|
|
|
AddStep("start sample", () => skinnableSound.Play());
|
2020-07-27 17:02:14 +08:00
|
|
|
|
2021-01-19 16:11:40 +08:00
|
|
|
AddAssert("sample playing", () => skinnableSound.IsPlaying);
|
2020-07-27 17:02:14 +08:00
|
|
|
|
2020-10-15 05:24:16 +08:00
|
|
|
AddStep("disable sample playback", () => skinSource.SamplePlaybackDisabled.Value = true);
|
2020-10-14 20:40:49 +08:00
|
|
|
|
2021-01-19 16:11:40 +08:00
|
|
|
AddUntilStep("sample not playing", () => !skinnableSound.IsPlaying);
|
2020-07-27 17:02:14 +08:00
|
|
|
|
2020-10-15 05:24:16 +08:00
|
|
|
AddStep("enable sample playback", () => skinSource.SamplePlaybackDisabled.Value = false);
|
2020-07-27 18:05:31 +08:00
|
|
|
|
2021-01-19 16:11:40 +08:00
|
|
|
AddAssert("sample not playing", () => !skinnableSound.IsPlaying);
|
|
|
|
AddAssert("sample not playing", () => !skinnableSound.IsPlaying);
|
|
|
|
AddAssert("sample not playing", () => !skinnableSound.IsPlaying);
|
2020-07-27 17:02:14 +08:00
|
|
|
}
|
2020-09-16 06:59:41 +08:00
|
|
|
|
2023-03-16 14:41:05 +08:00
|
|
|
[Test]
|
|
|
|
public void TestSampleUpdatedBeforePlaybackWhenNotPresent()
|
|
|
|
{
|
|
|
|
AddStep("make sample non-present", () => skinnableSound.Hide());
|
|
|
|
AddUntilStep("ensure not present", () => skinnableSound.IsPresent, () => Is.False);
|
|
|
|
|
|
|
|
AddUntilStep("ensure sample loaded", () => skinnableSound.ChildrenOfType<DrawableSample>().Single().Name, () => Is.EqualTo(sample_lookup));
|
|
|
|
|
2023-03-22 06:15:49 +08:00
|
|
|
AddStep("change source", () =>
|
2023-03-16 14:41:05 +08:00
|
|
|
{
|
|
|
|
skinSource.OverridingSample = new SampleVirtual("new skin");
|
|
|
|
skinSource.TriggerSourceChanged();
|
|
|
|
});
|
|
|
|
|
|
|
|
AddStep("start sample", () => skinnableSound.Play());
|
|
|
|
AddUntilStep("sample updated", () => skinnableSound.ChildrenOfType<DrawableSample>().Single().Name, () => Is.EqualTo("new skin"));
|
|
|
|
}
|
|
|
|
|
2020-09-16 06:59:41 +08:00
|
|
|
[Test]
|
|
|
|
public void TestSkinChangeDoesntPlayOnPause()
|
|
|
|
{
|
2023-03-16 14:10:35 +08:00
|
|
|
DrawableSample? sample = null;
|
2020-09-16 06:59:41 +08:00
|
|
|
AddStep("start sample", () =>
|
|
|
|
{
|
|
|
|
skinnableSound.Play();
|
|
|
|
sample = skinnableSound.ChildrenOfType<DrawableSample>().Single();
|
|
|
|
});
|
|
|
|
|
2021-01-19 16:11:40 +08:00
|
|
|
AddAssert("sample playing", () => skinnableSound.IsPlaying);
|
2020-09-16 06:59:41 +08:00
|
|
|
|
2020-10-15 05:24:16 +08:00
|
|
|
AddStep("disable sample playback", () => skinSource.SamplePlaybackDisabled.Value = true);
|
2021-01-19 16:11:40 +08:00
|
|
|
AddUntilStep("wait for sample to stop playing", () => !skinnableSound.IsPlaying);
|
2020-09-16 06:59:41 +08:00
|
|
|
|
2020-09-19 10:55:28 +08:00
|
|
|
AddStep("trigger skin change", () => skinSource.TriggerSourceChanged());
|
2020-09-16 06:59:41 +08:00
|
|
|
|
2020-09-19 10:56:35 +08:00
|
|
|
AddAssert("retrieve and ensure current sample is different", () =>
|
2020-09-16 06:59:41 +08:00
|
|
|
{
|
2023-03-16 14:10:35 +08:00
|
|
|
DrawableSample? oldSample = sample;
|
2020-09-19 10:56:35 +08:00
|
|
|
sample = skinnableSound.ChildrenOfType<DrawableSample>().Single();
|
|
|
|
return sample != oldSample;
|
2020-09-16 06:59:41 +08:00
|
|
|
});
|
|
|
|
|
2021-01-19 16:11:40 +08:00
|
|
|
AddAssert("new sample stopped", () => !skinnableSound.IsPlaying);
|
2020-10-15 05:24:16 +08:00
|
|
|
AddStep("enable sample playback", () => skinSource.SamplePlaybackDisabled.Value = false);
|
2020-09-16 06:59:41 +08:00
|
|
|
|
|
|
|
AddWaitStep("wait a bit", 5);
|
2021-01-19 16:11:40 +08:00
|
|
|
AddAssert("new sample not played", () => !skinnableSound.IsPlaying);
|
2020-09-16 06:59:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
[Cached(typeof(ISkinSource))]
|
2020-10-14 20:40:49 +08:00
|
|
|
private partial class TestSkinSourceContainer : Container, ISkinSource, ISamplePlaybackDisabler
|
2020-09-16 06:59:41 +08:00
|
|
|
{
|
|
|
|
[Resolved]
|
2023-03-16 14:10:35 +08:00
|
|
|
private ISkinSource source { get; set; } = null!;
|
2020-09-16 06:59:41 +08:00
|
|
|
|
2023-03-16 14:10:35 +08:00
|
|
|
public event Action? SourceChanged;
|
2020-09-16 06:59:41 +08:00
|
|
|
|
2020-10-14 20:40:49 +08:00
|
|
|
public Bindable<bool> SamplePlaybackDisabled { get; } = new Bindable<bool>();
|
|
|
|
|
2023-03-16 14:41:05 +08:00
|
|
|
public ISample? OverridingSample;
|
|
|
|
|
2020-10-14 20:40:49 +08:00
|
|
|
IBindable<bool> ISamplePlaybackDisabler.SamplePlaybackDisabled => SamplePlaybackDisabled;
|
|
|
|
|
2023-03-16 14:10:35 +08:00
|
|
|
public Drawable? GetDrawableComponent(ISkinComponentLookup lookup) => source.GetDrawableComponent(lookup);
|
|
|
|
public Texture? GetTexture(string componentName, WrapMode wrapModeS, WrapMode wrapModeT) => source.GetTexture(componentName, wrapModeS, wrapModeT);
|
2023-03-16 14:41:05 +08:00
|
|
|
public ISample? GetSample(ISampleInfo sampleInfo) => OverridingSample ?? source.GetSample(sampleInfo);
|
2023-03-16 14:10:35 +08:00
|
|
|
|
|
|
|
public IBindable<TValue>? GetConfig<TLookup, TValue>(TLookup lookup)
|
|
|
|
where TLookup : notnull
|
|
|
|
where TValue : notnull
|
|
|
|
{
|
|
|
|
return source.GetConfig<TLookup, TValue>(lookup);
|
|
|
|
}
|
|
|
|
|
|
|
|
public ISkin? FindProvider(Func<ISkin, bool> lookupFunction) => lookupFunction(this) ? this : source.FindProvider(lookupFunction);
|
|
|
|
public IEnumerable<ISkin> AllSources => new[] { this }.Concat(source.AllSources);
|
2020-09-16 06:59:41 +08:00
|
|
|
|
|
|
|
public void TriggerSourceChanged()
|
|
|
|
{
|
|
|
|
SourceChanged?.Invoke();
|
|
|
|
}
|
|
|
|
}
|
2020-07-27 16:46:10 +08:00
|
|
|
}
|
|
|
|
}
|