< 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:23
Uncovered lines:6
Coverable lines:29
Total lines:100
Line coverage:79.3% (23 of 29)
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%4.123050%
AddToLibrary()0%2.062075%
GetId()0%110100%
GetFontFromResources()0%7.336066.67%

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
 3717        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
 3732        public AssetPromise_Font(string src)
 33        {
 3734            this.src = src;
 3735        }
 36
 1637        protected override void OnAfterLoadOrReuse() { }
 38
 3839        protected override void OnBeforeLoadOrReuse() { }
 40
 41        protected override void OnCancelLoading()
 42        {
 2243            CoroutineStarter.Stop(fontCoroutine);
 2244        }
 45
 46        public override void Cleanup()
 47        {
 3348            base.Cleanup();
 3349            CoroutineStarter.Stop(fontCoroutine);
 3350        }
 51
 52        protected override void OnLoad(Action OnSuccess, Action<Exception> OnFail)
 53        {
 3254            if (fontsMapping.TryGetValue(src, out string fontResourceName))
 55            {
 3256                fontCoroutine = CoroutineStarter.Start(GetFontFromResources(OnSuccess, OnFail, fontResourceName));
 57            }
 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        {
 1066            if (!library.Add(asset))
 67            {
 068                return false;
 69            }
 70
 1071            asset = library.Get(asset.id);
 1072            return true;
 73        }
 74
 75        public override object GetId()
 76        {
 19177            return src;
 78        }
 79
 80        IEnumerator GetFontFromResources(Action OnSuccess, Action<Exception> OnFail, string fontResourceName)
 81        {
 3282            ResourceRequest request = Resources.LoadAsync($"{RESOURCE_FONT_FOLDER}/{fontResourceName}",
 83                typeof(TMP_FontAsset));
 84
 3285            yield return request;
 86
 1087            if (request.asset != null)
 88            {
 1089                asset.font = request.asset as TMP_FontAsset;
 1090                OnSuccess?.Invoke();
 91            }
 92            else
 93            {
 094                string message = $"couldn't fetch font from resources {fontResourceName}";
 095                Debug.Log(message);
 096                OnFail?.Invoke(new Exception(message));
 97            }
 1098        }
 99    }
 100}