| | 1 | | using DCL; |
| | 2 | | using DCL.Controllers; |
| | 3 | | using DCL.Helpers; |
| | 4 | | using DCL.Models; |
| | 5 | | using Newtonsoft.Json; |
| | 6 | | using System.Collections; |
| | 7 | | using System.Collections.Generic; |
| | 8 | | using System.Configuration; |
| | 9 | | using System.Linq; |
| | 10 | | using System.Reflection; |
| | 11 | | using DCL.Camera; |
| | 12 | | using NUnit.Framework; |
| | 13 | | using UnityEngine; |
| | 14 | | using UnityEngine.SceneManagement; |
| | 15 | | using UnityEngine.TestTools; |
| | 16 | | using Assert = UnityEngine.Assertions.Assert; |
| | 17 | | using DCL.Tutorial; |
| | 18 | | using NSubstitute; |
| | 19 | | using UnityEditor; |
| | 20 | |
|
| | 21 | | public class IntegrationTestSuite_Legacy |
| | 22 | | { |
| 99 | 23 | | 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 | |
|
| 365 | 36 | | protected virtual bool justSceneSetUp => false; |
| 0 | 37 | | protected virtual bool enableSceneIntegrityChecker => true; |
| | 38 | |
|
| | 39 | | protected TestSceneIntegrityChecker testSceneIntegrityChecker; |
| | 40 | |
|
| | 41 | | [UnitySetUp] |
| | 42 | | protected virtual IEnumerator SetUp() |
| | 43 | | { |
| 501 | 44 | | DCL.Configuration.EnvironmentSettings.RUNNING_TESTS = true; |
| | 45 | |
|
| 501 | 46 | | if (!sceneInitialized) |
| | 47 | | { |
| 99 | 48 | | yield return InitUnityScene(TEST_SCENE_NAME); |
| 99 | 49 | | sceneInitialized = true; |
| | 50 | | } |
| | 51 | |
|
| 501 | 52 | | runtimeGameObjectsRoot = new GameObject("_RuntimeGameObjectsRoot"); |
| | 53 | |
|
| 501 | 54 | | testSceneIntegrityChecker = new TestSceneIntegrityChecker(); |
| | 55 | |
|
| | 56 | | //NOTE(Brian): integrity checker is disabled in batch mode to make it run faster in CI |
| 501 | 57 | | if (Application.isBatchMode || !enableSceneIntegrityChecker) |
| | 58 | | { |
| 501 | 59 | | testSceneIntegrityChecker.enabled = false; |
| | 60 | | } |
| | 61 | |
|
| 501 | 62 | | if (justSceneSetUp) |
| | 63 | | { |
| 136 | 64 | | RenderProfileManifest.i.Initialize(); |
| | 65 | |
|
| 136 | 66 | | Environment.SetupWithBuilders |
| | 67 | | ( |
| | 68 | | MessagingContextFactory.CreateDefault, |
| | 69 | | PlatformContextFactory.CreateDefault, |
| | 70 | | WorldRuntimeContextFactory.CreateDefault, |
| | 71 | | HUDContextFactory.CreateDefault |
| | 72 | | ); |
| | 73 | |
|
| 136 | 74 | | SetUp_SceneController(); |
| | 75 | |
|
| 136 | 76 | | SetUp_TestScene(); |
| 136 | 77 | | SetUp_Renderer(); |
| 136 | 78 | | yield return testSceneIntegrityChecker.SaveSceneSnapshot(); |
| 136 | 79 | | yield return null; |
| | 80 | | //TODO(Brian): Remove when the init layer is ready |
| 136 | 81 | | Environment.i.platform.cullingController.Stop(); |
| 136 | 82 | | yield break; |
| | 83 | | } |
| | 84 | |
|
| 365 | 85 | | RenderProfileManifest.i.Initialize(); |
| | 86 | |
|
| 365 | 87 | | Environment.SetupWithBuilders |
| | 88 | | ( |
| | 89 | | MessagingContextFactory.CreateDefault, |
| | 90 | | PlatformContextFactory.CreateDefault, |
| | 91 | | WorldRuntimeContextFactory.CreateDefault, |
| | 92 | | HUDContextFactory.CreateDefault |
| | 93 | | ); |
| | 94 | |
|
| 365 | 95 | | SetUp_SceneController(); |
| 365 | 96 | | SetUp_TestScene(); |
| | 97 | |
|
| 365 | 98 | | yield return SetUp_Camera(); |
| 365 | 99 | | yield return SetUp_CharacterController(); |
| 365 | 100 | | SetUp_Renderer(); |
| 365 | 101 | | yield return testSceneIntegrityChecker.SaveSceneSnapshot(); |
| 365 | 102 | | yield return null; |
| | 103 | | //TODO(Brian): Remove when the init layer is ready |
| 365 | 104 | | Environment.i.platform.cullingController.Stop(); |
| 365 | 105 | | } |
| | 106 | |
|
| | 107 | | [UnityTearDown] |
| | 108 | | protected virtual IEnumerator TearDown() |
| | 109 | | { |
| 507 | 110 | | yield return null; |
| | 111 | |
|
| 507 | 112 | | if (runtimeGameObjectsRoot != null) |
| 501 | 113 | | Object.Destroy(runtimeGameObjectsRoot.gameObject); |
| | 114 | |
|
| 507 | 115 | | if (DCLCharacterController.i != null) |
| | 116 | | { |
| 507 | 117 | | DCLCharacterController.i.ResumeGravity(); |
| 507 | 118 | | DCLCharacterController.i.enabled = true; |
| | 119 | |
|
| 507 | 120 | | if (DCLCharacterController.i.characterController != null) |
| 507 | 121 | | DCLCharacterController.i.characterController.enabled = true; |
| | 122 | | } |
| | 123 | |
|
| 507 | 124 | | Environment.Dispose(); |
| | 125 | |
|
| 507 | 126 | | yield return TearDown_Memory(); |
| | 127 | |
|
| 507 | 128 | | if (MapRenderer.i != null) |
| 491 | 129 | | MapRenderer.i.Cleanup(); |
| | 130 | |
|
| 507 | 131 | | yield return testSceneIntegrityChecker?.TestSceneSnapshot(); |
| 507 | 132 | | } |
| | 133 | |
|
| | 134 | | protected void TearDown_PromiseKeepers() |
| | 135 | | { |
| 507 | 136 | | AssetPromiseKeeper_GLTF.i?.Cleanup(); |
| 507 | 137 | | AssetPromiseKeeper_AB_GameObject.i?.Cleanup(); |
| 507 | 138 | | AssetPromiseKeeper_AB.i?.Cleanup(); |
| 507 | 139 | | } |
| | 140 | |
|
| | 141 | | protected IEnumerator TearDown_Memory() |
| | 142 | | { |
| 507 | 143 | | TearDown_PromiseKeepers(); |
| | 144 | |
|
| 507 | 145 | | if (Environment.i.platform.memoryManager != null) |
| 507 | 146 | | yield return Environment.i.platform.memoryManager.CleanupPoolsIfNeeded(true); |
| | 147 | |
|
| 507 | 148 | | if (PoolManager.i != null) |
| 507 | 149 | | PoolManager.i.Cleanup(); |
| | 150 | |
|
| 507 | 151 | | Caching.ClearCache(); |
| 507 | 152 | | Resources.UnloadUnusedAssets(); |
| 507 | 153 | | } |
| | 154 | |
|
| | 155 | | protected IEnumerator InitUnityScene(string sceneName = null) |
| | 156 | | { |
| 105 | 157 | | yield return TestHelpers.UnloadAllUnityScenes(); |
| | 158 | |
|
| | 159 | | Scene? newScene; |
| | 160 | |
|
| 105 | 161 | | if (string.IsNullOrEmpty(sceneName)) |
| | 162 | | { |
| 0 | 163 | | newScene = SceneManager.CreateScene(TestHelpers.testingSceneName + (TestHelpers.testSceneIteration++)); |
| 0 | 164 | | if (newScene.HasValue) |
| | 165 | | { |
| 0 | 166 | | SceneManager.SetActiveScene(newScene.Value); |
| | 167 | | } |
| 0 | 168 | | } |
| | 169 | | else |
| | 170 | | { |
| 105 | 171 | | yield return SceneManager.LoadSceneAsync(sceneName); |
| 105 | 172 | | SceneManager.SetActiveScene(SceneManager.GetSceneByName(sceneName)); |
| | 173 | | } |
| 105 | 174 | | } |
| | 175 | |
|
| 1020 | 176 | | public void SetUp_TestScene() { scene = sceneController.CreateTestScene() as ParcelScene; } |
| | 177 | |
|
| | 178 | | public virtual IEnumerator SetUp_CharacterController() |
| | 179 | | { |
| 405 | 180 | | if (DCLCharacterController.i == null) |
| | 181 | | { |
| 0 | 182 | | GameObject.Instantiate(Resources.Load("Prefabs/CharacterController")); |
| | 183 | | } |
| | 184 | |
|
| 405 | 185 | | yield return null; |
| 405 | 186 | | Assert.IsTrue(DCLCharacterController.i != null); |
| 405 | 187 | | DCLCharacterController.i.gameObject.SetActive(true); |
| 405 | 188 | | DCLCharacterController.i.characterController.enabled = true; |
| 405 | 189 | | } |
| | 190 | |
|
| | 191 | | public virtual IEnumerator SetUp_Camera() |
| | 192 | | { |
| 365 | 193 | | cameraController = GameObject.FindObjectOfType<DCL.Camera.CameraController>(); |
| | 194 | |
|
| 365 | 195 | | if (cameraController == null) |
| 0 | 196 | | cameraController = GameObject.Instantiate(Resources.Load<GameObject>("CameraController")).GetComponent<Camer |
| | 197 | |
|
| 365 | 198 | | yield return null; |
| | 199 | |
|
| 365 | 200 | | var tpsMode = cameraController.GetCameraMode(CameraMode.ModeId.ThirdPerson) as CameraStateTPS; |
| | 201 | |
|
| 365 | 202 | | if ( tpsMode != null ) |
| | 203 | | { |
| 365 | 204 | | tpsMode.cameraDampOnGroundType.settings.enabled = false; |
| 365 | 205 | | tpsMode.cameraFreefall.settings.enabled = false; |
| 365 | 206 | | tpsMode.cameraDampOnSprint.settings.enabled = false; |
| | 207 | | } |
| 365 | 208 | | } |
| | 209 | |
|
| | 210 | | public void SetUp_SceneController() |
| | 211 | | { |
| 508 | 212 | | PoolManager.enablePrewarm = false; |
| 508 | 213 | | DCL.Configuration.ParcelSettings.VISUAL_LOADING_ENABLED = false; |
| 508 | 214 | | sceneController = Environment.i.world.sceneController; |
| 508 | 215 | | sceneController.deferredMessagesDecoding = false; |
| 508 | 216 | | sceneController.prewarmSceneMessagesPool = false; |
| 508 | 217 | | sceneController.prewarmEntitiesPool = false; |
| 508 | 218 | | } |
| | 219 | |
|
| | 220 | | private void SetUp_GlobalScene() |
| | 221 | | { |
| 6 | 222 | | string globalSceneId = "global-scene"; |
| | 223 | |
|
| 6 | 224 | | sceneController.CreateGlobalScene( |
| | 225 | | JsonConvert.SerializeObject( |
| | 226 | | new CreateGlobalSceneMessage |
| | 227 | | { |
| | 228 | | id = globalSceneId, |
| | 229 | | baseUrl = "", |
| | 230 | | }) |
| | 231 | | ); |
| 6 | 232 | | } |
| | 233 | |
|
| 1014 | 234 | | public virtual void SetUp_Renderer() { CommonScriptableObjects.rendererState.Set(true); } |
| | 235 | |
|
| | 236 | | protected virtual IEnumerator InitScene(bool spawnCharController = true, bool spawnTestScene = true, bool spawnUISce |
| | 237 | | { |
| 6 | 238 | | yield return InitUnityScene(TEST_SCENE_NAME); |
| | 239 | |
|
| 6 | 240 | | if (debugMode) |
| 0 | 241 | | Environment.i.platform.debugController.SetDebug(); |
| | 242 | |
|
| 6 | 243 | | SetUp_SceneController(); |
| | 244 | |
|
| 6 | 245 | | if (spawnTestScene) |
| 6 | 246 | | SetUp_TestScene(); |
| | 247 | |
|
| 6 | 248 | | if (spawnCharController) |
| 6 | 249 | | yield return SetUp_CharacterController(); |
| | 250 | |
|
| 6 | 251 | | var newPos = new Vector3(10, 0, 10); |
| 6 | 252 | | DCLCharacterController.i.SetPosition(newPos); |
| 6 | 253 | | yield return null; |
| | 254 | |
|
| 6 | 255 | | if (spawnUIScene) |
| | 256 | | { |
| 6 | 257 | | SetUp_GlobalScene(); |
| | 258 | | } |
| 6 | 259 | | } |
| | 260 | |
|
| 0 | 261 | | protected IEnumerator WaitForUICanvasUpdate() { yield break; } |
| | 262 | |
|
| 0 | 263 | | public static T Reflection_GetStaticField<T>(System.Type baseType, string fieldName) { return (T) baseType.GetField( |
| | 264 | |
|
| 5 | 265 | | public static T Reflection_GetField<T>(object instance, string fieldName) { return (T) instance.GetType().GetField(f |
| | 266 | |
|
| 0 | 267 | | public static void Reflection_SetField<T>(object instance, string fieldName, T newValue) { instance.GetType().GetFie |
| | 268 | |
|
| | 269 | | protected GameObject CreateTestGameObject(string name) |
| | 270 | | { |
| 22 | 271 | | GameObject gameObject = new GameObject(name); |
| 22 | 272 | | gameObject.transform.SetParent(runtimeGameObjectsRoot.transform); |
| 22 | 273 | | return gameObject; |
| | 274 | | } |
| | 275 | |
|
| | 276 | | protected GameObject InstantiateTestGameObject(GameObject reference) |
| | 277 | | { |
| 2 | 278 | | GameObject gameObject = Object.Instantiate(reference, runtimeGameObjectsRoot.transform, true); |
| 2 | 279 | | return gameObject; |
| | 280 | | } |
| | 281 | | } |