Skip to content

Commit 9bc4362

Browse files
committed
added test for adding device late
1 parent f42eb5e commit 9bc4362

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

Assets/Tests/InputSystem/Plugins/XRTests.cs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// ENABLE_VR is not defined on Game Core but the assembly is available with limited features when the XR module is enabled.
22
#if ENABLE_VR || UNITY_GAMECORE
33
using System;
4+
using System.Collections;
45
using System.Collections.Generic;
56
using System.Runtime.InteropServices;
67
using NUnit.Framework;
@@ -11,6 +12,7 @@
1112
using UnityEngine.InputSystem.LowLevel;
1213
using UnityEngine.InputSystem.Utilities;
1314
using UnityEngine.InputSystem.XR;
15+
using UnityEngine.TestTools;
1416
using UnityEngine.XR;
1517

1618
using Usages = UnityEngine.InputSystem.CommonUsages;
@@ -629,6 +631,57 @@ public void Components_TrackedPoseDriver_DoesNotEnableOrDisableReferenceActions(
629631
Assert.That(trackingStateInput.action.enabled, Is.False);
630632
}
631633

634+
[UnityTest]
635+
[Category("Components")]
636+
public IEnumerator LateAddedXRControllerCanLinkTrackedPoseDriver()
637+
{
638+
var go = new GameObject();
639+
var tpd = go.AddComponent<TrackedPoseDriver>();
640+
tpd.updateType = TrackedPoseDriver.UpdateType.UpdateAndBeforeRender;
641+
tpd.trackingType = TrackedPoseDriver.TrackingType.RotationAndPosition;
642+
tpd.ignoreTrackingState = false;
643+
var transform = tpd.transform;
644+
645+
var positionAction = new InputAction(binding: "<XRController>/devicePosition");
646+
var rotationAction = new InputAction(binding: "<XRController>/deviceRotation");
647+
var trackingStateAction = new InputAction(binding: "<XRController>/trackingState");
648+
tpd.positionInput = new InputActionProperty(positionAction);
649+
tpd.rotationInput = new InputActionProperty(rotationAction);
650+
tpd.trackingStateInput = new InputActionProperty(trackingStateAction);
651+
652+
yield return null;
653+
654+
Assert.That(positionAction.controls.Count, Is.EqualTo(0));
655+
Assert.That(rotationAction.controls.Count, Is.EqualTo(0));
656+
657+
var device = InputSystem.AddDevice<XRController>();
658+
InputSystem.AddDeviceUsage(device, "RightHand");
659+
660+
yield return null;
661+
662+
Assert.That(positionAction.controls.Count, Is.EqualTo(1));
663+
Assert.That(rotationAction.controls.Count, Is.EqualTo(1));
664+
665+
var position = new Vector3(1f, 2f, 3f);
666+
var rotation = new Quaternion(0.09853293f, 0.09853293f, 0.09853293f, 0.9853293f);
667+
using (StateEvent.From(device, out var stateEvent))
668+
{
669+
device.devicePosition.WriteValueIntoEvent(position, stateEvent);
670+
device.deviceRotation.WriteValueIntoEvent(rotation, stateEvent);
671+
device.trackingState.WriteValueIntoEvent((int)(InputTrackingState.Position | InputTrackingState.Rotation), stateEvent);
672+
673+
transform.position = Vector3.zero;
674+
transform.rotation = Quaternion.identity;
675+
InputSystem.QueueEvent(stateEvent);
676+
InputSystem.Update(InputUpdateType.Dynamic);
677+
}
678+
679+
yield return null;
680+
681+
Assert.That(transform.position, Is.EqualTo(position));
682+
Assert.That(transform.rotation, Is.EqualTo(rotation));
683+
}
684+
632685
[Test]
633686
[Category("Components")]
634687
public void Components_TrackedPoseDriver_RequiresResolvedTrackingStateBindings()

0 commit comments

Comments
 (0)