UniTaskTest.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. #if LITMOTION_TEST_UNITASK
  2. using System;
  3. using System.Collections;
  4. using System.Threading;
  5. using Cysharp.Threading.Tasks;
  6. using NUnit.Framework;
  7. using UnityEngine;
  8. using UnityEngine.TestTools;
  9. using UnityEngine.TestTools.Utils;
  10. namespace LitMotion.Tests.Runtime
  11. {
  12. public class UniTaskTest
  13. {
  14. readonly CancellationTokenSource cts = new();
  15. [OneTimeTearDown]
  16. public void OneTimeTearDown()
  17. {
  18. cts.Cancel();
  19. }
  20. [UnityTest]
  21. public IEnumerator Test_ToUniTask() => UniTask.ToCoroutine(async () =>
  22. {
  23. await LMotion.Create(0f, 10f, 1f)
  24. .BindToUnityLogger()
  25. .ToUniTask();
  26. });
  27. [UnityTest]
  28. public IEnumerator Test_ToUniTask_CompleteAndCancelAwait() => UniTask.ToCoroutine(async () =>
  29. {
  30. var completed = false;
  31. var source = new CancellationTokenSource();
  32. source.CancelAfterSlim(TimeSpan.FromSeconds(0.5f));
  33. var handle = LMotion.Create(0f, 10f, 1f)
  34. .WithOnComplete(() => completed = true)
  35. .RunWithoutBinding();
  36. try
  37. {
  38. await handle.ToUniTask(CancelBehaviour.CompleteAndCancelAwait, source.Token);
  39. }
  40. catch (OperationCanceledException)
  41. {
  42. Assert.IsFalse(handle.IsActive());
  43. Assert.IsTrue(completed);
  44. return;
  45. }
  46. Assert.Fail();
  47. });
  48. [UnityTest]
  49. public IEnumerator Test_ToUniTask_CancelAwait() => UniTask.ToCoroutine(async () =>
  50. {
  51. var completed = false;
  52. var canceled = false;
  53. var source = new CancellationTokenSource();
  54. source.CancelAfterSlim(TimeSpan.FromSeconds(0.5f));
  55. var handle = LMotion.Create(0f, 10f, 1f)
  56. .WithOnComplete(() => completed = true)
  57. .WithOnCancel(() => canceled = true)
  58. .RunWithoutBinding();
  59. try
  60. {
  61. await handle.ToUniTask(CancelBehaviour.CancelAwait, source.Token);
  62. }
  63. catch (OperationCanceledException)
  64. {
  65. Assert.IsTrue(handle.IsActive());
  66. Assert.IsFalse(canceled);
  67. Assert.IsFalse(completed);
  68. await UniTask.WaitForSeconds(1f);
  69. Assert.IsFalse(handle.IsActive());
  70. Assert.IsFalse(canceled);
  71. Assert.IsTrue(completed);
  72. return;
  73. }
  74. Assert.Fail();
  75. });
  76. [UnityTest]
  77. public IEnumerator Test_BindToAsyncReactiveProperty() => UniTask.ToCoroutine(async () =>
  78. {
  79. var reactiveProperty = new AsyncReactiveProperty<float>(0f);
  80. _ = LMotion.Create(0f, 10f, 1f)
  81. .WithOnComplete(() => reactiveProperty.Dispose())
  82. .BindToAsyncReactiveProperty(reactiveProperty);
  83. await foreach (var i in reactiveProperty.WithoutCurrent())
  84. {
  85. Debug.Log(i);
  86. }
  87. });
  88. [UnityTest]
  89. public IEnumerator Test_AwaitManyTimes() => UniTask.ToCoroutine(async () =>
  90. {
  91. var value = 0f;
  92. var startValue = 0f;
  93. var endValue = 10f;
  94. for (int i = 0; i < 50; i++)
  95. {
  96. await LMotion.Create(startValue, endValue, 0.1f)
  97. .Bind(x => value = x)
  98. .ToUniTask();
  99. Assert.That(value, Is.EqualTo(10f).Using(FloatEqualityComparer.Instance));
  100. }
  101. });
  102. [UnityTest]
  103. public IEnumerator Test_CancelWhileAwait() => UniTask.ToCoroutine(async () =>
  104. {
  105. var handle = LMotion.Create(0f, 10f, 1f).BindToUnityLogger();
  106. DelayedCall(0.2f, () => handle.Cancel()).Forget();
  107. try
  108. {
  109. await handle.ToUniTask();
  110. }
  111. catch (OperationCanceledException)
  112. {
  113. return;
  114. }
  115. Assert.Fail();
  116. });
  117. [UnityTest]
  118. public IEnumerator Test_CancelWhileAwait_WithCancelOnError() => UniTask.ToCoroutine(async () =>
  119. {
  120. LogAssert.ignoreFailingMessages = true;
  121. var handle = LMotion.Create(0f, 10f, 1f)
  122. .WithCancelOnError()
  123. .Bind(x =>
  124. {
  125. if (x > 5f) throw new Exception("Test");
  126. });
  127. try
  128. {
  129. await handle.ToUniTask();
  130. }
  131. catch (OperationCanceledException)
  132. {
  133. return;
  134. }
  135. Assert.Fail();
  136. });
  137. [UnityTest]
  138. public IEnumerator Test_CancelWhileAwaitFollowedByAnother() => UniTask.ToCoroutine(async () =>
  139. {
  140. var cancellationTokenSource = new CancellationTokenSource();
  141. LMotion.Create(0f, 10f, 1.0f)
  142. .RunWithoutBinding()
  143. .ToUniTask(cancellationTokenSource.Token)
  144. .Forget();
  145. await UniTask.Delay(100);
  146. cancellationTokenSource.Cancel();
  147. var canceled = await LMotion.Create(10.0f, 0.0f, 1.0f)
  148. .RunWithoutBinding()
  149. .ToUniTask()
  150. .SuppressCancellationThrow();
  151. Assert.IsFalse(canceled);
  152. });
  153. async UniTaskVoid DelayedCall(float delay, Action action)
  154. {
  155. await UniTask.WaitForSeconds(delay);
  156. action.Invoke();
  157. }
  158. }
  159. }
  160. #endif