SeedXRInputBindingsTests.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. using UnityEngine;
  2. using UnityEngine.TestTools;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using NUnit.Framework;
  6. using UnityEditor.XR.LegacyInputHelpers;
  7. using UnityEditor;
  8. namespace UnityEditor.XR.LegacyInputHelpers.Tests
  9. {
  10. [TestFixture]
  11. internal class TestSeededInput
  12. {
  13. const int kNumOverlaps = 4; // we know there are four overlaps between the input asset, and the seeded assets.
  14. const int kNumDupesToKeep = 2;
  15. [Test]
  16. public void SeededInput_FillsOutCompleteData()
  17. {
  18. // load the input asset
  19. var inputManagerAsset = AssetDatabase.LoadAllAssetsAtPath("ProjectSettings/InputManager.asset")[0];
  20. var serializedObject = new SerializedObject(inputManagerAsset);
  21. var inputManagerCurrentData = serializedObject.FindProperty("m_Axes");
  22. // cache the number of items so we can reset.
  23. int prevInputManagerSize = inputManagerCurrentData.arraySize;
  24. SeedXRInputBindings tsxib = new SeedXRInputBindings();
  25. Dictionary<string, SeedXRInputBindings.BindingData> axisMap = new Dictionary<string, SeedXRInputBindings.BindingData>();
  26. for (int i = 0; i < tsxib.axisList.Count; ++i)
  27. {
  28. axisMap.Add(tsxib.axisList[i].name, new SeedXRInputBindings.BindingData() { newDataIndex = i, exists = false, inputManagerIndex = -1 });
  29. }
  30. tsxib.GenerateXRBindings();
  31. inputManagerAsset = AssetDatabase.LoadAllAssetsAtPath("ProjectSettings/InputManager.asset")[0];
  32. serializedObject = new SerializedObject(inputManagerAsset);
  33. inputManagerCurrentData = serializedObject.FindProperty("m_Axes");
  34. // did we create the right number of things?
  35. Assert.That(inputManagerCurrentData.arraySize == (prevInputManagerSize + tsxib.axisList.Count) - kNumOverlaps); // we subtract kNumOverlaps because we know there are 4 things duplicated and they shouldnt be included.
  36. List<SeedXRInputBindings.InputAxis> currentInputData = new List<SeedXRInputBindings.InputAxis>();
  37. tsxib.LoadExistingDataAndCheckAgainstNewData(inputManagerCurrentData, ref axisMap, ref currentInputData);
  38. // the axis map should now be true for every element.
  39. foreach(var item in axisMap)
  40. {
  41. Assert.That(item.Value.exists == true);
  42. }
  43. inputManagerCurrentData.arraySize = prevInputManagerSize;
  44. serializedObject.ApplyModifiedProperties();
  45. AssetDatabase.Refresh();
  46. }
  47. [Test]
  48. public void SeededInput_DoesntAddDuplicates()
  49. {
  50. // load the input asset
  51. var inputManagerAsset = AssetDatabase.LoadAllAssetsAtPath("ProjectSettings/InputManager.asset")[0];
  52. var serializedObject = new SerializedObject(inputManagerAsset);
  53. var inputManagerCurrentData = serializedObject.FindProperty("m_Axes");
  54. // cache the number of items so we can reset.
  55. int prevInputManagerSize = inputManagerCurrentData.arraySize;
  56. inputManagerCurrentData.arraySize = 0;
  57. SeedXRInputBindings tsxib = new SeedXRInputBindings();
  58. Dictionary<string, SeedXRInputBindings.BindingData> axisMap = new Dictionary<string, SeedXRInputBindings.BindingData>();
  59. for (int i = 0; i < tsxib.axisList.Count; ++i)
  60. {
  61. axisMap.Add(tsxib.axisList[i].name, new SeedXRInputBindings.BindingData() { newDataIndex = i, exists = false, inputManagerIndex = -1 });
  62. }
  63. tsxib.GenerateXRBindings();
  64. // slam back the value to a smaller number
  65. inputManagerAsset = AssetDatabase.LoadAllAssetsAtPath("ProjectSettings/InputManager.asset")[0];
  66. serializedObject = new SerializedObject(inputManagerAsset);
  67. inputManagerCurrentData = serializedObject.FindProperty("m_Axes");
  68. // we want to maintain a few records to ensure that we dont add duplicates.
  69. inputManagerCurrentData.arraySize = kNumDupesToKeep;
  70. List<SeedXRInputBindings.InputAxis> currentInputData = new List<SeedXRInputBindings.InputAxis>();
  71. tsxib.LoadExistingDataAndCheckAgainstNewData(inputManagerCurrentData, ref axisMap, ref currentInputData);
  72. // now, we should only have two elements that match, seeing as we only left two items in the asset.
  73. int trueCount = 0;
  74. foreach (var item in axisMap)
  75. {
  76. if (item.Value.exists)
  77. trueCount++;
  78. }
  79. Assert.That(trueCount == kNumDupesToKeep);
  80. tsxib.GenerateXRBindings();
  81. inputManagerAsset = AssetDatabase.LoadAllAssetsAtPath("ProjectSettings/InputManager.asset")[0];
  82. serializedObject = new SerializedObject(inputManagerAsset);
  83. inputManagerCurrentData = serializedObject.FindProperty("m_Axes");
  84. Assert.That(inputManagerCurrentData.arraySize == (prevInputManagerSize + tsxib.axisList.Count) - kNumOverlaps); // we subtract kNumOverlaps because we know there are 4 things duplicated and they shouldnt be included.
  85. tsxib.LoadExistingDataAndCheckAgainstNewData(inputManagerCurrentData, ref axisMap, ref currentInputData);
  86. // the axis map should now be true for every element.
  87. foreach (var item in axisMap)
  88. {
  89. Assert.That(item.Value.exists == true);
  90. }
  91. inputManagerCurrentData.arraySize = prevInputManagerSize;
  92. serializedObject.ApplyModifiedProperties();
  93. AssetDatabase.Refresh();
  94. }
  95. }
  96. }