Skip to content

Commit

Permalink
[SDK-591] Replace ienumerator with async/await (#172)
Browse files Browse the repository at this point in the history
## [SDK-591](https://ready-player-me.atlassian.net/browse/SDK-591)

## Description

- Refactor tests by replacing ienumerator to async/await
  • Loading branch information
rYuuk authored Nov 30, 2023
1 parent b5a254d commit 9066624
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 52 deletions.
38 changes: 24 additions & 14 deletions Tests/Editor/AvatarAPITests.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using NUnit.Framework;
using UnityEngine;
using UnityEngine.TestTools;
using Object = UnityEngine.Object;

namespace ReadyPlayerMe.Core.Tests
Expand Down Expand Up @@ -108,8 +106,8 @@ private TextureChannel[] GetAllTextureChannels()
return textureChannels;
}

[UnityTest]
public IEnumerator AvatarLoader_Avatar_API_Mesh_LOD()
[Test]
public async Task AvatarLoader_Avatar_API_Mesh_LOD()
{
var avatarConfigs = new Queue<AvatarConfig>();
avatarConfigs.Enqueue(avatarConfigLow);
Expand All @@ -135,15 +133,18 @@ public IEnumerator AvatarLoader_Avatar_API_Mesh_LOD()
loader.AvatarConfig = avatarConfigs.Dequeue();
loader.LoadAvatar(AVATAR_API_AVATAR_URL);

yield return new WaitUntil(() => vertexCounts.Count == 3 || failureType != FailureType.None);
while (vertexCounts.Count != 3 && failureType == FailureType.None)
{
await Task.Yield();
}

Assert.AreEqual(FailureType.None, failureType);
Assert.Less(vertexCounts[0], vertexCounts[1]);
Assert.Less(vertexCounts[1], vertexCounts[2]);
}

[UnityTest]
public IEnumerator AvatarLoader_Avatar_API_TextureSize()
[Test]
public async Task AvatarLoader_Avatar_API_TextureSize()
{
var avatarConfigs = new Queue<AvatarConfig>();
avatarConfigs.Enqueue(avatarConfigLow);
Expand All @@ -169,16 +170,19 @@ public IEnumerator AvatarLoader_Avatar_API_TextureSize()
loader.AvatarConfig = avatarConfigs.Dequeue();
loader.LoadAvatar(AVATAR_API_AVATAR_URL);

yield return new WaitUntil(() => textureSizes.Count == 3 || failureType != FailureType.None);
while (textureSizes.Count != 3 && failureType == FailureType.None)
{
await Task.Yield();
}

Assert.AreEqual(FailureType.None, failureType);
Assert.AreEqual(TEXTURE_SIZE_LOW, textureSizes[0]);
Assert.AreEqual(TEXTURE_SIZE_MED, textureSizes[1]);
Assert.AreEqual(TEXTURE_SIZE_HIGH, textureSizes[2]);
}

[UnityTest]
public IEnumerator AvatarLoader_Avatar_API_MorphTargets_None()
[Test]
public async Task AvatarLoader_Avatar_API_MorphTargets_None()
{
GameObject avatar = null;
var failureType = FailureType.None;
Expand All @@ -192,7 +196,10 @@ public IEnumerator AvatarLoader_Avatar_API_MorphTargets_None()
loader.AvatarConfig = avatarConfigLow;
loader.LoadAvatar(AVATAR_API_AVATAR_URL);

yield return new WaitUntil(() => avatar != null || failureType != FailureType.None);
while (avatar == null && failureType == FailureType.None)
{
await Task.Yield();
}

var blendShapeCount = avatar.GetComponentInChildren<SkinnedMeshRenderer>().sharedMesh.blendShapeCount;
Debug.Log(blendShapeCount);
Expand All @@ -202,8 +209,8 @@ public IEnumerator AvatarLoader_Avatar_API_MorphTargets_None()
Assert.Zero(blendShapeCount);
}

[UnityTest]
public IEnumerator AvatarLoader_Avatar_API_MorphTargets_Oculus()
[Test]
public async Task AvatarLoader_Avatar_API_MorphTargets_Oculus()
{
GameObject avatar = null;
var failureType = FailureType.None;
Expand All @@ -217,7 +224,10 @@ public IEnumerator AvatarLoader_Avatar_API_MorphTargets_Oculus()
loader.AvatarConfig = avatarConfigMed;
loader.LoadAvatar(AVATAR_API_AVATAR_URL);

yield return new WaitUntil(() => avatar != null || failureType != FailureType.None);
while (avatar == null && failureType == FailureType.None)
{
await Task.Yield();
}

var blendShapeCount = avatar.GetComponentInChildren<SkinnedMeshRenderer>().sharedMesh.blendShapeCount;

Expand Down
55 changes: 36 additions & 19 deletions Tests/Editor/AvatarLoaderTests.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
using System;
using System.Collections;
using System.Linq;
using System.Threading.Tasks;
using NUnit.Framework;
using UnityEngine;
using UnityEngine.TestTools;
using Object = UnityEngine.Object;

namespace ReadyPlayerMe.Core.Tests
Expand All @@ -25,8 +23,8 @@ public void Cleanup()
}
}

