This commit is contained in:
2025-01-17 13:10:42 +01:00
commit 4536213c91
15115 changed files with 1442174 additions and 0 deletions

View File

@@ -0,0 +1,13 @@
using UnityEngine.UI;
namespace UnityEngine.UI.Tests
{
// Make a non-abstract Graphic.
public class ConcreteGraphic : Graphic
{
public override string ToString()
{
return string.Format("{0} '{1}'", GetType().Name, this.gameObject.name);
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 4d31e19fd539ac54c8d9151da43683e9
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,21 @@
using System;
using System.Runtime.Serialization;
namespace UnityEngine.UI.Tests.Utils
{
class ExceptionUtils
{
public static void PreserveStackTrace(Exception e)
{
var ctx = new StreamingContext(StreamingContextStates.CrossAppDomain);
var mgr = new ObjectManager(null, ctx);
var si = new SerializationInfo(e.GetType(), new FormatterConverter());
e.GetObjectData(si, ctx);
mgr.RegisterObject(e, 1, si); // prepare for SetObjectData
mgr.DoFixups(); // ObjectManager calls SetObjectData
// voila, e is unmodified save for _remoteStackTraceString
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 0b2706df6fdff50448f84a3f6629b40f
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,73 @@
using System.Collections.Generic;
using NUnit.Framework;
namespace UnityEngine.UI.Tests
{
static class GraphicTestHelper
{
public static void TestOnPopulateMeshDefaultBehavior(Graphic graphic, VertexHelper vh)
{
Assert.AreEqual(4, vh.currentVertCount);
List<UIVertex> verts = new List<UIVertex>();
vh.GetUIVertexStream(verts);
// The vertices for the 2 triangles of the canvas
UIVertex[] expectedVertices =
{
new UIVertex
{
color = graphic.color,
position = new Vector3(graphic.rectTransform.rect.x, graphic.rectTransform.rect.y),
uv0 = new Vector2(0f, 0f),
normal = new Vector3(0, 0, -1),
tangent = new Vector4(1, 0, 0, -1)
},
new UIVertex
{
color = graphic.color,
position = new Vector3(graphic.rectTransform.rect.x, graphic.rectTransform.rect.y + graphic.rectTransform.rect.height),
uv0 = new Vector2(0f, 1f),
normal = new Vector3(0, 0, -1),
tangent = new Vector4(1, 0, 0, -1)
},
new UIVertex
{
color = graphic.color,
position = new Vector3(graphic.rectTransform.rect.x + graphic.rectTransform.rect.width, graphic.rectTransform.rect.y + graphic.rectTransform.rect.height),
uv0 = new Vector2(1f, 1f),
normal = new Vector3(0, 0, -1),
tangent = new Vector4(1, 0, 0, -1)
},
new UIVertex
{
color = graphic.color,
position = new Vector3(graphic.rectTransform.rect.x + graphic.rectTransform.rect.width, graphic.rectTransform.rect.y + graphic.rectTransform.rect.height),
uv0 = new Vector2(1f, 1f),
normal = new Vector3(0, 0, -1),
tangent = new Vector4(1, 0, 0, -1)
},
new UIVertex
{
color = graphic.color,
position = new Vector3(graphic.rectTransform.rect.x + graphic.rectTransform.rect.width, graphic.rectTransform.rect.y),
uv0 = new Vector2(1f, 0f),
normal = new Vector3(0, 0, -1),
tangent = new Vector4(1, 0, 0, -1)
},
new UIVertex
{
color = graphic.color,
position = new Vector3(graphic.rectTransform.rect.x, graphic.rectTransform.rect.y),
uv0 = new Vector2(0f, 0f),
normal = new Vector3(0, 0, -1),
tangent = new Vector4(1, 0, 0, -1)
},
};
for (int i = 0; i < verts.Count; i++)
{
Assert.AreEqual(expectedVertices[i], verts[i]);
}
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 621a76c0c3927aa45aba7d2ad0c8e1c8
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,50 @@
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using UnityEngine;
namespace UnityEngine.UI.Tests
{
// Hook into the graphic callback so we can do our check.
public class ImageHook : Image
{
public bool isGeometryUpdated;
public bool isLayoutRebuild;
public bool isMaterialRebuilt;
public Rect cachedClipRect;
public void ResetTest()
{
isGeometryUpdated = false;
isLayoutRebuild = false;
isMaterialRebuilt = false;
}
public override void SetLayoutDirty()
{
base.SetLayoutDirty();
isLayoutRebuild = true;
}
public override void SetMaterialDirty()
{
base.SetMaterialDirty();
isMaterialRebuilt = true;
}
protected override void UpdateGeometry()
{
base.UpdateGeometry();
isGeometryUpdated = true;
}
public override void SetClipRect(Rect clipRect, bool validRect)
{
cachedClipRect = clipRect;
if (validRect)
canvasRenderer.EnableRectClipping(clipRect);
else
canvasRenderer.DisableRectClipping();
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 5da88f348abcbd94585671a69c672781
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,55 @@
using System;
using System.Reflection;
namespace UnityEngine.UI.Tests
{
class PrivateFieldSetter<T> : IDisposable
{
private object m_Obj;
private FieldInfo m_FieldInfo;
private object m_OldValue;
public PrivateFieldSetter(object obj, string field, object value)
{
m_Obj = obj;
m_FieldInfo = typeof(T).GetField(field, BindingFlags.NonPublic | BindingFlags.Instance);
m_OldValue = m_FieldInfo.GetValue(obj);
m_FieldInfo.SetValue(obj, value);
}
public void Dispose()
{
m_FieldInfo.SetValue(m_Obj, m_OldValue);
}
}
static class PrivateStaticField
{
public static T GetValue<T>(Type staticType, string fieldName)
{
var type = staticType;
FieldInfo field = null;
while (field == null && type != null)
{
field = type.GetField(fieldName, BindingFlags.Static | BindingFlags.NonPublic);
type = type.BaseType;
}
return (T)field.GetValue(null);
}
}
static class PrivateField
{
public static T GetValue<T>(this object o, string fieldName)
{
var type = o.GetType();
FieldInfo field = null;
while (field == null && type != null)
{
field = type.GetField(fieldName, BindingFlags.Instance | BindingFlags.NonPublic);
type = type.BaseType;
}
return field != null ? (T)field.GetValue(o) : default(T);
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: ea944378afa086f438b3352ad7b0e835
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,149 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using NUnit.Framework;
using UnityEngine.EventSystems;
namespace UnityEngine.UI.Tests
{
public static class UIBehaviourExtensions
{
private static object InvokeMethodAndRethrow(Type type, Object obj, string methodName, params object[] args)
{
BindingFlags flags = BindingFlags.Default;
flags |= BindingFlags.Public;
flags |= BindingFlags.NonPublic;
if (obj != null)
flags |= BindingFlags.Instance;
else
flags |= BindingFlags.Static;
MethodInfo method;
try
{
// Attempt to get the method plainly at first
method = type.GetMethod(methodName, flags);
}
catch (AmbiguousMatchException)
{
// If it's ambiguous, then attempt to get it using its params (though nulls would mess things up).
method = type.GetMethod(methodName, flags, null, args.Select(a => a != null ? a.GetType() : null).Where(a => a != null).ToArray(), new ParameterModifier[0]);
}
Assert.NotNull(method, string.Format("Not method {0} found on object {1}", methodName, obj));
return method.Invoke(obj, args);
}
public static object InvokeMethodAndRethrow<T>(Object obj, string methodName, params object[] args)
{
return InvokeMethodAndRethrow(typeof(T), obj, methodName, args);
}
public static object InvokeMethodAndRethrow(Object obj, string methodName, params object[] args)
{
return InvokeMethodAndRethrow(obj.GetType(), obj, methodName, args);
}
public static void InvokeOnEnable(this UIBehaviour behaviour)
{
InvokeMethodAndRethrow(behaviour, "OnEnable");
}
public static void InvokeOnDisable(this UIBehaviour behaviour)
{
InvokeMethodAndRethrow(behaviour, "OnDisable");
}
public static void InvokeAwake(this UIBehaviour behaviour)
{
InvokeMethodAndRethrow(behaviour, "Awake");
}
public static void InvokeRebuild(this UIBehaviour behaviour, CanvasUpdate type)
{
InvokeMethodAndRethrow(behaviour, "Rebuild", type);
}
public static void InvokeLateUpdate(this UIBehaviour behaviour)
{
InvokeMethodAndRethrow(behaviour, "LateUpdate");
}
public static void InvokeUpdate(this UIBehaviour behaviour)
{
InvokeMethodAndRethrow(behaviour, "Update");
}
public static void InvokeOnRectTransformDimensionsChange(this UIBehaviour behaviour)
{
InvokeMethodAndRethrow(behaviour, "OnRectTransformDimensionsChange");
}
public static void InvokeOnCanvasGroupChanged(this UIBehaviour behaviour)
{
InvokeMethodAndRethrow(behaviour, "OnCanvasGroupChanged");
}
public static void InvokeOnDidApplyAnimationProperties(this UIBehaviour behaviour)
{
InvokeMethodAndRethrow(behaviour, "OnDidApplyAnimationProperties");
}
}
public static class SelectableExtensions
{
public static void InvokeOnPointerDown(this Selectable selectable, PointerEventData data)
{
UIBehaviourExtensions.InvokeMethodAndRethrow<Selectable>(selectable, "OnPointerDown", data);
}
public static void InvokeOnPointerUp(this Selectable selectable, PointerEventData data)
{
UIBehaviourExtensions.InvokeMethodAndRethrow<Selectable>(selectable, "OnPointerUp", data);
}
public static void InvokeOnPointerEnter(this Selectable selectable, PointerEventData data)
{
UIBehaviourExtensions.InvokeMethodAndRethrow<Selectable>(selectable, "OnPointerEnter", data);
}
public static void InvokeOnPointerExit(this Selectable selectable, PointerEventData data)
{
UIBehaviourExtensions.InvokeMethodAndRethrow<Selectable>(selectable, "OnPointerExit", data);
}
public static void InvokeTriggerAnimation(this Selectable selectable, string triggerName)
{
UIBehaviourExtensions.InvokeMethodAndRethrow<Selectable>(selectable, "TriggerAnimation", triggerName);
}
public static void InvokeOnSelect(this Selectable selectable, string triggerName)
{
UIBehaviourExtensions.InvokeMethodAndRethrow<Selectable>(selectable, "OnSelect", triggerName);
}
}
public static class GraphicExtension
{
public static void InvokeOnPopulateMesh(this Graphic graphic, VertexHelper vh)
{
UIBehaviourExtensions.InvokeMethodAndRethrow(graphic, "OnPopulateMesh", vh);
}
}
public static class GraphicRaycasterExtension
{
public static void InvokeRaycast(Canvas canvas, Camera eventCamera, Vector2 pointerPosition, List<Graphic> results)
{
UIBehaviourExtensions.InvokeMethodAndRethrow<GraphicRaycaster>(null, "Raycast", canvas, eventCamera, pointerPosition, results);
}
}
public static class ToggleGroupExtension
{
public static void InvokeValidateToggleIsInGroup(this ToggleGroup tgroup, Toggle toggle)
{
UIBehaviourExtensions.InvokeMethodAndRethrow<ToggleGroup>(tgroup, "ValidateToggleIsInGroup", toggle);
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: d548f82e7e9c0ed49ae5b4a5eecfb2f7
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: