123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- using System;
- using System.Collections;
- using NUnit.Framework;
- using UnityEngine;
- using UnityEngine.TestTools;
- namespace LitMotion.Tests.Runtime
- {
- public class CallbackTest
- {
- [UnityTest]
- public IEnumerator Test_OnCancel()
- {
- var canceled = false;
- var handle = LMotion.Create(0f, 10f, 2f)
- .WithOnCancel(() => canceled = true)
- .RunWithoutBinding();
- yield return new WaitForSeconds(0.5f);
- handle.Cancel();
- Assert.IsTrue(canceled);
- }
- [UnityTest]
- public IEnumerator Test_OnComplete()
- {
- var completed = false;
- LMotion.Create(0f, 10f, 2f)
- .WithOnComplete(() => completed = true)
- .RunWithoutBinding();
- yield return new WaitForSeconds(2.1f);
- Assert.IsTrue(completed);
- }
- [UnityTest]
- public IEnumerator Test_CreateOnCallback()
- {
- var created = false;
- var completed = false;
- LMotion.Create(0f, 10f, 1f)
- .Bind(x =>
- {
- if (x > 5f && !created)
- {
- LMotion.Create(0f, 10f, 1f)
- .WithOnComplete(() => completed = true)
- .RunWithoutBinding();
- created = true;
- }
- });
- yield return new WaitForSeconds(2.1f);
- Assert.IsTrue(created);
- Assert.IsTrue(completed);
- }
- [Test]
- public void Test_CompleteOnCallback_Self()
- {
- LogAssert.Expect(LogType.Exception, "InvalidOperationException: Recursion of Complete call was detected.");
- MotionHandle handle = default;
- handle = LMotion.Create(0f, 10f, 1f)
- .WithOnComplete(() => handle.Complete())
- .RunWithoutBinding();
- handle.Complete();
- }
- [Test]
- public void Test_CompleteOnCallback_CircularReference()
- {
- LogAssert.Expect(LogType.Exception, "InvalidOperationException: Recursion of Complete call was detected.");
- MotionHandle handle1 = default;
- MotionHandle handle2 = default;
- handle1 = LMotion.Create(0f, 10f, 1f)
- .WithOnComplete(() => handle2.Complete())
- .RunWithoutBinding();
- handle2 = LMotion.Create(0f, 10f, 1f)
- .WithOnComplete(() => handle1.Complete())
- .RunWithoutBinding();
- handle1.Complete();
- }
- [UnityTest]
- public IEnumerator Test_CompleteOnCallback_Other()
- {
- MotionHandle otherHandle = LMotion.Create(0f, 10f, 5f).RunWithoutBinding();
- LMotion.Create(0f, 10f, 0.5f)
- .WithOnComplete(() => otherHandle.Complete())
- .RunWithoutBinding();
- yield return otherHandle.ToYieldInteraction();
- }
- [UnityTest]
- public IEnumerator Test_ThrowExceptionInsideCallback()
- {
- LogAssert.Expect(LogType.Exception, "Exception: Test");
- yield return LMotion.Create(0f, 10f, 0.5f)
- .WithOnComplete(() => throw new Exception("Test"))
- .RunWithoutBinding()
- .ToYieldInteraction();
- }
- [Test]
- public void Test_ThrowExceptionInsideCallback_ThenCompleteManually()
- {
- LogAssert.Expect(LogType.Exception, "Exception: Test");
- var handle = LMotion.Create(0f, 10f, 0.5f)
- .WithOnComplete(() => throw new Exception("Test"))
- .RunWithoutBinding();
- handle.Complete();
- }
- [UnityTest]
- public IEnumerator Test_WithCancelOnError()
- {
- LogAssert.ignoreFailingMessages = true;
- var completed = false;
- LMotion.Create(0f, 10f, 0.5f)
- .WithCancelOnError()
- .WithOnComplete(() => completed = true)
- .Bind(x => throw new Exception("Test"));
- yield return new WaitForSeconds(0.7f);
- Assert.IsFalse(completed);
- LogAssert.ignoreFailingMessages = false;
- }
- [UnityTest]
- public IEnumerator Test_RegisterUnhandledExceptionHandler()
- {
- var defaultHandler = MotionDispatcher.GetUnhandledExceptionHandler();
- MotionDispatcher.RegisterUnhandledExceptionHandler(ex => Debug.LogWarning(ex));
- LogAssert.NoUnexpectedReceived();
- yield return LMotion.Create(0f, 10f, 0.5f)
- .WithOnComplete(() => throw new Exception("Test"))
- .RunWithoutBinding()
- .ToYieldInteraction();
- MotionDispatcher.RegisterUnhandledExceptionHandler(defaultHandler);
- }
- }
- }
|