Skip to content

Fix XR Tests for 2019.3 #1158

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
128 changes: 99 additions & 29 deletions Assets/Tests/InputSystem/Plugins/XRTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,33 @@

internal class XRTests : CoreTestsFixture
{

#if UNITY_2019_3_OR_NEWER
[Test]
[Category("Devices")]
[TestCase(InputDeviceCharacteristics.HeadMounted, "XRHMD", typeof(XRHMD))]
[TestCase((InputDeviceCharacteristics.HeldInHand | InputDeviceCharacteristics.Controller), "XRController", typeof(XRController))]
[TestCase((InputDeviceCharacteristics.HeldInHand | InputDeviceCharacteristics.Controller | InputDeviceCharacteristics.Left), "XRController", typeof(XRController))]
[TestCase(InputDeviceCharacteristics.TrackedDevice, null, typeof(UnityEngine.InputSystem.InputDevice))]
[TestCase(InputDeviceCharacteristics.None, null, typeof(UnityEngine.InputSystem.InputDevice))]
public void Devices_XRDeviceCharacteristicsDeterminesTypeOfDevice(InputDeviceCharacteristics characteristics, string baseLayoutName, Type expectedType)
{
var deviceDescription = CreateSimpleDeviceDescriptionByType(characteristics);
runtime.ReportNewInputDevice(deviceDescription.ToJson());

InputSystem.Update();

Assert.That(InputSystem.devices, Has.Count.EqualTo(1));
var createdDevice = InputSystem.devices[0];

Assert.That(createdDevice, Is.TypeOf(expectedType));

var generatedLayout = InputSystem.LoadLayout(
$"{XRUtilities.InterfaceCurrent}::{deviceDescription.manufacturer}::{deviceDescription.product}");
Assert.That(generatedLayout, Is.Not.Null);
Assert.That(generatedLayout.baseLayouts, Is.EquivalentTo(new[] { new InternedString(baseLayoutName) }));
}
#else
[Test]
[Category("Devices")]
[TestCase(InputDeviceRole.Generic, "XRHMD", typeof(XRHMD))]
Expand All @@ -32,7 +59,7 @@ internal class XRTests : CoreTestsFixture
[TestCase(InputDeviceRole.Unknown, null, typeof(UnityEngine.InputSystem.InputDevice))]
public void Devices_XRDeviceRoleDeterminesTypeOfDevice(InputDeviceRole role, string baseLayoutName, Type expectedType)
{
var deviceDescription = CreateSimpleDeviceDescriptionByRole(role);
var deviceDescription = CreateSimpleDeviceDescriptionByType(role);
runtime.ReportNewInputDevice(deviceDescription.ToJson());

InputSystem.Update();
Expand All @@ -50,12 +77,18 @@ public void Devices_XRDeviceRoleDeterminesTypeOfDevice(InputDeviceRole role, str
else
Assert.That(generatedLayout.baseLayouts, Is.EquivalentTo(new[] { new InternedString(baseLayoutName) }));
}
#endif

[Test]
[Category("Devices")]
public void Devices_CanChangeHandednessOfXRController()
{
var deviceDescription = CreateSimpleDeviceDescriptionByRole(InputDeviceRole.LeftHanded);
#if UNITY_2019_3_OR_NEWER
var deviceDescription = CreateSimpleDeviceDescriptionByType(InputDeviceCharacteristics.Controller | InputDeviceCharacteristics.HeldInHand | InputDeviceCharacteristics.Left);
#else
var deviceDescription = CreateSimpleDeviceDescriptionByType(InputDeviceRole.LeftHanded);
#endif

runtime.ReportNewInputDevice(deviceDescription.ToJson());

InputSystem.Update();
Expand All @@ -79,7 +112,11 @@ public void Devices_CanChangeHandednessOfXRController()
[Category("Layouts")]
public void Layouts_XRLayoutIsNamespacedAsInterfaceManufacturerDevice()
{
var deviceDescription = CreateSimpleDeviceDescriptionByRole(InputDeviceRole.Generic);
#if UNITY_2019_3_OR_NEWER
var deviceDescription = CreateSimpleDeviceDescriptionByType(InputDeviceCharacteristics.HeadMounted);
#else
var deviceDescription = CreateSimpleDeviceDescriptionByType(InputDeviceRole.Generic);
#endif
runtime.ReportNewInputDevice(deviceDescription.ToJson());

InputSystem.Update();
Expand All @@ -96,7 +133,11 @@ public void Layouts_XRLayoutIsNamespacedAsInterfaceManufacturerDevice()
[Category("Layouts")]
public void Layouts_XRLayoutWithoutManufacturer_IsNamespacedAsInterfaceDevice()
{
var deviceDescription = CreateSimpleDeviceDescriptionByRole(InputDeviceRole.Generic);
#if UNITY_2019_3_OR_NEWER
var deviceDescription = CreateSimpleDeviceDescriptionByType(InputDeviceCharacteristics.HeadMounted);
#else
var deviceDescription = CreateSimpleDeviceDescriptionByType(InputDeviceRole.Generic);
#endif
deviceDescription.manufacturer = null;
runtime.ReportNewInputDevice(deviceDescription.ToJson());

Expand Down Expand Up @@ -146,7 +187,11 @@ public void Layouts_XRLayoutFeatures_OnlyContainAllowedCharacters()
[Category("Layouts")]
public void Layouts_XRDevicesWithNoOrInvalidCapabilities_DoNotCreateLayouts()
{
var deviceDescription = CreateSimpleDeviceDescriptionByRole(InputDeviceRole.Generic);
#if UNITY_2019_3_OR_NEWER
var deviceDescription = CreateSimpleDeviceDescriptionByType(InputDeviceCharacteristics.HeadMounted);
#else
var deviceDescription = CreateSimpleDeviceDescriptionByType(InputDeviceRole.Generic);
#endif
deviceDescription.capabilities = null;
runtime.ReportNewInputDevice(deviceDescription.ToJson());

Expand Down Expand Up @@ -746,29 +791,37 @@ public void Layouts_PoseControlsCanBeCreatedBySubcontrols()

private const int kNumBaseHMDControls = 10;

static InputDeviceCharacteristics CharacteristicsFromInputDeviceRole(InputDeviceRole role)
#if UNITY_2019_3_OR_NEWER
InputDeviceRole RoleFromCharacteristics(InputDeviceCharacteristics characteristics)
{
switch (role)
{
case InputDeviceRole.Generic:
return InputDeviceCharacteristics.HeadMounted;
case InputDeviceRole.LeftHanded:
return InputDeviceCharacteristics.Controller | InputDeviceCharacteristics.HeldInHand | InputDeviceCharacteristics.Left;
case InputDeviceRole.RightHanded:
return InputDeviceCharacteristics.Controller | InputDeviceCharacteristics.HeldInHand | InputDeviceCharacteristics.Right;
case InputDeviceRole.GameController:
return InputDeviceCharacteristics.Controller;
case InputDeviceRole.TrackingReference:
return InputDeviceCharacteristics.TrackingReference;
case InputDeviceRole.HardwareTracker:
return InputDeviceCharacteristics.TrackedDevice;
case InputDeviceRole.LegacyController:
return InputDeviceCharacteristics.Controller;
}
return InputDeviceCharacteristics.None;
if ((characteristics & InputDeviceCharacteristics.Left) != 0)
return InputDeviceRole.LeftHanded;
if ((characteristics & InputDeviceCharacteristics.Right) != 0)
return InputDeviceRole.RightHanded;
if ((characteristics & InputDeviceCharacteristics.TrackingReference) != 0)
return InputDeviceRole.TrackingReference;
if ((characteristics & InputDeviceCharacteristics.HeadMounted) != 0)
return InputDeviceRole.Generic;
if ((characteristics & InputDeviceCharacteristics.HeldInHand) != 0)
return InputDeviceRole.Generic;
if ((characteristics & InputDeviceCharacteristics.EyeTracking) != 0)
return InputDeviceRole.Generic;
if ((characteristics & InputDeviceCharacteristics.Camera) != 0)
return InputDeviceRole.Generic;
if ((characteristics & InputDeviceCharacteristics.Controller) != 0)
return InputDeviceRole.GameController;
if ((characteristics & InputDeviceCharacteristics.TrackedDevice) != 0)
return InputDeviceRole.HardwareTracker;

return InputDeviceRole.LegacyController;
}
#endif

private static InputDeviceDescription CreateSimpleDeviceDescriptionByRole(InputDeviceRole role)
#if UNITY_2019_3_OR_NEWER
private static InputDeviceDescription CreateSimpleDeviceDescriptionByType(InputDeviceCharacteristics deviceCharacteristics)
#else
private static InputDeviceDescription CreateSimpleDeviceDescriptionByType(InputDeviceRole role)
#endif
{
return new InputDeviceDescription
{
Expand All @@ -777,7 +830,11 @@ private static InputDeviceDescription CreateSimpleDeviceDescriptionByRole(InputD
manufacturer = "Manufacturer",
capabilities = new XRDeviceDescriptor
{
characteristics = CharacteristicsFromInputDeviceRole(role),
#if UNITY_2019_3_OR_NEWER
characteristics = deviceCharacteristics,
#else
deviceRole = role,
#endif
inputFeatures = new List<XRFeatureDescriptor>()
{
new XRFeatureDescriptor()
Expand All @@ -799,7 +856,12 @@ private static InputDeviceDescription CreateMangledNameDeviceDescription()
manufacturer = "__Manufacturer::",
capabilities = new XRDeviceDescriptor
{
characteristics = CharacteristicsFromInputDeviceRole(InputDeviceRole.Generic),
#if UNITY_2019_3_OR_NEWER
characteristics = InputDeviceCharacteristics.HeadMounted,
#else
deviceRole = InputDeviceRole.Generic,
#endif

inputFeatures = new List<XRFeatureDescriptor>()
{
new XRFeatureDescriptor()
Expand Down Expand Up @@ -833,7 +895,11 @@ public static InputDeviceDescription CreateDeviceDescription()
manufacturer = "XRManufacturer",
capabilities = new XRDeviceDescriptor
{
characteristics = CharacteristicsFromInputDeviceRole(InputDeviceRole.Generic),
#if UNITY_2019_3_OR_NEWER
characteristics = InputDeviceCharacteristics.HeadMounted,
#else
deviceRole = InputDeviceRole.Generic,
#endif
inputFeatures = new List<XRFeatureDescriptor>()
{
new XRFeatureDescriptor()
Expand Down Expand Up @@ -905,7 +971,11 @@ public static InputDeviceDescription CreateDeviceDescription()
manufacturer = "XRManufacturer",
capabilities = new XRDeviceDescriptor
{
characteristics = CharacteristicsFromInputDeviceRole(InputDeviceRole.Generic),
#if UNITY_2019_3_OR_NEWER
characteristics = InputDeviceCharacteristics.HeadMounted,
#else
deviceRole = InputDeviceRole.Generic,
#endif
inputFeatures = new List<XRFeatureDescriptor>()
{
new XRFeatureDescriptor()
Expand Down