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(); ManagerObject = GameObject.CreatePrimitive(PrimitiveType.Cube); Manager = ManagerObject.AddComponent(); Manager.UpdateRate = 4; Manager.GameVelocity = 1; Manager.UpdateRateInSeconds = 1 / 4; Handler.ManagerObject = Manager; GameObject person = GameObject.CreatePrimitive(PrimitiveType.Cube); person.AddComponent(); Handler.prefabHuman = person; Handler.prefabCar = person; Handler.prefabBike = person; Handler.prefabTruck = person; Dictionary sensorList = new Dictionary(); 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()) { GameObject.Destroy(item.gameObject); } yield return null; yield return new ExitPlayMode(); } private List createTestInputs(int amountInputs, double time, int move = 0) { List inputs = new List(); 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().Length - 1); List testInputs = createTestInputs(amountInputs, 0); Handler.Handle(testInputs); yield return null; Assert.AreEqual(amountInputs, GameObject.FindObjectsOfType().Length - 1); foreach (var input in testInputs) { checkInputEqualsGameObject(input, delete: true); } yield return null; Assert.AreEqual(0, GameObject.FindObjectsOfType().Length - 1); } [UnityTest] public IEnumerator GameObjectsCorrectlyUpdated() { yield return null; Assert.AreEqual(0, GameObject.FindObjectsOfType().Length - 1); int amountChecks = 4; amountInputs = 5; float updateRate = 1 / 4; List oldTestInputs; List testInputs; for (int i = 0; i < amountChecks; i++) { oldTestInputs = createTestInputs(amountInputs, i * updateRate); Handler.Handle(oldTestInputs); yield return null; Assert.AreEqual(amountInputs, GameObject.FindObjectsOfType().Length - 1); testInputs = createTestInputs(5, (i + 1) * updateRate, i + 1); Handler.Handle(testInputs); yield return null; Assert.AreEqual(amountInputs, GameObject.FindObjectsOfType().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().Length - 1); amountInputs = 5; List testInputs = createTestInputs(amountInputs, 0); Handler.Handle(testInputs); yield return null; Assert.AreEqual(amountInputs, GameObject.FindObjectsOfType().Length - 1); foreach (var obj in GameObject.FindObjectsOfType()) { Handler.DeleteObject(obj); } yield return null; yield return null; Assert.AreEqual(0, GameObject.FindObjectsOfType().Length - 1); } [UnityTest] public IEnumerator CorrectlyCleared() { yield return null; Assert.AreEqual(0, GameObject.FindObjectsOfType().Length - 1); amountInputs = 5; List testInputs = createTestInputs(amountInputs, 0); Handler.Handle(testInputs); yield return null; Assert.AreEqual(amountInputs, GameObject.FindObjectsOfType().Length - 1); testInputs = createTestInputs(amountInputs, 0.25, 1); Handler.Handle(testInputs); Handler.ClearAll(); yield return null; yield return null; Assert.AreEqual(0, GameObject.FindObjectsOfType().Length - 1); testInputs = createTestInputs(amountInputs, 0.5, 2); Handler.Handle(testInputs); Handler.ClearAll(); yield return null; Assert.AreEqual(0, GameObject.FindObjectsOfType().Length - 1); yield return null; Assert.AreEqual(0, GameObject.FindObjectsOfType().Length - 1); } } }