| | 1 | | using DCL.Components; |
| | 2 | | using DCL.Helpers; |
| | 3 | | using DCL.Models; |
| | 4 | | using DCL; |
| | 5 | | using Newtonsoft.Json; |
| | 6 | | using NUnit.Framework; |
| | 7 | | using System.Collections; |
| | 8 | | using DCL.Camera; |
| | 9 | | using UnityEngine; |
| | 10 | | using UnityEngine.TestTools; |
| | 11 | |
|
| | 12 | | namespace Tests |
| | 13 | | { |
| | 14 | | public class CursorControllerTests : IntegrationTestSuite_Legacy |
| | 15 | | { |
| 0 | 16 | | protected override bool enableSceneIntegrityChecker => false; |
| | 17 | |
|
| | 18 | | protected override IEnumerator SetUp() |
| | 19 | | { |
| 4 | 20 | | yield return base.SetUp(); |
| 4 | 21 | | Environment.i.world.sceneController.SortScenesByDistance(); |
| 4 | 22 | | sceneInitialized = false; |
| 4 | 23 | | } |
| | 24 | |
|
| | 25 | | [UnityTest] |
| | 26 | | public IEnumerator OnPointerHoverFeedbackIsDisplayedCorrectly() |
| | 27 | | { |
| | 28 | | IDCLEntity entity; |
| | 29 | | BoxShape shape; |
| | 30 | |
|
| 1 | 31 | | shape = TestHelpers.InstantiateEntityWithShape<BoxShape, BoxShape.Model>( |
| | 32 | | scene, |
| | 33 | | DCL.Models.CLASS_ID.BOX_SHAPE, |
| | 34 | | Vector3.zero, |
| | 35 | | out entity, |
| | 36 | | new BoxShape.Model() { }); |
| | 37 | |
|
| 1 | 38 | | TestHelpers.SetEntityTransform(scene, entity, new Vector3(8, 2, 10), Quaternion.identity, new Vector3(3, 3, |
| 1 | 39 | | yield return shape.routine; |
| | 40 | |
|
| 1 | 41 | | var OnPointerDownModel = new OnPointerDown.Model() |
| | 42 | | { |
| | 43 | | type = OnPointerDown.NAME, |
| | 44 | | uuid = "pointerevent-1" |
| | 45 | | }; |
| | 46 | |
|
| 1 | 47 | | var component = TestHelpers.EntityComponentCreate<OnPointerDown, OnPointerDown.Model>(scene, entity, |
| | 48 | | OnPointerDownModel, CLASS_ID_COMPONENT.UUID_CALLBACK); |
| | 49 | |
|
| 1 | 50 | | Assert.IsTrue(component != null); |
| | 51 | |
|
| 1 | 52 | | yield return null; |
| | 53 | |
|
| 1 | 54 | | var cursorController = GameObject.FindObjectOfType<CursorController>(); |
| | 55 | |
|
| | 56 | | // Check cursor shows normal sprite |
| 1 | 57 | | Assert.AreEqual(cursorController.cursorImage.sprite, cursorController.normalCursor); |
| | 58 | |
|
| 1 | 59 | | DCLCharacterController.i.PauseGravity(); |
| 1 | 60 | | DCLCharacterController.i.SetPosition(new Vector3(8, 1, 7f)); |
| | 61 | |
|
| 1 | 62 | | var cameraController = GameObject.FindObjectOfType<CameraController>(); |
| | 63 | |
|
| | 64 | | // Rotate camera towards the interactive object |
| 1 | 65 | | var cameraRotationPayload = new CameraController.SetRotationPayload() |
| | 66 | | { |
| | 67 | | x = 45, |
| | 68 | | y = 0, |
| | 69 | | z = 0 |
| | 70 | | }; |
| | 71 | |
|
| 1 | 72 | | cameraController.SetRotation(JsonConvert.SerializeObject(cameraRotationPayload, Formatting.None, new JsonSer |
| | 73 | | { |
| | 74 | | ReferenceLoopHandling = ReferenceLoopHandling.Ignore |
| | 75 | | })); |
| | 76 | |
|
| 1 | 77 | | yield return null; |
| | 78 | |
|
| | 79 | | // Check cursor shows hover sprite |
| 1 | 80 | | Assert.AreEqual(cursorController.cursorImage.sprite, cursorController.hoverCursor); |
| | 81 | |
|
| | 82 | | // Rotate the camera away from the interactive object |
| 1 | 83 | | cameraRotationPayload = new CameraController.SetRotationPayload() |
| | 84 | | { |
| | 85 | | x = 0, |
| | 86 | | y = 0, |
| | 87 | | z = 0, |
| | 88 | | cameraTarget = (DCLCharacterController.i.transform.position - entity.gameObject.transform.position) |
| | 89 | | }; |
| | 90 | |
|
| 1 | 91 | | cameraController.SetRotation(JsonConvert.SerializeObject(cameraRotationPayload, Formatting.None, new JsonSer |
| | 92 | | { |
| | 93 | | ReferenceLoopHandling = ReferenceLoopHandling.Ignore |
| | 94 | | })); |
| | 95 | |
|
| 1 | 96 | | yield return null; |
| | 97 | |
|
| | 98 | | // Check cursor shows normal sprite |
| 1 | 99 | | Assert.AreEqual(cursorController.cursorImage.sprite, cursorController.normalCursor); |
| | 100 | |
|
| 1 | 101 | | DCLCharacterController.i.ResumeGravity(); |
| 1 | 102 | | } |
| | 103 | |
|
| | 104 | | [UnityTest] |
| | 105 | | public IEnumerator OnPointerHoverFeedbackNotDisplayedOnInvisibles() |
| | 106 | | { |
| | 107 | | IDCLEntity entity; |
| | 108 | | BoxShape shape; |
| | 109 | |
|
| 1 | 110 | | shape = TestHelpers.InstantiateEntityWithShape<BoxShape, BoxShape.Model>( |
| | 111 | | scene, |
| | 112 | | DCL.Models.CLASS_ID.BOX_SHAPE, |
| | 113 | | Vector3.zero, |
| | 114 | | out entity, |
| | 115 | | new BoxShape.Model() { }); |
| | 116 | |
|
| 1 | 117 | | TestHelpers.SetEntityTransform(scene, entity, new Vector3(8, 2, 10), Quaternion.identity, new Vector3(3, 3, |
| 1 | 118 | | yield return shape.routine; |
| | 119 | |
|
| 1 | 120 | | var OnPointerDownModel = new OnPointerDown.Model() |
| | 121 | | { |
| | 122 | | type = OnPointerDown.NAME, |
| | 123 | | uuid = "pointerevent-1" |
| | 124 | | }; |
| | 125 | |
|
| 1 | 126 | | var component = TestHelpers.EntityComponentCreate<OnPointerDown, OnPointerDown.Model>(scene, entity, |
| | 127 | | OnPointerDownModel, CLASS_ID_COMPONENT.UUID_CALLBACK); |
| | 128 | |
|
| 1 | 129 | | Assert.IsTrue(component != null); |
| | 130 | |
|
| 1 | 131 | | yield return null; |
| | 132 | |
|
| 1 | 133 | | var cursorController = GameObject.FindObjectOfType<CursorController>(); |
| | 134 | |
|
| | 135 | | // Check cursor shows normal sprite |
| 1 | 136 | | Assert.AreEqual(cursorController.cursorImage.sprite, cursorController.normalCursor); |
| | 137 | |
|
| 1 | 138 | | DCLCharacterController.i.PauseGravity(); |
| 1 | 139 | | DCLCharacterController.i.SetPosition(new Vector3(8, 1, 7f)); |
| | 140 | |
|
| | 141 | | // Rotate camera towards the interactive object |
| 1 | 142 | | cameraController.SetRotation(45, 0, 0); |
| | 143 | |
|
| 1 | 144 | | yield return null; |
| | 145 | |
|
| | 146 | | // Check cursor shows hover sprite |
| 1 | 147 | | Assert.AreEqual(cursorController.cursorImage.sprite, cursorController.hoverCursor); |
| | 148 | |
|
| | 149 | | // Make shape invisible |
| 1 | 150 | | TestHelpers.UpdateShape(scene, shape.id, JsonConvert.SerializeObject( |
| | 151 | | new |
| | 152 | | { |
| | 153 | | visible = false, |
| | 154 | | withCollisions = false, |
| | 155 | | isPointerBlocker = false |
| | 156 | | })); |
| | 157 | |
|
| 1 | 158 | | yield return null; |
| | 159 | |
|
| | 160 | | // Check cursor shows normal sprite |
| 1 | 161 | | Assert.AreEqual(cursorController.cursorImage.sprite, cursorController.normalCursor); |
| | 162 | |
|
| 1 | 163 | | DCLCharacterController.i.ResumeGravity(); |
| 1 | 164 | | } |
| | 165 | |
|
| | 166 | | [UnityTest] |
| | 167 | | public IEnumerator FeedbackIsNotDisplayedOnParent() |
| | 168 | | { |
| 1 | 169 | | var cursorController = GameObject.FindObjectOfType<CursorController>(); |
| | 170 | |
|
| 1 | 171 | | Assert.IsNotNull(cameraController, "camera is null?"); |
| | 172 | |
|
| | 173 | | // Create parent entity |
| | 174 | | IDCLEntity blockingEntity; |
| 1 | 175 | | BoxShape blockingShape = TestHelpers.InstantiateEntityWithShape<BoxShape, BoxShape.Model>( |
| | 176 | | scene, |
| | 177 | | DCL.Models.CLASS_ID.BOX_SHAPE, |
| | 178 | | Vector3.zero, |
| | 179 | | out blockingEntity, |
| | 180 | | new BoxShape.Model() { }); |
| 1 | 181 | | TestHelpers.SetEntityTransform(scene, blockingEntity, new Vector3(3, 3, 3), Quaternion.identity, new Vector3 |
| 1 | 182 | | yield return blockingShape.routine; |
| | 183 | |
|
| | 184 | | // Create target entity for click |
| | 185 | | IDCLEntity clickTargetEntity; |
| 1 | 186 | | BoxShape clickTargetShape = TestHelpers.InstantiateEntityWithShape<BoxShape, BoxShape.Model>( |
| | 187 | | scene, |
| | 188 | | DCL.Models.CLASS_ID.BOX_SHAPE, |
| | 189 | | Vector3.zero, |
| | 190 | | out clickTargetEntity, |
| | 191 | | new BoxShape.Model() { }); |
| 1 | 192 | | TestHelpers.SetEntityTransform(scene, clickTargetEntity, new Vector3(0, 0, 5), Quaternion.identity, new Vect |
| 1 | 193 | | yield return clickTargetShape.routine; |
| | 194 | |
|
| | 195 | | // Enparent target entity as a child of the blocking entity |
| 1 | 196 | | TestHelpers.SetEntityParent(scene, clickTargetEntity, blockingEntity); |
| | 197 | |
|
| | 198 | | // Set character position and camera rotation |
| 1 | 199 | | DCLCharacterController.i.SetPosition(new Vector3(3, 2, 1)); |
| 1 | 200 | | yield return null; |
| | 201 | |
|
| | 202 | | // Create pointer down component and add it to target entity |
| 1 | 203 | | string onPointerId = "pointerevent-1"; |
| 1 | 204 | | var OnPointerDownModel = new OnPointerDown.Model() |
| | 205 | | { |
| | 206 | | type = OnPointerDown.NAME, |
| | 207 | | uuid = onPointerId |
| | 208 | | }; |
| 1 | 209 | | var component = TestHelpers.EntityComponentCreate<OnPointerDown, OnPointerDown.Model>(scene, clickTargetEnti |
| | 210 | | OnPointerDownModel, CLASS_ID_COMPONENT.UUID_CALLBACK); |
| | 211 | |
|
| 1 | 212 | | Assert.IsTrue(component != null); |
| | 213 | |
|
| | 214 | | // Check if target entity is triggered by looking at the parent entity |
| 1 | 215 | | Assert.AreEqual(cursorController.cursorImage.sprite, cursorController.normalCursor); |
| | 216 | |
|
| | 217 | | // Move character in front of target entity and rotate camera |
| 1 | 218 | | DCLCharacterController.i.SetPosition(new Vector3(3, 2, 12)); |
| 1 | 219 | | cameraController.SetRotation(0, 0, 0, new Vector3(0, 0, -1)); |
| | 220 | |
|
| 1 | 221 | | yield return null; |
| | 222 | |
|
| | 223 | | // Check if target entity is triggered when looked at directly |
| 1 | 224 | | Assert.AreEqual(cursorController.cursorImage.sprite, cursorController.hoverCursor); |
| 1 | 225 | | } |
| | 226 | |
|
| | 227 | | [UnityTest] |
| | 228 | | public IEnumerator OnPointerHoverFeedbackIsBlockedByUI() |
| | 229 | | { |
| | 230 | | IDCLEntity entity; |
| | 231 | | BoxShape shape; |
| | 232 | |
|
| 1 | 233 | | shape = TestHelpers.InstantiateEntityWithShape<BoxShape, BoxShape.Model>( |
| | 234 | | scene, |
| | 235 | | DCL.Models.CLASS_ID.BOX_SHAPE, |
| | 236 | | Vector3.zero, |
| | 237 | | out entity, |
| | 238 | | new BoxShape.Model() { }); |
| | 239 | |
|
| 1 | 240 | | TestHelpers.SetEntityTransform(scene, entity, new Vector3(8, 2, 10), Quaternion.identity, new Vector3(3, 3, |
| 1 | 241 | | yield return shape.routine; |
| | 242 | |
|
| 1 | 243 | | var onPointerDownModel = new OnPointerDown.Model() |
| | 244 | | { |
| | 245 | | type = OnPointerDown.NAME, |
| | 246 | | uuid = "pointerevent-1" |
| | 247 | | }; |
| 1 | 248 | | var component = TestHelpers.EntityComponentCreate<OnPointerDown, OnPointerDown.Model>(scene, entity, |
| | 249 | | onPointerDownModel, CLASS_ID_COMPONENT.UUID_CALLBACK); |
| 1 | 250 | | Assert.IsTrue(component != null); |
| | 251 | |
|
| 1 | 252 | | yield return null; |
| | 253 | |
|
| 1 | 254 | | DCLCharacterController.i.SetPosition(new Vector3(8, 1, 7f)); |
| | 255 | |
|
| 1 | 256 | | var cameraController = GameObject.FindObjectOfType<CameraController>(); |
| | 257 | |
|
| | 258 | | // Rotate camera towards the interactive object |
| 1 | 259 | | cameraController.SetRotation(45, 0, 0); |
| | 260 | |
|
| 1 | 261 | | yield return null; |
| | 262 | |
|
| 1 | 263 | | var hoverCanvasController = InteractionHoverCanvasController.i; |
| 1 | 264 | | Assert.IsNotNull(hoverCanvasController); |
| | 265 | |
|
| | 266 | | // Check hover feedback is enabled |
| 1 | 267 | | Assert.IsTrue(hoverCanvasController.canvas.enabled); |
| | 268 | |
|
| | 269 | | // Put UI in the middle |
| 1 | 270 | | UIScreenSpace screenSpaceShape = |
| | 271 | | TestHelpers.SharedComponentCreate<UIScreenSpace, UIScreenSpace.Model>(scene, |
| | 272 | | CLASS_ID.UI_SCREEN_SPACE_SHAPE); |
| 1 | 273 | | yield return screenSpaceShape.routine; |
| | 274 | |
|
| 1 | 275 | | UIContainerRect uiContainerRectShape = |
| | 276 | | TestHelpers.SharedComponentCreate<UIContainerRect, UIContainerRect.Model>(scene, |
| | 277 | | CLASS_ID.UI_CONTAINER_RECT); |
| 1 | 278 | | yield return uiContainerRectShape.routine; |
| | 279 | |
|
| 1 | 280 | | yield return null; |
| | 281 | |
|
| | 282 | | // Check hover feedback is no longer enabled |
| 1 | 283 | | Assert.IsFalse(hoverCanvasController.canvas.enabled); |
| | 284 | |
|
| 1 | 285 | | DCLCharacterController.i.ResumeGravity(); |
| 1 | 286 | | } |
| | 287 | | } |
| | 288 | | } |