No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

IntegrationTests.cs 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. using System.Collections;
  2. using NUnit.Framework;
  3. using UnityEngine;
  4. using UnityEngine.InputSystem;
  5. using UnityEngine.InputSystem.LowLevel;
  6. using UnityEngine.Scripting;
  7. using UnityEngine.TestTools;
  8. #if UNITY_EDITOR
  9. using UnityEngine.InputSystem.Editor;
  10. #endif
  11. // Disable irrelevant warning about there not being underscores in method names.
  12. #pragma warning disable CA1707
  13. // These tests are the only ones that we put *in* the package. The rest of our tests live in Assets/Tests and run separately
  14. // from our CI and not through upm-ci. This also means that IntegrationTests is the only thing we put on trunk through our
  15. // verified package.
  16. //
  17. // Rationale:
  18. // (1) Our APIVerificationTests have extra package requirements and thus need a custom package manifest.json. This will not
  19. // work with upm-ci.
  20. // (2) The tests we have in Assets/Tests exercise the input system in isolation. Having these run on trunk in addition to our
  21. // CI in the input system repo adds little value while adding extra execution time to trunk QV runs. This is unlike
  22. // the integration tests here which add value to trunk by making sure the input system is intact all the way through
  23. // to the native input module.
  24. // (3) If we added everything in Assets/Tests to the package, we would add more stuff to user projects that has no value to users.
  25. //
  26. // NOTE: The tests here are necessary to pass the requirement imposed by upm-ci that a package MUST have tests in it.
  27. public class IntegrationTests
  28. {
  29. [Preserve]
  30. public static void PreserveMethods()
  31. {
  32. // Workaround a bug in com.unity.test-framework.utp-reporter
  33. // Due Stripping set to to High System.ComponentModel.StringConverter ctor is stripped, making first test Integration_CanSendAndReceiveEvents to fail
  34. // MissingMethodException: Constructor on type 'System.ComponentModel.StringConverter' not found.
  35. //at System.RuntimeType.CreateInstanceImpl(System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder binder, System.Object[] args, System.Globalization.CultureInfo culture, System.Object[] activationAttributes, System.Threading.StackCrawlMark & stackMark)[0x001f0] in < b6074dacdf2142f38da4050b03a225bb >:0
  36. //at System.Activator.CreateInstance(System.Type type, System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder binder, System.Object[] args, System.Globalization.CultureInfo culture, System.Object[] activationAttributes)[0x00095] in < b6074dacdf2142f38da4050b03a225bb >:0
  37. //at System.Activator.CreateInstance(System.Type type, System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder binder, System.Object[] args, System.Globalization.CultureInfo culture)[0x00000] in < b6074dacdf2142f38da4050b03a225bb >:0
  38. //at System.SecurityUtils.SecureCreateInstance(System.Type type, System.Object[] args, System.Boolean allowNonPublic)[0x0003a] in < 43cff77c8e644fb3bd45df5f20310d13 >:0
  39. //at System.SecurityUtils.SecureCreateInstance(System.Type type)[0x00000] in < 43cff77c8e644fb3bd45df5f20310d13 >:0
  40. //at System.ComponentModel.ReflectTypeDescriptionProvider.CreateInstance(System.Type objectType, System.Type callingType)[0x0001a] in < 43cff77c8e644fb3bd45df5f20310d13 >:0
  41. //at System.ComponentModel.ReflectTypeDescriptionProvider.SearchIntrinsicTable(System.Collections.Hashtable table, System.Type callingType)[0x0015d] in < 43cff77c8e644fb3bd45df5f20310d13 >:0
  42. //at System.ComponentModel.ReflectTypeDescriptionProvider + ReflectedTypeData.GetConverter(System.Object instance)[0x000fc] in < 43cff77c8e644fb3bd45df5f20310d13 >:0
  43. //at System.ComponentModel.ReflectTypeDescriptionProvider.GetConverter(System.Type type, System.Object instance)[0x00008] in < 43cff77c8e644fb3bd45df5f20310d13 >:0
  44. //at System.ComponentModel.TypeDescriptor + TypeDescriptionNode + DefaultTypeDescriptor.System.ComponentModel.ICustomTypeDescriptor.GetConverter()[0x00016] in < 43cff77c8e644fb3bd45df5f20310d13 >:0
  45. //at System.ComponentModel.TypeDescriptor.GetConverter(System.Type type)[0x0000b] in < 43cff77c8e644fb3bd45df5f20310d13 >:0
  46. //at Newtonsoft.Json.Serialization.JsonTypeReflector.CanTypeDescriptorConvertString(System.Type type, System.ComponentModel.TypeConverter & typeConverter)[0x00000] in < 489f342f5a6b4cd3856aec7b3d5c47e7 >:0
  47. //at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.TryConvertToString(System.Object value, System.Type type, System.String & s)[0x00000] in < 489f342f5a6b4cd3856aec7b3d5c47e7 >:0
  48. //at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.GetPropertyName(Newtonsoft.Json.JsonWriter writer, System.Object name, Newtonsoft.Json.Serialization.JsonContract contract, System.Boolean & escape)[0x00127] in < 489f342f5a6b4cd3856aec7b3d5c47e7 >:0
  49. //at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeDictionary(Newtonsoft.Json.JsonWriter writer, System.Collections.IDictionary values, Newtonsoft.Json.Serialization.JsonDictionaryContract contract, Newtonsoft.Json.Serialization.JsonProperty member, Newtonsoft.Json.Serialization.JsonContainerContract collectionContract, Newtonsoft.Json.Serialization.JsonProperty containerProperty)[0x000c6] in < 489f342f5a6b4cd3856aec7b3d5c47e7 >:0
  50. //at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(Newtonsoft.Json.JsonWriter writer, System.Object value, Newtonsoft.Json.Serialization.JsonContract valueContract, Newtonsoft.Json.Serialization.JsonProperty member, Newtonsoft.Json.Serialization.JsonContainerContract containerContract, Newtonsoft.Json.Serialization.JsonProperty containerProperty)[0x0013d] in < 489f342f5a6b4cd3856aec7b3d5c47e7 >:0
  51. //at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize(Newtonsoft.Json.JsonWriter jsonWriter, System.Object value, System.Type objectType)[0x00079] in < 489f342f5a6b4cd3856aec7b3d5c47e7 >:0
  52. //at Newtonsoft.Json.JsonSerializer.SerializeInternal(Newtonsoft.Json.JsonWriter jsonWriter, System.Object value, System.Type objectType)[0x0023a] in < 489f342f5a6b4cd3856aec7b3d5c47e7 >:0
  53. //at Newtonsoft.Json.JsonSerializer.Serialize(Newtonsoft.Json.JsonWriter jsonWriter, System.Object value, System.Type objectType)[0x00000] in < 489f342f5a6b4cd3856aec7b3d5c47e7 >:0
  54. //at Newtonsoft.Json.JsonConvert.SerializeObjectInternal(System.Object value, System.Type type, Newtonsoft.Json.JsonSerializer jsonSerializer)[0x00028] in < 489f342f5a6b4cd3856aec7b3d5c47e7 >:0
  55. //at Newtonsoft.Json.JsonConvert.SerializeObject(System.Object value, System.Type type, Newtonsoft.Json.Formatting formatting, Newtonsoft.Json.JsonSerializerSettings settings)[0x0000e] in < 489f342f5a6b4cd3856aec7b3d5c47e7 >:0
  56. //at Newtonsoft.Json.JsonConvert.SerializeObject(System.Object value, Newtonsoft.Json.Formatting formatting, Newtonsoft.Json.JsonSerializerSettings settings)[0x00000] in < 489f342f5a6b4cd3856aec7b3d5c47e7 >:0
  57. //at Newtonsoft.Json.JsonConvert.SerializeObject(System.Object value, Newtonsoft.Json.Formatting formatting)[0x00000] in < 489f342f5a6b4cd3856aec7b3d5c47e7 >:0
  58. //at Unity.TestProtocol.UnityTestProtocolMessageBuilder.BuildMessage(System.Collections.Specialized.OrderedDictionary fields)[0x00001] in < eea339da6b5e4d4bb255bfef95601890 >:0
  59. //at Unity.TestProtocol.UnityTestProtocolMessageBuilder.Serialize(Unity.TestProtocol.Message message)[0x0006c] in < eea339da6b5e4d4bb255bfef95601890 >:0
  60. //at Unity.TestFramework.UTPReporter.TestResultToUtpMessage.Send(NUnit.Framework.Interfaces.IXmlNodeBuilder xmlNodeBuilder)[0x0002d] in F:\Projects\InputSystem\Library\PackageCache\com.unity.test - framework.utp - reporter@0.1.3 - preview.18\Runtime\TestResultsHandler.cs:65
  61. //at Unity.TestFramework.UTPReporter.TestResultToUtpMessage.TestStarted(NUnit.Framework.Interfaces.ITest testStartedResult)[0x00001] in F:\Projects\InputSystem\Library\PackageCache\com.unity.test - framework.utp - reporter@0.1.3 - preview.18\Runtime\TestResultsHandler.cs:50
  62. //at UnityEngine.TestRunner.Utils.TestRunCallbackListener +<> c__DisplayClass5_0.< TestStarted > b__0(UnityEngine.TestRunner.ITestRunCallback callback)[0x00000] in F:\Projects\InputSystem\Library\PackageCache\com.unity.test - framework@1.1.14\UnityEngine.TestRunner\Utils\TestRunCallbackListener.cs:55
  63. //at UnityEngine.TestRunner.Utils.TestRunCallbackListener.InvokeAllCallbacks(System.Action`1[T] invoker)[0x0002d] in F:\Projects\InputSystem\Library\PackageCache\com.unity.test - framework@1.1.14\UnityEngine.TestRunner\Utils\TestRunCallbackListener.cs:38
  64. var dummy = new System.ComponentModel.StringConverter();
  65. }
  66. [SetUp]
  67. public virtual void Setup()
  68. {
  69. // The standalone player can go out of focus when running tests. This makes the devices added during tests set
  70. // as disabled. Which in turn makes the InputSystem update not process input state changes for devices.
  71. // By ignoring focus, we can protect ourselves against this and always update the device state in the standalone
  72. // test players
  73. InputSystem.settings.backgroundBehavior = InputSettings.BackgroundBehavior.IgnoreFocus;
  74. }
  75. [TearDown]
  76. public virtual void TearDown()
  77. {
  78. InputSystem.settings.backgroundBehavior = default;
  79. }
  80. [Test]
  81. [Category("Integration")]
  82. public void Integration_CanSendAndReceiveEvents()
  83. {
  84. var keyboard = InputSystem.AddDevice<Keyboard>();
  85. try
  86. {
  87. InputSystem.QueueStateEvent(keyboard, new KeyboardState(Key.A));
  88. InputSystem.Update();
  89. Assert.That(keyboard.aKey.isPressed, Is.True);
  90. }
  91. finally
  92. {
  93. InputSystem.RemoveDevice(keyboard);
  94. }
  95. }
  96. #if UNITY_EDITOR
  97. [Test]
  98. [Category("Integration")]
  99. public void Integration_CanChangeInputBackendPlayerSettingInEditor()
  100. {
  101. // Save current player settings so we can restore them.
  102. var oldEnabled = EditorPlayerSettingHelpers.oldSystemBackendsEnabled;
  103. var newEnabled = EditorPlayerSettingHelpers.newSystemBackendsEnabled;
  104. // Enable new and disable old.
  105. EditorPlayerSettingHelpers.newSystemBackendsEnabled = true;
  106. EditorPlayerSettingHelpers.oldSystemBackendsEnabled = false;
  107. Assert.That(EditorPlayerSettingHelpers.newSystemBackendsEnabled, Is.True);
  108. Assert.That(EditorPlayerSettingHelpers.oldSystemBackendsEnabled, Is.False);
  109. // Enable old and disable new.
  110. EditorPlayerSettingHelpers.newSystemBackendsEnabled = false;
  111. EditorPlayerSettingHelpers.oldSystemBackendsEnabled = true;
  112. Assert.That(EditorPlayerSettingHelpers.newSystemBackendsEnabled, Is.False);
  113. Assert.That(EditorPlayerSettingHelpers.oldSystemBackendsEnabled, Is.True);
  114. // Enable both.
  115. EditorPlayerSettingHelpers.newSystemBackendsEnabled = true;
  116. EditorPlayerSettingHelpers.oldSystemBackendsEnabled = true;
  117. Assert.That(EditorPlayerSettingHelpers.newSystemBackendsEnabled, Is.True);
  118. Assert.That(EditorPlayerSettingHelpers.oldSystemBackendsEnabled, Is.True);
  119. // Restore previous settings.
  120. EditorPlayerSettingHelpers.oldSystemBackendsEnabled = oldEnabled;
  121. EditorPlayerSettingHelpers.newSystemBackendsEnabled = newEnabled;
  122. }
  123. #endif
  124. #if UNITY_STANDALONE_WIN || UNITY_EDITOR_WIN
  125. [UnityTest]
  126. [Category("Integration")]
  127. [Ignore("Unstable due to 1252825")]
  128. public IEnumerator WindowsInput_RemoteDesktopMouseMovements_AreDetected()
  129. {
  130. var mouse = InputSystem.GetDevice<Mouse>();
  131. var currentPosition = mouse.position.ReadValue();
  132. yield return new WaitForSeconds(0.1f);
  133. Assert.AreEqual(currentPosition, mouse.position.ReadValue(), "Expected mouse position to not change when no input was sent. Please do not move the mouse during this test.");
  134. WinUserInput.SendRDPMouseMoveEvent(10, 10);
  135. yield return new WaitForSeconds(0.1f);
  136. Assert.AreNotEqual(currentPosition, mouse.position.ReadValue(), "Expected mouse position to have moved when sending RDP/absolute values.");
  137. currentPosition = mouse.position.ReadValue();
  138. WinUserInput.SendRDPMouseMoveEvent(100, 100);
  139. yield return new WaitForSeconds(0.1f);
  140. Assert.AreNotEqual(currentPosition, mouse.position.ReadValue(), "Expected mouse position to have moved when sending RDP/absolute values.");
  141. }
  142. [UnityTest]
  143. [Category("Integration")]
  144. [Ignore("Unstable due to 1252825")]
  145. public IEnumerator WindowsInput_MouseMovements_AreDetected()
  146. {
  147. var mouse = InputSystem.GetDevice<Mouse>();
  148. var currentPosition = mouse.position.ReadValue();
  149. yield return new WaitForSeconds(0.1f);
  150. Assert.AreEqual(currentPosition, mouse.position.ReadValue(), "Expected mouse position to not change when no input was sent. Please do not move the mouse during this test.");
  151. WinUserInput.SendMouseMoveEvent(10, 10);
  152. yield return new WaitForSeconds(0.1f);
  153. Assert.AreNotEqual(currentPosition, mouse.position.ReadValue(), "Expected mouse position to have moved when sending relative values.");
  154. currentPosition = mouse.position.ReadValue();
  155. WinUserInput.SendMouseMoveEvent(100, 100);
  156. yield return new WaitForSeconds(0.1f);
  157. Assert.AreNotEqual(currentPosition, mouse.position.ReadValue(), "Expected mouse position to have moved when sending relative values.");
  158. }
  159. #endif // UNITY_2019_3_OR_NEWER && (UNITY_STANDALONE_WIN || UNITY_EDITOR_WIN)
  160. }