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