test: add test for icon of components (#389)

This commit is contained in:
anatawa12 2023-08-13 17:15:25 +09:00 committed by GitHub
parent ef920646c6
commit 4ac4201af0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 70 additions and 0 deletions

View File

@ -0,0 +1,67 @@
using System;
using System.Collections.Generic;
using System.Linq;
using nadena.dev.modular_avatar.core;
using NUnit.Framework;
using UnityEditor;
using UnityEngine;
using Activator = nadena.dev.modular_avatar.core.Activator;
using Object = UnityEngine.Object;
namespace modular_avatar_tests
{
public class ComponentSettingsTest : TestBase
{
GameObject _gameObject;
Texture2D _iconTexture;
[SetUp]
public virtual void Setup()
{
base.Setup();
_gameObject = new GameObject();
_iconTexture = AssetDatabase.LoadAssetAtPath<Texture2D>(
AssetDatabase.GUIDToAssetPath("a8edd5bd1a0a64a40aa99cc09fb5f198"));
}
public override void Teardown()
{
base.Teardown();
Object.DestroyImmediate(_gameObject);
}
[Test]
[TestCaseSource(nameof(ComponentTypes))]
public void CheckDisallowMultipleComponentIsSpecified(Type type)
{
// excluded types
if (type == typeof(Activator)) return;
if (type == typeof(AvatarActivator)) return;
if (type == typeof(TestComponent)) return;
// get icon
var component = (MonoBehaviour)_gameObject.AddComponent(type);
var monoScript = MonoScript.FromMonoBehaviour(component);
var scriptPath = AssetDatabase.GetAssetPath(monoScript);
var monoImporter = (MonoImporter)AssetImporter.GetAtPath(scriptPath);
// in Unity 2021.2, we can use monoImporter.GetIcon()
// but it's not available in unity 2019 so use SerializedObject
var serializedImporter = new SerializedObject(monoImporter);
var iconProperty = serializedImporter.FindProperty("icon");
var icon = iconProperty.objectReferenceValue;
// check the icon
Assert.That(icon, Is.EqualTo(_iconTexture));
}
/// <returns>All non-abstract MonoBehaviour classes</returns>
static IEnumerable<Type> ComponentTypes()
{
return
typeof(AvatarTagComponent).Assembly
.GetTypes()
.Where(x => x.IsClass && !x.IsAbstract)
.Where(x => typeof(MonoBehaviour).IsAssignableFrom(x));
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: f6c6070093d243a69c5798baf57a76e6
timeCreated: 1691910617