稳定 Audio PlayMode 测试
This commit is contained in:
@@ -17,6 +17,7 @@ namespace FlowScope.Tests.PlayMode.Audio
|
||||
private AudioClip bgmA;
|
||||
private AudioClip bgmB;
|
||||
private AudioClip sfx;
|
||||
private GameObject listenerObject;
|
||||
|
||||
[SetUp]
|
||||
public void SetUp()
|
||||
@@ -29,6 +30,12 @@ namespace FlowScope.Tests.PlayMode.Audio
|
||||
resources.Add("bgm-a", bgmA);
|
||||
resources.Add("bgm-b", bgmB);
|
||||
resources.Add("sfx", sfx);
|
||||
|
||||
if (Object.FindObjectOfType<AudioListener>() == null)
|
||||
{
|
||||
listenerObject = new GameObject("AudioServiceTestListener");
|
||||
listenerObject.AddComponent<AudioListener>();
|
||||
}
|
||||
}
|
||||
|
||||
[TearDown]
|
||||
@@ -38,6 +45,11 @@ namespace FlowScope.Tests.PlayMode.Audio
|
||||
{
|
||||
Object.Destroy(service.gameObject);
|
||||
}
|
||||
|
||||
if (listenerObject != null)
|
||||
{
|
||||
Object.DestroyImmediate(listenerObject);
|
||||
}
|
||||
}
|
||||
|
||||
[UnityTest]
|
||||
@@ -129,10 +141,15 @@ namespace FlowScope.Tests.PlayMode.Audio
|
||||
|
||||
handle.Stop(0.1f);
|
||||
|
||||
yield return new WaitForSeconds(0.15f);
|
||||
Assert.That(handle.IsPlaying, Is.False);
|
||||
Assert.That(bgmSource.isPlaying, Is.False);
|
||||
Assert.That(bgmSource.volume, Is.EqualTo(0f).Within(0.01f));
|
||||
yield return WaitUntilOrFail(
|
||||
() => bgmSource == null || (!bgmSource.isPlaying && bgmSource.volume <= 0.01f),
|
||||
1f,
|
||||
"BGM fade-out did not stop and reach zero volume.");
|
||||
if (bgmSource != null)
|
||||
{
|
||||
Assert.That(bgmSource.isPlaying, Is.False);
|
||||
Assert.That(bgmSource.volume, Is.EqualTo(0f).Within(0.01f));
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -151,6 +168,20 @@ namespace FlowScope.Tests.PlayMode.Audio
|
||||
return gameObject.AddComponent<AudioService>().Initialize(resources, config);
|
||||
}
|
||||
|
||||
private static System.Collections.IEnumerator WaitUntilOrFail(
|
||||
Func<bool> condition,
|
||||
float timeoutSeconds,
|
||||
string message)
|
||||
{
|
||||
var deadline = Time.realtimeSinceStartup + timeoutSeconds;
|
||||
while (!condition() && Time.realtimeSinceStartup < deadline)
|
||||
{
|
||||
yield return null;
|
||||
}
|
||||
|
||||
Assert.That(condition(), Is.True, message);
|
||||
}
|
||||
|
||||
private static AudioClip CreateClip(string name, float lengthSeconds)
|
||||
{
|
||||
var sampleCount = Mathf.CeilToInt(44100 * lengthSeconds);
|
||||
|
||||
Reference in New Issue
Block a user