ObjectHandlerTest.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using NUnit.Framework;
  5. using UnityEngine;
  6. using UnityEngine.TestTools;
  7. using ObjectScripts;
  8. using CSVReader;
  9. namespace Tests
  10. {
  11. public class ObjectHandlerTest
  12. {
  13. private GameObject ObjectHandler;
  14. private ObjectHandler Handler;
  15. private GameObject ManagerObject;
  16. private AbstractManager Manager;
  17. private int amountInputs = 10;
  18. [UnitySetUp]
  19. public IEnumerator SetUp()
  20. {
  21. yield return new EnterPlayMode();
  22. ObjectHandler = GameObject.CreatePrimitive(PrimitiveType.Cube);
  23. Handler = ObjectHandler.AddComponent<ObjectHandler>();
  24. ManagerObject = GameObject.CreatePrimitive(PrimitiveType.Cube);
  25. Manager = ManagerObject.AddComponent<TestManager>();
  26. Manager.UpdateRate = 4;
  27. Manager.GameVelocity = 1;
  28. Manager.UpdateRateInSeconds = 1 / 4;
  29. Handler.ManagerObject = Manager;
  30. GameObject person = GameObject.CreatePrimitive(PrimitiveType.Cube);
  31. person.AddComponent<DataObject>();
  32. Handler.prefabHuman = person;
  33. Handler.prefabCar = person;
  34. Handler.prefabBike = person;
  35. Handler.prefabTruck = person;
  36. Dictionary<int, GameObject> sensorList = new Dictionary<int, GameObject>();
  37. for (var i = 0; i < amountInputs; i++)
  38. {
  39. sensorList.Add(i, new GameObject());
  40. }
  41. Handler.SensorList = sensorList;
  42. // yield return new WaitForSecondsRealtime(3);
  43. // ManagerObject.Handler = Handler;
  44. yield return null;
  45. }
  46. [UnityTearDown]
  47. public IEnumerator TearDown()
  48. {
  49. foreach (MonoBehaviour item in GameObject.FindObjectsOfType<MonoBehaviour>())
  50. {
  51. GameObject.Destroy(item.gameObject);
  52. }
  53. yield return null;
  54. yield return new ExitPlayMode();
  55. }
  56. private List<InputObject> createTestInputs(int amountInputs, double time, int move = 0)
  57. {
  58. List<InputObject> inputs = new List<InputObject>();
  59. InputObject input;
  60. EntityType[] allTypes = (EntityType[])(System.Enum.GetValues(typeof(EntityType)));
  61. for (int id = 0; id < amountInputs; id++)
  62. {
  63. int value = id + move;
  64. float posX = (value % 3 == 0) ? -value / 100 : value / 100, posY = (value % 2 == 0) ? -value + 0.2f : value - 10, posZ = (value % 7 == 0) ? value : 0;
  65. float rotX = 0, rotY = 0, rotZ = 0;
  66. float rotHeadX = 0, rotHeadY = 0, rotHeadZ = 0;
  67. byte rTop = (value % 4 == 0) ? byte.MaxValue : byte.MinValue, gTop = (byte)(value % byte.MaxValue), bTop = (byte)(value % byte.MaxValue),
  68. rBot = (byte)(value % byte.MaxValue), gBot = (byte)(value % byte.MaxValue), bBot = (byte)(value % byte.MaxValue);
  69. uint botColor = 333333;
  70. uint topColor = 555555;
  71. int sensorID = id;
  72. EntityType type = (EntityType)allTypes.GetValue(value % allTypes.Length);
  73. // Debug.Log((id, time, posX, posY, posZ, rotX, rotY, rotZ, rTop, gTop, bTop, rBot, gBot, bBot, type).ToString());
  74. input = new InputObject(id, time, posX, posY, posZ, rotX, rotY, rotZ, rotHeadX, rotHeadY, rotHeadZ, topColor, botColor, type, sensorID);
  75. inputs.Add(input);
  76. }
  77. return inputs;
  78. }
  79. private void checkInputEqualsGameObject(InputObject input, InputObject inputBeforeUpdate = null, bool delete = false)
  80. {
  81. Debug.LogWarning("Rotation and SensorID isn't checked");
  82. GameObject obj = GameObject.Find(input.ID.ToString());
  83. Assert.NotNull(obj, string.Format("Object {0} not created", input.ID));
  84. DataObject data;
  85. Assert.True(Handler.DataObjectDictionary.TryGetValue(input.ID, out data), string.Format("Object {0} not in DataObjectDictionary", input.ID));
  86. Assert.AreEqual(data.Handler, Handler);
  87. Assert.AreEqual(data.ID, input.ID);
  88. Assert.AreEqual(data.Type, input.Type);
  89. Assert.AreEqual(data.Timestamp, input.Time);
  90. if (inputBeforeUpdate == null)
  91. {
  92. Assert.AreEqual(data.StartPos, input.Pos);
  93. }
  94. else
  95. {
  96. Assert.AreEqual(data.StartPos, inputBeforeUpdate.Pos);
  97. }
  98. Assert.AreEqual(data.TargetPos, input.Pos);
  99. Assert.AreEqual(data.TimeSinceLastUpdate, 0f);
  100. Assert.AreEqual(data.BotColor, input.BotColor);
  101. Assert.AreEqual(data.TopColor, input.TopColor);
  102. Assert.AreEqual(data.Handler.UpdateRate, Manager.UpdateRateInSeconds);
  103. if (delete)
  104. {
  105. GameObject.Destroy(obj);
  106. }
  107. }
  108. [UnityTest]
  109. public IEnumerator GameObjectsCorrectlyCreated()
  110. {
  111. // Use the Assert class to test conditions.
  112. // Use yield to skip a frame.
  113. yield return null;
  114. Assert.AreEqual(0, GameObject.FindObjectsOfType<DataObject>().Length - 1);
  115. List<InputObject> testInputs = createTestInputs(amountInputs, 0);
  116. Handler.Handle(testInputs);
  117. yield return null;
  118. Assert.AreEqual(amountInputs, GameObject.FindObjectsOfType<DataObject>().Length - 1);
  119. foreach (var input in testInputs)
  120. {
  121. checkInputEqualsGameObject(input, delete: true);
  122. }
  123. yield return null;
  124. Assert.AreEqual(0, GameObject.FindObjectsOfType<DataObject>().Length - 1);
  125. }
  126. [UnityTest]
  127. public IEnumerator GameObjectsCorrectlyUpdated()
  128. {
  129. yield return null;
  130. Assert.AreEqual(0, GameObject.FindObjectsOfType<DataObject>().Length - 1);
  131. int amountChecks = 4;
  132. amountInputs = 5;
  133. float updateRate = 1 / 4;
  134. List<InputObject> oldTestInputs;
  135. List<InputObject> testInputs;
  136. for (int i = 0; i < amountChecks; i++)
  137. {
  138. oldTestInputs = createTestInputs(amountInputs, i * updateRate);
  139. Handler.Handle(oldTestInputs);
  140. yield return null;
  141. Assert.AreEqual(amountInputs, GameObject.FindObjectsOfType<DataObject>().Length - 1);
  142. testInputs = createTestInputs(5, (i + 1) * updateRate, i + 1);
  143. Handler.Handle(testInputs);
  144. yield return null;
  145. Assert.AreEqual(amountInputs, GameObject.FindObjectsOfType<DataObject>().Length - 1);
  146. for (int j = 0; j < amountInputs; j++)
  147. {
  148. if (i >= amountChecks - 1)
  149. {
  150. checkInputEqualsGameObject(testInputs[j], oldTestInputs[j], delete: true);
  151. }
  152. else
  153. {
  154. checkInputEqualsGameObject(testInputs[j], oldTestInputs[j]);
  155. }
  156. }
  157. }
  158. }
  159. [UnityTest]
  160. public IEnumerator GameObjectsCorrectlyDestroyed()
  161. {
  162. yield return null;
  163. Assert.AreEqual(0, GameObject.FindObjectsOfType<DataObject>().Length - 1);
  164. amountInputs = 5;
  165. List<InputObject> testInputs = createTestInputs(amountInputs, 0);
  166. Handler.Handle(testInputs);
  167. yield return null;
  168. Assert.AreEqual(amountInputs, GameObject.FindObjectsOfType<DataObject>().Length - 1);
  169. foreach (var obj in GameObject.FindObjectsOfType<DataObject>())
  170. {
  171. Handler.DeleteObject(obj);
  172. }
  173. yield return null;
  174. yield return null;
  175. Assert.AreEqual(0, GameObject.FindObjectsOfType<DataObject>().Length - 1);
  176. }
  177. [UnityTest]
  178. public IEnumerator CorrectlyCleared()
  179. {
  180. yield return null;
  181. Assert.AreEqual(0, GameObject.FindObjectsOfType<DataObject>().Length - 1);
  182. amountInputs = 5;
  183. List<InputObject> testInputs = createTestInputs(amountInputs, 0);
  184. Handler.Handle(testInputs);
  185. yield return null;
  186. Assert.AreEqual(amountInputs, GameObject.FindObjectsOfType<DataObject>().Length - 1);
  187. testInputs = createTestInputs(amountInputs, 0.25, 1);
  188. Handler.Handle(testInputs);
  189. Handler.ClearAll();
  190. yield return null;
  191. yield return null;
  192. Assert.AreEqual(0, GameObject.FindObjectsOfType<DataObject>().Length - 1);
  193. testInputs = createTestInputs(amountInputs, 0.5, 2);
  194. Handler.Handle(testInputs);
  195. Handler.ClearAll();
  196. yield return null;
  197. Assert.AreEqual(0, GameObject.FindObjectsOfType<DataObject>().Length - 1);
  198. yield return null;
  199. Assert.AreEqual(0, GameObject.FindObjectsOfType<DataObject>().Length - 1);
  200. }
  201. }
  202. }