< Summary

Class:DCL.Components.DCLAudioClip
Assembly:MainScripts
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/Audio/DCLAudioClip.cs
Covered lines:31
Uncovered lines:20
Coverable lines:51
Total lines:135
Line coverage:60.7% (31 of 51)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
Model()0%110100%
GetDataFromJSON(...)0%110100%
DCLAudioClip()0%110100%
GetClassId()0%2100%
OnComplete(...)0%3.143075%
OnFail(...)0%6200%
TryToLoad()0%550100%
Unload()0%2100%
ApplyChanges()0%14.2512075%
Dispose()0%2100%

File(s)

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

#LineLine coverage
 1using System;
 2using System.Collections;
 3using DCL.Helpers;
 4using DCL.Models;
 5using UnityEngine;
 6
 7namespace DCL.Components
 8{
 9    public class DCLAudioClip : BaseDisposable
 10    {
 11        [Serializable]
 12        public class Model : BaseModel
 13        {
 14            public string url;
 15            public bool loop = false;
 5316            public bool shouldTryToLoad = true;
 17
 18            [Range(0f, 1f)]
 5319            public double volume = 1f;
 20
 1821            public override BaseModel GetDataFromJSON(string json) { return Utils.SafeFromJson<Model>(json); }
 22        }
 23
 24        public AudioClip audioClip;
 25        private bool isDisposed = false;
 26        private AssetPromise_AudioClip audioClipPromise = null;
 27
 28        public enum LoadState
 29        {
 30            IDLE,
 31            LOADING_IN_PROGRESS,
 32            LOADING_FAILED,
 33            LOADING_COMPLETED,
 34        }
 35
 036        public LoadState loadingState { get; private set; }
 37        public event Action<DCLAudioClip> OnLoadingFinished;
 38
 1739        public DCLAudioClip()
 40        {
 1741            model = new Model();
 42
 1743            loadingState = LoadState.IDLE;
 1744        }
 45
 046        public double volume => ((Model) model).volume;
 47
 048        public bool isLoop => ((Model) model).loop;
 49
 050        public bool shouldTryLoad => ((Model) model).shouldTryToLoad;
 51
 052        public override int GetClassId() { return (int) CLASS_ID.AUDIO_CLIP; }
 53
 54        void OnComplete(Asset_AudioClip assetAudioClip)
 55        {
 1456            if (assetAudioClip.audioClip != null)
 57            {
 1458                this.audioClip = assetAudioClip.audioClip;
 1459                loadingState = LoadState.LOADING_COMPLETED;
 1460            }
 61            else
 62            {
 063                loadingState = LoadState.LOADING_FAILED;
 64            }
 65
 1466            if (OnLoadingFinished != null)
 67            {
 068                OnLoadingFinished.Invoke(this);
 69            }
 1470        }
 71
 72        void OnFail(Asset_AudioClip assetAudioClip, Exception exception)
 73        {
 074            loadingState = LoadState.LOADING_FAILED;
 075            OnLoadingFinished?.Invoke(this);
 076        }
 77
 78        IEnumerator TryToLoad()
 79        {
 1480            if (loadingState != LoadState.LOADING_IN_PROGRESS
 81                && loadingState != LoadState.LOADING_COMPLETED)
 82            {
 1483                loadingState = LoadState.LOADING_IN_PROGRESS;
 1484                Model model = (Model) this.model;
 85
 1486                audioClipPromise = new AssetPromise_AudioClip(model.url, scene.contentProvider);
 1487                audioClipPromise.OnSuccessEvent += OnComplete;
 1488                audioClipPromise.OnFailEvent += OnFail;
 89
 1490                AssetPromiseKeeper_AudioClip.i.Keep(audioClipPromise);
 91
 1492                yield return audioClipPromise;
 93            }
 1494        }
 95
 96        void Unload()
 97        {
 098            loadingState = LoadState.IDLE;
 099            AssetPromiseKeeper_AudioClip.i.Forget(audioClipPromise);
 0100        }
 101
 102        public override IEnumerator ApplyChanges(BaseModel newModel)
 103        {
 36104            yield return new WaitUntil(() => CommonScriptableObjects.rendererState.Get());
 105
 106            //If the scene creates and destroy the component before our renderer has been turned on bad things happen!
 107            //TODO: Analyze if we can catch this upstream and stop the IEnumerator
 18108            if (isDisposed)
 0109                yield break;
 110
 18111            Model model = (Model) newModel;
 112
 18113            if (!string.IsNullOrEmpty(model.url))
 114            {
 14115                if (model.shouldTryToLoad && audioClip == null)
 116                {
 14117                    yield return TryToLoad();
 14118                }
 0119                else if (!model.shouldTryToLoad && audioClip != null)
 120                {
 0121                    Unload();
 122                }
 123            }
 124
 18125            yield return null;
 17126        }
 127
 128        public override void Dispose()
 129        {
 0130            isDisposed = true;
 0131            AssetPromiseKeeper_AudioClip.i.Forget(audioClipPromise);
 0132            base.Dispose();
 0133        }
 134    }
 135}