StandaloneSubsystemTests.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Reflection;
  6. using NUnit.Framework;
  7. using Unity.Subsystem.Registration;
  8. using UnityEngine;
  9. using UnityEngine.TestTools;
  10. #if !UNITY_2019_2_OR_NEWER
  11. using UnityEngine.Experimental;
  12. #endif
  13. namespace Unity.Subsystem.Registration
  14. {
  15. [TestFixture]
  16. public class StandaloneSubsystemTestFixture
  17. {
  18. public class TestSubsystemDescriptor : SubsystemDescriptor<TestSubsystem>
  19. {
  20. public bool holdsThings { get; set; }
  21. }
  22. public abstract class TestSubsystem : Subsystem<TestSubsystemDescriptor>
  23. {
  24. public bool StartCalled { get; set; }
  25. public bool StopCalled { get; set; }
  26. public bool DestroyCalled { get; set; }
  27. public bool IsRunning { get; set; }
  28. public abstract int GetNumThings();
  29. }
  30. public class ConcreteTestSubsystem : TestSubsystem
  31. {
  32. #if UNITY_2019_3_OR_NEWER
  33. protected override void OnDestroy() { DestroyCalled = true; }
  34. #else
  35. public override void Destroy() { DestroyCalled = true; }
  36. #endif
  37. public override void Start() { StartCalled = true; IsRunning = true; }
  38. public override void Stop() { StopCalled = true; IsRunning = false; }
  39. #if UNITY_2019_2_OR_NEWER
  40. public override bool running { get { return IsRunning; } }
  41. #else
  42. public bool running { get { return IsRunning; } }
  43. #endif
  44. public override int GetNumThings()
  45. {
  46. return 66;
  47. }
  48. }
  49. [Test, Order(2)]
  50. public void UseSubsystemTest()
  51. {
  52. List<TestSubsystemDescriptor> descriptors = new List<TestSubsystemDescriptor>();
  53. SubsystemManager.GetSubsystemDescriptors<TestSubsystemDescriptor>(descriptors);
  54. Assert.That(1 == descriptors.Count, "TestSubsystemDescriptor not registered.");
  55. Assert.That("RuntimeTestSubsystem" == descriptors[0].id, "Subsystem ID doesn't match registered ID.");
  56. TestSubsystem subsystem = descriptors[0].Create();
  57. Assert.That(null != subsystem, "Create() failed in test subsystem descriptor.");
  58. // Method call works
  59. Assert.That(66 == subsystem.GetNumThings(), "Test method on TestSubsystem failed.");
  60. }
  61. [Test, Order(1)]
  62. public void RegisterSubsystemTest()
  63. {
  64. TestSubsystemDescriptor descriptor = new TestSubsystemDescriptor();
  65. List<TestSubsystemDescriptor> descriptors = new List<TestSubsystemDescriptor>();
  66. SubsystemManager.GetSubsystemDescriptors<TestSubsystemDescriptor>(descriptors);
  67. Assert.That(0 == descriptors.Count, "TestSubsystemDescriptor already registered.");
  68. // Populate the descriptor object
  69. descriptor.holdsThings = true;
  70. descriptor.id = "RuntimeTestSubsystem";
  71. descriptor.subsystemImplementationType = typeof(ConcreteTestSubsystem);
  72. // Register the descriptor
  73. Assert.That(true == SubsystemRegistration.CreateDescriptor(descriptor), "Descriptor not added.");
  74. Assert.That(false == SubsystemRegistration.CreateDescriptor(descriptor), "Descriptor added twice.");
  75. SubsystemManager.GetSubsystemDescriptors<TestSubsystemDescriptor>(descriptors);
  76. Assert.That(1 == descriptors.Count, "TestSubsystemDescriptor not registered.");
  77. }
  78. }
  79. }