using System.Collections; using System.Collections.Generic; using NUnit.Framework; using ObjectScripts; using UnityEngine; using UnityEngine.TestTools; namespace Tests { public class ObjectMovement { private GameObject gameObject; private DataObject DataObject; private Vector3 TargetPos; [UnitySetUp] public IEnumerator SetUp() { yield return new EnterPlayMode(); Object objectPrefab = Resources.Load("Prefabs/Person"); Object sensorPrefab = Resources.Load("Prefabs/Sensor"); gameObject = (GameObject)MonoBehaviour.Instantiate(objectPrefab); DataObject = gameObject.GetComponent(); Object dummy = Resources.Load("Prefabs/GameObject"); var dummy2 = (GameObject)MonoBehaviour.Instantiate(dummy); dummy2.AddComponent(); DataObject.SetUp(0, dummy2.GetComponent(), CSVReader.EntityType.PERSON, 999, 1); DataObject.Sensor = (GameObject)MonoBehaviour.Instantiate(sensorPrefab); DataObject.Sensor.transform.position = new Vector3(0, 0, 0); TargetPos = new Vector3(100, 0, 300); DataObject.SetNewTargetPos(TargetPos); Debug.Log("SetUp"); } /* * Test if Object travels to TargetPosition in time. */ [UnityTest] public IEnumerator TraveledToDestination() { yield return new WaitForSeconds((float)DataObject.Handler.UpdateRate); Assert.NotNull(DataObject); Assert.NotNull(gameObject); Assert.AreEqual(DataObject.TargetPos, gameObject.transform.position); } /* * Test Velocity calculation */ [UnityTest] public IEnumerator CorrectVelocity() { var velocity = Vector3.Distance(DataObject.StartPos, DataObject.TargetPos) / DataObject.Handler.UpdateRate; // in mid UpdateRate, when half the time passed until next target pos is set, the velocity should be correct yield return new WaitForSeconds((float)DataObject.Handler.UpdateRate / 2); Assert.NotNull(DataObject); Assert.NotNull(gameObject); Assert.AreEqual(velocity, DataObject.Velocity, 0.00001); } /* * Test if Velocity > 0, Animation should be running */ [UnityTest] public IEnumerator IsAnimatedWhenMoving() { Animator animator = gameObject.GetComponentInChildren(); var hash = Animator.StringToHash("moving"); // Wait until Object is moving yield return new WaitUntil(() => DataObject.Velocity > 0); Assert.AreEqual(DataObject.Velocity > 0, animator.GetBool(hash)); } /* * Test if Object is turned in Target Direction */ [UnityTest] public IEnumerator TurnedInCorrectDirection() { // Wait until Object finished first frame yield return new WaitForEndOfFrame(); Assert.NotNull(DataObject); Assert.NotNull(gameObject); var actual = gameObject.transform.rotation; gameObject.transform.LookAt(DataObject.Sensor.transform.TransformPoint(DataObject.Rot)); var expected = gameObject.transform.rotation; Assert.IsTrue(actual.Equals(expected)); } /* * Test if Object is faced in Target Direction */ [UnityTest] public IEnumerator LookingInCorrectDirection() { // Wait until Object finished first frame yield return new WaitForEndOfFrame(); var head = gameObject.GetComponent().Head.transform; var actual = head.transform.rotation; head.transform.LookAt(DataObject.Sensor.transform.TransformPoint(DataObject.HeadRot)); head.transform.Rotate(Vector3.back, 90); var expected = head.transform.rotation; Assert.IsTrue(actual.Equals(expected)); } } }