diff --git a/Tests/Editor/Grabbing.meta b/Tests/Editor/Grabbing.meta new file mode 100644 index 0000000..10952c7 --- /dev/null +++ b/Tests/Editor/Grabbing.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 8294de65677130e4aa29ff95e16f9fc6 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Tests/Editor/Grabbing/GrabbingTest.cs b/Tests/Editor/Grabbing/GrabbingTest.cs new file mode 100644 index 0000000..45d0d86 --- /dev/null +++ b/Tests/Editor/Grabbing/GrabbingTest.cs @@ -0,0 +1,222 @@ +using System; +using UnityEngine; +using UnityEngine.TestTools; + +#if pHUMANOID4 +using NUnit.Framework; +using Passer; + +namespace Passer { + using Pawn; + using Humanoid; + + public class GrabbingTest : IPrebuildSetup { + + public void Setup() { + UnityEditor.SceneManagement.EditorSceneManager.OpenScene("[Test]GrabbingHumanoid.unity"); + } + + [Test] + public void Rigidbody() { + // Can be grabbed on any place on the mesh. + try { + HumanoidControl humanoid = UnityEngine.Object.FindObjectOfType(); + + GameObject obj = GameObject.Find("CubeRigidbody"); + Assert.IsFalse(obj == null); + + #region Grab + GameObject grabbedObject = Grab(humanoid, obj); + Assert.AreEqual(grabbedObject, obj, "Object is not grabbed"); + #endregion + + #region LetGo + grabbedObject = LetGo(humanoid); + Assert.IsTrue(grabbedObject == null, "Object is not released"); + #endregion + } + catch (Exception e) { + Debug.LogError("Test failed"); + throw (e); + } + } + + [Test] + public void RigidbodyHandle() { + // Can be grabbed on the handle. + // Snaps back into the socket when let go. + try { + Setup(); + + HumanoidControl humanoid = UnityEngine.Object.FindObjectOfType(); + + GameObject obj = GameObject.Find("CubeWithHandle"); + Assert.IsFalse(obj == null); + + #region Grab + + GameObject grabbedObject = Grab(humanoid, obj); + Assert.AreEqual(grabbedObject, obj); + + #endregion + + #region LetGo + grabbedObject = LetGo(humanoid); + Assert.IsTrue(grabbedObject == null); + #endregion + } + catch (Exception e) { + Debug.LogError("Test failed"); + throw (e); + } + } + + [Test] + public void KinematicRigidbodyWithoutPhysics() { + // the controller holds the Rigidbody. + try { + HumanoidControl humanoid = UnityEngine.Object.FindObjectOfType(); + humanoid.leftHandTarget.physics = false; + + GameObject obj = GameObject.Find("KinematicCube"); + Assert.IsFalse(obj == null); + + #region Grab + GameObject grabbedObject = Grab(humanoid, obj); + Assert.AreEqual(grabbedObject, obj); + #endregion + + #region LetGo + grabbedObject = LetGo(humanoid); + Assert.IsTrue(grabbedObject == null); + #endregion + } + catch (Exception e) { + Debug.LogError("Test failed"); + throw (e); + } + } + + [Test] + public void KinematicRigidbodyWithPhysics() { + // the Rigidbody will move the controller. + try { + HumanoidControl humanoid = UnityEngine.Object.FindObjectOfType(); + humanoid.leftHandTarget.physics = true; + + GameObject obj = GameObject.Find("KinematicCube"); + Assert.IsFalse(obj == null); + + #region Grab + GameObject grabbedObject = Grab(humanoid, obj); + Assert.AreEqual(grabbedObject, obj); + #endregion + + #region LetGo + grabbedObject = LetGo(humanoid); + Assert.IsTrue(grabbedObject == null); + #endregion + } + catch (Exception e) { + Debug.LogError("Test failed"); + throw (e); + } + } + + [Test] + public void KinematicRigidbodyLimitations() { + // the Rigidbody will move the controller. + try { + HumanoidControl humanoid = UnityEngine.Object.FindObjectOfType(); + + GameObject obj = GameObject.Find("KinematicCubeLimitations"); + Assert.IsFalse(obj == null); + + #region Grab + GameObject grabbedObject = Grab(humanoid, obj); + Assert.AreEqual(grabbedObject, obj); + #endregion + + #region LetGo + grabbedObject = LetGo(humanoid); + Assert.IsTrue(grabbedObject == null); + #endregion + } + catch (Exception e) { + Debug.LogError("Test failed"); + throw (e); + } + } + + [Test] + public void StaticObject() { + // Static object can not be grabbed + try { + HumanoidControl humanoid = UnityEngine.Object.FindObjectOfType(); + + GameObject obj = GameObject.Find("StaticCube"); + Assert.IsFalse(obj == null); + + #region Grab + GameObject grabbedObject = Grab(humanoid, obj); + Assert.IsTrue(grabbedObject == null); + #endregion + + #region LetGo + grabbedObject = LetGo(humanoid); + Assert.IsTrue(grabbedObject == null); + #endregion + } + catch (Exception e) { + Debug.LogError("Test failed"); + throw (e); + } + } + + [Test] + public void StaticObjectHandle() { + // Can be grabbed but cannot move. + // When Body Pull is enabled, the position of the pawn will move. + try { + HumanoidControl humanoid = UnityEngine.Object.FindObjectOfType(); + + GameObject obj = GameObject.Find("StaticCubeWithHandle"); + Assert.IsFalse(obj == null); + + Handle handle = obj.GetComponentInChildren(); + Assert.IsFalse(handle == null); + + #region Grab + GameObject grabbedObject = Grab(humanoid, obj); + Assert.AreEqual(grabbedObject, handle.gameObject); + #endregion + + #region LetGo + grabbedObject = LetGo(humanoid); + Assert.IsTrue(grabbedObject == null); + #endregion + } + catch (Exception e) { + Debug.LogError("Test failed"); + throw (e); + } + } + + #region Utilities + + protected GameObject Grab(HumanoidControl humanoid, GameObject obj) { + humanoid.leftHandTarget.Grab(obj); + return humanoid.leftHandTarget.grabbedObject; + } + + protected GameObject LetGo(HumanoidControl humanoid) { + humanoid.leftHandTarget.LetGo(); + return humanoid.leftHandTarget.grabbedObject; + } + + #endregion + } + +} + +#endif \ No newline at end of file diff --git a/Tests/Editor/Grabbing/GrabbingTest.cs.meta b/Tests/Editor/Grabbing/GrabbingTest.cs.meta new file mode 100644 index 0000000..52ce2e0 --- /dev/null +++ b/Tests/Editor/Grabbing/GrabbingTest.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: a216cd04d4ed1164ca1e93007ad1bec5 +timeCreated: 1558946624 +licenseType: Free +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Tests/Editor/Grabbing/GrabbingTestHumanoid.cs b/Tests/Editor/Grabbing/GrabbingTestHumanoid.cs new file mode 100644 index 0000000..1f9f71a --- /dev/null +++ b/Tests/Editor/Grabbing/GrabbingTestHumanoid.cs @@ -0,0 +1,339 @@ +/* +using NUnit.Framework; +using System; +using UnityEngine; + +namespace Passer { + using Pawn; + using Humanoid; + + public class GrabbingTestHumanoid { + + public static string testScene = "GrabbingHumanoid"; + + protected void Setup() { + UnityEditor.SceneManagement.EditorSceneManager.OpenScene( + "Assets/_TestCases/Grabbing/[Test]" + testScene + ".unity"); + } + + [Test] + public void Rigidbody() { + Setup(); + + // Can be grabbed on any place on the mesh. + try { + HumanoidControl humanoid = UnityEngine.Object.FindObjectOfType(); + PawnControl pawn = null; + if (humanoid == null) { + pawn = UnityEngine.Object.FindObjectOfType(); + Assert.IsFalse(pawn == null); + } + + GameObject obj = GameObject.Find("CubeRigidbody"); + Assert.IsFalse(obj == null); + + #region Grab + + GameObject grabbedObject = Grab(humanoid, pawn, obj); + + Assert.AreEqual(grabbedObject, obj); + + Collider[] colliders = grabbedObject.GetComponentsInChildren(); + foreach (Collider collider in colliders) + Assert.IsFalse(collider.isTrigger); + + Assert.AreEqual(grabbedObject.transform.parent, humanoid.leftHandTarget.handRigidbody.transform); + + #endregion + + #region LetGo + grabbedObject = LetGo(humanoid, pawn); + Assert.IsTrue(grabbedObject == null); + #endregion + } + catch (Exception e) { + Debug.LogError("Test failed"); + throw (e); + } + } + + [Test] + public void RigidbodyHandle() { + Setup(); + + // Can be grabbed on the handle. + // Snaps back into the socket when let go. + try { + HumanoidControl humanoid = UnityEngine.Object.FindObjectOfType(); + PawnControl pawn = null; + if (humanoid == null) { + pawn = UnityEngine.Object.FindObjectOfType(); + Assert.IsFalse(pawn == null); + } + + GameObject obj = GameObject.Find("CubeWithHandle"); + Assert.IsFalse(obj == null); + + #region Grab + + GameObject grabbedObject = Grab(humanoid, pawn, obj); + + Assert.AreEqual(grabbedObject, obj); + + Collider[] colliders = grabbedObject.GetComponentsInChildren(); + foreach (Collider collider in colliders) + Assert.IsFalse(collider.isTrigger); + + Assert.AreEqual(grabbedObject.transform.parent, humanoid.leftHandTarget.grabSocket.transform); + + #endregion + + #region LetGo + grabbedObject = LetGo(humanoid, pawn); + Assert.IsTrue(grabbedObject == null); + #endregion + } + catch (Exception e) { + Debug.LogError("Test failed"); + throw (e); + } + } + + [Test] + public void RigidbodyJoint() { + Setup(); + + // Can be grabbed on the handle. + // Snaps back into the socket when let go. + try { + HumanoidControl humanoid = UnityEngine.Object.FindObjectOfType(); + PawnControl pawn = null; + if (humanoid == null) { + pawn = UnityEngine.Object.FindObjectOfType(); + Assert.IsFalse(pawn == null); + } + + GameObject obj = GameObject.Find("CubeWithJoint"); + Assert.IsFalse(obj == null); + + #region Grab + + GameObject grabbedObject = Grab(humanoid, pawn, obj); + + Assert.AreEqual(grabbedObject, obj); + + Collider[] colliders = grabbedObject.GetComponentsInChildren(); + foreach (Collider collider in colliders) + Assert.IsFalse(collider.isTrigger); + + Assert.AreEqual(grabbedObject.transform.parent, humanoid.leftHandTarget.grabSocket.transform); + + #endregion + + #region LetGo + grabbedObject = LetGo(humanoid, pawn); + Assert.IsTrue(grabbedObject == null); + #endregion + } + catch (Exception e) { + Debug.LogError("Test failed"); + throw (e); + } + } + + + [Test] + public void KinematicRigidbodyWithoutPhysics() { + Setup(); + + // the controller holds the Rigidbody. + try { + HumanoidControl humanoid = UnityEngine.Object.FindObjectOfType(); + PawnControl pawn = null; + if (humanoid == null) { + pawn = UnityEngine.Object.FindObjectOfType(); + Assert.IsFalse(pawn == null); + pawn.leftHandTarget.physics = false; + } + else { + humanoid.leftHandTarget.physics = false; + } + + GameObject obj = GameObject.Find("KinematicCube"); + Assert.IsFalse(obj == null); + + #region Grab + GameObject grabbedObject = Grab(humanoid, pawn, obj); + Assert.AreEqual(grabbedObject, obj); + #endregion + + #region LetGo + grabbedObject = LetGo(humanoid, pawn); + Assert.IsTrue(grabbedObject == null); + #endregion + } + catch (Exception e) { + Debug.LogError("Test failed"); + throw (e); + } + } + + [Test] + public void KinematicRigidbodyWithPhysics() { + Setup(); + + // the Rigidbody will move the controller. + try { + HumanoidControl humanoid = UnityEngine.Object.FindObjectOfType(); + PawnControl pawn = null; + if (humanoid == null) { + pawn = UnityEngine.Object.FindObjectOfType(); + Assert.IsFalse(pawn == null); + pawn.leftHandTarget.physics = true; + } + else { + humanoid.leftHandTarget.physics = true; + } + + GameObject obj = GameObject.Find("KinematicCube"); + Assert.IsFalse(obj == null); + + #region Grab + GameObject grabbedObject = Grab(humanoid, pawn, obj); + Assert.AreEqual(grabbedObject, obj); + #endregion + + #region LetGo + grabbedObject = LetGo(humanoid, pawn); + Assert.IsTrue(grabbedObject == null); + #endregion + } + catch (Exception e) { + Debug.LogError("Test failed"); + throw (e); + } + } + + [Test] + public void KinematicRigidbodyLimitations() { + Setup(); + + // the Rigidbody will move the controller. + try { + HumanoidControl humanoid = UnityEngine.Object.FindObjectOfType(); + PawnControl pawn = null; + if (humanoid == null) { + pawn = UnityEngine.Object.FindObjectOfType(); + Assert.IsFalse(pawn == null); + } + + GameObject obj = GameObject.Find("KinematicCubeLimitations"); + Assert.IsFalse(obj == null); + + #region Grab + GameObject grabbedObject = Grab(humanoid, pawn, obj); + Assert.AreEqual(grabbedObject, obj); + #endregion + + #region LetGo + grabbedObject = LetGo(humanoid, pawn); + Assert.IsTrue(grabbedObject == null); + #endregion + } + catch (Exception e) { + Debug.LogError("Test failed"); + throw (e); + } + } + + [Test] + public void StaticObject() { + Setup(); + + // Static object can not be grabbed + try { + HumanoidControl humanoid = UnityEngine.Object.FindObjectOfType(); + PawnControl pawn = null; + if (humanoid == null) { + pawn = UnityEngine.Object.FindObjectOfType(); + Assert.IsFalse(pawn == null); + } + + GameObject obj = GameObject.Find("StaticCube"); + Assert.IsFalse(obj == null); + + #region Grab + + GameObject grabbedObject = Grab(humanoid, pawn, obj); + Assert.IsTrue(grabbedObject == null); + + #endregion + + #region LetGo + grabbedObject = LetGo(humanoid, pawn); + Assert.IsTrue(grabbedObject == null); + #endregion + } + catch (Exception e) { + Debug.LogError("Test failed"); + throw (e); + } + } + + [Test] + public void StaticObjectHandle() { + Setup(); + + // Can be grabbed but cannot move. + // When Body Pull is enabled, the position of the pawn will move. + try { + HumanoidControl humanoid = UnityEngine.Object.FindObjectOfType(); + PawnControl pawn = null; + if (humanoid == null) { + pawn = UnityEngine.Object.FindObjectOfType(); + Assert.IsFalse(pawn == null); + } + + GameObject obj = GameObject.Find("StaticCubeWithHandle"); + Assert.IsFalse(obj == null); + + Handle handle = obj.GetComponentInChildren(); + Assert.IsFalse(handle == null); + + #region Grab + + GameObject grabbedObject = Grab(humanoid, pawn, obj); + Assert.AreEqual(grabbedObject, handle.gameObject); + + Assert.AreEqual(humanoid.leftHandTarget.hand.bone.transform.parent, handle.transform); + + #endregion + + #region LetGo + grabbedObject = LetGo(humanoid, pawn); + Assert.IsTrue(grabbedObject == null); + #endregion + } + catch (Exception e) { + Debug.LogError("Test failed"); + throw (e); + } + } + + #region Utilities + + protected GameObject Grab(HumanoidControl humanoid, PawnControl pawn, GameObject obj) { + humanoid.leftHandTarget.Grab(obj, false); + return humanoid.leftHandTarget.grabbedObject; + } + + protected GameObject LetGo(HumanoidControl humanoid, PawnControl pawn) { + humanoid.leftHandTarget.LetGo(); + return humanoid.leftHandTarget.grabbedObject; + } + + #endregion + } + +} +*/ diff --git a/Tests/Editor/Grabbing/GrabbingTestHumanoid.cs.meta b/Tests/Editor/Grabbing/GrabbingTestHumanoid.cs.meta new file mode 100644 index 0000000..a198d37 --- /dev/null +++ b/Tests/Editor/Grabbing/GrabbingTestHumanoid.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: c1216c052fa7b92489256144c8a3892b +timeCreated: 1558946624 +licenseType: Free +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Tests/Editor/Socket.meta b/Tests/Editor/Socket.meta new file mode 100644 index 0000000..bb75961 --- /dev/null +++ b/Tests/Editor/Socket.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 21e5ec4d7a6c9ed4fbcbe173682201b8 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Tests/Editor/Socket/SocketTest.cs b/Tests/Editor/Socket/SocketTest.cs new file mode 100644 index 0000000..c36cdf8 --- /dev/null +++ b/Tests/Editor/Socket/SocketTest.cs @@ -0,0 +1,501 @@ +using System; +using UnityEngine; +using UnityEngine.TestTools; + +#if pHUMANOID4 +using NUnit.Framework; +using Passer; +using Passer.Humanoid; +public class SocketTest : IPrebuildSetup { + + public static string testScene = "Sockets"; + + public void Setup() { + Debug.Log("Setup SocketTest"); + UnityEditor.SceneManagement.EditorSceneManager.OpenScene( + "Assets/_TestCases/[Test]" + testScene + ".unity"); + } + +#region Static Socket + [Test] + [Category("Socket")] + public void StaticSocket_RigidbodyHandle() { + // Rigidbody handles will get a fixed joint without attachedRigidbody + try { + + GameObject socketObj = GameObject.Find("StaticSocket"); + Socket socket = socketObj.GetComponent(); + Assert.IsFalse(socket == null); + + GameObject handleObj = GameObject.Find("RigidbodyHandle"); + Handle handle = handleObj.GetComponent(); + Assert.IsFalse(handle == null); + + socket.transform.localPosition = Vector3.zero; + handle.transform.localPosition = Vector3.zero; + + Transform socketParent = socket.transform.parent; + Transform handleParent = handle.transform.parent; + + Vector3 socketPosition = socket.transform.position; + Vector3 handlePosition = handle.transform.position; + +#region Attach + socket.Attach(handle.transform, false); + + Joint joint = handle.GetComponent(); + Assert.IsTrue(joint != null); + + Assert.AreEqual(socket.attachedTransform, handle.transform); + Assert.AreNotEqual(handle.transform.parent, socket.transform); + + // Handle should be at socket position + float distance = Vector3.Distance(socket.transform.position, handle.transform.position); + Assert.Less(distance, 0.0001F); + // Socket should not have moved + distance = Vector3.Distance(socketPosition, socket.transform.position); + Assert.Less(distance, 0.0001F); +#endregion + +#region Release + socket.Release(); + + Assert.IsTrue(socket.attachedTransform == null); + Assert.AreNotEqual(handle.transform.parent, socket.transform); + + // Handle should still be at socket position + distance = Vector3.Distance(socket.transform.position, handle.transform.position); + Assert.Less(distance, 0.0001F); +#endregion + + } + catch (Exception e) { + Debug.LogError("Test failed"); + throw (e); + } + } + + [Test] + [Category("Socket")] + public void StaticSocket_StaticHandle() { + // Static Handles cannot be attached to static sockets + try { + GameObject socketObj = GameObject.Find("StaticSocket"); + Socket socket = socketObj.GetComponent(); + Assert.IsTrue(socket != null); + + GameObject handleObj = GameObject.Find("StaticHandle"); + Handle handle = handleObj.GetComponent(); + Assert.IsTrue(handle != null); + + Transform socketParent = socket.transform.parent; + Transform handleParent = handle.transform.parent; + + Vector3 socketPosition = socket.transform.position; + Vector3 handlePosition = handle.transform.position; + +#region Attach + socket.Attach(handle.transform, false); + + Assert.IsTrue(socket.attachedTransform == null); + + // Handle should not have moved + float distance = Vector3.Distance(handle.transform.position, handlePosition); + Assert.Less(distance, 0.0001F); + // Socket should not have moved + distance = Vector3.Distance(socket.transform.position, socketPosition); + Assert.Less(distance, 0.0001F); +#endregion + +#region Release + socket.Release(); + + Assert.IsTrue(socket.attachedTransform == null); + + // Handle should not have moved + Assert.Less(Vector3.Distance(handle.transform.position, handlePosition), 0.0001F); + // Socket should not have moved + Assert.Less(Vector3.Distance(socket.transform.position, socketPosition), 0.0001F); + +#endregion + + socket.transform.localPosition = Vector3.zero; + handle.transform.localPosition = Vector3.zero; + } + catch (Exception e) { + Debug.LogError("Test failed"); + throw (e); + } + } + + [Test] + [Category("Socket")] + public void StaticSocket_KinematicHandle() { + // Kinematic Handles should be parented to the socket + try { + GameObject socketObj = GameObject.Find("StaticSocket"); + Socket socket = socketObj.GetComponent(); + + Assert.IsTrue(socket != null); + + GameObject handleObj = GameObject.Find("KinematicHandle"); + Handle handle = handleObj.GetComponent(); + Assert.IsTrue(handle != null); + + Transform socketParent = socket.transform.parent; + Transform handleParent = handle.transform.parent; + + Vector3 socketPosition = socket.transform.position; + Vector3 handlePosition = handle.transform.position; + +#region Attach + socket.Attach(handle.transform, false); + + Assert.IsTrue(socket.attachedTransform != null); + Assert.AreEqual(handle.transform.parent, socket.transform); + + // Handle should be at socket position + Assert.Less(Vector3.Distance(socket.transform.position, handle.transform.position), 0.0001F); + // Socket should not have moved + Assert.Less(Vector3.Distance(socketPosition, socket.transform.position), 0.0001F); +#endregion + +#region Release + socket.Release(); + + Assert.IsTrue(socket.attachedTransform == null); + Assert.AreEqual(handle.transform.parent, handleParent); + + // Handle should still be at socket position + Assert.Less(Vector3.Distance(socket.transform.position, handle.transform.position), 0.0001F); +#endregion + + socket.transform.localPosition = Vector3.zero; + handle.transform.localPosition = Vector3.zero; + } + catch (Exception e) { + Debug.LogError("Test failed"); + throw (e); + } + } +#endregion + +#region Rigidbody Socket + [Test] + [Category("Socket")] + public void RigidbodySocket_RigidbodyHandle() { + // Rigidbody Handles will be parented to Rigidbody Sockets + try { + GameObject socketObj = GameObject.Find("RigidbodySocket"); + Socket socket = socketObj.GetComponent(); + Assert.IsTrue(socket != null); + + GameObject handleObj = GameObject.Find("RigidbodyHandle"); + Handle handle = handleObj.GetComponent(); + Assert.IsTrue(handle != null); + + Transform socketParent = socket.transform.parent; + Transform handleParent = handle.transform.parent; + + Vector3 socketPosition = socket.transform.position; + Vector3 handlePosition = handle.transform.position; + +#region Attach + socket.Attach(handle.transform, false); + + Assert.AreEqual(socket.attachedTransform, handle.transform); + Assert.AreEqual(handle.transform.parent, socket.transform); + + // Handle should be at socket position + float distance = Vector3.Distance(socket.transform.position, handle.transform.position); + Assert.Less(distance, 0.0001F); + // Socket should not have moved + distance = Vector3.Distance(socketPosition, socket.transform.position); + Assert.Less(distance, 0.0001F); +#endregion + +#region Release + socket.Release(); + + Assert.IsTrue(socket.attachedTransform == null); + Assert.AreEqual(handle.transform.parent, handleParent); + + // Handle should be at socket position + distance = Vector3.Distance(socket.transform.position, handle.transform.position); + Assert.Less(distance, 0.0001F); +#endregion + + socket.transform.localPosition = Vector3.zero; + handle.transform.localPosition = Vector3.zero; + } + catch (Exception e) { + Debug.LogError("Test failed"); + throw (e); + } + } + + [Test] + [Category("Socket")] + public void RigidbodySocket_StaticHandle() { + // Rigidbody sockets will get a fixed joint to a static handle + try { + GameObject socketObj = GameObject.Find("RigidbodySocket"); + Socket socket = socketObj.GetComponent(); + Assert.IsFalse(socket == null); + + GameObject handleObj = GameObject.Find("StaticHandle"); + Handle handle = handleObj.GetComponent(); + Assert.IsFalse(handle == null); + + Transform socketParent = socket.transform.parent; + Transform handleParent = handle.transform.parent; + + Vector3 socketPosition = socket.transform.position; + Vector3 handlePosition = handle.transform.position; + +#region Attach + socket.Attach(handle.transform, false); + + Joint joint = socket.GetComponent(); + Assert.IsTrue(joint != null); + Assert.IsTrue(joint.connectedBody == null); + + Assert.AreEqual(socket.attachedTransform, handle.transform); + Assert.AreNotEqual(handle.transform.parent, socket.transform); + + // Socket should be at handle position + Assert.Less(Vector3.Distance(socket.transform.position, handle.transform.position), 0.0001F); + // Handle should not have moved + Assert.Less(Vector3.Distance(handle.transform.position, handlePosition), 0.0001F); +#endregion + +#region Release + socket.Release(); + + Assert.IsTrue(socket.attachedTransform == null); + Assert.AreNotEqual(handle.transform.parent, socket.transform); + + joint = socket.GetComponent(); + Assert.IsTrue(joint == null); + + // Socket should still be at handle position + Assert.Less(Vector3.Distance(socket.transform.position, handle.transform.position), 0.0001F); +#endregion + + socket.transform.localPosition = Vector3.zero; + handle.transform.localPosition = Vector3.zero; + } + catch (Exception e) { + Debug.LogError("Test failed"); + throw (e); + } + } + + [Test] + [Category("Socket")] + public void RigidbodySocket_KinematicHandle() { + // Rigidbody sockets will get parented to kinematic handles + try { + GameObject socketObj = GameObject.Find("RigidbodySocket"); + Socket socket = socketObj.GetComponent(); + Assert.IsFalse(socket == null); + + GameObject handleObj = GameObject.Find("KinematicHandle"); + Handle handle = handleObj.GetComponent(); + Assert.IsFalse(handle == null); + + Transform socketParent = socket.transform.parent; + Transform handleParent = handle.transform.parent; + + Vector3 socketPosition = socket.transform.position; + Vector3 handlePosition = handle.transform.position; + +#region Attach + socket.Attach(handle.transform, false); + + Assert.AreEqual(socket.attachedTransform, handle.transform); + Assert.AreEqual(socket.transform.parent, handle.transform); + + // Socket should be at handle position + Assert.Less(Vector3.Distance(socket.transform.position, handle.transform.position), 0.0001F); + // Handle should not have moved + Assert.Less(Vector3.Distance(handle.transform.position, handlePosition), 0.0001F); + +#endregion + +#region Release + socket.Release(); + + Assert.IsTrue(socket.attachedTransform == null, "Socket still is attached to transform"); + Assert.AreEqual(socket.transform.parent, socketParent, "Socket is not restored to previous parent"); + + // Socket should be at handle position + Assert.Less(Vector3.Distance(socket.transform.position, handle.transform.position), 0.0001F); +#endregion + + socket.transform.localPosition = Vector3.zero; + handle.transform.localPosition = Vector3.zero; + } + catch (Exception e) { + Debug.LogError("Test failed"); + throw (e); + } + } +#endregion + +#region Kinematic Socket + [Test] + [Category("Socket")] + public void KinematicSocket_RigidbodyHandle() { + Setup(); + // Rigidbody Handles will be parented to Kinematic Sockets + try { + GameObject socketObj = GameObject.Find("KinematicSocket"); + Socket socket = socketObj.GetComponent(); + Assert.IsTrue(socket != null); + + GameObject handleObj = GameObject.Find("RigidbodyHandle"); + Handle handle = handleObj.GetComponent(); + Assert.IsTrue(handle != null); + + Transform socketParent = socket.transform.parent; + Transform handleParent = handle.transform.parent; + + Vector3 socketPosition = socket.transform.position; + Vector3 handlePosition = handle.transform.position; + +#region Attach + socket.Attach(handle.transform, false); + + Assert.AreEqual(socket.attachedTransform, handle.transform); + Assert.AreEqual(handle.transform.parent, socket.transform); + + // Handle should be at socket position + float distance = Vector3.Distance(socket.transform.position, handle.transform.position); + Assert.Less(distance, 0.0001F); + // Socket should not have moved + distance = Vector3.Distance(socketPosition, socket.transform.position); + Assert.Less(distance, 0.0001F); +#endregion + +#region Release + socket.Release(); + + Assert.IsTrue(socket.attachedTransform == null); + Assert.AreEqual(handle.transform.parent, handleParent); + + // Handle should still be at socket position + distance = Vector3.Distance(socket.transform.position, handle.transform.position); + Assert.Less(distance, 0.0001F); +#endregion + + socket.transform.localPosition = Vector3.zero; + handle.transform.localPosition = Vector3.zero; + } + catch (Exception e) { + Debug.LogError("Test failed"); + throw (e); + } + } + + [Test] + [Category("Socket")] + public void KinematicSocket_StaticHandle() { + Setup(); + + // Kinematic Sockets should be parented to a static handle + try { + GameObject socketObj = GameObject.Find("KinematicSocket"); + Socket socket = socketObj.GetComponent(); + Assert.IsTrue(socket != null); + + GameObject handleObj = GameObject.Find("StaticHandle"); + Handle handle = handleObj.GetComponent(); + Assert.IsTrue(handle != null); + + Transform socketParent = socket.transform.parent; + Transform handleParent = handle.transform.parent; + + Vector3 socketPosition = socket.transform.position; + Vector3 handlePosition = handle.transform.position; + +#region Attach + socket.Attach(handle.transform, false); + + Assert.AreEqual(socket.attachedTransform, handle.transform); + Assert.AreEqual(socket.transform.parent, handle.transform); + + // Handle should be at socket position + Assert.Less(Vector3.Distance(socket.transform.position, handle.transform.position), 0.0001F); + // Handle should not have moved + Assert.Less(Vector3.Distance(handlePosition, handle.transform.position), 0.0001F); +#endregion + +#region Release + socket.Release(); + + Assert.IsTrue(socket.attachedTransform == null); + Assert.AreEqual(socket.transform.parent, socketParent); +#endregion + + socket.transform.localPosition = Vector3.zero; + handle.transform.localPosition = Vector3.zero; + } + catch (Exception e) { + Debug.LogError("Test failed"); + throw (e); + } + } + + [Test] + [Category("Socket")] + public void KinematicSocket_KinematicHandle() { + Setup(); + + // Kinematic Handles should be parented to a kinematic socket + try { + GameObject socketObj = GameObject.Find("KinematicSocket"); + Socket socket = socketObj.GetComponent(); + Assert.IsTrue(socket != null); + + GameObject handleObj = GameObject.Find("KinematicHandle"); + Handle handle = handleObj.GetComponent(); + Assert.IsTrue(handle != null); + + Transform socketParent = socket.transform.parent; + Transform handleParent = handle.transform.parent; + + Vector3 socketPosition = socket.transform.position; + Vector3 handlePosition = handle.transform.position; + +#region Attach + socket.Attach(handle.transform, false); + + Assert.IsTrue(socket.attachedTransform != null); + Assert.AreEqual(handle.transform.parent, socket.transform); + + // Handle should be at socket position + Assert.Less(Vector3.Distance(socket.transform.position, handle.transform.position), 0.0001F); + // Socket should not have moved + Assert.Less(Vector3.Distance(socketPosition, socket.transform.position), 0.0001F); +#endregion + +#region Release + socket.Release(); + + Assert.IsTrue(socket.attachedTransform == null); + Assert.AreEqual(handle.transform.parent, handleParent); +#endregion + + socket.transform.localPosition = Vector3.zero; + handle.transform.localPosition = Vector3.zero; + } + catch (Exception e) { + Debug.LogError("Test failed"); + throw (e); + } + } +#endregion + +} +#endif \ No newline at end of file diff --git a/Tests/Editor/Socket/SocketTest.cs.meta b/Tests/Editor/Socket/SocketTest.cs.meta new file mode 100644 index 0000000..2f0a08b --- /dev/null +++ b/Tests/Editor/Socket/SocketTest.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: 50c15dd580f1d4044a08b1a27d70e6bd +timeCreated: 1551084484 +licenseType: Free +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: