| | 1 | | using System.Collections.Generic; |
| | 2 | | using DCL; |
| | 3 | | using DCL.Models; |
| | 4 | | using Newtonsoft.Json; |
| | 5 | | using NUnit.Framework; |
| | 6 | | using UnityEngine; |
| | 7 | | using Assert = UnityEngine.Assertions.Assert; |
| | 8 | |
|
| | 9 | | public class MessageQueueHandler_Mock : IMessageQueueHandler |
| | 10 | | { |
| | 11 | | public List<QueuedSceneMessage_Scene> messagesList = new List<QueuedSceneMessage_Scene>(); |
| | 12 | | public Queue<QueuedSceneMessage_Scene> sceneMessagesPool { get; } = new Queue<QueuedSceneMessage_Scene>(); |
| | 13 | |
|
| | 14 | | private static bool OUTPUT_ASSERT_CODE_ON_CONSOLE = false; |
| | 15 | | private int i = 0; |
| | 16 | |
|
| | 17 | | public void EnqueueSceneMessage(QueuedSceneMessage_Scene message) |
| | 18 | | { |
| | 19 | | messagesList.Add(message); |
| | 20 | |
|
| | 21 | | if (OUTPUT_ASSERT_CODE_ON_CONSOLE) |
| | 22 | | { |
| | 23 | | var output = JsonConvert.SerializeObject(message); |
| | 24 | | output = output.Replace(@"""", @""""""); |
| | 25 | | i++; |
| | 26 | | Debug.Log($"string json{i} = JsonConvert.SerializeObject(queueHandler.messagesList[{i - 1}]);"); |
| | 27 | | Debug.Log($"string json{i}base = @\"{output}\";"); |
| | 28 | | Debug.Log($"Assert.AreEqual(json{i}base, json{i});"); |
| | 29 | | } |
| | 30 | | } |
| | 31 | | } |
| | 32 | |
|
| | 33 | | public class EntryPointWorldShould |
| | 34 | | { |
| | 35 | | private EntryPoint_World entryPoint; |
| | 36 | | private MessageQueueHandler_Mock queueHandler; |
| | 37 | |
|
| | 38 | | [SetUp] |
| | 39 | | public void SetUp() |
| | 40 | | { |
| 5 | 41 | | queueHandler = new MessageQueueHandler_Mock(); |
| 5 | 42 | | entryPoint = new EntryPoint_World(queueHandler); |
| | 43 | |
|
| | 44 | | const string sceneId = "test-scene-id"; |
| | 45 | | const string tag = "test-tag"; |
| 5 | 46 | | EntryPoint_World.SetSceneId(sceneId); |
| 5 | 47 | | EntryPoint_World.SetTag(tag); |
| 5 | 48 | | } |
| | 49 | |
|
| | 50 | | [Test] |
| | 51 | | public void QueueEntityMessagesCorrectly() |
| | 52 | | { |
| | 53 | | const string entityId_1 = "1"; |
| | 54 | | const string entityId_2 = "2"; |
| | 55 | |
|
| 1 | 56 | | EntryPoint_World.SetEntityId(entityId_1); |
| 1 | 57 | | EntryPoint_World.CreateEntity(); |
| 1 | 58 | | EntryPoint_World.SetEntityId(entityId_2); |
| 1 | 59 | | EntryPoint_World.CreateEntity(); |
| 1 | 60 | | EntryPoint_World.SetEntityParent(entityId_1); |
| | 61 | |
|
| 1 | 62 | | EntryPoint_World.SetEntityId(entityId_1); |
| 1 | 63 | | EntryPoint_World.RemoveEntity(); |
| 1 | 64 | | EntryPoint_World.SetEntityId(entityId_2); |
| 1 | 65 | | EntryPoint_World.RemoveEntity(); |
| | 66 | |
|
| 1 | 67 | | string json1 = JsonConvert.SerializeObject(queueHandler.messagesList[0]); |
| 1 | 68 | | string json1base = @"{""method"":""CreateEntity"",""payload"":{""entityId"":""1""},""tag"":""test-tag"",""type"" |
| 1 | 69 | | Assert.AreEqual(json1base, json1); |
| | 70 | |
|
| 1 | 71 | | string json2 = JsonConvert.SerializeObject(queueHandler.messagesList[1]); |
| 1 | 72 | | string json2base = @"{""method"":""CreateEntity"",""payload"":{""entityId"":""2""},""tag"":""test-tag"",""type"" |
| 1 | 73 | | Assert.AreEqual(json2base, json2); |
| | 74 | |
|
| 1 | 75 | | string json3 = JsonConvert.SerializeObject(queueHandler.messagesList[2]); |
| 1 | 76 | | string json3base = @"{""method"":""SetEntityParent"",""payload"":{""entityId"":""2"",""parentId"":""1""},""tag"" |
| 1 | 77 | | Assert.AreEqual(json3base, json3); |
| | 78 | |
|
| 1 | 79 | | string json4 = JsonConvert.SerializeObject(queueHandler.messagesList[3]); |
| 1 | 80 | | string json4base = @"{""method"":""RemoveEntity"",""payload"":{""entityId"":""1""},""tag"":""test-tag"",""type"" |
| 1 | 81 | | Assert.AreEqual(json4base, json4); |
| | 82 | |
|
| 1 | 83 | | string json5 = JsonConvert.SerializeObject(queueHandler.messagesList[4]); |
| 1 | 84 | | string json5base = @"{""method"":""RemoveEntity"",""payload"":{""entityId"":""2""},""tag"":""test-tag"",""type"" |
| 1 | 85 | | Assert.AreEqual(json5base, json5); |
| 1 | 86 | | } |
| | 87 | |
|
| | 88 | | [Test] |
| | 89 | | public void QueueSharedComponentMessagesCorrectly() |
| | 90 | | { |
| | 91 | | const string entityId = "1"; |
| | 92 | | const string componentId = "component-1"; |
| | 93 | | const int componentClass = 1; |
| | 94 | |
|
| 1 | 95 | | EntryPoint_World.SetEntityId(entityId); |
| 1 | 96 | | EntryPoint_World.CreateEntity(); |
| 1 | 97 | | EntryPoint_World.SharedComponentCreate(componentClass, componentId); |
| 1 | 98 | | EntryPoint_World.SharedComponentAttach(componentId, null); |
| 1 | 99 | | EntryPoint_World.SharedComponentUpdate(componentId, "{}"); |
| 1 | 100 | | EntryPoint_World.SharedComponentDispose(componentId); |
| | 101 | |
|
| 1 | 102 | | string json1 = JsonConvert.SerializeObject(queueHandler.messagesList[0]); |
| 1 | 103 | | string json1base = @"{""method"":""CreateEntity"",""payload"":{""entityId"":""1""},""tag"":""test-tag"",""type"" |
| 1 | 104 | | Assert.AreEqual(json1base, json1); |
| | 105 | |
|
| 1 | 106 | | string json2 = JsonConvert.SerializeObject(queueHandler.messagesList[1]); |
| 1 | 107 | | string json2base = @"{""method"":""ComponentCreated"",""payload"":{""id"":""component-1"",""classId"":1,""name"" |
| 1 | 108 | | Assert.AreEqual(json2base, json2); |
| | 109 | |
|
| 1 | 110 | | string json3 = JsonConvert.SerializeObject(queueHandler.messagesList[2]); |
| 1 | 111 | | string json3base = @"{""method"":""AttachEntityComponent"",""payload"":{""entityId"":""1"",""id"":""component-1" |
| 1 | 112 | | Assert.AreEqual(json3base, json3); |
| | 113 | |
|
| 1 | 114 | | string json4 = JsonConvert.SerializeObject(queueHandler.messagesList[3]); |
| 1 | 115 | | string json4base = @"{""method"":""ComponentUpdated"",""payload"":{""componentId"":""component-1"",""json"":""{} |
| 1 | 116 | | Assert.AreEqual(json4base, json4); |
| | 117 | |
|
| 1 | 118 | | string json5 = JsonConvert.SerializeObject(queueHandler.messagesList[4]); |
| 1 | 119 | | string json5base = @"{""method"":""ComponentDisposed"",""payload"":{""id"":""component-1""},""tag"":""test-tag"" |
| 1 | 120 | | Assert.AreEqual(json5base, json5); |
| 1 | 121 | | } |
| | 122 | |
|
| | 123 | | [Test] |
| | 124 | | public void QueueEntityComponentCorrectly() |
| | 125 | | { |
| | 126 | | const string entityId = "1"; |
| | 127 | | const string componentId = "component-1"; |
| | 128 | | const int componentClass = 1; |
| | 129 | |
|
| 1 | 130 | | EntryPoint_World.SetEntityId(entityId); |
| 1 | 131 | | EntryPoint_World.CreateEntity(); |
| 1 | 132 | | EntryPoint_World.EntityComponentCreateOrUpdate(componentClass, componentId); |
| 1 | 133 | | EntryPoint_World.EntityComponentDestroy(componentId); |
| | 134 | |
|
| 1 | 135 | | Assert.AreEqual(3, queueHandler.messagesList.Count); |
| | 136 | |
|
| 1 | 137 | | string json1base = @"{""method"":""CreateEntity"",""payload"":{""entityId"":""1""},""tag"":""test-tag"",""type"" |
| 1 | 138 | | string json2base = @"{""method"":""UpdateEntityComponent"",""payload"":{""entityId"":""1"",""classId"":1,""json" |
| 1 | 139 | | string json3base = @"{""method"":""ComponentRemoved"",""payload"":{""entityId"":""1"",""name"":""component-1""}, |
| | 140 | |
|
| 1 | 141 | | string json1 = JsonConvert.SerializeObject(queueHandler.messagesList[0]); |
| 1 | 142 | | string json2 = JsonConvert.SerializeObject(queueHandler.messagesList[1]); |
| 1 | 143 | | string json3 = JsonConvert.SerializeObject(queueHandler.messagesList[2]); |
| | 144 | |
|
| 1 | 145 | | Assert.AreEqual(json1base, json1); |
| 1 | 146 | | Assert.AreEqual(json2base, json2); |
| 1 | 147 | | Assert.AreEqual(json3base, json3); |
| 1 | 148 | | } |
| | 149 | |
|
| | 150 | | [Test] |
| | 151 | | public void QueueQueryCorrectly() |
| | 152 | | { |
| 1 | 153 | | Protocol.QueryPayload payload = new Protocol.QueryPayload(); |
| 1 | 154 | | payload.queryType = 79014; |
| 1 | 155 | | payload.raycastPayload = new Protocol.RaycastQueryPayload |
| | 156 | | { |
| | 157 | | direction = Vector3.right, |
| | 158 | | distance = 10, |
| | 159 | | id = 66, |
| | 160 | | origin = Vector3.zero, |
| | 161 | | raycastType = 1 |
| | 162 | | }; |
| 1 | 163 | | EntryPoint_World.Query(payload); |
| | 164 | |
|
| 1 | 165 | | string json1 = JsonConvert.SerializeObject(queueHandler.messagesList[0]); |
| 1 | 166 | | string json1base = @"{""method"":""Query"",""payload"":{""queryType"":null,""payload"":{""sceneId"":""test-scene |
| 1 | 167 | | Assert.AreEqual(json1base, json1); |
| 1 | 168 | | } |
| | 169 | |
|
| | 170 | | [Test] |
| | 171 | | public void QueueSceneReadyCorrectly() |
| | 172 | | { |
| 1 | 173 | | EntryPoint_World.SceneReady(); |
| | 174 | |
|
| 1 | 175 | | string json1 = JsonConvert.SerializeObject(queueHandler.messagesList[0]); |
| 1 | 176 | | string json1base = @"{""method"":""InitMessagesFinished"",""payload"":{},""tag"":""test-tag"",""type"":1,""scene |
| 1 | 177 | | Assert.AreEqual(json1base, json1); |
| 1 | 178 | | } |
| | 179 | | } |