SignalingEventProviderTest.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. using System;
  2. using UnityEngine;
  3. using NUnit.Framework;
  4. using Unity.WebRTC;
  5. using UnityEngine.EventSystems;
  6. using UnityEngine.TestTools;
  7. namespace Unity.RenderStreaming.RuntimeTest
  8. {
  9. class CreatedConnectionHandlerTest : MonoBehaviour,
  10. IMonoBehaviourTest, ICreatedConnectionHandler
  11. {
  12. public bool IsTestFinished { get; private set; }
  13. public SignalingEventData Data { get; private set; }
  14. public void OnCreatedConnection(SignalingEventData data)
  15. {
  16. IsTestFinished = true;
  17. this.Data = data;
  18. }
  19. }
  20. class DeletedConnectionHandlerTest : MonoBehaviour,
  21. IMonoBehaviourTest, IDeletedConnectionHandler
  22. {
  23. public bool IsTestFinished { get; private set; }
  24. public SignalingEventData Data { get; private set; }
  25. public void OnDeletedConnection(SignalingEventData data)
  26. {
  27. IsTestFinished = true;
  28. this.Data = data;
  29. }
  30. }
  31. class ConnectHandlerTest : MonoBehaviour,
  32. IMonoBehaviourTest, IConnectHandler
  33. {
  34. public bool IsTestFinished { get; private set; }
  35. public SignalingEventData Data { get; private set; }
  36. public void OnConnect(SignalingEventData data)
  37. {
  38. IsTestFinished = true;
  39. this.Data = data;
  40. }
  41. }
  42. class DisconnectHandlerTest : MonoBehaviour,
  43. IMonoBehaviourTest, IDisconnectHandler
  44. {
  45. public bool IsTestFinished { get; private set; }
  46. public SignalingEventData Data { get; private set; }
  47. public void OnDisconnect(SignalingEventData data)
  48. {
  49. IsTestFinished = true;
  50. this.Data = data;
  51. }
  52. }
  53. class OfferHandlerTest : MonoBehaviour,
  54. IMonoBehaviourTest, IOfferHandler
  55. {
  56. public bool IsTestFinished { get; private set; }
  57. public SignalingEventData Data { get; private set; }
  58. public void OnOffer(SignalingEventData data)
  59. {
  60. IsTestFinished = true;
  61. this.Data = data;
  62. }
  63. }
  64. class AnswerHandlerTest : MonoBehaviour,
  65. IMonoBehaviourTest, IAnswerHandler
  66. {
  67. public bool IsTestFinished { get; private set; }
  68. public SignalingEventData Data { get; private set; }
  69. public void OnAnswer(SignalingEventData data)
  70. {
  71. IsTestFinished = true;
  72. this.Data = data;
  73. }
  74. }
  75. class AddReceiverHandlerTest : MonoBehaviour,
  76. IMonoBehaviourTest, IAddReceiverHandler
  77. {
  78. public bool IsTestFinished { get; private set; }
  79. public SignalingEventData Data { get; private set; }
  80. public void OnAddReceiver(SignalingEventData data)
  81. {
  82. IsTestFinished = true;
  83. this.Data = data;
  84. }
  85. }
  86. class AddChannelHandlerTest : MonoBehaviour,
  87. IMonoBehaviourTest, IAddChannelHandler
  88. {
  89. public bool IsTestFinished { get; private set; }
  90. public SignalingEventData Data { get; private set; }
  91. public void OnAddChannel(SignalingEventData data)
  92. {
  93. IsTestFinished = true;
  94. this.Data = data;
  95. }
  96. }
  97. /// <summary>
  98. /// note:: Moq is not supported IL2CPP platform,
  99. /// this class should be replaced Moq `Raise` method.
  100. /// </summary>
  101. class MockDelegate : IRenderStreamingDelegate
  102. {
  103. public event Action onStart;
  104. public event Action<string> onCreatedConnection;
  105. public event Action<string> onDeletedConnection;
  106. public event Action<string, string> onGotOffer;
  107. public event Action<string, string> onGotAnswer;
  108. public event Action<string> onConnect;
  109. public event Action<string> onDisconnect;
  110. public event Action<string, RTCRtpTransceiver> onAddTransceiver;
  111. public event Action<string, RTCDataChannel> onAddChannel;
  112. public void RaiseOnStart()
  113. {
  114. onStart?.Invoke();
  115. }
  116. public void RaiseOnCreatedConnection(string connectionId)
  117. {
  118. onCreatedConnection?.Invoke(connectionId);
  119. }
  120. public void RaiseOnDeletedConnection(string connectionId)
  121. {
  122. onDeletedConnection?.Invoke(connectionId);
  123. }
  124. public void RaiseOnGotOffer(string connectionId, string sdp)
  125. {
  126. onGotOffer?.Invoke(connectionId, sdp);
  127. }
  128. public void RaiseOnGotAnswer(string connectionId, string sdp)
  129. {
  130. onGotAnswer?.Invoke(connectionId, sdp);
  131. }
  132. public void RaiseOnConnect(string connectionId)
  133. {
  134. onConnect?.Invoke(connectionId);
  135. }
  136. public void RaiseOnDisconnect(string connectionId)
  137. {
  138. onDisconnect?.Invoke(connectionId);
  139. }
  140. public void RaiseOnAddTransceiver(string connectionId, RTCRtpTransceiver transceiver)
  141. {
  142. onAddTransceiver?.Invoke(connectionId, transceiver);
  143. }
  144. public void RaiseOnAddChannel(string connectionId, RTCDataChannel channel)
  145. {
  146. onAddChannel?.Invoke(connectionId, channel);
  147. }
  148. }
  149. [UnityPlatform(exclude = new[] { RuntimePlatform.OSXEditor, RuntimePlatform.OSXPlayer, RuntimePlatform.LinuxEditor, RuntimePlatform.LinuxPlayer })]
  150. class SignalingEventProviderTest
  151. {
  152. private EventSystem m_eventSystem;
  153. private SignalingEventProvider m_provider;
  154. private MockDelegate _mDelegate;
  155. [OneTimeSetUp]
  156. public void OneTimeSetUp()
  157. {
  158. m_eventSystem = new GameObject("EventSystem").AddComponent<EventSystem>();
  159. }
  160. [OneTimeTearDown]
  161. public void OneTimeTearDown()
  162. {
  163. UnityEngine.Object.DestroyImmediate(m_eventSystem);
  164. }
  165. [SetUp]
  166. public void SetUp()
  167. {
  168. _mDelegate = new MockDelegate();
  169. m_provider = new SignalingEventProvider(_mDelegate);
  170. }
  171. [Test]
  172. public void Subscribe()
  173. {
  174. var test = new MonoBehaviourTest<CreatedConnectionHandlerTest>();
  175. Assert.That(m_provider.Subscribe(test.component), Is.True);
  176. // return false if it is already registered.
  177. Assert.That(m_provider.Subscribe(test.component), Is.False);
  178. Assert.That(m_provider.Unsubscribe(test.component), Is.True);
  179. // return false if it is not found.
  180. Assert.That(m_provider.Unsubscribe(test.component), Is.False);
  181. }
  182. [Test]
  183. public void OnCreatedConnection()
  184. {
  185. var connectionId = "12345";
  186. var test = new MonoBehaviourTest<CreatedConnectionHandlerTest>();
  187. m_provider.Subscribe(test.component);
  188. _mDelegate.RaiseOnCreatedConnection(connectionId);
  189. Assert.That(test.component.IsTestFinished, Is.True);
  190. Assert.That(test.component.Data.connectionId, Is.EqualTo(connectionId));
  191. UnityEngine.Object.DestroyImmediate(test.gameObject);
  192. }
  193. [Test]
  194. public void OnDeletedConnection()
  195. {
  196. var connectionId = "12345";
  197. var test = new MonoBehaviourTest<DeletedConnectionHandlerTest>();
  198. m_provider.Subscribe(test.component);
  199. _mDelegate.RaiseOnDeletedConnection(connectionId);
  200. Assert.That(test.component.IsTestFinished, Is.True);
  201. Assert.That(test.component.Data.connectionId, Is.EqualTo(connectionId));
  202. UnityEngine.Object.DestroyImmediate(test.gameObject);
  203. }
  204. [Test]
  205. public void OnConnect()
  206. {
  207. var connectionId = "12345";
  208. var test = new MonoBehaviourTest<ConnectHandlerTest>();
  209. m_provider.Subscribe(test.component);
  210. _mDelegate.RaiseOnConnect(connectionId);
  211. Assert.That(test.component.IsTestFinished, Is.True);
  212. Assert.That(test.component.Data.connectionId, Is.EqualTo(connectionId));
  213. UnityEngine.Object.DestroyImmediate(test.gameObject);
  214. }
  215. [Test]
  216. public void OnDisconnect()
  217. {
  218. var connectionId = "12345";
  219. var test = new MonoBehaviourTest<DisconnectHandlerTest>();
  220. m_provider.Subscribe(test.component);
  221. _mDelegate.RaiseOnDisconnect(connectionId);
  222. Assert.That(test.component.IsTestFinished, Is.True);
  223. Assert.That(test.component.Data.connectionId, Is.EqualTo(connectionId));
  224. UnityEngine.Object.DestroyImmediate(test.gameObject);
  225. }
  226. [Test]
  227. public void OnOffer()
  228. {
  229. var connectionId = "12345";
  230. var sdp = "this is sdp";
  231. var test = new MonoBehaviourTest<OfferHandlerTest>();
  232. m_provider.Subscribe(test.component);
  233. _mDelegate.RaiseOnGotOffer(connectionId, sdp);
  234. Assert.That(test.component.IsTestFinished, Is.True);
  235. Assert.That(test.component.Data.connectionId, Is.EqualTo(connectionId));
  236. Assert.That(test.component.Data.sdp, Is.EqualTo(sdp));
  237. UnityEngine.Object.DestroyImmediate(test.gameObject);
  238. }
  239. [Test]
  240. public void OnAnswer()
  241. {
  242. var connectionId = "12345";
  243. var sdp = "this is sdp";
  244. var test = new MonoBehaviourTest<AnswerHandlerTest>();
  245. m_provider.Subscribe(test.component);
  246. _mDelegate.RaiseOnGotAnswer(connectionId, sdp);
  247. Assert.That(test.component.IsTestFinished, Is.True);
  248. Assert.That(test.component.Data.connectionId, Is.EqualTo(connectionId));
  249. Assert.That(test.component.Data.sdp, Is.EqualTo(sdp));
  250. UnityEngine.Object.DestroyImmediate(test.gameObject);
  251. }
  252. [Test]
  253. public void OnAddReceiver()
  254. {
  255. var connectionId = "12345";
  256. // todo:: create a receiver instance
  257. var test = new MonoBehaviourTest<AddReceiverHandlerTest>();
  258. m_provider.Subscribe(test.component);
  259. _mDelegate.RaiseOnAddTransceiver(connectionId, null);
  260. Assert.That(test.component.IsTestFinished, Is.True);
  261. Assert.That(test.component.Data.connectionId, Is.EqualTo(connectionId));
  262. Assert.That(test.component.Data.transceiver, Is.Null);
  263. UnityEngine.Object.DestroyImmediate(test.gameObject);
  264. }
  265. [Test]
  266. public void OnAddChannel()
  267. {
  268. var connectionId = "12345";
  269. var peer = new RTCPeerConnection();
  270. var channel = peer.CreateDataChannel("test");
  271. var test = new MonoBehaviourTest<AddChannelHandlerTest>();
  272. m_provider.Subscribe(test.component);
  273. _mDelegate.RaiseOnAddChannel(connectionId, channel);
  274. Assert.That(test.component.IsTestFinished, Is.True);
  275. Assert.That(test.component.Data.connectionId, Is.EqualTo(connectionId));
  276. Assert.That(test.component.Data.channel, Is.EqualTo(channel));
  277. UnityEngine.Object.DestroyImmediate(test.gameObject);
  278. }
  279. }
  280. }