< Summary

Class:IntegrationTestSuite_Legacy
Assembly:TestHelpers
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/Tests/IntegrationTestSuite_Legacy.cs
Covered lines:108
Uncovered lines:11
Coverable lines:119
Total lines:281
Line coverage:90.7% (108 of 119)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
SetUp()0%13130100%
TearDown()0%11110100%
TearDown_PromiseKeepers()0%440100%
TearDown_Memory()0%550100%
InitUnityScene()0%9.166055.56%
SetUp_TestScene()0%110100%
SetUp_CharacterController()0%4.054085.71%
SetUp_Camera()0%5.025090%
SetUp_SceneController()0%110100%
SetUp_GlobalScene()0%110100%
SetUp_Renderer()0%110100%
InitScene()0%9.039092.86%
WaitForUICanvasUpdate()0%6200%
Reflection_GetStaticField[T](...)0%2100%
Reflection_GetField[T](...)0%110100%
Reflection_SetField[T](...)0%2100%
CreateTestGameObject(...)0%110100%
InstantiateTestGameObject(...)0%110100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/Tests/IntegrationTestSuite_Legacy.cs

#LineLine coverage
 1using DCL;
 2using DCL.Controllers;
 3using DCL.Helpers;
 4using DCL.Models;
 5using Newtonsoft.Json;
 6using System.Collections;
 7using System.Collections.Generic;
 8using System.Configuration;
 9using System.Linq;
 10using System.Reflection;
 11using DCL.Camera;
 12using NUnit.Framework;
 13using UnityEngine;
 14using UnityEngine.SceneManagement;
 15using UnityEngine.TestTools;
 16using Assert = UnityEngine.Assertions.Assert;
 17using DCL.Tutorial;
 18using NSubstitute;
 19using UnityEditor;
 20
 21public class IntegrationTestSuite_Legacy
 22{
 9923    protected virtual string TEST_SCENE_NAME => "MainTest";
 24
 25    protected bool sceneInitialized = false;
 26    protected ISceneController sceneController;
 27    protected ParcelScene scene;
 28    protected DCL.Camera.CameraController cameraController;
 29
 30    /// <summary>
 31    /// Use this as a parent for your dynamically created gameobjects in tests
 32    /// so they are cleaned up automatically in the teardown
 33    /// </summary>
 34    private GameObject runtimeGameObjectsRoot;
 35
 36536    protected virtual bool justSceneSetUp => false;
 037    protected virtual bool enableSceneIntegrityChecker => true;
 38
 39    protected TestSceneIntegrityChecker testSceneIntegrityChecker;
 40
 41    [UnitySetUp]
 42    protected virtual IEnumerator SetUp()
 43    {
 50144        DCL.Configuration.EnvironmentSettings.RUNNING_TESTS = true;
 45
 50146        if (!sceneInitialized)
 47        {
 9948            yield return InitUnityScene(TEST_SCENE_NAME);
 9949            sceneInitialized = true;
 50        }
 51
 50152        runtimeGameObjectsRoot = new GameObject("_RuntimeGameObjectsRoot");
 53
 50154        testSceneIntegrityChecker = new TestSceneIntegrityChecker();
 55
 56        //NOTE(Brian): integrity checker is disabled in batch mode to make it run faster in CI
 50157        if (Application.isBatchMode || !enableSceneIntegrityChecker)
 58        {
 50159            testSceneIntegrityChecker.enabled = false;
 60        }
 61
 50162        if (justSceneSetUp)
 63        {
 13664            RenderProfileManifest.i.Initialize();
 65
 13666            Environment.SetupWithBuilders
 67            (
 68                MessagingContextFactory.CreateDefault,
 69                PlatformContextFactory.CreateDefault,
 70                WorldRuntimeContextFactory.CreateDefault,
 71                HUDContextFactory.CreateDefault
 72            );
 73
 13674            SetUp_SceneController();
 75
 13676            SetUp_TestScene();
 13677            SetUp_Renderer();
 13678            yield return testSceneIntegrityChecker.SaveSceneSnapshot();
 13679            yield return null;
 80            //TODO(Brian): Remove when the init layer is ready
 13681            Environment.i.platform.cullingController.Stop();
 13682            yield break;
 83        }
 84
 36585        RenderProfileManifest.i.Initialize();
 86
 36587        Environment.SetupWithBuilders
 88        (
 89            MessagingContextFactory.CreateDefault,
 90            PlatformContextFactory.CreateDefault,
 91            WorldRuntimeContextFactory.CreateDefault,
 92            HUDContextFactory.CreateDefault
 93        );
 94
 36595        SetUp_SceneController();
 36596        SetUp_TestScene();
 97
 36598        yield return SetUp_Camera();
 36599        yield return SetUp_CharacterController();
 365100        SetUp_Renderer();
 365101        yield return testSceneIntegrityChecker.SaveSceneSnapshot();
 365102        yield return null;
 103        //TODO(Brian): Remove when the init layer is ready
 365104        Environment.i.platform.cullingController.Stop();
 365105    }
 106
 107    [UnityTearDown]
 108    protected virtual IEnumerator TearDown()
 109    {
 507110        yield return null;
 111
 507112        if (runtimeGameObjectsRoot != null)
 501113            Object.Destroy(runtimeGameObjectsRoot.gameObject);
 114
 507115        if (DCLCharacterController.i != null)
 116        {
 507117            DCLCharacterController.i.ResumeGravity();
 507118            DCLCharacterController.i.enabled = true;
 119
 507120            if (DCLCharacterController.i.characterController != null)
 507121                DCLCharacterController.i.characterController.enabled = true;
 122        }
 123
 507124        Environment.Dispose();
 125
 507126        yield return TearDown_Memory();
 127
 507128        if (MapRenderer.i != null)
 491129            MapRenderer.i.Cleanup();
 130
 507131        yield return testSceneIntegrityChecker?.TestSceneSnapshot();
 507132    }
 133
 134    protected void TearDown_PromiseKeepers()
 135    {
 507136        AssetPromiseKeeper_GLTF.i?.Cleanup();
 507137        AssetPromiseKeeper_AB_GameObject.i?.Cleanup();
 507138        AssetPromiseKeeper_AB.i?.Cleanup();
 507139    }
 140
 141    protected IEnumerator TearDown_Memory()
 142    {
 507143        TearDown_PromiseKeepers();
 144
 507145        if (Environment.i.platform.memoryManager != null)
 507146            yield return Environment.i.platform.memoryManager.CleanupPoolsIfNeeded(true);
 147
 507148        if (PoolManager.i != null)
 507149            PoolManager.i.Cleanup();
 150
 507151        Caching.ClearCache();
 507152        Resources.UnloadUnusedAssets();
 507153    }
 154
 155    protected IEnumerator InitUnityScene(string sceneName = null)
 156    {
 105157        yield return TestHelpers.UnloadAllUnityScenes();
 158
 159        Scene? newScene;
 160
 105161        if (string.IsNullOrEmpty(sceneName))
 162        {
 0163            newScene = SceneManager.CreateScene(TestHelpers.testingSceneName + (TestHelpers.testSceneIteration++));
 0164            if (newScene.HasValue)
 165            {
 0166                SceneManager.SetActiveScene(newScene.Value);
 167            }
 0168        }
 169        else
 170        {
 105171            yield return SceneManager.LoadSceneAsync(sceneName);
 105172            SceneManager.SetActiveScene(SceneManager.GetSceneByName(sceneName));
 173        }
 105174    }
 175
 1020176    public void SetUp_TestScene() { scene = sceneController.CreateTestScene() as ParcelScene; }
 177
 178    public virtual IEnumerator SetUp_CharacterController()
 179    {
 405180        if (DCLCharacterController.i == null)
 181        {
 0182            GameObject.Instantiate(Resources.Load("Prefabs/CharacterController"));
 183        }
 184
 405185        yield return null;
 405186        Assert.IsTrue(DCLCharacterController.i != null);
 405187        DCLCharacterController.i.gameObject.SetActive(true);
 405188        DCLCharacterController.i.characterController.enabled = true;
 405189    }
 190
 191    public virtual IEnumerator SetUp_Camera()
 192    {
 365193        cameraController = GameObject.FindObjectOfType<DCL.Camera.CameraController>();
 194
 365195        if (cameraController == null)
 0196            cameraController = GameObject.Instantiate(Resources.Load<GameObject>("CameraController")).GetComponent<Camer
 197
 365198        yield return null;
 199
 365200        var tpsMode = cameraController.GetCameraMode(CameraMode.ModeId.ThirdPerson) as CameraStateTPS;
 201
 365202        if ( tpsMode != null )
 203        {
 365204            tpsMode.cameraDampOnGroundType.settings.enabled = false;
 365205            tpsMode.cameraFreefall.settings.enabled = false;
 365206            tpsMode.cameraDampOnSprint.settings.enabled = false;
 207        }
 365208    }
 209
 210    public void SetUp_SceneController()
 211    {
 508212        PoolManager.enablePrewarm = false;
 508213        DCL.Configuration.ParcelSettings.VISUAL_LOADING_ENABLED = false;
 508214        sceneController = Environment.i.world.sceneController;
 508215        sceneController.deferredMessagesDecoding = false;
 508216        sceneController.prewarmSceneMessagesPool = false;
 508217        sceneController.prewarmEntitiesPool = false;
 508218    }
 219
 220    private void SetUp_GlobalScene()
 221    {
 6222        string globalSceneId = "global-scene";
 223
 6224        sceneController.CreateGlobalScene(
 225            JsonConvert.SerializeObject(
 226                new CreateGlobalSceneMessage
 227                {
 228                    id = globalSceneId,
 229                    baseUrl = "",
 230                })
 231        );
 6232    }
 233
 1014234    public virtual void SetUp_Renderer() { CommonScriptableObjects.rendererState.Set(true); }
 235
 236    protected virtual IEnumerator InitScene(bool spawnCharController = true, bool spawnTestScene = true, bool spawnUISce
 237    {
 6238        yield return InitUnityScene(TEST_SCENE_NAME);
 239
 6240        if (debugMode)
 0241            Environment.i.platform.debugController.SetDebug();
 242
 6243        SetUp_SceneController();
 244
 6245        if (spawnTestScene)
 6246            SetUp_TestScene();
 247
 6248        if (spawnCharController)
 6249            yield return SetUp_CharacterController();
 250
 6251        var newPos = new Vector3(10, 0, 10);
 6252        DCLCharacterController.i.SetPosition(newPos);
 6253        yield return null;
 254
 6255        if (spawnUIScene)
 256        {
 6257            SetUp_GlobalScene();
 258        }
 6259    }
 260
 0261    protected IEnumerator WaitForUICanvasUpdate() { yield break; }
 262
 0263    public static T Reflection_GetStaticField<T>(System.Type baseType, string fieldName) { return (T) baseType.GetField(
 264
 5265    public static T Reflection_GetField<T>(object instance, string fieldName) { return (T) instance.GetType().GetField(f
 266
 0267    public static void Reflection_SetField<T>(object instance, string fieldName, T newValue) { instance.GetType().GetFie
 268
 269    protected GameObject CreateTestGameObject(string name)
 270    {
 22271        GameObject gameObject = new GameObject(name);
 22272        gameObject.transform.SetParent(runtimeGameObjectsRoot.transform);
 22273        return gameObject;
 274    }
 275
 276    protected GameObject InstantiateTestGameObject(GameObject reference)
 277    {
 2278        GameObject gameObject = Object.Instantiate(reference, runtimeGameObjectsRoot.transform, true);
 2279        return gameObject;
 280    }
 281}