[UnityTest]
public IEnumerator AvatarLoader_Complete_Load()
[Test]
public async Task AvatarLoader_Complete_Load()
{
var avatarUrl = string.Empty;
var failureType = FailureType.None;
Expand All @@ -39,8 +37,11 @@ public IEnumerator AvatarLoader_Complete_Load()
};
loader.OnFailed += (sender, args) => { failureType = args.Type; };
loader.LoadAvatar(TestAvatarData.DefaultAvatarUri.ModelUrl);

yield return new WaitUntil(() => avatar != null || failureType != FailureType.None);

while (avatar == null && failureType == FailureType.None)
{
await Task.Yield();
}

Assert.AreEqual(TestAvatarData.DefaultAvatarUri.ModelUrl, avatarUrl);
Assert.AreEqual(FailureType.None, failureType);
Expand Down Expand Up @@ -73,8 +74,8 @@ public async Task AvatarLoader_Complete_Load_Async()
}
}

[UnityTest]
public IEnumerator AvatarLoader_Fail_Load()
[Test]
public async Task AvatarLoader_Fail_Load()
{
var failureType = FailureType.None;
var avatarUrl = string.Empty;
Expand All @@ -87,14 +88,17 @@ public IEnumerator AvatarLoader_Fail_Load()
};
loader.LoadAvatar(TestAvatarData.WrongUri.ModelUrl);

yield return new WaitUntil(() => failureType != FailureType.None);
while (failureType == FailureType.None)
{
await Task.Yield();
}

Assert.AreEqual(TestAvatarData.WrongUri.ModelUrl, avatarUrl);
Assert.AreNotEqual(FailureType.None, failureType);
}

[UnityTest]
public IEnumerator AvatarLoader_Clears_Persistent_Cache()
[Test]
public async Task AvatarLoader_Clears_Persistent_Cache()
{
AvatarLoaderSettings settings = AvatarLoaderSettings.LoadSettings();
settings.AvatarCachingEnabled = true;
Expand All @@ -106,7 +110,10 @@ public IEnumerator AvatarLoader_Clears_Persistent_Cache()
loader.OnFailed += (_, args) => failureType = args.Type;
loader.LoadAvatar(TestAvatarData.DefaultAvatarUri.ModelUrl);

yield return new WaitUntil(() => avatar != null || failureType != FailureType.None);
while (avatar == null && failureType == FailureType.None)
{
await Task.Yield();
}

Assert.AreEqual(FailureType.None, failureType);
Assert.AreEqual(false, AvatarCache.IsCacheEmpty());
Expand All @@ -117,8 +124,8 @@ public IEnumerator AvatarLoader_Clears_Persistent_Cache()
settings.AvatarCachingEnabled = false;
}

[UnityTest]
public IEnumerator AvatarLoader_Cancel_Loading()
[Test]
public async Task AvatarLoader_Cancel_Loading()
{
var failureType = FailureType.None;
var loader = new AvatarObjectLoader();
Expand All @@ -141,15 +148,15 @@ public IEnumerator AvatarLoader_Cancel_Loading()
}

frameCount++;
yield return null;
await Task.Yield();
}

Assert.AreNotEqual(FailureType.None, failureType);
Assert.AreEqual(null, avatar);
}

[UnityTest]
public IEnumerator AvatarLoader_Low_LOD_Smaller_than_High_LOD()
[Test]
public async Task AvatarLoader_Low_LOD_Smaller_than_High_LOD()
{
var failureType = FailureType.None;

Expand All @@ -166,7 +173,12 @@ public IEnumerator AvatarLoader_Low_LOD_Smaller_than_High_LOD()
loader.AvatarConfig = avatarConfig;
loader.OnFailed += (sender, args) => { failureType = args.Type; };
loader.LoadAvatar(TestAvatarData.DefaultAvatarUri.ModelUrl);
yield return new WaitUntil(() => avatar != null || failureType != FailureType.None);

while (avatar == null && failureType == FailureType.None)
{
await Task.Yield();
}

var thisRenderer = avatar.GetComponentsInChildren<SkinnedMeshRenderer>();
var lowLODVertices = thisRenderer.Aggregate(0, (totalVertices, renderer) => totalVertices + renderer.sharedMesh.vertexCount);

Expand All @@ -182,7 +194,12 @@ public IEnumerator AvatarLoader_Low_LOD_Smaller_than_High_LOD()
};
loader.OnFailed += (sender, args) => { failureType = args.Type; };
loader.LoadAvatar(TestAvatarData.DefaultAvatarUri.ModelUrl);
yield return new WaitUntil(() => avatar != null || failureType != FailureType.None);

