123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- 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<DataObject>();
- Object dummy = Resources.Load("Prefabs/GameObject");
- var dummy2 = (GameObject)MonoBehaviour.Instantiate(dummy);
- dummy2.AddComponent<TestObjectHanlder>();
- DataObject.SetUp(0, dummy2.GetComponent<TestObjectHanlder>(), 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<Animator>();
- 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<Movement>().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));
- }
- }
- }
|