TestMovingObjects.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using NUnit.Framework;
  4. using ObjectScripts;
  5. using UnityEngine;
  6. using UnityEngine.TestTools;
  7. namespace Tests
  8. {
  9. public class ObjectMovement
  10. {
  11. private GameObject gameObject;
  12. private DataObject DataObject;
  13. private Vector3 TargetPos;
  14. [UnitySetUp]
  15. public IEnumerator SetUp()
  16. {
  17. yield return new EnterPlayMode();
  18. Object objectPrefab = Resources.Load("Prefabs/Person");
  19. Object sensorPrefab = Resources.Load("Prefabs/Sensor");
  20. gameObject = (GameObject)MonoBehaviour.Instantiate(objectPrefab);
  21. DataObject = gameObject.GetComponent<DataObject>();
  22. Object dummy = Resources.Load("Prefabs/GameObject");
  23. var dummy2 = (GameObject)MonoBehaviour.Instantiate(dummy);
  24. dummy2.AddComponent<TestObjectHanlder>();
  25. DataObject.SetUp(0, dummy2.GetComponent<TestObjectHanlder>(), CSVReader.EntityType.PERSON, 999, 1);
  26. DataObject.Sensor = (GameObject)MonoBehaviour.Instantiate(sensorPrefab);
  27. DataObject.Sensor.transform.position = new Vector3(0, 0, 0);
  28. TargetPos = new Vector3(100, 0, 300);
  29. DataObject.SetNewTargetPos(TargetPos);
  30. Debug.Log("SetUp");
  31. }
  32. /*
  33. * Test if Object travels to TargetPosition in time.
  34. */
  35. [UnityTest]
  36. public IEnumerator TraveledToDestination()
  37. {
  38. yield return new WaitForSeconds((float)DataObject.Handler.UpdateRate);
  39. Assert.NotNull(DataObject);
  40. Assert.NotNull(gameObject);
  41. Assert.AreEqual(DataObject.TargetPos, gameObject.transform.position);
  42. }
  43. /*
  44. * Test Velocity calculation
  45. */
  46. [UnityTest]
  47. public IEnumerator CorrectVelocity()
  48. {
  49. var velocity = Vector3.Distance(DataObject.StartPos, DataObject.TargetPos) / DataObject.Handler.UpdateRate;
  50. // in mid UpdateRate, when half the time passed until next target pos is set, the velocity should be correct
  51. yield return new WaitForSeconds((float)DataObject.Handler.UpdateRate / 2);
  52. Assert.NotNull(DataObject);
  53. Assert.NotNull(gameObject);
  54. Assert.AreEqual(velocity, DataObject.Velocity, 0.00001);
  55. }
  56. /*
  57. * Test if Velocity > 0, Animation should be running
  58. */
  59. [UnityTest]
  60. public IEnumerator IsAnimatedWhenMoving()
  61. {
  62. Animator animator = gameObject.GetComponentInChildren<Animator>();
  63. var hash = Animator.StringToHash("moving");
  64. // Wait until Object is moving
  65. yield return new WaitUntil(() => DataObject.Velocity > 0);
  66. Assert.AreEqual(DataObject.Velocity > 0, animator.GetBool(hash));
  67. }
  68. /*
  69. * Test if Object is turned in Target Direction
  70. */
  71. [UnityTest]
  72. public IEnumerator TurnedInCorrectDirection()
  73. {
  74. // Wait until Object finished first frame
  75. yield return new WaitForEndOfFrame();
  76. Assert.NotNull(DataObject);
  77. Assert.NotNull(gameObject);
  78. var actual = gameObject.transform.rotation;
  79. gameObject.transform.LookAt(DataObject.Sensor.transform.TransformPoint(DataObject.Rot));
  80. var expected = gameObject.transform.rotation;
  81. Assert.IsTrue(actual.Equals(expected));
  82. }
  83. /*
  84. * Test if Object is faced in Target Direction
  85. */
  86. [UnityTest]
  87. public IEnumerator LookingInCorrectDirection()
  88. {
  89. // Wait until Object finished first frame
  90. yield return new WaitForEndOfFrame();
  91. var head = gameObject.GetComponent<Movement>().Head.transform;
  92. var actual = head.transform.rotation;
  93. head.transform.LookAt(DataObject.Sensor.transform.TransformPoint(DataObject.HeadRot));
  94. head.transform.Rotate(Vector3.back, 90);
  95. var expected = head.transform.rotation;
  96. Assert.IsTrue(actual.Equals(expected));
  97. }
  98. }
  99. }