< Summary

Class:Tests.AudioTests
Assembly:AudioTests
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/Audio/Tests/AudioTests.cs
Covered lines:129
Uncovered lines:0
Coverable lines:129
Total lines:312
Line coverage:100% (129 of 129)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
TearDown()0%110100%
CreateAudioClip(...)0%110100%
CreateAudioClip(...)0%110100%
CreateAndLoadAudioClip()0%770100%
CreateAndLoadAudioClipTest()0%330100%
PlayAudioTestWithoutFinishLoading()0%330100%
AudioComponentMissingValuesGetDefaultedOnUpdate()0%330100%
AudioClipMissingValuesGetDefaultedOnUpdate()0%440100%
AudioIsLooped()0%880100%
AudioIsNotLooped()0%770100%
AudioClipAttachedGetsReplacedOnNewAttachment()0%330100%
VolumeWhenAudioCreatedWithNoUserInScene()0%550100%
VolumeWhenAudioCreatedWithUserInScene()0%550100%
VolumeIsMutedWhenUserLeavesScene()0%550100%
VolumeIsUnmutedWhenUserEntersScene()0%550100%
AudioStreamComponentCreation()0%550100%
AudioClip_OnReadyBeforeLoading()0%110100%
AudioClip_OnReadyWaitLoading()0%330100%
AudioClip_OnReadyAfterLoadingInstantlyCalled()0%330100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/Audio/Tests/AudioTests.cs

