123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using NUnit.Framework;
- using UnityEngine;
- using UnityEngine.TestTools;
- using ObjectScripts;
- using CSVReader;
- namespace Tests
- {
- public class ObjectHandlerTest
- {
- private GameObject ObjectHandler;
- private ObjectHandler Handler;
- private GameObject ManagerObject;
- private AbstractManager Manager;
- private int amountInputs = 10;
- [UnitySetUp]
- public IEnumerator SetUp()
- {
- yield return new EnterPlayMode();
- ObjectHandler = GameObject.CreatePrimitive(PrimitiveType.Cube);
- Handler = ObjectHandler.AddComponent<ObjectHandler>();
- ManagerObject = GameObject.CreatePrimitive(PrimitiveType.Cube);
- Manager = ManagerObject.AddComponent<TestManager>();
- Manager.UpdateRate = 4;
- Manager.GameVelocity = 1;
- Manager.UpdateRateInSeconds = 1 / 4;
- Handler.ManagerObject = Manager;
- GameObject person = GameObject.CreatePrimitive(PrimitiveType.Cube);
- person.AddComponent<DataObject>();
- Handler.prefabHuman = person;
- Handler.prefabCar = person;
- Handler.prefabBike = person;
- Handler.prefabTruck = person;
- Dictionary<int, GameObject> sensorList = new Dictionary<int, GameObject>();
- for (var i = 0; i < amountInputs; i++)
- {
- sensorList.Add(i, new GameObject());
- }
- Handler.SensorList = sensorList;
- // yield return new WaitForSecondsRealtime(3);
- // ManagerObject.Handler = Handler;
- yield return null;
- }
- [UnityTearDown]
- public IEnumerator TearDown()
- {
- foreach (MonoBehaviour item in GameObject.FindObjectsOfType<MonoBehaviour>())
- {
- GameObject.Destroy(item.gameObject);
- }
- yield return null;
-
- yield return new ExitPlayMode();
- }
- private List<InputObject> createTestInputs(int amountInputs, double time, int move = 0)
- {
- List<InputObject> inputs = new List<InputObject>();
- InputObject input;
- EntityType[] allTypes = (EntityType[])(System.Enum.GetValues(typeof(EntityType)));
- for (int id = 0; id < amountInputs; id++)
- {
- int value = id + move;
- float posX = (value % 3 == 0) ? -value / 100 : value / 100, posY = (value % 2 == 0) ? -value + 0.2f : value - 10, posZ = (value % 7 == 0) ? value : 0;
- float rotX = 0, rotY = 0, rotZ = 0;
- float rotHeadX = 0, rotHeadY = 0, rotHeadZ = 0;
- byte rTop = (value % 4 == 0) ? byte.MaxValue : byte.MinValue, gTop = (byte)(value % byte.MaxValue), bTop = (byte)(value % byte.MaxValue),
- rBot = (byte)(value % byte.MaxValue), gBot = (byte)(value % byte.MaxValue), bBot = (byte)(value % byte.MaxValue);
- uint botColor = 333333;
- uint topColor = 555555;
- int sensorID = id;
- EntityType type = (EntityType)allTypes.GetValue(value % allTypes.Length);
- // Debug.Log((id, time, posX, posY, posZ, rotX, rotY, rotZ, rTop, gTop, bTop, rBot, gBot, bBot, type).ToString());
- input = new InputObject(id, time, posX, posY, posZ, rotX, rotY, rotZ, rotHeadX, rotHeadY, rotHeadZ, topColor, botColor, type, sensorID);
- inputs.Add(input);
- }
- return inputs;
- }
- private void checkInputEqualsGameObject(InputObject input, InputObject inputBeforeUpdate = null, bool delete = false)
- {
- Debug.LogWarning("Rotation and SensorID isn't checked");
- GameObject obj = GameObject.Find(input.ID.ToString());
- Assert.NotNull(obj, string.Format("Object {0} not created", input.ID));
- DataObject data;
- Assert.True(Handler.DataObjectDictionary.TryGetValue(input.ID, out data), string.Format("Object {0} not in DataObjectDictionary", input.ID));
- Assert.AreEqual(data.Handler, Handler);
- Assert.AreEqual(data.ID, input.ID);
- Assert.AreEqual(data.Type, input.Type);
- Assert.AreEqual(data.Timestamp, input.Time);
- if (inputBeforeUpdate == null)
- {
- Assert.AreEqual(data.StartPos, input.Pos);
- }
- else
- {
- Assert.AreEqual(data.StartPos, inputBeforeUpdate.Pos);
- }
- Assert.AreEqual(data.TargetPos, input.Pos);
- Assert.AreEqual(data.TimeSinceLastUpdate, 0f);
- Assert.AreEqual(data.BotColor, input.BotColor);
- Assert.AreEqual(data.TopColor, input.TopColor);
- Assert.AreEqual(data.Handler.UpdateRate, Manager.UpdateRateInSeconds);
- if (delete)
- {
- GameObject.Destroy(obj);
- }
- }
- [UnityTest]
- public IEnumerator GameObjectsCorrectlyCreated()
- {
- // Use the Assert class to test conditions.
- // Use yield to skip a frame.
- yield return null;
- Assert.AreEqual(0, GameObject.FindObjectsOfType<DataObject>().Length - 1);
- List<InputObject> testInputs = createTestInputs(amountInputs, 0);
- Handler.Handle(testInputs);
- yield return null;
- Assert.AreEqual(amountInputs, GameObject.FindObjectsOfType<DataObject>().Length - 1);
- foreach (var input in testInputs)
- {
- checkInputEqualsGameObject(input, delete: true);
- }
- yield return null;
- Assert.AreEqual(0, GameObject.FindObjectsOfType<DataObject>().Length - 1);
- }
- [UnityTest]
- public IEnumerator GameObjectsCorrectlyUpdated()
- {
- yield return null;
- Assert.AreEqual(0, GameObject.FindObjectsOfType<DataObject>().Length - 1);
- int amountChecks = 4;
- amountInputs = 5;
- float updateRate = 1 / 4;
- List<InputObject> oldTestInputs;
- List<InputObject> testInputs;
- for (int i = 0; i < amountChecks; i++)
- {
- oldTestInputs = createTestInputs(amountInputs, i * updateRate);
- Handler.Handle(oldTestInputs);
- yield return null;
- Assert.AreEqual(amountInputs, GameObject.FindObjectsOfType<DataObject>().Length - 1);
- testInputs = createTestInputs(5, (i + 1) * updateRate, i + 1);
- Handler.Handle(testInputs);
- yield return null;
- Assert.AreEqual(amountInputs, GameObject.FindObjectsOfType<DataObject>().Length - 1);
- for (int j = 0; j < amountInputs; j++)
- {
- if (i >= amountChecks - 1)
- {
- checkInputEqualsGameObject(testInputs[j], oldTestInputs[j], delete: true);
- }
- else
- {
- checkInputEqualsGameObject(testInputs[j], oldTestInputs[j]);
- }
- }
- }
- }
- [UnityTest]
- public IEnumerator GameObjectsCorrectlyDestroyed()
- {
- yield return null;
- Assert.AreEqual(0, GameObject.FindObjectsOfType<DataObject>().Length - 1);
- amountInputs = 5;
- List<InputObject> testInputs = createTestInputs(amountInputs, 0);
- Handler.Handle(testInputs);
- yield return null;
- Assert.AreEqual(amountInputs, GameObject.FindObjectsOfType<DataObject>().Length - 1);
- foreach (var obj in GameObject.FindObjectsOfType<DataObject>())
- {
- Handler.DeleteObject(obj);
- }
- yield return null;
- yield return null;
- Assert.AreEqual(0, GameObject.FindObjectsOfType<DataObject>().Length - 1);
- }
- [UnityTest]
- public IEnumerator CorrectlyCleared()
- {
- yield return null;
- Assert.AreEqual(0, GameObject.FindObjectsOfType<DataObject>().Length - 1);
- amountInputs = 5;
- List<InputObject> testInputs = createTestInputs(amountInputs, 0);
- Handler.Handle(testInputs);
- yield return null;
- Assert.AreEqual(amountInputs, GameObject.FindObjectsOfType<DataObject>().Length - 1);
- testInputs = createTestInputs(amountInputs, 0.25, 1);
- Handler.Handle(testInputs);
- Handler.ClearAll();
- yield return null;
- yield return null;
- Assert.AreEqual(0, GameObject.FindObjectsOfType<DataObject>().Length - 1);
- testInputs = createTestInputs(amountInputs, 0.5, 2);
- Handler.Handle(testInputs);
- Handler.ClearAll();
- yield return null;
- Assert.AreEqual(0, GameObject.FindObjectsOfType<DataObject>().Length - 1);
- yield return null;
- Assert.AreEqual(0, GameObject.FindObjectsOfType<DataObject>().Length - 1);
- }
- }
- }
|