using Amphora; using Azimuth; using System; using System.Collections.Generic; using UnitTestSharp; namespace Susurrus.UnitTests { public class SoundPackLoadTests : TestFixture { public class DeferringModule : NullModule, IModule { public readonly List Pending = new List(); public new SoundLoad LoadSound(ByteView encoded) { SoundLoad returnMe = new SoundLoad(); Pending.Add(returnMe); return returnMe; } } public class Rig { public readonly Mixer Mixer; public readonly Bus Effects; public readonly Dictionary Buses; public readonly MemorySoundSource Source = new MemorySoundSource(); public Rig(IModule module) { Mixer = new Mixer(module, new ScriptedGenerator()); Effects = Mixer.AddBus("Effects"); Buses = new Dictionary { { "Effects", Effects } }; } public Rig WithFiles(params string[] paths) { foreach (string path in paths) { Source.Add(path, new ByteView(SoundDecoderTests.WavFile())); } return this; } public SoundPackLoad Start(IModule module, Type packType) { return new SoundPackLoad(packType, module, Mixer, Source, Buses); } public int NextDefinitionIndex() { return Mixer.Define(new SoundDefinition(SoundDefinitionTests.Clip(), Effects)).Index; } } public static readonly SoundSpec SharedHum = new SoundSpec("Effects", "hum.ogg", loop: Loop.Whole); [SoundPack("Test")] public static class Hums { public static readonly SoundSpec Hum = SharedHum; public static readonly SoundSpec Click = new SoundSpec("Effects", "click.ogg"); public static readonly SoundSpec HumQuietly = new SoundSpec("Effects", "hum.ogg", baseVolume: 0.2f); } [SoundPack("Test")] public static class Twins { public static readonly SoundSpec First = SharedHum; public static readonly SoundSpec Second = SharedHum; } [SoundPack("Test")] public static class WrongBus { public static readonly SoundSpec Hum = new SoundSpec("Effects", "hum.ogg"); public static readonly SoundSpec Click = new SoundSpec("Music", "click.ogg"); } [SoundPack("Test")] public static class LoopPastTheEnd { public static readonly SoundSpec Hum = new SoundSpec("Effects", "hum.ogg", loop: new Loop(TimeSpan.FromSeconds(5))); } [SoundPack("Test")] public static class Songs { public static readonly SoundSpec Song = new SoundSpec("Effects", "song.ogg", loop: Loop.Whole, streamed: true); } [SoundPack("Test")] public static class HalfStreamed { public static readonly SoundSpec Whole = new SoundSpec("Effects", "song.ogg"); public static readonly SoundSpec Streamed = new SoundSpec("Effects", "song.ogg", streamed: true); } public class CtorTests : TestFixture { public void NullModule_Throws() { Rig rig = new Rig(new NullModule()); CheckThrow(typeof(ArgumentNullException)); new SoundPackLoad(typeof(Hums), null, rig.Mixer, rig.Source, rig.Buses); } public void NullMixer_Throws() { Rig rig = new Rig(new NullModule()); CheckThrow(typeof(ArgumentNullException)); new SoundPackLoad(typeof(Hums), new NullModule(), null, rig.Source, rig.Buses); } public void NullSource_Throws() { Rig rig = new Rig(new NullModule()); CheckThrow(typeof(ArgumentNullException)); new SoundPackLoad(typeof(Hums), new NullModule(), rig.Mixer, null, rig.Buses); } public void NullBuses_Throws() { Rig rig = new Rig(new NullModule()); CheckThrow(typeof(ArgumentNullException)); new SoundPackLoad(typeof(Hums), new NullModule(), rig.Mixer, rig.Source, null); } public void ATypeWithoutTheAttribute_Throws() { Rig rig = new Rig(new NullModule()); CheckThrow(typeof(ArgumentException)); new SoundPackLoad(typeof(SoundPackTests.Unmarked), new NullModule(), rig.Mixer, rig.Source, rig.Buses); } public void AFileTheSourceLacks_FailsAtOnceNamingIt() { DeferringModule module = new DeferringModule(); Rig rig = new Rig(module).WithFiles("Test/hum.ogg"); SoundPackLoad load = rig.Start(module, typeof(Hums)); CheckTrue(load.IsDone); CheckFalse(load.Result.IsOk); CheckEqual("A sound file couldn't be read. Test/click.ogg isn't in the source.", load.Result.Status.Describe()); } public void AFileBothStreamedAndWhole_FailsNamingIt() { NullModule module = new NullModule(); Rig rig = new Rig(module).WithFiles("Test/song.ogg"); SoundPackLoad load = rig.Start(module, typeof(HalfStreamed)); CheckTrue(load.IsDone); CheckEqual("A sound in the pack can't be defined. Test/song.ogg is both streamed and loaded whole.", load.Result.Status.Describe()); } public void AFileTheSourceLacks_StartsNoLoads() { DeferringModule module = new DeferringModule(); Rig rig = new Rig(module).WithFiles("Test/hum.ogg"); rig.Start(module, typeof(Hums)); CheckEqual(0, module.Pending.Count); } } public class IsDoneTests : TestFixture { public void WhileABackendHasLoadsUnfinished_IsFalse() { DeferringModule module = new DeferringModule(); SoundPackLoad load = new Rig(module).WithFiles("Test/hum.ogg", "Test/click.ogg") .Start(module, typeof(Hums)); CheckFalse(load.IsDone); module.Pending[0].Complete(SoundDefinitionTests.Clip()); CheckFalse(load.IsDone); } public void OnceEveryLoadIsFinished_IsTrue() { DeferringModule module = new DeferringModule(); SoundPackLoad load = new Rig(module).WithFiles("Test/hum.ogg", "Test/click.ogg") .Start(module, typeof(Hums)); module.Pending[0].Complete(SoundDefinitionTests.Clip()); module.Pending[1].Complete(SoundDefinitionTests.Clip()); CheckTrue(load.IsDone); } public void WithABackendThatDecodesAtOnce_IsTrueStraightAway() { NullModule module = new NullModule(); Rig rig = new Rig(module).WithFiles("Test/hum.ogg", "Test/click.ogg"); CheckTrue(rig.Start(module, typeof(Hums)).IsDone); } } public class ResultTests : TestFixture { public void BeforeTheLoadIsDone_Throws() { DeferringModule module = new DeferringModule(); SoundPackLoad load = new Rig(module).WithFiles("Test/hum.ogg", "Test/click.ogg") .Start(module, typeof(Hums)); CheckThrow(typeof(InvalidOperationException)); StatusOr unused = load.Result; } public void ReadsEachFileOnce_EvenWhenSpecsShareIt() { DeferringModule module = new DeferringModule(); new Rig(module).WithFiles("Test/hum.ogg", "Test/click.ogg").Start(module, typeof(Hums)); CheckEqual(2, module.Pending.Count); } public void DefinesEverySpecOnTheMixer() { NullModule module = new NullModule { EndsVoicesOnUpdate = false }; Rig rig = new Rig(module).WithFiles("Test/hum.ogg", "Test/click.ogg"); SoundPack pack = rig.Start(module, typeof(Hums)).Result.Value; CheckFalse(pack.IdOf(Hums.Hum) == pack.IdOf(Hums.Click)); CheckFalse(pack.IdOf(Hums.Hum) == pack.IdOf(Hums.HumQuietly)); CheckFalse(rig.Mixer.Play(pack.IdOf(Hums.Click)).IsNone); CheckFalse(rig.Mixer.Play(pack.IdOf(Hums.HumQuietly)).IsNone); CheckEqual(3, rig.NextDefinitionIndex()); } public void CarriesTheSpecsSettingsIntoTheDefinitions() { NullModule module = new NullModule { EndsVoicesOnUpdate = false }; Rig rig = new Rig(module).WithFiles("Test/hum.ogg", "Test/click.ogg"); SoundPack pack = rig.Start(module, typeof(Hums)).Result.Value; Voice voice = rig.Mixer.Play(pack.IdOf(Hums.HumQuietly)); CheckEqual(0.2f, module.VolumeOf(voice)); } public void AStreamedSpec_PlaysOneVoiceAtATime() { NullModule module = new NullModule { EndsVoicesOnUpdate = false }; Rig rig = new Rig(module).WithFiles("Test/song.ogg"); SoundPack pack = rig.Start(module, typeof(Songs)).Result.Value; CheckFalse(rig.Mixer.Play(pack.IdOf(Songs.Song)).IsNone); CheckTrue(rig.Mixer.Play(pack.IdOf(Songs.Song)).IsNone); } public void ASpecThatIsNotStreamed_PlaysManyVoicesAtOnce() { NullModule module = new NullModule { EndsVoicesOnUpdate = false }; Rig rig = new Rig(module).WithFiles("Test/hum.ogg", "Test/click.ogg"); SoundPack pack = rig.Start(module, typeof(Hums)).Result.Value; CheckFalse(rig.Mixer.Play(pack.IdOf(Hums.Click)).IsNone); CheckFalse(rig.Mixer.Play(pack.IdOf(Hums.Click)).IsNone); } public void ASpecTwoFieldsShare_IsDefinedOnce() { NullModule module = new NullModule(); Rig rig = new Rig(module).WithFiles("Test/hum.ogg"); SoundPack pack = rig.Start(module, typeof(Twins)).Result.Value; CheckEqual(pack.IdOf(Twins.First), pack.IdOf(Twins.Second)); CheckEqual(1, rig.NextDefinitionIndex()); } public void AFileThatFailsToLoad_FailsNamingItAndWhy() { DeferringModule module = new DeferringModule(); SoundPackLoad load = new Rig(module).WithFiles("Test/hum.ogg", "Test/click.ogg") .Start(module, typeof(Hums)); module.Pending[0].Complete(SoundDefinitionTests.Clip()); module.Pending[1].Complete(SoundFailures.WavMalformed.Fail("bad")); CheckFalse(load.Result.IsOk); CheckEqual("A sound file couldn't be loaded. Test/click.ogg: This isn't a valid WAV file. bad", load.Result.Status.Describe()); } public void AFileThatFailsToLoad_DisposesTheOnesThatLoaded() { DeferringModule module = new DeferringModule(); SoundPackLoad load = new Rig(module).WithFiles("Test/hum.ogg", "Test/click.ogg") .Start(module, typeof(Hums)); Sound loaded = SoundDefinitionTests.Clip(); module.Pending[0].Complete(loaded); module.Pending[1].Complete(SoundFailures.WavMalformed.Fail("bad")); CheckFalse(load.Result.IsOk); CheckTrue(loaded.IsDisposed); } public void ABusThePackWasNotGiven_FailsNamingTheSpec() { NullModule module = new NullModule(); Rig rig = new Rig(module).WithFiles("Test/hum.ogg", "Test/click.ogg"); StatusOr result = rig.Start(module, typeof(WrongBus)).Result; CheckFalse(result.IsOk); CheckEqual( "A sound in the pack can't be defined. Click plays on the bus Music, which the pack wasn't given.", result.Status.Describe()); } public void ARefusedSpec_DefinesNothingOnTheMixer() { NullModule module = new NullModule(); Rig rig = new Rig(module).WithFiles("Test/hum.ogg", "Test/click.ogg"); rig.Start(module, typeof(WrongBus)); CheckEqual(0, rig.NextDefinitionIndex()); } public void ARefusedSpec_DisposesTheLoadedSounds() { DeferringModule module = new DeferringModule(); SoundPackLoad load = new Rig(module).WithFiles("Test/hum.ogg", "Test/click.ogg") .Start(module, typeof(WrongBus)); Sound first = SoundDefinitionTests.Clip(); Sound second = SoundDefinitionTests.Clip(); module.Pending[0].Complete(first); module.Pending[1].Complete(second); CheckFalse(load.Result.IsOk); CheckTrue(first.IsDisposed); CheckTrue(second.IsDisposed); } public void ALoopThatLeavesTheSound_FailsNamingTheSpec() { NullModule module = new NullModule(); Rig rig = new Rig(module).WithFiles("Test/hum.ogg"); StatusOr result = rig.Start(module, typeof(LoopPastTheEnd)).Result; CheckFalse(result.IsOk); CheckTrue(result.Status.Describe().StartsWith("A sound in the pack can't be defined. Hum: ")); } public void ABusFromAnotherMixer_FailsNamingTheBus() { NullModule module = new NullModule(); Rig rig = new Rig(module).WithFiles("Test/hum.ogg", "Test/click.ogg"); rig.Buses["Effects"] = new Rig(new NullModule()).Effects; StatusOr result = rig.Start(module, typeof(Hums)).Result; CheckFalse(result.IsOk); CheckTrue(result.Status.Describe().Contains("The bus Effects didn't come from this mixer.")); } public void ABusFromAnotherMixer_DisposesTheLoadedSounds() { DeferringModule module = new DeferringModule(); Rig rig = new Rig(module).WithFiles("Test/hum.ogg", "Test/click.ogg"); rig.Buses["Effects"] = new Rig(new NullModule()).Effects; SoundPackLoad load = rig.Start(module, typeof(Hums)); Sound first = SoundDefinitionTests.Clip(); Sound second = SoundDefinitionTests.Clip(); module.Pending[0].Complete(first); module.Pending[1].Complete(second); CheckFalse(load.Result.IsOk); CheckTrue(first.IsDisposed); CheckTrue(second.IsDisposed); } } } }