while (avatar == null && failureType == FailureType.None)
{
await Task.Yield();
}

thisRenderer = avatar.GetComponentsInChildren<SkinnedMeshRenderer>();
var highLODVertices = thisRenderer.Aggregate(0, (totalVertices, renderer) => totalVertices + renderer.sharedMesh.vertexCount);

Expand Down
48 changes: 29 additions & 19 deletions Tests/Editor/AvatarRenderLoaderTests.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
using System.Collections;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Threading.Tasks;
using NUnit.Framework;
using UnityEngine;
using UnityEngine.TestTools;

namespace ReadyPlayerMe.Core.Tests
{
Expand All @@ -13,8 +12,8 @@ public class AvatarRenderLoaderTests
private const float BLENDSHAPE_VALUE = 0.5f;
private const string RENDER_STRING = "?pose=relaxed&blendShapes[mouthSmile]=0.5&camera=portrait&size=800&background=255,255,255";

[UnityTest]
public IEnumerator RenderLoader_Load()
[Test]
public async Task RenderLoader_Load()
{
Texture2D renderTexture = null;
var failureType = FailureType.None;
Expand All @@ -28,14 +27,17 @@ public IEnumerator RenderLoader_Load()
Camera = RENDER_SCENE
});

yield return new WaitUntil(() => renderTexture != null || failureType != FailureType.None);
while (renderTexture == null && failureType == FailureType.None)
{
await Task.Yield();
}

Assert.AreEqual(FailureType.None, failureType);
Assert.IsNotNull(renderTexture);
}

[UnityTest]
public IEnumerator Fail_RenderLoader_Load_Wrong_Url()
[Test]
public async Task Fail_RenderLoader_Load_Wrong_Url()
{
Texture2D renderTexture = null;
var failureType = FailureType.None;
Expand All @@ -49,14 +51,17 @@ public IEnumerator Fail_RenderLoader_Load_Wrong_Url()
Camera = RENDER_SCENE
});

yield return new WaitUntil(() => renderTexture != null || failureType != FailureType.None);
while (renderTexture == null && failureType == FailureType.None)
{
await Task.Yield();
}

Assert.AreEqual(FailureType.MetadataDownloadError, failureType);
Assert.IsNull(renderTexture);
}

[UnityTest]
public IEnumerator RenderLoader_Load_With_Correct_BlendShape_Parameters()
[Test]
public async Task RenderLoader_Load_With_Correct_BlendShape_Parameters()
{
Texture2D renderTexture = null;
var failureType = FailureType.None;
Expand All @@ -75,14 +80,16 @@ public IEnumerator RenderLoader_Load_With_Correct_BlendShape_Parameters()
}
);

yield return new WaitUntil(() => renderTexture != null || failureType != FailureType.None);

while (renderTexture == null && failureType == FailureType.None)
{
await Task.Yield();
}
Assert.AreEqual(FailureType.None, failureType);
Assert.IsNotNull(renderTexture);
}

[UnityTest]
public IEnumerator RenderLoader_Load_Incorrect_BlendShape_Shape_Parameter()
[Test]
public async Task RenderLoader_Load_Incorrect_BlendShape_Shape_Parameter()
{
Texture2D renderTexture = null;
var failureType = FailureType.None;
Expand All @@ -101,11 +108,14 @@ public IEnumerator RenderLoader_Load_Incorrect_BlendShape_Shape_Parameter()
}
);

yield return new WaitUntil(() => renderTexture != null || failureType != FailureType.None);
while (renderTexture == null && failureType == FailureType.None)
{
await Task.Yield();
}

Assert.AreEqual(FailureType.None, failureType);
Assert.IsNotNull(renderTexture);

}

// create a render string test
Expand All @@ -114,8 +124,8 @@ public void RenderLoader_CreateRenderString()
{
var renderSettings = new AvatarRenderSettings
{
BlendShapes = new List<BlendShape>
{ new BlendShape(RENDER_BLENDSHAPE, BLENDSHAPE_VALUE) }
BlendShapes = new List<BlendShape>
{ new BlendShape(RENDER_BLENDSHAPE, BLENDSHAPE_VALUE) }
};

var renderString = renderSettings.GetParametersAsString();
Expand Down

0 comments on commit 9066624

Please sign in to comment.