Add test
parent
0579984355
commit
1b76f77608
|
@ -4,6 +4,7 @@ using Cysharp.Threading.Tasks;
|
||||||
using FluentAssertions;
|
using FluentAssertions;
|
||||||
using NUnit.Framework;
|
using NUnit.Framework;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
using UnityEngine.Networking;
|
||||||
using UnityEngine.TestTools;
|
using UnityEngine.TestTools;
|
||||||
|
|
||||||
#if CSHARP_7_OR_LATER || (UNITY_2018_3_OR_NEWER && (NET_STANDARD_2_0 || NET_4_6))
|
#if CSHARP_7_OR_LATER || (UNITY_2018_3_OR_NEWER && (NET_STANDARD_2_0 || NET_4_6))
|
||||||
|
@ -13,11 +14,20 @@ namespace Cysharp.Threading.TasksTests
|
||||||
{
|
{
|
||||||
public class AsyncOperationTest
|
public class AsyncOperationTest
|
||||||
{
|
{
|
||||||
|
[UnityTest]
|
||||||
|
public IEnumerator ResourcesLoad_Completed() => UniTask.ToCoroutine(async () =>
|
||||||
|
{
|
||||||
|
var asyncOperation = Resources.LoadAsync<Texture2D>("sample_texture");
|
||||||
|
await asyncOperation.ToUniTask();
|
||||||
|
asyncOperation.isDone.Should().BeTrue();
|
||||||
|
asyncOperation.asset.GetType().Should().Be(typeof(Texture2D));
|
||||||
|
});
|
||||||
|
|
||||||
[UnityTest]
|
[UnityTest]
|
||||||
public IEnumerator ResourcesLoad_CancelOnPlayerLoop() => UniTask.ToCoroutine(async () =>
|
public IEnumerator ResourcesLoad_CancelOnPlayerLoop() => UniTask.ToCoroutine(async () =>
|
||||||
{
|
{
|
||||||
var cts = new CancellationTokenSource();
|
var cts = new CancellationTokenSource();
|
||||||
var task = Resources.LoadAsync<Texture>("sample_texture").ToUniTask(cancellationToken: cts.Token, handleImmediately: false);
|
var task = Resources.LoadAsync<Texture>("sample_texture").ToUniTask(cancellationToken: cts.Token, cancelImmediately: false);
|
||||||
|
|
||||||
cts.Cancel();
|
cts.Cancel();
|
||||||
task.Status.Should().Be(UniTaskStatus.Pending);
|
task.Status.Should().Be(UniTaskStatus.Pending);
|
||||||
|
@ -31,12 +41,50 @@ namespace Cysharp.Threading.TasksTests
|
||||||
{
|
{
|
||||||
{
|
{
|
||||||
var cts = new CancellationTokenSource();
|
var cts = new CancellationTokenSource();
|
||||||
var task = Resources.LoadAsync<Texture>("sample_texture").ToUniTask(cancellationToken: cts.Token, handleImmediately: true);
|
var task = Resources.LoadAsync<Texture>("sample_texture").ToUniTask(cancellationToken: cts.Token, cancelImmediately: true);
|
||||||
|
|
||||||
cts.Cancel();
|
cts.Cancel();
|
||||||
task.Status.Should().Be(UniTaskStatus.Canceled);
|
task.Status.Should().Be(UniTaskStatus.Canceled);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if ENABLE_UNITYWEBREQUEST && (!UNITY_2019_1_OR_NEWER || UNITASK_WEBREQUEST_SUPPORT)
|
||||||
|
[UnityTest]
|
||||||
|
public IEnumerator UnityWebRequest_Completed() => UniTask.ToCoroutine(async () =>
|
||||||
|
{
|
||||||
|
var filePath = System.IO.Path.Combine(Application.dataPath, "Tests", "Resources", "sample_texture.png");
|
||||||
|
var asyncOperation = UnityWebRequest.Get($"file://{filePath}").SendWebRequest();
|
||||||
|
await asyncOperation.ToUniTask();
|
||||||
|
|
||||||
|
asyncOperation.isDone.Should().BeTrue();
|
||||||
|
asyncOperation.webRequest.result.Should().Be(UnityWebRequest.Result.Success);
|
||||||
|
});
|
||||||
|
|
||||||
|
[UnityTest]
|
||||||
|
public IEnumerator UnityWebRequest_CancelOnPlayerLoop() => UniTask.ToCoroutine(async () =>
|
||||||
|
{
|
||||||
|
var cts = new CancellationTokenSource();
|
||||||
|
var filePath = System.IO.Path.Combine(Application.dataPath, "Tests", "Resources", "sample_texture.png");
|
||||||
|
var task = UnityWebRequest.Get($"file://{filePath}").SendWebRequest().ToUniTask(cancellationToken: cts.Token);
|
||||||
|
|
||||||
|
cts.Cancel();
|
||||||
|
task.Status.Should().Be(UniTaskStatus.Pending);
|
||||||
|
|
||||||
|
await UniTask.NextFrame();
|
||||||
|
task.Status.Should().Be(UniTaskStatus.Canceled);
|
||||||
|
});
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void UnityWebRequest_CancelImmediately()
|
||||||
|
{
|
||||||
|
var cts = new CancellationTokenSource();
|
||||||
|
cts.Cancel();
|
||||||
|
var filePath = System.IO.Path.Combine(Application.dataPath, "Tests", "Resources", "sample_texture.png");
|
||||||
|
var task = UnityWebRequest.Get($"file://{filePath}").SendWebRequest().ToUniTask(cancellationToken: cts.Token, cancelImmediately: true);
|
||||||
|
|
||||||
|
task.Status.Should().Be(UniTaskStatus.Canceled);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Reference in New Issue