CallTesting.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. using UnityEngine;
  2. namespace UnityTest
  3. {
  4. public class CallTesting : MonoBehaviour
  5. {
  6. public enum Functions
  7. {
  8. CallAfterSeconds,
  9. CallAfterFrames,
  10. Start,
  11. Update,
  12. FixedUpdate,
  13. LateUpdate,
  14. OnDestroy,
  15. OnEnable,
  16. OnDisable,
  17. OnControllerColliderHit,
  18. OnParticleCollision,
  19. OnJointBreak,
  20. OnBecameInvisible,
  21. OnBecameVisible,
  22. OnTriggerEnter,
  23. OnTriggerExit,
  24. OnTriggerStay,
  25. OnCollisionEnter,
  26. OnCollisionExit,
  27. OnCollisionStay,
  28. #if !UNITY_4_0 && !UNITY_4_0_1 && !UNITY_4_1 && !UNITY_4_2
  29. OnTriggerEnter2D,
  30. OnTriggerExit2D,
  31. OnTriggerStay2D,
  32. OnCollisionEnter2D,
  33. OnCollisionExit2D,
  34. OnCollisionStay2D,
  35. #endif
  36. }
  37. public enum Method
  38. {
  39. Pass,
  40. Fail
  41. }
  42. public int afterFrames = 0;
  43. public float afterSeconds = 0.0f;
  44. public Functions callOnMethod = Functions.Start;
  45. public Method methodToCall;
  46. private int startFrame = 0;
  47. private float startTime;
  48. private void TryToCallTesting (Functions invokingMethod)
  49. {
  50. if (invokingMethod == callOnMethod)
  51. {
  52. if (methodToCall == Method.Pass)
  53. IntegrationTest.Pass (gameObject);
  54. else
  55. IntegrationTest.Fail (gameObject);
  56. afterFrames = 0;
  57. afterSeconds = 0.0f;
  58. startTime = float.PositiveInfinity;
  59. startFrame = int.MinValue;
  60. }
  61. }
  62. public void Start ()
  63. {
  64. startTime = Time.time;
  65. startFrame = afterFrames;
  66. TryToCallTesting (Functions.Start);
  67. }
  68. public void Update ()
  69. {
  70. TryToCallTesting (Functions.Update);
  71. CallAfterSeconds ();
  72. CallAfterFrames ();
  73. }
  74. private void CallAfterFrames ()
  75. {
  76. if (afterFrames > 0 && (startFrame + afterFrames) <= Time.frameCount)
  77. TryToCallTesting (Functions.CallAfterFrames);
  78. }
  79. private void CallAfterSeconds ()
  80. {
  81. if ((startTime + afterSeconds) <= Time.time)
  82. TryToCallTesting (Functions.CallAfterSeconds);
  83. }
  84. public void OnDisable ()
  85. {
  86. TryToCallTesting (Functions.OnDisable);
  87. }
  88. public void OnEnable ()
  89. {
  90. TryToCallTesting (Functions.OnEnable);
  91. }
  92. public void OnDestroy ()
  93. {
  94. TryToCallTesting (Functions.OnDestroy);
  95. }
  96. public void FixedUpdate ()
  97. {
  98. TryToCallTesting (Functions.FixedUpdate);
  99. }
  100. public void LateUpdate ()
  101. {
  102. TryToCallTesting (Functions.LateUpdate);
  103. }
  104. public void OnControllerColliderHit ()
  105. {
  106. TryToCallTesting (Functions.OnControllerColliderHit);
  107. }
  108. public void OnParticleCollision ()
  109. {
  110. TryToCallTesting (Functions.OnParticleCollision);
  111. }
  112. public void OnJointBreak ()
  113. {
  114. TryToCallTesting (Functions.OnJointBreak);
  115. }
  116. public void OnBecameInvisible ()
  117. {
  118. TryToCallTesting (Functions.OnBecameInvisible);
  119. }
  120. public void OnBecameVisible ()
  121. {
  122. TryToCallTesting (Functions.OnBecameVisible);
  123. }
  124. public void OnTriggerEnter ()
  125. {
  126. TryToCallTesting (Functions.OnTriggerEnter);
  127. }
  128. public void OnTriggerExit ()
  129. {
  130. TryToCallTesting (Functions.OnTriggerExit);
  131. }
  132. public void OnTriggerStay ()
  133. {
  134. TryToCallTesting (Functions.OnTriggerStay);
  135. }
  136. public void OnCollisionEnter ()
  137. {
  138. TryToCallTesting (Functions.OnCollisionEnter);
  139. }
  140. public void OnCollisionExit ()
  141. {
  142. TryToCallTesting (Functions.OnCollisionExit);
  143. }
  144. public void OnCollisionStay ()
  145. {
  146. TryToCallTesting (Functions.OnCollisionStay);
  147. }
  148. #if !UNITY_4_0 && !UNITY_4_0_1 && !UNITY_4_1 && !UNITY_4_2
  149. public void OnTriggerEnter2D ()
  150. {
  151. TryToCallTesting (Functions.OnTriggerEnter2D);
  152. }
  153. public void OnTriggerExit2D ()
  154. {
  155. TryToCallTesting (Functions.OnTriggerExit2D);
  156. }
  157. public void OnTriggerStay2D ()
  158. {
  159. TryToCallTesting (Functions.OnTriggerStay2D);
  160. }
  161. public void OnCollisionEnter2D ()
  162. {
  163. TryToCallTesting (Functions.OnCollisionEnter2D);
  164. }
  165. public void OnCollisionExit2D ()
  166. {
  167. TryToCallTesting (Functions.OnCollisionExit2D);
  168. }
  169. public void OnCollisionStay2D ()
  170. {
  171. TryToCallTesting (Functions.OnCollisionStay2D);
  172. }
  173. #endif
  174. }
  175. }