Skip to content

Commit

Permalink
[SDK-381] add test for avatar loader window (#110)
Browse files Browse the repository at this point in the history
- add test to verify, that the prefab created is a directly linked to object in world without overrides or changes.
  • Loading branch information
rk132 authored Sep 3, 2023
2 parents a79d78a + 086ffa3 commit 9468d81
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 4 deletions.
6 changes: 3 additions & 3 deletions Editor/Utils/EditorUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ public static bool IsUrlShortcodeValid(string urlString)
return !string.IsNullOrEmpty(urlString) &&
(Regex.Match(urlString, SHORT_CODE_REGEX).Length > 0 || Uri.IsWellFormedUriString(urlString, UriKind.Absolute) && urlString.EndsWith(".glb"));
}
public static GameObject CreateAvatarPrefab(AvatarMetadata avatarMetadata, string path)

public static GameObject CreateAvatarPrefab(AvatarMetadata avatarMetadata, string path, string prefabPath = null)
{
var modelFilePath = $"{path}.glb";
AssetDatabase.Refresh();
Expand All @@ -53,7 +53,7 @@ public static GameObject CreateAvatarPrefab(AvatarMetadata avatarMetadata, strin
var avatarData = newAvatar.AddComponent<AvatarData>();
avatarData.AvatarMetadata = avatarMetadata;
avatarData.AvatarId = newAvatar.name;
CreatePrefab(newAvatar, $"{path}.prefab");
CreatePrefab(newAvatar, prefabPath ?? $"{path}.prefab");
return newAvatar;
}
}
Expand Down
52 changes: 52 additions & 0 deletions Tests/Editor/AvatarLoaderWindowTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System.Collections;
using System.Reflection;
using JetBrains.Annotations;
using NUnit.Framework;
using ReadyPlayerMe.Core.Editor;
using UnityEditor;
using UnityEngine;
using UnityEngine.TestTools;
using Assert = UnityEngine.Assertions.Assert;

namespace ReadyPlayerMe.Core.Tests
{
public class AvatarLoaderWindowTests
{
private GameObject avatar;

[TearDown]
public void Cleanup()
{
TestUtils.DeleteAvatarDirectoryIfExists(TestAvatarData.DefaultAvatarUri.Guid, true);
TestUtils.DeleteCachedAvatar(TestAvatarData.DefaultAvatarUri.Guid);
if (avatar != null)
{
Object.DestroyImmediate(avatar);
}
}

[UnityTest]
public IEnumerator Avatar_Loaded_Stored_And_No_Overrides()
{
AvatarLoaderEditor window = EditorWindow.GetWindow<AvatarLoaderEditor>();

MethodInfo loadAvatarMethod = typeof(AvatarLoaderEditor).GetMethod("LoadAvatar", BindingFlags.NonPublic | BindingFlags.Instance);

Assert.IsNotNull(loadAvatarMethod);
loadAvatarMethod.Invoke(window, new object[] { TestAvatarData.DefaultAvatarUri.ModelUrl });

var time = System.DateTime.Now;

do
{
yield return null;
avatar = GameObject.Find(TestAvatarData.DefaultAvatarUri.Guid);
} while (avatar == null && System.DateTime.Now.Subtract(time).Seconds < 5);

window.Close();
Assert.IsNotNull(avatar);
var overrides = PrefabUtility.HasPrefabInstanceAnyOverrides(avatar.gameObject, false);
Assert.IsFalse(overrides);
}
}
}
3 changes: 3 additions & 0 deletions Tests/Editor/AvatarLoaderWindowTests.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions Tests/Editor/Common/TestUtils.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System.IO;
using UnityEditor;
using UnityEngine;
using UnityEngine.Assertions;

namespace ReadyPlayerMe.Core.Tests
{
Expand Down Expand Up @@ -28,5 +30,12 @@ public static void DeleteAvatarDirectoryIfExists(string avatarGuid, bool recursi
Directory.Delete(path, recursive);
}
}

public static void DeleteCachedAvatar(string avatarGuid)
{
var deleteAsset = AssetDatabase.DeleteAsset($"Assets/Ready Player Me/Avatars/{avatarGuid}");
Assert.IsTrue(deleteAsset);
AssetDatabase.Refresh();
}
}
}
3 changes: 2 additions & 1 deletion Tests/Editor/ReadyPlayerMe.Core.Editor.Tests.asmdef
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"name": "ReadyPlayerMe.Core.Editor.Tests",
"rootNamespace": "",
"references": [
"ReadyPlayerMe.Core"
"ReadyPlayerMe.Core",
"ReadyPlayerMe.Core.Editor"
],
"includePlatforms": [
"Editor"
Expand Down

0 comments on commit 9468d81

Please sign in to comment.