暫無描述
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

UnityLifecycleManagerTests.cs 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. using System.Collections;
  2. using NUnit.Framework;
  3. using UnityEngine.TestTools;
  4. using UnityEngine.SceneManagement;
  5. using UnityEngine.Advertisements.Utilities;
  6. namespace UnityEngine.Advertisements.Tests
  7. {
  8. public class UnityLifecycleManagerTests
  9. {
  10. private const string k_CoroutineExecutorGameObjectName = UnityLifecycleManager.gameObjectName;
  11. private IUnityLifecycleManager m_UnityLifecycleManager;
  12. private bool m_CoroutineVerification;
  13. [SetUp]
  14. public void Setup()
  15. {
  16. m_UnityLifecycleManager = new UnityLifecycleManager();
  17. }
  18. [TearDown]
  19. public void TearDown()
  20. {
  21. m_UnityLifecycleManager?.Dispose();
  22. m_UnityLifecycleManager = null;
  23. }
  24. [Test]
  25. public void TestReferenceCount()
  26. {
  27. using (new UnityLifecycleManager())
  28. {
  29. using (new UnityLifecycleManager())
  30. {
  31. m_UnityLifecycleManager.Dispose();
  32. Assert.That(GameObject.Find(k_CoroutineExecutorGameObjectName), Is.Not.Null, "Expected " + k_CoroutineExecutorGameObjectName + " to exists");
  33. }
  34. Assert.That(GameObject.Find(k_CoroutineExecutorGameObjectName), Is.Not.Null, "Expected " + k_CoroutineExecutorGameObjectName + " to exists");
  35. }
  36. }
  37. [Test]
  38. public void GameObjectExists()
  39. {
  40. Assert.That(GameObject.Find(k_CoroutineExecutorGameObjectName), Is.Not.Null, "Expected " + k_CoroutineExecutorGameObjectName + " to exists");
  41. }
  42. [Test]
  43. public void GameObjectDestroyed()
  44. {
  45. Assert.That(GameObject.Find(k_CoroutineExecutorGameObjectName), Is.Not.Null, "Expected " + k_CoroutineExecutorGameObjectName + " to exists");
  46. m_UnityLifecycleManager.Dispose();
  47. Assert.That(GameObject.Find(k_CoroutineExecutorGameObjectName), Is.Null, "Expected " + k_CoroutineExecutorGameObjectName + " to be destroyed");
  48. }
  49. [Test]
  50. public void CheckGameObject()
  51. {
  52. var gameObject = GameObject.Find(k_CoroutineExecutorGameObjectName);
  53. Assert.That(gameObject, Is.Not.Null);
  54. Assert.That(gameObject.hideFlags, Is.EqualTo(HideFlags.HideInHierarchy | HideFlags.HideInInspector));
  55. var components = gameObject.GetComponents<MonoBehaviour>();
  56. Assert.That(components.Length, Is.EqualTo(2), "Game object should have two MonoBehaviour components");
  57. var applicationQuitMonoBehavior = gameObject.GetComponent<CoroutineExecutor>();
  58. Assert.That(applicationQuitMonoBehavior, Is.Not.Null, "Unable to find ApplicationQuit MonoBehavior");
  59. Assert.That(applicationQuitMonoBehavior.hideFlags, Is.EqualTo(HideFlags.HideInHierarchy | HideFlags.HideInInspector));
  60. var coroutineExecutorMonoBehavior = gameObject.GetComponent<CoroutineExecutor>();
  61. Assert.That(coroutineExecutorMonoBehavior, Is.Not.Null, "Unable to find CoroutineExecutor MonoBehavior");
  62. Assert.That(coroutineExecutorMonoBehavior.hideFlags, Is.EqualTo(HideFlags.HideInHierarchy | HideFlags.HideInInspector));
  63. }
  64. [Test]
  65. public void OnlyOwnerShouldDestroyObject()
  66. {
  67. Assert.That(GameObject.Find(k_CoroutineExecutorGameObjectName), Is.Not.Null);
  68. using (new UnityLifecycleManager())
  69. {
  70. Assert.That(Resources.FindObjectsOfTypeAll<CoroutineExecutor>().Length, Is.EqualTo(1));
  71. }
  72. Assert.That(GameObject.Find(k_CoroutineExecutorGameObjectName), Is.Not.Null);
  73. }
  74. [UnityTest]
  75. public IEnumerator GameObjectExistsWithNoComponent()
  76. {
  77. m_UnityLifecycleManager?.Dispose();
  78. yield return null; //Make sure the above disposed object gets cleaned up properly
  79. Assert.That(GameObject.Find(k_CoroutineExecutorGameObjectName), Is.Null, "Expected " + k_CoroutineExecutorGameObjectName + " to be destroyed");
  80. GameObject dummyCoroutineExecutor = new GameObject(k_CoroutineExecutorGameObjectName);
  81. Assert.That(GameObject.Find(k_CoroutineExecutorGameObjectName), Is.Not.Null, "Expected " + k_CoroutineExecutorGameObjectName + " to exists");
  82. Assert.That(GameObject.Find(k_CoroutineExecutorGameObjectName).GetComponent<CoroutineExecutor>(), Is.Null, "Expected " + k_CoroutineExecutorGameObjectName + " to be an empty GameObject");
  83. Assert.That(GameObject.Find(k_CoroutineExecutorGameObjectName).GetComponent<ApplicationQuit>(), Is.Null, "Expected " + k_CoroutineExecutorGameObjectName + " to be an empty GameObject");
  84. m_UnityLifecycleManager = new UnityLifecycleManager();
  85. Assert.That(GameObject.Find(k_CoroutineExecutorGameObjectName), Is.Not.Null, "Expected " + k_CoroutineExecutorGameObjectName + " to exists");
  86. Assert.That(GameObject.Find(k_CoroutineExecutorGameObjectName).GetComponent<CoroutineExecutor>(), Is.Not.Null, "Expected " + k_CoroutineExecutorGameObjectName + " to exists with a CoroutineExecutor MonoBehavior attached to the GameObject");
  87. Assert.That(GameObject.Find(k_CoroutineExecutorGameObjectName).GetComponent<ApplicationQuit>(), Is.Not.Null, "Expected " + k_CoroutineExecutorGameObjectName + " to exists with a ApplicationQuit MonoBehavior attached to the GameObject");
  88. }
  89. [UnityTest]
  90. public IEnumerator KeepsObjectWhenNewSceneLoaded()
  91. {
  92. m_UnityLifecycleManager.Dispose();
  93. m_UnityLifecycleManager = null;
  94. var scene = SceneManager.CreateScene("test");
  95. while (!scene.isLoaded) yield return null;
  96. SceneManager.SetActiveScene(scene);
  97. m_UnityLifecycleManager = new UnityLifecycleManager();
  98. yield return SceneManager.UnloadSceneAsync(scene);
  99. Assert.NotNull(GameObject.Find(k_CoroutineExecutorGameObjectName), "Expected " + k_CoroutineExecutorGameObjectName + " to stay when scene unloaded");
  100. }
  101. [UnityTest]
  102. public IEnumerator CoroutineExecutorRunsCoroutines()
  103. {
  104. m_CoroutineVerification = false;
  105. yield return m_UnityLifecycleManager.StartCoroutine(VerifyExecution());
  106. Assert.True(m_CoroutineVerification, "This variable was not properly set in the VerifyExecution coroutine");
  107. }
  108. [UnityTest]
  109. public IEnumerator CoroutineExecutorRunsCoroutinesAfterBeingDestroyed()
  110. {
  111. var coroutineExecutorGameObject = GameObject.Find(k_CoroutineExecutorGameObjectName);
  112. GameObject.DestroyImmediate(coroutineExecutorGameObject);
  113. m_CoroutineVerification = false;
  114. yield return m_UnityLifecycleManager.StartCoroutine(VerifyExecution());
  115. Assert.True(m_CoroutineVerification, "This variable was not properly set in the VerifyExecution coroutine");
  116. }
  117. [UnityTest]
  118. [Timeout(10000)]
  119. public IEnumerator CoroutineExecutorCanRunAction()
  120. {
  121. var hasPostedSuccessfully = false;
  122. m_UnityLifecycleManager.Post(() => {
  123. hasPostedSuccessfully = true;
  124. });
  125. while (!hasPostedSuccessfully) yield return null;
  126. Assert.That(hasPostedSuccessfully, Is.True, "Post was not executed successfully");
  127. }
  128. [UnityTest]
  129. public IEnumerator CoroutineExecutorRunsActionsAfterBeingDestroyed() {
  130. var coroutineExecutorGameObject = GameObject.Find(k_CoroutineExecutorGameObjectName);
  131. GameObject.DestroyImmediate(coroutineExecutorGameObject);
  132. m_CoroutineVerification = false;
  133. m_UnityLifecycleManager.Post(() => {
  134. m_CoroutineVerification = true;
  135. });
  136. yield return null;
  137. Assert.True(m_CoroutineVerification, "This variable was not properly set by the action run via Post");
  138. }
  139. [UnityTest]
  140. public IEnumerator JungleRunnerRegression()
  141. {
  142. //Destroy all GameObjects in the scene the try and run a coroutine. Keep the PlaymodeTestsController GameObject only.
  143. var gameObjectList = GameObject.FindObjectsOfType<GameObject>();
  144. for (int i = 0; i < gameObjectList.Length; i++)
  145. {
  146. var go = gameObjectList[i];
  147. if (go.GetComponent("UnityEngine.TestTools.TestRunner.PlaymodeTestsController") != null)
  148. {
  149. continue;
  150. }
  151. GameObject.DestroyImmediate(go);
  152. }
  153. m_CoroutineVerification = false;
  154. yield return m_UnityLifecycleManager.StartCoroutine(VerifyExecution());
  155. Assert.True(m_CoroutineVerification, "This variable was not properly set in the VerifyExecution coroutine");
  156. }
  157. [UnityTest]
  158. public IEnumerator JungleRunnerRegression_Post()
  159. {
  160. //Destroy all GameObjects in the scene the try and run a coroutine. Keep the PlaymodeTestsController GameObject only.
  161. var gameObjectList = GameObject.FindObjectsOfType<GameObject>();
  162. for (int i = 0; i < gameObjectList.Length; i++)
  163. {
  164. var go = gameObjectList[i];
  165. if (go.GetComponent("UnityEngine.TestTools.TestRunner.PlaymodeTestsController") != null)
  166. {
  167. continue;
  168. }
  169. GameObject.DestroyImmediate(go);
  170. }
  171. m_CoroutineVerification = false;
  172. m_UnityLifecycleManager.Post(() => {
  173. m_CoroutineVerification = true;
  174. });
  175. yield return null;
  176. Assert.True(m_CoroutineVerification, "This variable was not properly set in the VerifyExecution coroutine");
  177. }
  178. [UnityTest]
  179. public IEnumerator SetOnApplicationQuitCallback()
  180. {
  181. bool callbackWasCalled = false;
  182. m_UnityLifecycleManager.SetOnApplicationQuitCallback(() => {
  183. callbackWasCalled = true;
  184. });
  185. GameObject.Find(k_CoroutineExecutorGameObjectName).SendMessage("OnApplicationQuit");
  186. yield return null;
  187. Assert.That(callbackWasCalled, Is.True, "Callback was not called as expected.");
  188. }
  189. private IEnumerator VerifyExecution()
  190. {
  191. yield return null;
  192. m_CoroutineVerification = true;
  193. }
  194. }
  195. }