< 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:32
Uncovered lines:19
Coverable lines:51
Total lines:142
Line coverage:62.7% (32 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%660100%
Unload()0%12300%
ApplyChanges()0%14.2512075%
Dispose()0%110100%

File(s)

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

#LineLine coverage
 1using DCL.Helpers;
 2using DCL.Models;
 3using System;
 4using System.Collections;
 5using UnityEngine;
 6
 7namespace DCL.Components
 8{
 9    public class DCLAudioClip : BaseDisposable
 10    {
 11        [System.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
 27        public enum LoadState
 28        {
 29            IDLE,
 30            LOADING_IN_PROGRESS,
 31            LOADING_FAILED,
 32            LOADING_COMPLETED,
 33        }
 34
 035        public LoadState loadingState { get; private set; }
 36        public event Action<DCLAudioClip> OnLoadingFinished;
 37
 1738        public DCLAudioClip()
 39        {
 1740            model = new Model();
 41
 1742            loadingState = LoadState.IDLE;
 1743        }
 44
 045        public double volume => ((Model) model).volume;
 46
 047        public bool isLoop => ((Model) model).loop;
 48
 049        public bool shouldTryLoad => ((Model) model).shouldTryToLoad;
 50
 051        public override int GetClassId() { return (int) CLASS_ID.AUDIO_CLIP; }
 52
 53        void OnComplete(AudioClip clip)
 54        {
 1355            if (clip != null)
 56            {
 1357                this.audioClip = clip;
 1358                loadingState = LoadState.LOADING_COMPLETED;
 1359            }
 60            else
 61            {
 062                loadingState = LoadState.LOADING_FAILED;
 63            }
 64
 1365            if (OnLoadingFinished != null)
 66            {
 067                OnLoadingFinished.Invoke(this);
 68            }
 1369        }
 70
 71        void OnFail(string error)
 72        {
 073            loadingState = LoadState.LOADING_FAILED;
 74
 075            if (OnLoadingFinished != null)
 76            {
 077                OnLoadingFinished.Invoke(this);
 78            }
 079        }
 80
 81        IEnumerator TryToLoad()
 82        {
 1483            if (loadingState != LoadState.LOADING_IN_PROGRESS
 84                && loadingState != LoadState.LOADING_COMPLETED)
 85            {
 1486                loadingState = LoadState.LOADING_IN_PROGRESS;
 1487                Model model = (Model) this.model;
 1488                if (scene.contentProvider.HasContentsUrl(model.url))
 89                {
 1490                    yield return Utils.FetchAudioClip(
 91                        scene.contentProvider.GetContentsUrl(model.url),
 92                        Utils.GetAudioTypeFromUrlName(model.url),
 93                        OnComplete,
 94                        OnFail);
 95                }
 96            }
 1397        }
 98
 99        void Unload()
 100        {
 0101            if (audioClip != null && loadingState != LoadState.IDLE)
 102            {
 0103                audioClip.UnloadAudioData();
 0104                audioClip = null;
 0105                loadingState = LoadState.IDLE;
 106            }
 0107        }
 108
 109        public override IEnumerator ApplyChanges(BaseModel newModel)
 110        {
 36111            yield return new WaitUntil(() => CommonScriptableObjects.rendererState.Get());
 112
 113            //If the scene creates and destroy the component before our renderer has been turned on bad things happen!
 114            //TODO: Analyze if we can catch this upstream and stop the IEnumerator
 18115            if (isDisposed)
 0116                yield break;
 117
 18118            Model model = (Model) newModel;
 119
 18120            if (!string.IsNullOrEmpty(model.url))
 121            {
 14122                if (model.shouldTryToLoad && audioClip == null)
 123                {
 14124                    yield return TryToLoad();
 13125                }
 0126                else if (!model.shouldTryToLoad && audioClip != null)
 127                {
 0128                    Unload();
 129                }
 130            }
 131
 17132            yield return null;
 17133        }
 134
 135        public override void Dispose()
 136        {
 17137            isDisposed = true;
 17138            Utils.SafeDestroy(audioClip);
 17139            base.Dispose();
 17140        }
 141    }
 142}