< Summary

Class:DCL.AssetPromise_AudioClip
Assembly:AssetPromiseKeeper
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/AssetManager/AudioClip/AssetPromise_AudioClip.cs
Covered lines:29
Uncovered lines:6
Coverable lines:35
Total lines:94
Line coverage:82.8% (29 of 35)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
AssetPromise_AudioClip(...)0%110100%
AssetPromise_AudioClip(...)0%220100%
OnLoad(...)0%3.583060%
OnCancelLoading()0%220100%
AddToLibrary()0%2.062075%
OnBeforeLoadOrReuse()0%110100%
OnAfterLoadOrReuse()0%110100%
GetId()0%110100%
GetAudioTypeFromUrlName(...)0%7.96062.5%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/AssetManager/AudioClip/AssetPromise_AudioClip.cs

#LineLine coverage
 1using System;
 2using UnityEngine;
 3using UnityEngine.Networking;
 4
 5namespace DCL
 6{
 7    public class AssetPromise_AudioClip : AssetPromise<Asset_AudioClip>
 8    {
 9        private readonly string url;
 10        private readonly string id;
 11        private readonly AudioType audioType;
 12        private readonly IWebRequestController webRequestController;
 13
 14        private WebRequestAsyncOperation webRequestAsyncOperation;
 15
 5416        public AssetPromise_AudioClip(string clipPath, ContentProvider provider) : this(clipPath, provider, Environment.
 17
 2718        public AssetPromise_AudioClip(string clipPath, ContentProvider provider, IWebRequestController webRequestControl
 19        {
 2720            this.url = provider.GetContentsUrl(clipPath);
 2721            this.id = this.url != null ? this.url.Substring(this.url.LastIndexOf('/') + 1) : $"{GetHashCode()}";
 2722            this.audioType = GetAudioTypeFromUrlName(clipPath);
 2723            this.webRequestController = webRequestController;
 2724        }
 25
 26        protected override void OnLoad(Action OnSuccess, Action<Exception> OnFail)
 27        {
 2628            if (string.IsNullOrEmpty(url))
 29            {
 030                OnFail?.Invoke(new Exception("Audio clip url is null or empty"));
 031                return;
 32            }
 33
 2634            webRequestAsyncOperation = webRequestController.GetAudioClip(url, audioType,
 35                request =>
 36                {
 2037                    asset.audioClip = DownloadHandlerAudioClip.GetContent(request.webRequest);
 2038                    OnSuccess?.Invoke();
 2039                },
 40                request =>
 41                {
 342                    OnFail?.Invoke(new Exception($"Audio clip failed to fetch: {request?.webRequest?.error}"));
 343                });
 2644        }
 45
 46        protected override void OnCancelLoading()
 47        {
 648            webRequestAsyncOperation?.Dispose();
 649            webRequestAsyncOperation = null;
 650        }
 51
 52        protected override bool AddToLibrary()
 53        {
 2054            if (!library.Add(asset))
 55            {
 056                return false;
 57            }
 2058            asset = library.Get(asset.id);
 2059            return true;
 60        }
 61
 2962        protected override void OnBeforeLoadOrReuse() { }
 63
 2364        protected override void OnAfterLoadOrReuse() { }
 65
 66        public override object GetId()
 67        {
 14568            return id;
 69        }
 70
 71        private static AudioType GetAudioTypeFromUrlName(string url)
 72        {
 2773            if (string.IsNullOrEmpty(url))
 74            {
 075                Debug.LogError("GetAudioTypeFromUrlName >>> Null url!");
 076                return AudioType.UNKNOWN;
 77            }
 78
 2779            string ext = url.Substring(url.Length - 3).ToLower();
 80
 81            switch (ext)
 82            {
 83                case "mp3":
 084                    return AudioType.MPEG;
 85                case "wav":
 986                    return AudioType.WAV;
 87                case "ogg":
 1588                    return AudioType.OGGVORBIS;
 89                default:
 390                    return AudioType.UNKNOWN;
 91            }
 92        }
 93    }
 94}