#LineLine coverage
 1using DCL.Components;
 2using DCL.Controllers;
 3using DCL.Helpers;
 4using DCL.Models;
 5using NUnit.Framework;
 6using System.Collections;
 7using System.Linq;
 8using Newtonsoft.Json;
 9using UnityEngine;
 10using UnityEngine.TestTools;
 11
 12namespace Tests
 13{
 14    public class AudioTests : IntegrationTestSuite_Legacy
 15    {
 16        protected override IEnumerator TearDown()
 17        {
 1518            sceneController.enabled = true;
 1519            return base.TearDown();
 20        }
 21
 22        public DCLAudioClip CreateAudioClip(string url, bool loop, bool shouldTryToLoad, double volume)
 23        {
 324            DCLAudioClip.Model model = new DCLAudioClip.Model
 25            {
 26                url = url,
 27                loop = loop,
 28                shouldTryToLoad = shouldTryToLoad,
 29                volume = volume
 30            };
 31
 332            return CreateAudioClip(model);
 33        }
 34
 435        public DCLAudioClip CreateAudioClip(DCLAudioClip.Model model) { return TestHelpers.SharedComponentCreate<DCLAudi
 36
 37        public IEnumerator CreateAndLoadAudioClip(bool waitForLoading = true)
 38        {
 239            var entity = TestHelpers.CreateSceneEntity(scene);
 240            yield return null;
 41
 242            yield return TestHelpers.CreateAudioSourceWithClipForEntity(entity);
 43
 244            DCLAudioSource dclAudioSource = entity.gameObject.GetComponentInChildren<DCLAudioSource>();
 245            AudioSource unityAudioSource = dclAudioSource.GetComponentInChildren<AudioSource>();
 46
 247            Assert.IsTrue(scene.entities.ContainsKey(entity.entityId), "Entity was not created correctly!");
 248            Assert.IsTrue(dclAudioSource != null, "DCLAudioSource Creation Failure!");
 249            Assert.IsTrue(unityAudioSource != null, "Unity AudioSource Creation Failure!");
 50
 251            yield return dclAudioSource.routine;
 52
 253            Assert.IsTrue(unityAudioSource.isPlaying, "Audio Source is not playing when it should!");
 54
 55            //NOTE(Brian): Stop test
 256            yield return TestHelpers.CreateAudioSource(scene,
 57                entityId: entity.entityId,
 58                audioClipId: "audioClipTest",
 59                playing: false);
 60
 261            yield return null;
 62
 263            Assert.IsTrue(!unityAudioSource.isPlaying, "Audio Source is playing when it should NOT play!");
 264        }
 65
 66        /// <summary>
 67        /// This should test creating a audioclip/audiosource couple, wait for audioClip load and send playing:true afte
 68        /// </summary>
 69        [UnityTest]
 270        public IEnumerator CreateAndLoadAudioClipTest() { yield return CreateAndLoadAudioClip(waitForLoading: true); }
 71
 72        /// <summary>
 73        /// This should test creating a audioclip/audiosource couple but send playing:true before the audioClip finished
 74        /// </summary>
 75        [UnityTest]
 276        public IEnumerator PlayAudioTestWithoutFinishLoading() { yield return CreateAndLoadAudioClip(waitForLoading: fal
 77
 78        [UnityTest]
 279        public IEnumerator AudioComponentMissingValuesGetDefaultedOnUpdate() { yield return TestHelpers.TestEntityCompon
 80
 81        [UnityTest]
 82        public IEnumerator AudioClipMissingValuesGetDefaultedOnUpdate()
 83        {
 84            // 1. Create component with non-default configs
 185            DCLAudioClip.Model componentModel = new DCLAudioClip.Model
 86            {
 87                loop = true,
 88                shouldTryToLoad = false,
 89                volume = 0.8f
 90            };
 91
 192            DCLAudioClip audioClip = CreateAudioClip(componentModel);
 93
 194            yield return audioClip.routine;
 95
 96            // 2. Check configured values
 197            Assert.IsTrue(audioClip.isLoop);
 198            Assert.IsFalse(audioClip.shouldTryLoad);
 199            Assert.AreEqual(0.8f, audioClip.volume);
 100
 101            // 3. Update component with missing values
 1102            componentModel = new DCLAudioClip.Model { };
 103
 1104            scene.SharedComponentUpdate(audioClip.id, JsonUtility.ToJson(componentModel));
 105
 1106            yield return audioClip.routine;
 107
 108            // 4. Check defaulted values
 1109            Assert.IsFalse(audioClip.isLoop);
 1110            Assert.IsTrue(audioClip.shouldTryLoad);
 1111            Assert.AreEqual(1f, audioClip.volume);
 1112        }
 113
 114        [UnityTest]
 115        public IEnumerator AudioIsLooped()
 116        {
 1117            var entity = TestHelpers.CreateSceneEntity(scene);
 1118            yield return null;
 119
 1120            yield return TestHelpers.LoadAudioClip(scene, "1", TestAssetsUtils.GetPath() + "/Audio/short_effect.ogg", fa
 121
 1122            yield return TestHelpers.CreateAudioSource(scene, entity.entityId, "1", true, loop: true);
 123
 2124            DCLAudioSource dclAudioSource = entity.components.Values.FirstOrDefault(x => x is DCLAudioSource) as DCLAudi
 1125            float initTime = dclAudioSource.audioSource.clip.length - 0.05f;
 1126            dclAudioSource.audioSource.time = initTime;
 1127            yield return new WaitForSeconds(0.1f);
 128
 1129            Assert.IsTrue(dclAudioSource.playTime > 0 && dclAudioSource.playTime < initTime);
 1130        }
 131
 132        [UnityTest]
 133        public IEnumerator AudioIsNotLooped()
 134        {
 1135            var entity = TestHelpers.CreateSceneEntity(scene);
 1136            yield return null;
 137
 1138            yield return TestHelpers.LoadAudioClip(scene, "1", TestAssetsUtils.GetPath() + "/Audio/short_effect.ogg", fa
 139
 1140            yield return TestHelpers.CreateAudioSource(scene, entity.entityId, "1", true, loop: false);
 141
 2142            DCLAudioSource dclAudioSource = entity.components.Values.FirstOrDefault(x => x is DCLAudioSource) as DCLAudi
 1143            dclAudioSource.audioSource.time = dclAudioSource.audioSource.clip.length - 0.05f;
 1144            yield return new WaitForSeconds(0.1f);
 145
 1146            Assert.AreEqual(0, dclAudioSource.playTime);
 1147        }
 148
 149        [UnityTest]
 150        public IEnumerator AudioClipAttachedGetsReplacedOnNewAttachment()
 151        {
 1152            yield return TestHelpers.TestAttachedSharedComponentOfSameTypeIsReplaced<DCLAudioClip.Model, DCLAudioClip>(
 153                scene, CLASS_ID.AUDIO_CLIP);
 1154        }
 155
 156        [UnityTest]
 157        public IEnumerator VolumeWhenAudioCreatedWithNoUserInScene()
 158        {
 159            // We disable SceneController monobehaviour to avoid its current scene id update
 1160            sceneController.enabled = false;
 161
 162            // Set current scene as a different one
 1163            CommonScriptableObjects.sceneID.Set("unexistent-scene");
 164
 1165            var entity = TestHelpers.CreateSceneEntity(scene);
 1166            yield return null;
 167
 1168            yield return TestHelpers.CreateAudioSourceWithClipForEntity(entity);
 169
 1170            DCLAudioSource dclAudioSource = entity.gameObject.GetComponentInChildren<DCLAudioSource>();
 1171            yield return dclAudioSource.routine;
 172
 1173            AudioSource unityAudioSource = dclAudioSource.GetComponentInChildren<AudioSource>();
 174
 175            // Check the volume
 1176            Assert.AreEqual(0f, unityAudioSource.volume);
 1177        }
 178
 179        [UnityTest]
 180        public IEnumerator VolumeWhenAudioCreatedWithUserInScene()
 181        {
 182            // We disable SceneController monobehaviour to avoid its current scene id update
 1183            sceneController.enabled = false;
 184
 185            // Set current scene with this scene's id
 1186            CommonScriptableObjects.sceneID.Set(scene.sceneData.id);
 187
 1188            var entity = TestHelpers.CreateSceneEntity(scene);
 1189            yield return null;
 190
 1191            yield return TestHelpers.CreateAudioSourceWithClipForEntity(entity);
 192
 1193            DCLAudioSource dclAudioSource = entity.gameObject.GetComponentInChildren<DCLAudioSource>();
 1194            yield return dclAudioSource.routine;
 195
 1196            AudioSource unityAudioSource = dclAudioSource.GetComponentInChildren<AudioSource>();
 197
 198            // Check the volume
 1199            Assert.AreEqual(unityAudioSource.volume, dclAudioSource.volume);
 1200        }
 201
 202        [UnityTest]
 203        public IEnumerator VolumeIsMutedWhenUserLeavesScene()
 204        {
 205            // We disable SceneController monobehaviour to avoid its current scene id update
 1206            sceneController.enabled = false;
 207
 208            // Set current scene with this scene's id
 1209            CommonScriptableObjects.sceneID.Set(scene.sceneData.id);
 210
 1211            var entity = TestHelpers.CreateSceneEntity(scene);
 1212            yield return null;
 213
 1214            yield return TestHelpers.CreateAudioSourceWithClipForEntity(entity);
 215
 1216            DCLAudioSource dclAudioSource = entity.gameObject.GetComponentInChildren<DCLAudioSource>();
 1217            yield return dclAudioSource.routine;
 218
 1219            AudioSource unityAudioSource = dclAudioSource.GetComponentInChildren<AudioSource>();
 220
 221            // Set current scene as a different one
 1222            CommonScriptableObjects.sceneID.Set("unexistent-scene");
 223
 224            // Check the volume
 1225            Assert.AreEqual(unityAudioSource.volume, 0f);
 1226        }
 227
 228        [UnityTest]
 229        public IEnumerator VolumeIsUnmutedWhenUserEntersScene()
 230        {
 231            // We disable SceneController monobehaviour to avoid its current scene id update
 1232            sceneController.enabled = false;
 233
 234            // Set current scene as a different one
 1235            CommonScriptableObjects.sceneID.Set("unexistent-scene");
 236
 1237            var entity = TestHelpers.CreateSceneEntity(scene);
 1238            yield return null;
 239
 1240            yield return TestHelpers.CreateAudioSourceWithClipForEntity(entity);
 241
 1242            DCLAudioSource dclAudioSource = entity.gameObject.GetComponentInChildren<DCLAudioSource>();
 1243            yield return dclAudioSource.routine;
 244
 1245            AudioSource unityAudioSource = dclAudioSource.GetComponentInChildren<AudioSource>();
 246
 247            // Set current scene with this scene's id
 1248            CommonScriptableObjects.sceneID.Set(scene.sceneData.id);
 249
 250            // Check the volume
 1251            Assert.AreEqual(unityAudioSource.volume, dclAudioSource.volume);
 1252        }
 253
 254        [UnityTest]
 255        public IEnumerator AudioStreamComponentCreation()
 256        {
 1257            var entity = TestHelpers.CreateSceneEntity(scene);
 1258            DCLAudioStream.Model model = new DCLAudioStream.Model()
 259            {
 260                url = "https://audio.dcl.guru/radio/8110/radio.mp3",
 261                playing = false,
 262                volume = 1f
 263            };
 1264            DCLAudioStream component = TestHelpers.EntityComponentCreate<DCLAudioStream, DCLAudioStream.Model>(scene, en
 265
 1266            yield return component.routine;
 1267            Assert.IsFalse(component.GetModel().playing);
 268
 1269            model.playing = true;
 1270            component.UpdateFromModel(model);
 1271            yield return component.routine;
 1272            Assert.IsTrue(component.GetModel().playing);
 273
 1274            model.playing = false;
 1275            component.UpdateFromModel(model);
 1276            yield return component.routine;
 1277            Assert.IsFalse(component.GetModel().playing);
 1278        }
 279
 280        [Test]
 281        public void AudioClip_OnReadyBeforeLoading()
 282        {
 1283            DCLAudioClip dclAudioClip = CreateAudioClip(TestAssetsUtils.GetPath() + "/Audio/short_effect.ogg", true, tru
 1284            bool isOnReady = false;
 3285            dclAudioClip.CallWhenReady((x) => { isOnReady = true; });
 286
 1287            Assert.IsTrue(isOnReady); //DCLAudioClip is ready on creation
 1288        }
 289
 290        [UnityTest]
 291        public IEnumerator AudioClip_OnReadyWaitLoading()
 292        {
 1293            DCLAudioClip dclAudioClip = CreateAudioClip(TestAssetsUtils.GetPath() + "/Audio/short_effect.ogg", true, tru
 1294            bool isOnReady = false;
 3295            dclAudioClip.CallWhenReady((x) => { isOnReady = true; });
 1296            yield return dclAudioClip.routine;
 297
 1298            Assert.IsTrue(isOnReady);
 1299        }
 300
 301        [UnityTest]
 302        public IEnumerator AudioClip_OnReadyAfterLoadingInstantlyCalled()
 303        {
 1304            DCLAudioClip dclAudioClip = CreateAudioClip(TestAssetsUtils.GetPath() + "/Audio/short_effect.ogg", true, tru
 1305            yield return dclAudioClip.routine;
 1306            bool isOnReady = false;
 3307            dclAudioClip.CallWhenReady((x) => { isOnReady = true; });
 308
 1309            Assert.IsTrue(isOnReady);
 1310        }
 311    }
 312}