CallbackTest.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. using System;
  2. using System.Collections;
  3. using NUnit.Framework;
  4. using UnityEngine;
  5. using UnityEngine.TestTools;
  6. namespace LitMotion.Tests.Runtime
  7. {
  8. public class CallbackTest
  9. {
  10. [UnityTest]
  11. public IEnumerator Test_OnCancel()
  12. {
  13. var canceled = false;
  14. var handle = LMotion.Create(0f, 10f, 2f)
  15. .WithOnCancel(() => canceled = true)
  16. .RunWithoutBinding();
  17. yield return new WaitForSeconds(0.5f);
  18. handle.Cancel();
  19. Assert.IsTrue(canceled);
  20. }
  21. [UnityTest]
  22. public IEnumerator Test_OnComplete()
  23. {
  24. var completed = false;
  25. LMotion.Create(0f, 10f, 2f)
  26. .WithOnComplete(() => completed = true)
  27. .RunWithoutBinding();
  28. yield return new WaitForSeconds(2.1f);
  29. Assert.IsTrue(completed);
  30. }
  31. [UnityTest]
  32. public IEnumerator Test_CreateOnCallback()
  33. {
  34. var created = false;
  35. var completed = false;
  36. LMotion.Create(0f, 10f, 1f)
  37. .Bind(x =>
  38. {
  39. if (x > 5f && !created)
  40. {
  41. LMotion.Create(0f, 10f, 1f)
  42. .WithOnComplete(() => completed = true)
  43. .RunWithoutBinding();
  44. created = true;
  45. }
  46. });
  47. yield return new WaitForSeconds(2.1f);
  48. Assert.IsTrue(created);
  49. Assert.IsTrue(completed);
  50. }
  51. [Test]
  52. public void Test_CompleteOnCallback_Self()
  53. {
  54. LogAssert.Expect(LogType.Exception, "InvalidOperationException: Recursion of Complete call was detected.");
  55. MotionHandle handle = default;
  56. handle = LMotion.Create(0f, 10f, 1f)
  57. .WithOnComplete(() => handle.Complete())
  58. .RunWithoutBinding();
  59. handle.Complete();
  60. }
  61. [Test]
  62. public void Test_CompleteOnCallback_CircularReference()
  63. {
  64. LogAssert.Expect(LogType.Exception, "InvalidOperationException: Recursion of Complete call was detected.");
  65. MotionHandle handle1 = default;
  66. MotionHandle handle2 = default;
  67. handle1 = LMotion.Create(0f, 10f, 1f)
  68. .WithOnComplete(() => handle2.Complete())
  69. .RunWithoutBinding();
  70. handle2 = LMotion.Create(0f, 10f, 1f)
  71. .WithOnComplete(() => handle1.Complete())
  72. .RunWithoutBinding();
  73. handle1.Complete();
  74. }
  75. [UnityTest]
  76. public IEnumerator Test_CompleteOnCallback_Other()
  77. {
  78. MotionHandle otherHandle = LMotion.Create(0f, 10f, 5f).RunWithoutBinding();
  79. LMotion.Create(0f, 10f, 0.5f)
  80. .WithOnComplete(() => otherHandle.Complete())
  81. .RunWithoutBinding();
  82. yield return otherHandle.ToYieldInteraction();
  83. }
  84. [UnityTest]
  85. public IEnumerator Test_ThrowExceptionInsideCallback()
  86. {
  87. LogAssert.Expect(LogType.Exception, "Exception: Test");
  88. yield return LMotion.Create(0f, 10f, 0.5f)
  89. .WithOnComplete(() => throw new Exception("Test"))
  90. .RunWithoutBinding()
  91. .ToYieldInteraction();
  92. }
  93. [Test]
  94. public void Test_ThrowExceptionInsideCallback_ThenCompleteManually()
  95. {
  96. LogAssert.Expect(LogType.Exception, "Exception: Test");
  97. var handle = LMotion.Create(0f, 10f, 0.5f)
  98. .WithOnComplete(() => throw new Exception("Test"))
  99. .RunWithoutBinding();
  100. handle.Complete();
  101. }
  102. [UnityTest]
  103. public IEnumerator Test_WithCancelOnError()
  104. {
  105. LogAssert.ignoreFailingMessages = true;
  106. var completed = false;
  107. LMotion.Create(0f, 10f, 0.5f)
  108. .WithCancelOnError()
  109. .WithOnComplete(() => completed = true)
  110. .Bind(x => throw new Exception("Test"));
  111. yield return new WaitForSeconds(0.7f);
  112. Assert.IsFalse(completed);
  113. LogAssert.ignoreFailingMessages = false;
  114. }
  115. [UnityTest]
  116. public IEnumerator Test_RegisterUnhandledExceptionHandler()
  117. {
  118. var defaultHandler = MotionDispatcher.GetUnhandledExceptionHandler();
  119. MotionDispatcher.RegisterUnhandledExceptionHandler(ex => Debug.LogWarning(ex));
  120. LogAssert.NoUnexpectedReceived();
  121. yield return LMotion.Create(0f, 10f, 0.5f)
  122. .WithOnComplete(() => throw new Exception("Test"))
  123. .RunWithoutBinding()
  124. .ToYieldInteraction();
  125. MotionDispatcher.RegisterUnhandledExceptionHandler(defaultHandler);
  126. }
  127. }
  128. }