InterceptedEventsPreviewTests.cs
2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
using System;
using System.Reflection;
using System.Collections.Generic;
using UnityEngine.EventSystems;
using UnityEditor;
using NUnit.Framework;
public class InterceptedEventsPreviewTests
{
[Test]
public void InterceptedEventsPreviewCacheUsingTypeCacheReturnsSameTypes()
{
var typeCacheEventInterfaces = new List<Type>();
TypeCache.TypeCollection types = TypeCache.GetTypesDerivedFrom<IEventSystemHandler>();
foreach (var type in types)
{
if (!type.IsInterface)
continue;
typeCacheEventInterfaces.Add(type);
}
var appDomainEventInterfaces = new List<Type>();
foreach (var type in GetAccessibleTypesInLoadedAssemblies())
{
if (!type.IsInterface)
continue;
appDomainEventInterfaces.Add(type);
}
Assert.AreNotEqual(typeCacheEventInterfaces.Count, appDomainEventInterfaces.Count, "Did not find the same number of EventInterface types");
for (int i = 0; i < typeCacheEventInterfaces.Count; ++i)
{
Assert.Contains(typeCacheEventInterfaces[i], appDomainEventInterfaces);
}
}
private static IEnumerable<Type> GetAccessibleTypesInLoadedAssemblies()
{
var assemblies = AppDomain.CurrentDomain.GetAssemblies();
for (var i = 0; i < assemblies.Length; ++i)
{
Type[] types;
var assembly = assemblies[i];
if (assembly == null)
continue;
try
{
types = assembly.GetTypes();
}
catch (ReflectionTypeLoadException e)
{
// assembly.GetTypes() might fail in case the Assembly cannot resolve all its references,
// or in case it was built targetting a newer version of .NET.
// In case the resolution fails for some types, we can still access the ones that have been
// properly loaded.
types = e.Types;
}
for (var j = 0; j < types.Length; ++j)
{
var type = types[j];
if (type == null)
continue;
yield return type;
}
}
}
}