| | 1 | | using System; |
| | 2 | | using System.Collections; |
| | 3 | | using System.Linq; |
| | 4 | | using DCL; |
| | 5 | | using DCL.Components; |
| | 6 | | using DCL.Configuration; |
| | 7 | | using DCL.Controllers; |
| | 8 | | using DCL.Helpers; |
| | 9 | | using DCL.Models; |
| | 10 | | using Newtonsoft.Json; |
| | 11 | | using NUnit.Framework; |
| | 12 | | using UnityEngine; |
| | 13 | | using UnityEngine.TestTools; |
| | 14 | | using UnityGLTF; |
| | 15 | | using Environment = DCL.Environment; |
| | 16 | | using WaitUntil = UnityEngine.WaitUntil; |
| | 17 | |
|
| | 18 | | namespace Tests |
| | 19 | | { |
| | 20 | | public class UUIDComponentShould : IntegrationTestSuite |
| | 21 | | { |
| | 22 | | private ParcelScene scene; |
| | 23 | |
|
| | 24 | | protected override WorldRuntimeContext CreateRuntimeContext() |
| | 25 | | { |
| 4 | 26 | | return DCL.Tests.WorldRuntimeContextFactory.CreateWithCustomMocks |
| | 27 | | ( |
| | 28 | | sceneController: new SceneController(), |
| | 29 | | state: new WorldState(), |
| | 30 | | componentFactory: new RuntimeComponentFactory() |
| | 31 | | ); |
| | 32 | | } |
| | 33 | |
|
| | 34 | | protected override PlatformContext CreatePlatformContext() |
| | 35 | | { |
| 4 | 36 | | return DCL.Tests.PlatformContextFactory.CreateWithGenericMocks( |
| | 37 | | WebRequestController.Create() |
| | 38 | | ); |
| | 39 | | } |
| | 40 | |
|
| | 41 | | [UnitySetUp] |
| | 42 | | protected override IEnumerator SetUp() |
| | 43 | | { |
| 4 | 44 | | yield return base.SetUp(); |
| 4 | 45 | | scene = Environment.i.world.sceneController.CreateTestScene() as ParcelScene; |
| 4 | 46 | | } |
| | 47 | |
|
| | 48 | | [UnityTest] |
| | 49 | | public IEnumerator BeDestroyedCorrectlyWhenReceivingComponentDestroyMessage() |
| | 50 | | { |
| 1 | 51 | | var shape = TestHelpers.CreateEntityWithBoxShape(scene, Vector3.zero, true); |
| 1 | 52 | | IDCLEntity entity = shape.attachedEntities.First(); |
| | 53 | |
|
| 1 | 54 | | yield return shape.routine; |
| | 55 | |
|
| 1 | 56 | | string onPointerId = "pointerevent-1"; |
| 1 | 57 | | var model = new OnClick.Model() |
| | 58 | | { |
| | 59 | | type = OnClick.NAME, |
| | 60 | | uuid = onPointerId |
| | 61 | | }; |
| | 62 | |
|
| 1 | 63 | | TestHelpers.EntityComponentCreate<OnClick, OnClick.Model>(scene, entity, |
| | 64 | | model, CLASS_ID_COMPONENT.UUID_CALLBACK); |
| | 65 | |
|
| 1 | 66 | | model.type = OnPointerDown.NAME; |
| | 67 | |
|
| 1 | 68 | | TestHelpers.EntityComponentCreate<OnPointerDown, OnPointerDown.Model>(scene, entity, |
| | 69 | | model, CLASS_ID_COMPONENT.UUID_CALLBACK); |
| | 70 | |
|
| 1 | 71 | | model.type = OnPointerUp.NAME; |
| | 72 | |
|
| 1 | 73 | | TestHelpers.EntityComponentCreate<OnPointerUp, OnPointerUp.Model>(scene, entity, |
| | 74 | | model, CLASS_ID_COMPONENT.UUID_CALLBACK); |
| | 75 | |
|
| 1 | 76 | | Assert.IsTrue( entity.components.ContainsKey( CLASS_ID_COMPONENT.UUID_ON_CLICK )); |
| 1 | 77 | | Assert.IsTrue( entity.components.ContainsKey( CLASS_ID_COMPONENT.UUID_ON_UP )); |
| 1 | 78 | | Assert.IsTrue( entity.components.ContainsKey( CLASS_ID_COMPONENT.UUID_ON_DOWN )); |
| | 79 | |
|
| 1 | 80 | | scene.EntityComponentRemove(entity.entityId, OnPointerDown.NAME); |
| 1 | 81 | | scene.EntityComponentRemove(entity.entityId, OnPointerUp.NAME); |
| 1 | 82 | | scene.EntityComponentRemove(entity.entityId, OnClick.NAME); |
| | 83 | |
|
| 1 | 84 | | Assert.IsFalse( entity.components.ContainsKey( CLASS_ID_COMPONENT.UUID_ON_CLICK )); |
| 1 | 85 | | Assert.IsFalse( entity.components.ContainsKey( CLASS_ID_COMPONENT.UUID_ON_UP )); |
| 1 | 86 | | Assert.IsFalse( entity.components.ContainsKey( CLASS_ID_COMPONENT.UUID_ON_DOWN )); |
| 1 | 87 | | } |
| | 88 | |
|
| | 89 | | [UnityTest] |
| | 90 | | public IEnumerator NotCreateCollidersOnLoadingShape() |
| | 91 | | { |
| | 92 | | // 1. Instantiate entity and add an OnPointerDown component |
| 1 | 93 | | string entityId = "1"; |
| 1 | 94 | | var entity = TestHelpers.CreateSceneEntity(scene, entityId); |
| | 95 | |
|
| 1 | 96 | | string onPointerId = "pointerevent-1"; |
| 1 | 97 | | var model = new OnPointerEvent.Model() |
| | 98 | | { |
| | 99 | | type = OnPointerDown.NAME, |
| | 100 | | uuid = onPointerId |
| | 101 | | }; |
| 1 | 102 | | var onPointerDownComponent = TestHelpers.EntityComponentCreate<OnPointerDown, OnPointerEvent.Model>(scene, e |
| | 103 | | model, CLASS_ID_COMPONENT.UUID_CALLBACK); |
| | 104 | |
|
| | 105 | | // 2. Attach a shape |
| 1 | 106 | | var shapeModel = new LoadableShape.Model(); |
| 1 | 107 | | shapeModel.src = TestAssetsUtils.GetPath() + "/GLB/Lantern/Lantern.glb"; |
| 1 | 108 | | var componentId = TestHelpers.CreateAndSetShape(scene, entityId, DCL.Models.CLASS_ID.GLTF_SHAPE, JsonConvert |
| | 109 | |
|
| | 110 | | // 3. Change a shape component property while it loads |
| 1 | 111 | | shapeModel.visible = false; |
| 1 | 112 | | TestHelpers.UpdateShape(scene, componentId, JsonConvert.SerializeObject(shapeModel)); |
| | 113 | |
|
| 1 | 114 | | var pointerEventColliders = onPointerDownComponent.pointerEventHandler.eventColliders.colliders; |
| 1 | 115 | | Assert.IsTrue(pointerEventColliders == null || pointerEventColliders.Length == 0); |
| | 116 | |
|
| 1 | 117 | | LoadWrapper gltfShape = GLTFShape.GetLoaderForEntity(scene.entities[entityId]); |
| 11 | 118 | | yield return new WaitUntil(() => gltfShape.alreadyLoaded); |
| | 119 | |
|
| | 120 | | // 4. Check if the PointerEvent colliders were created |
| 1 | 121 | | pointerEventColliders = onPointerDownComponent.pointerEventHandler.eventColliders.colliders; |
| 1 | 122 | | Assert.IsTrue(pointerEventColliders != null && pointerEventColliders.Length > 0); |
| 1 | 123 | | } |
| | 124 | |
|
| | 125 | | [UnityTest] |
| | 126 | | public IEnumerator NotRecreateCollidersWhenShapeDoesntChange() |
| | 127 | | { |
| | 128 | | // 1. Instantiate entity and add an OnPointerDown component |
| 1 | 129 | | string entityId = "1"; |
| 1 | 130 | | var entity = TestHelpers.CreateSceneEntity(scene, entityId); |
| | 131 | |
|
| 1 | 132 | | string onPointerId = "pointerevent-1"; |
| 1 | 133 | | var model = new OnPointerDown.Model() |
| | 134 | | { |
| | 135 | | type = OnPointerDown.NAME, |
| | 136 | | uuid = onPointerId |
| | 137 | | }; |
| 1 | 138 | | var onPointerDownComponent = TestHelpers.EntityComponentCreate<OnPointerDown, OnPointerDown.Model>(scene, en |
| | 139 | | model, CLASS_ID_COMPONENT.UUID_CALLBACK); |
| | 140 | |
|
| | 141 | | // 2. Attach a shape |
| 1 | 142 | | var shapeModel = new LoadableShape<LoadWrapper_GLTF, LoadableShape.Model>.Model(); |
| 1 | 143 | | shapeModel.src = TestAssetsUtils.GetPath() + "/GLB/Lantern/Lantern.glb"; |
| 1 | 144 | | var componentId = TestHelpers.CreateAndSetShape(scene, entityId, DCL.Models.CLASS_ID.GLTF_SHAPE, JsonConvert |
| | 145 | |
|
| 1 | 146 | | LoadWrapper gltfShape = GLTFShape.GetLoaderForEntity(scene.entities[entityId]); |
| 11 | 147 | | yield return new WaitUntil(() => gltfShape.alreadyLoaded); |
| | 148 | |
|
| 1 | 149 | | var pointerEventColliders = onPointerDownComponent.pointerEventHandler.eventColliders.colliders; |
| 1 | 150 | | Assert.IsTrue(pointerEventColliders != null || pointerEventColliders.Length > 0); |
| | 151 | |
|
| | 152 | | // 3. Change a shape component property conserving the same glb |
| 1 | 153 | | shapeModel.visible = false; |
| 1 | 154 | | TestHelpers.UpdateShape(scene, componentId, JsonConvert.SerializeObject(shapeModel)); |
| 1 | 155 | | yield return null; |
| | 156 | |
|
| | 157 | | // 4. Check the same colliders were kept |
| 1 | 158 | | Assert.IsTrue(pointerEventColliders == onPointerDownComponent.pointerEventHandler.eventColliders.colliders); |
| 1 | 159 | | } |
| | 160 | |
|
| | 161 | | [UnityTest] |
| | 162 | | public IEnumerator NotLeaveCollidersOnRecycledMeshes() |
| | 163 | | { |
| | 164 | | // 1. Instantiate entity and add an OnPointerDown component |
| 1 | 165 | | string entity1Id = "1"; |
| 1 | 166 | | var entity1 = TestHelpers.CreateSceneEntity(scene, entity1Id); |
| | 167 | |
|
| 1 | 168 | | string onPointerId = "pointerevent-1"; |
| 1 | 169 | | var onPointerEventModel = new OnPointerDown.Model() |
| | 170 | | { |
| | 171 | | type = OnPointerDown.NAME, |
| | 172 | | uuid = onPointerId |
| | 173 | | }; |
| 1 | 174 | | var onPointerDownComponent = TestHelpers.EntityComponentCreate<OnPointerDown, OnPointerDown.Model>(scene, en |
| | 175 | | onPointerEventModel, CLASS_ID_COMPONENT.UUID_CALLBACK); |
| | 176 | |
|
| | 177 | | // 2. Attach a shape |
| 1 | 178 | | var shapeModel = new LoadableShape<LoadWrapper_GLTF, LoadableShape.Model>.Model(); |
| 1 | 179 | | shapeModel.src = TestAssetsUtils.GetPath() + "/GLB/Lantern/Lantern.glb"; |
| 1 | 180 | | var shapeComponentId = TestHelpers.CreateAndSetShape(scene, entity1Id, DCL.Models.CLASS_ID.GLTF_SHAPE, JsonC |
| | 181 | |
|
| 1 | 182 | | LoadWrapper gltfShapeLoader1 = GLTFShape.GetLoaderForEntity(scene.entities[entity1Id]); |
| | 183 | |
|
| 11 | 184 | | yield return new WaitUntil(() => gltfShapeLoader1.alreadyLoaded); |
| 1 | 185 | | yield return null; |
| | 186 | |
|
| | 187 | | // 3. Save the mesh GO reference |
| 1 | 188 | | Transform shapeInstanceRootTransform = entity1.meshRootGameObject.transform.GetChild(0); |
| 1 | 189 | | Debug.Log("saved mesh GO: ", shapeInstanceRootTransform); |
| | 190 | |
|
| | 191 | | // 4. Remove shape so that it returns to its pool |
| 1 | 192 | | entity1.RemoveSharedComponent(typeof(BaseShape)); |
| 1 | 193 | | yield return null; |
| | 194 | |
|
| | 195 | | // 5. Check that the pooled mesh doesn't have the collider children and the onPointerEvent component |
| | 196 | | // doesn't have any instantiated collider (since its entity doesn't have a mesh now) |
| 1 | 197 | | var childMeshColliders = shapeInstanceRootTransform.GetComponentsInChildren<MeshCollider>(true); |
| 2 | 198 | | foreach (MeshCollider collider in childMeshColliders) |
| | 199 | | { |
| 0 | 200 | | Assert.IsTrue(collider.gameObject.layer != PhysicsLayers.onPointerEventLayer); |
| | 201 | | } |
| | 202 | |
|
| 1 | 203 | | Assert.IsNull(onPointerDownComponent.pointerEventHandler.eventColliders.colliders); |
| 1 | 204 | | } |
| | 205 | | } |
| | 206 | | } |