Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SDK-381] add test for avatar loader window #110

Merged
merged 2 commits into from
Sep 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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