< Summary

Class:DCL.AssetPromise_Font
Assembly:AssetPromiseKeeper
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/AssetManager/Font/AssetPromise_Font.cs
Covered lines:25
Uncovered lines:6
Coverable lines:31
Total lines:100
Line coverage:80.6% (25 of 31)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
AssetPromise_Font(...)0%110100%
OnAfterLoadOrReuse()0%110100%
OnBeforeLoadOrReuse()0%110100%
OnCancelLoading()0%110100%
Cleanup()0%110100%
OnLoad(...)0%3.583060%
AddToLibrary()0%2.062075%
GetId()0%110100%
GetFontFromResources()0%6.976070%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/AssetManager/Font/AssetPromise_Font.cs

#LineLine coverage
 1using System;
 2using System.Collections;
 3using System.Collections.Generic;
 4using TMPro;
 5using UnityEngine;
 6
 7namespace DCL
 8{
 9    public class AssetPromise_Font : AssetPromise<Asset_Font>
 10    {
 11        private const string RESOURCE_FONT_FOLDER = "Fonts & Materials";
 12        private const string DEFAULT_SANS_SERIF_HEAVY = "Inter-Heavy SDF";
 13        private const string DEFAULT_SANS_SERIF_BOLD = "Inter-Bold SDF";
 14        private const string DEFAULT_SANS_SERIF_SEMIBOLD = "Inter-SemiBold SDF";
 15        private const string DEFAULT_SANS_SERIF = "Inter-Regular SDF";
 16
 3817        private readonly Dictionary<string, string> fontsMapping = new Dictionary<string, string>()
 18        {
 19            { "builtin:SF-UI-Text-Regular SDF", DEFAULT_SANS_SERIF },
 20            { "builtin:SF-UI-Text-Heavy SDF", DEFAULT_SANS_SERIF_HEAVY },
 21            { "builtin:SF-UI-Text-Semibold SDF", DEFAULT_SANS_SERIF_SEMIBOLD },
 22            { "builtin:LiberationSans SDF", "LiberationSans SDF" },
 23            { "SansSerif", DEFAULT_SANS_SERIF },
 24            { "SansSerif_Heavy", DEFAULT_SANS_SERIF_HEAVY },
 25            { "SansSerif_Bold", DEFAULT_SANS_SERIF_BOLD },
 26            { "SansSerif_SemiBold", DEFAULT_SANS_SERIF_SEMIBOLD },
 27        };
 28
 29        private string src;
 30        private Coroutine fontCoroutine;
 31
 3832        public AssetPromise_Font(string src)
 33        {
 3834            this.src = src;
 3835        }
 36
 1737        protected override void OnAfterLoadOrReuse() { }
 38
 3939        protected override void OnBeforeLoadOrReuse() { }
 40
 41        protected override void OnCancelLoading()
 42        {
 2243            CoroutineStarter.Stop(fontCoroutine);
 2244        }
 45
 46        public override void Cleanup()
 47        {
 3448            base.Cleanup();
 3449            CoroutineStarter.Stop(fontCoroutine);
 3450        }
 51
 52        protected override void OnLoad(Action OnSuccess, Action<Exception> OnFail)
 53        {
 3354            if (fontsMapping.TryGetValue(src, out string fontResourceName))
 55            {
 3356                fontCoroutine = CoroutineStarter.Start(GetFontFromResources(OnSuccess, OnFail, fontResourceName));
 3357            }
 58            else
 59            {
 060                OnFail?.Invoke(new Exception("Font doesn't correspond with any know font"));
 61            }
 062        }
 63
 64        protected override bool AddToLibrary()
 65        {
 1166            if (!library.Add(asset))
 67            {
 068                return false;
 69            }
 70
 1171            asset = library.Get(asset.id);
 1172            return true;
 73        }
 74
 75        public override object GetId()
 76        {
 19777            return src;
 78        }
 79
 80        IEnumerator GetFontFromResources(Action OnSuccess, Action<Exception> OnFail, string fontResourceName)
 81        {
 3382            ResourceRequest request = Resources.LoadAsync($"{RESOURCE_FONT_FOLDER}/{fontResourceName}",
 83                typeof(TMP_FontAsset));
 84
 3385            yield return request;
 86
 1187            if (request.asset != null)
 88            {
 1189                asset.font = request.asset as TMP_FontAsset;
 1190                OnSuccess?.Invoke();
 1191            }
 92            else
 93            {
 094                string message = $"couldn't fetch font from resources {fontResourceName}";
 095                Debug.Log(message);
 096                OnFail?.Invoke(new Exception(message));
 97            }
 1198        }
 99    }
 100}