< Summary

Class:DCL.Components.DCLFont
Assembly:MainScripts
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/Font/DCLFont.cs
Covered lines:23
Uncovered lines:12
Coverable lines:35
Total lines:95
Line coverage:65.7% (23 of 35)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
GetDataFromJSON(...)0%110100%
DCLFont()0%110100%
GetClassId()0%2100%
SetFontFromComponent()0%12.638058.33%
ApplyChanges()0%6.26082.35%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/Font/DCLFont.cs

#LineLine coverage
 1using System.Collections;
 2using System.Collections.Generic;
 3using DCL.Controllers;
 4using DCL.Helpers;
 5using DCL.Models;
 6using TMPro;
 7using UnityEngine;
 8
 9namespace DCL.Components
 10{
 11    public class DCLFont : BaseDisposable
 12    {
 13        const string RESOURCE_FONT_PREFIX = "builtin:";
 14        const string RESOURCE_FONT_FOLDER = "Fonts & Materials";
 15
 16        [System.Serializable]
 17        public class Model : BaseModel
 18        {
 19            public string src;
 20
 321            public override BaseModel GetDataFromJSON(string json) { return Utils.SafeFromJson<Model>(json); }
 22        }
 23
 024        public bool loaded { private set; get; } = false;
 025        public bool error { private set; get; } = false;
 26
 027        public TMP_FontAsset fontAsset { private set; get; }
 28
 929        public DCLFont() { model = new Model(); }
 30
 031        public override int GetClassId() { return (int) CLASS_ID.FONT; }
 32
 33        public static IEnumerator SetFontFromComponent(IParcelScene scene, string componentId, TMP_Text text)
 34        {
 335            if (!scene.disposableComponents.ContainsKey(componentId))
 36            {
 037                Debug.Log($"couldn't fetch font, the DCLFont component with id {componentId} doesn't exist");
 038                yield break;
 39            }
 40
 341            DCLFont fontComponent = scene.disposableComponents[componentId] as DCLFont;
 342            if (fontComponent == null)
 43            {
 044                Debug.Log($"couldn't fetch font, the shared component with id {componentId} is NOT a DCLFont");
 045                yield break;
 46            }
 47
 348            while (!fontComponent.loaded && !fontComponent.error)
 49            {
 050                yield return null;
 51            }
 52
 353            if (!fontComponent.error)
 54            {
 255                text.font = fontComponent.fontAsset;
 56            }
 357        }
 58
 59        public override IEnumerator ApplyChanges(BaseModel newModel)
 60        {
 361            Model model = (Model) newModel;
 62
 363            if (string.IsNullOrEmpty(model.src))
 64            {
 065                error = true;
 066                yield break;
 67            }
 68
 369            if (model.src.StartsWith(RESOURCE_FONT_PREFIX))
 70            {
 271                string resourceName = model.src.Substring(RESOURCE_FONT_PREFIX.Length);
 72
 273                ResourceRequest request = Resources.LoadAsync(string.Format("{0}/{1}", RESOURCE_FONT_FOLDER, resourceNam
 274                yield return request;
 75
 276                if (request.asset != null)
 77                {
 278                    fontAsset = request.asset as TMP_FontAsset;
 279                }
 80                else
 81                {
 082                    Debug.Log($"couldn't fetch font from resources {resourceName}");
 83                }
 84
 285                loaded = true;
 286                error = fontAsset == null;
 287            }
 88            else
 89            {
 90                // NOTE: only support fonts in resources
 191                error = true;
 92            }
 393        }
 94    }
 95}