| | 1 | | using DCL; |
| | 2 | | using NUnit.Framework; |
| | 3 | | using System; |
| | 4 | | using System.Collections; |
| | 5 | | using UnityEngine; |
| | 6 | | using UnityEngine.TestTools; |
| | 7 | | using WebSocketSharp; |
| | 8 | | using Environment = DCL.Environment; |
| | 9 | |
|
| | 10 | | namespace Tests |
| | 11 | | { |
| | 12 | | [Explicit] |
| | 13 | | public class WSSTests : IntegrationTestSuite |
| | 14 | | { |
| | 15 | | protected override IEnumerator SetUp() |
| | 16 | | { |
| 0 | 17 | | Environment.SetupWithBuilders |
| | 18 | | ( |
| | 19 | | MessagingContextFactory.CreateDefault, |
| | 20 | | PlatformContextFactory.CreateDefault, |
| | 21 | | WorldRuntimeContextFactory.CreateDefault, |
| | 22 | | HUDContextFactory.CreateDefault |
| | 23 | | ); |
| 0 | 24 | | yield break; |
| | 25 | | } |
| | 26 | |
|
| | 27 | | [UnityTest] |
| | 28 | | public IEnumerator BasicConnectionTest() |
| | 29 | | { |
| 0 | 30 | | GameObject wssControllerGO = new GameObject("WSS Controller"); |
| | 31 | |
|
| 0 | 32 | | WSSController wssController = wssControllerGO.AddComponent<WSSController>(); |
| 0 | 33 | | DCLCharacterController.i.gravity = 0; |
| | 34 | |
|
| 0 | 35 | | yield return new WaitForSeconds(1.0f); |
| | 36 | |
|
| 0 | 37 | | Assert.IsTrue(wssController.isServerReady); |
| | 38 | |
|
| 0 | 39 | | using (WebSocket ws = new WebSocket("ws://localhost:5000/dcl")) |
| | 40 | | { |
| | 41 | | try |
| | 42 | | { |
| 0 | 43 | | ws.Connect(); |
| 0 | 44 | | } |
| 0 | 45 | | catch (Exception e) |
| | 46 | | { |
| 0 | 47 | | Debug.LogError("message: " + e.ToString()); |
| 0 | 48 | | Assert.Fail("Failed to connect to decentraland service!"); |
| 0 | 49 | | } |
| | 50 | |
|
| 0 | 51 | | string payloadTest = (Resources.Load("TestJSON/SceneLoadingTest") as TextAsset).text; |
| | 52 | |
|
| 0 | 53 | | DCLWebSocketService.Message message = new DCLWebSocketService.Message() |
| | 54 | | { |
| | 55 | | type = "LoadParcelScenes", |
| | 56 | | payload = payloadTest |
| | 57 | | }; |
| | 58 | |
|
| 0 | 59 | | string json = ""; |
| | 60 | | try |
| | 61 | | { |
| 0 | 62 | | json = JsonUtility.ToJson(message); |
| 0 | 63 | | } |
| | 64 | | catch (Exception e) |
| | 65 | | { |
| 0 | 66 | | Debug.LogError(e.ToString()); |
| 0 | 67 | | Assert.Fail("Invalid object?: " + json); |
| 0 | 68 | | } |
| | 69 | |
|
| 0 | 70 | | ws.Send(json); |
| 0 | 71 | | Debug.Log("<color=#007F00><b>WSSTests</b></color> >>> Waiting for server response..."); |
| | 72 | |
|
| 0 | 73 | | string lastMessage = ""; |
| | 74 | |
|
| 0 | 75 | | ws.OnMessage += (sender, e) => |
| | 76 | | { |
| 0 | 77 | | Debug.Log("<color=#007F00><b>WSSTests</b></color> >>> Server sent: " + e.Data); |
| 0 | 78 | | lastMessage = e.Data; |
| 0 | 79 | | }; |
| | 80 | |
|
| 0 | 81 | | float time = 0; |
| 0 | 82 | | while (time < 8.0f) |
| | 83 | | { |
| 0 | 84 | | time += Time.unscaledDeltaTime; |
| | 85 | |
|
| 0 | 86 | | if (lastMessage.Contains("OK!")) |
| | 87 | | { |
| | 88 | | break; |
| | 89 | | } |
| | 90 | |
|
| 0 | 91 | | yield return null; |
| | 92 | | } |
| | 93 | |
|
| 0 | 94 | | Assert.LessOrEqual(Mathf.FloorToInt(time), 8); |
| | 95 | |
|
| 0 | 96 | | string loadedSceneID = "0,0"; |
| | 97 | |
|
| 0 | 98 | | yield return null; |
| | 99 | |
|
| 0 | 100 | | Assert.IsTrue(DCL.Environment.i.world.state.loadedScenes.ContainsKey(loadedSceneID), |
| | 101 | | "Expected loadedScene not found!"); |
| 0 | 102 | | Assert.IsTrue(DCL.Environment.i.world.state.loadedScenes[loadedSceneID] != null, |
| | 103 | | "Expected loadedScene found but was null!!!"); |
| 0 | 104 | | } |
| | 105 | |
|
| | 106 | |
|
| 0 | 107 | | UnityEngine.Object.Destroy(wssControllerGO); |
| | 108 | | // Use the Assert class to test conditions. |
| | 109 | | // Use yield to skip a frame. |
| 0 | 110 | | yield return null; |
| 0 | 111 | | } |
| | 112 | | } |
| | 113 | | } |