using System; using UnityEngine; using NUnit.Framework; using Unity.WebRTC; using UnityEngine.EventSystems; using UnityEngine.TestTools; namespace Unity.RenderStreaming.RuntimeTest { class CreatedConnectionHandlerTest : MonoBehaviour, IMonoBehaviourTest, ICreatedConnectionHandler { public bool IsTestFinished { get; private set; } public SignalingEventData Data { get; private set; } public void OnCreatedConnection(SignalingEventData data) { IsTestFinished = true; this.Data = data; } } class DeletedConnectionHandlerTest : MonoBehaviour, IMonoBehaviourTest, IDeletedConnectionHandler { public bool IsTestFinished { get; private set; } public SignalingEventData Data { get; private set; } public void OnDeletedConnection(SignalingEventData data) { IsTestFinished = true; this.Data = data; } } class ConnectHandlerTest : MonoBehaviour, IMonoBehaviourTest, IConnectHandler { public bool IsTestFinished { get; private set; } public SignalingEventData Data { get; private set; } public void OnConnect(SignalingEventData data) { IsTestFinished = true; this.Data = data; } } class DisconnectHandlerTest : MonoBehaviour, IMonoBehaviourTest, IDisconnectHandler { public bool IsTestFinished { get; private set; } public SignalingEventData Data { get; private set; } public void OnDisconnect(SignalingEventData data) { IsTestFinished = true; this.Data = data; } } class OfferHandlerTest : MonoBehaviour, IMonoBehaviourTest, IOfferHandler { public bool IsTestFinished { get; private set; } public SignalingEventData Data { get; private set; } public void OnOffer(SignalingEventData data) { IsTestFinished = true; this.Data = data; } } class AnswerHandlerTest : MonoBehaviour, IMonoBehaviourTest, IAnswerHandler { public bool IsTestFinished { get; private set; } public SignalingEventData Data { get; private set; } public void OnAnswer(SignalingEventData data) { IsTestFinished = true; this.Data = data; } } class AddReceiverHandlerTest : MonoBehaviour, IMonoBehaviourTest, IAddReceiverHandler { public bool IsTestFinished { get; private set; } public SignalingEventData Data { get; private set; } public void OnAddReceiver(SignalingEventData data) { IsTestFinished = true; this.Data = data; } } class AddChannelHandlerTest : MonoBehaviour, IMonoBehaviourTest, IAddChannelHandler { public bool IsTestFinished { get; private set; } public SignalingEventData Data { get; private set; } public void OnAddChannel(SignalingEventData data) { IsTestFinished = true; this.Data = data; } } /// /// note:: Moq is not supported IL2CPP platform, /// this class should be replaced Moq `Raise` method. /// class MockDelegate : IRenderStreamingDelegate { public event Action onStart; public event Action onCreatedConnection; public event Action onDeletedConnection; public event Action onGotOffer; public event Action onGotAnswer; public event Action onConnect; public event Action onDisconnect; public event Action onAddTransceiver; public event Action onAddChannel; public void RaiseOnStart() { onStart?.Invoke(); } public void RaiseOnCreatedConnection(string connectionId) { onCreatedConnection?.Invoke(connectionId); } public void RaiseOnDeletedConnection(string connectionId) { onDeletedConnection?.Invoke(connectionId); } public void RaiseOnGotOffer(string connectionId, string sdp) { onGotOffer?.Invoke(connectionId, sdp); } public void RaiseOnGotAnswer(string connectionId, string sdp) { onGotAnswer?.Invoke(connectionId, sdp); } public void RaiseOnConnect(string connectionId) { onConnect?.Invoke(connectionId); } public void RaiseOnDisconnect(string connectionId) { onDisconnect?.Invoke(connectionId); } public void RaiseOnAddTransceiver(string connectionId, RTCRtpTransceiver transceiver) { onAddTransceiver?.Invoke(connectionId, transceiver); } public void RaiseOnAddChannel(string connectionId, RTCDataChannel channel) { onAddChannel?.Invoke(connectionId, channel); } } [UnityPlatform(exclude = new[] { RuntimePlatform.OSXEditor, RuntimePlatform.OSXPlayer, RuntimePlatform.LinuxEditor, RuntimePlatform.LinuxPlayer })] class SignalingEventProviderTest { private EventSystem m_eventSystem; private SignalingEventProvider m_provider; private MockDelegate _mDelegate; [OneTimeSetUp] public void OneTimeSetUp() { m_eventSystem = new GameObject("EventSystem").AddComponent(); } [OneTimeTearDown] public void OneTimeTearDown() { UnityEngine.Object.DestroyImmediate(m_eventSystem); } [SetUp] public void SetUp() { _mDelegate = new MockDelegate(); m_provider = new SignalingEventProvider(_mDelegate); } [Test] public void Subscribe() { var test = new MonoBehaviourTest(); Assert.That(m_provider.Subscribe(test.component), Is.True); // return false if it is already registered. Assert.That(m_provider.Subscribe(test.component), Is.False); Assert.That(m_provider.Unsubscribe(test.component), Is.True); // return false if it is not found. Assert.That(m_provider.Unsubscribe(test.component), Is.False); } [Test] public void OnCreatedConnection() { var connectionId = "12345"; var test = new MonoBehaviourTest(); m_provider.Subscribe(test.component); _mDelegate.RaiseOnCreatedConnection(connectionId); Assert.That(test.component.IsTestFinished, Is.True); Assert.That(test.component.Data.connectionId, Is.EqualTo(connectionId)); UnityEngine.Object.DestroyImmediate(test.gameObject); } [Test] public void OnDeletedConnection() { var connectionId = "12345"; var test = new MonoBehaviourTest(); m_provider.Subscribe(test.component); _mDelegate.RaiseOnDeletedConnection(connectionId); Assert.That(test.component.IsTestFinished, Is.True); Assert.That(test.component.Data.connectionId, Is.EqualTo(connectionId)); UnityEngine.Object.DestroyImmediate(test.gameObject); } [Test] public void OnConnect() { var connectionId = "12345"; var test = new MonoBehaviourTest(); m_provider.Subscribe(test.component); _mDelegate.RaiseOnConnect(connectionId); Assert.That(test.component.IsTestFinished, Is.True); Assert.That(test.component.Data.connectionId, Is.EqualTo(connectionId)); UnityEngine.Object.DestroyImmediate(test.gameObject); } [Test] public void OnDisconnect() { var connectionId = "12345"; var test = new MonoBehaviourTest(); m_provider.Subscribe(test.component); _mDelegate.RaiseOnDisconnect(connectionId); Assert.That(test.component.IsTestFinished, Is.True); Assert.That(test.component.Data.connectionId, Is.EqualTo(connectionId)); UnityEngine.Object.DestroyImmediate(test.gameObject); } [Test] public void OnOffer() { var connectionId = "12345"; var sdp = "this is sdp"; var test = new MonoBehaviourTest(); m_provider.Subscribe(test.component); _mDelegate.RaiseOnGotOffer(connectionId, sdp); Assert.That(test.component.IsTestFinished, Is.True); Assert.That(test.component.Data.connectionId, Is.EqualTo(connectionId)); Assert.That(test.component.Data.sdp, Is.EqualTo(sdp)); UnityEngine.Object.DestroyImmediate(test.gameObject); } [Test] public void OnAnswer() { var connectionId = "12345"; var sdp = "this is sdp"; var test = new MonoBehaviourTest(); m_provider.Subscribe(test.component); _mDelegate.RaiseOnGotAnswer(connectionId, sdp); Assert.That(test.component.IsTestFinished, Is.True); Assert.That(test.component.Data.connectionId, Is.EqualTo(connectionId)); Assert.That(test.component.Data.sdp, Is.EqualTo(sdp)); UnityEngine.Object.DestroyImmediate(test.gameObject); } [Test] public void OnAddReceiver() { var connectionId = "12345"; // todo:: create a receiver instance var test = new MonoBehaviourTest(); m_provider.Subscribe(test.component); _mDelegate.RaiseOnAddTransceiver(connectionId, null); Assert.That(test.component.IsTestFinished, Is.True); Assert.That(test.component.Data.connectionId, Is.EqualTo(connectionId)); Assert.That(test.component.Data.transceiver, Is.Null); UnityEngine.Object.DestroyImmediate(test.gameObject); } [Test] public void OnAddChannel() { var connectionId = "12345"; var peer = new RTCPeerConnection(); var channel = peer.CreateDataChannel("test"); var test = new MonoBehaviourTest(); m_provider.Subscribe(test.component); _mDelegate.RaiseOnAddChannel(connectionId, channel); Assert.That(test.component.IsTestFinished, Is.True); Assert.That(test.component.Data.connectionId, Is.EqualTo(connectionId)); Assert.That(test.component.Data.channel, Is.EqualTo(channel)); UnityEngine.Object.DestroyImmediate(test.gameObject); } } }