SignalingManagerTest.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. using System.IO;
  2. using System.Linq;
  3. using NUnit.Framework;
  4. using Unity.RenderStreaming.RuntimeTest.Signaling;
  5. using Unity.RenderStreaming.Signaling;
  6. using UnityEngine;
  7. using UnityEngine.TestTools;
  8. using Object = UnityEngine.Object;
  9. namespace Unity.RenderStreaming.RuntimeTest
  10. {
  11. class SignalingManagerTest
  12. {
  13. SignalingManager component;
  14. [SetUp]
  15. public void SetUp()
  16. {
  17. GameObject obj = new GameObject();
  18. obj.SetActive(false);
  19. component = obj.AddComponent<SignalingManager>();
  20. }
  21. [TearDown]
  22. public void TearDown()
  23. {
  24. Object.DestroyImmediate(component.gameObject);
  25. }
  26. [Test]
  27. public void DoNothing()
  28. {
  29. }
  30. [Test]
  31. public void UseDefaultSettings()
  32. {
  33. Assert.That(component.useDefaultSettings, Is.True);
  34. component.useDefaultSettings = false;
  35. Assert.That(component.useDefaultSettings, Is.False);
  36. }
  37. [Test]
  38. public void Run()
  39. {
  40. var handler = component.gameObject.AddComponent<SingleConnection>();
  41. var handlers = new SignalingHandlerBase[] { handler };
  42. ISignaling mock = new MockSignaling();
  43. component.runOnAwake = false;
  44. component.gameObject.SetActive(true);
  45. component.Run(signaling: mock, handlers: handlers);
  46. }
  47. [Test, Ignore("Failed this test on macOS and Linux platform because of the signaling process.")]
  48. public void RunDefault()
  49. {
  50. var handler = component.gameObject.AddComponent<SingleConnection>();
  51. var handlers = new SignalingHandlerBase[] { handler };
  52. ISignaling mock = new MockSignaling();
  53. component.runOnAwake = false;
  54. component.gameObject.SetActive(true);
  55. component.Run(handlers:handlers);
  56. }
  57. [Test]
  58. public void ThrowExceptionIfHandlerIsNullOrEmpty()
  59. {
  60. ISignaling mock = new MockSignaling();
  61. component.runOnAwake = false;
  62. component.gameObject.SetActive(true);
  63. Assert.That(() => component.Run(signaling: mock), Throws.InvalidOperationException);
  64. var handlers = new SignalingHandlerBase[] {};
  65. Assert.That(() => component.Run(signaling: mock, handlers:handlers),
  66. Throws.InvalidOperationException);
  67. }
  68. [Test]
  69. public void RunAgain()
  70. {
  71. var handler = component.gameObject.AddComponent<SingleConnection>();
  72. var handlers = new SignalingHandlerBase[] { handler };
  73. ISignaling mock = new MockSignaling();
  74. component.runOnAwake = false;
  75. component.gameObject.SetActive(true);
  76. component.Run(signaling:mock, handlers:handlers);
  77. component.Stop();
  78. component.Run(signaling:mock, handlers:handlers);
  79. }
  80. [Test]
  81. public void GetDefaultSignalingSettings()
  82. {
  83. component.runOnAwake = false;
  84. component.gameObject.SetActive(true);
  85. var settings = component.GetSignalingSettings() as WebSocketSignalingSettings;
  86. Assert.That(settings, Is.Not.Null);
  87. }
  88. [Test]
  89. public void AddAndRemoveSignalingHandler()
  90. {
  91. component.runOnAwake = false;
  92. component.gameObject.SetActive(true);
  93. component.SetSignalingSettings(new MockSignalingSettings());
  94. var handler = component.gameObject.AddComponent<SingleConnection>();
  95. component.AddSignalingHandler(handler);
  96. Assert.That(() => component.Run(), Throws.Nothing);
  97. component.RemoveSignalingHandler(handler);
  98. }
  99. [Test]
  100. public void ThrowExceptionSetSignalingOnRunning()
  101. {
  102. component.runOnAwake = false;
  103. component.gameObject.SetActive(true);
  104. component.SetSignalingSettings(new MockSignalingSettings());
  105. var handler = component.gameObject.AddComponent<SingleConnection>();
  106. component.AddSignalingHandler(handler);
  107. Assert.That(() => component.Run(), Throws.Nothing);
  108. Assert.That(component.Running, Is.True);
  109. Assert.That(() => component.SetSignalingSettings(new MockSignalingSettings()), Throws.InvalidOperationException);
  110. }
  111. [Test]
  112. [UnityPlatform(exclude = new[] { RuntimePlatform.Android, RuntimePlatform.IPhonePlayer })]
  113. public void EvaluateCommandlineArguments()
  114. {
  115. // Change signaling type.
  116. SignalingSettings settings = new WebSocketSignalingSettings("ws://127.0.0.1");
  117. Assert.That(settings.iceServers, Is.Empty);
  118. string[] arguments = { "-signalingType", "http" };
  119. Assert.That(SignalingManager.EvaluateCommandlineArguments(ref settings, arguments), Is.True);
  120. Assert.That(settings, Is.TypeOf<HttpSignalingSettings>());
  121. Assert.That(settings.iceServers, Is.Not.Empty);
  122. settings = new HttpSignalingSettings("http://127.0.0.1");
  123. Assert.That(settings.iceServers, Is.Empty);
  124. arguments = new string[]{ "-signalingType", "websocket" };
  125. Assert.That(SignalingManager.EvaluateCommandlineArguments(ref settings, arguments), Is.True);
  126. Assert.That(settings, Is.TypeOf<WebSocketSignalingSettings>());
  127. Assert.That(settings.iceServers, Is.Not.Empty);
  128. // Change signaling url.
  129. settings = new WebSocketSignalingSettings("ws://127.0.0.1");
  130. string url = "ws://192.168.10.10";
  131. arguments = new[] { "-signalingUrl", url };
  132. Assert.That(SignalingManager.EvaluateCommandlineArguments(ref settings, arguments), Is.True);
  133. Assert.That(settings, Is.TypeOf<WebSocketSignalingSettings>());
  134. Assert.That((settings as WebSocketSignalingSettings).url, Is.EqualTo(url));
  135. settings = new HttpSignalingSettings("http://127.0.0.1");
  136. url = "http://192.168.10.10";
  137. arguments = new[] { "-signalingUrl", url };
  138. Assert.That(SignalingManager.EvaluateCommandlineArguments(ref settings, arguments), Is.True);
  139. Assert.That(settings, Is.TypeOf<HttpSignalingSettings>());
  140. Assert.That((settings as HttpSignalingSettings).url, Is.EqualTo(url));
  141. // Import json for ice server settings.
  142. settings = new WebSocketSignalingSettings("ws://127.0.0.1");
  143. string json = "{\"iceServers\":[{\"credential\":\"pass\",\"username\":\"user\",\"credentialType\":\"password\"," +
  144. "\"urls\":[\"turn:192.168.10.10:3478?transport=udp\"]}]}";
  145. string filepath = "dummy.json";
  146. File.WriteAllText(filepath, json);
  147. arguments = new[] { "-importJson", filepath };
  148. var info = JsonUtility.FromJson<CommandLineInfo>(json);
  149. Assert.That(SignalingManager.EvaluateCommandlineArguments(ref settings, arguments), Is.True);
  150. Assert.That(settings, Is.TypeOf<WebSocketSignalingSettings>());
  151. Assert.That(settings.iceServers.Count, Is.EqualTo(1));
  152. Assert.That(settings.iceServers.ElementAt(0).credential, Is.EqualTo(info.iceServers[0].credential));
  153. Assert.That(settings.iceServers.ElementAt(0).credentialType, Is.EqualTo((IceCredentialType)info.iceServers[0].credentialType));
  154. File.Delete(filepath);
  155. // Import json to change signaling type.
  156. settings = new WebSocketSignalingSettings("ws://127.0.0.1");
  157. json = "{\"signalingType\":\"websocket\"}";
  158. File.WriteAllText(filepath, json);
  159. arguments = new[] { "-importJson", filepath };
  160. Assert.That(SignalingManager.EvaluateCommandlineArguments(ref settings, arguments), Is.True);
  161. Assert.That(settings, Is.TypeOf<WebSocketSignalingSettings>());
  162. }
  163. }
  164. }