changeset 507:f6b8300e8234

tests (calls): add tests for the "Calls" class: rel 425
author Goffi <goffi@goffi.org>
date Wed, 25 Oct 2023 15:29:12 +0200
parents 0480f883f0a6
children d87b9a6b0b69
files tests/unit/test_plugin_calls.py
diffstat 1 files changed, 130 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/tests/unit/test_plugin_calls.py	Wed Oct 25 15:28:44 2023 +0200
+++ b/tests/unit/test_plugin_calls.py	Wed Oct 25 15:29:12 2023 +0200
@@ -31,6 +31,7 @@
 def host(monkeypatch):
     host = MagicMock()
     host.a_bridge = AsyncMock()
+    host.app.expand = lambda s: s
     monkeypatch.setattr(G, "_host", host, raising=False)
     return host
 
@@ -54,6 +55,31 @@
     return instance
 
 
+@pytest.fixture
+def calls(monkeypatch, host):
+    """Fixture for Call UI instantiation."""
+    for attr in ("header_box", "local_video", "remote_video", "screen_manager"):
+        monkeypatch.setattr(
+            plugin_wid_calls.Calls,
+            attr,
+            MagicMock()
+        )
+    calls = plugin_wid_calls.Calls(
+        host,
+        "test_peer@example.org",
+        ["test_profile"]
+    )
+    calls.jid_selector = MagicMock()
+    calls.header_input = MagicMock()
+    calls.header_input.text = "fake_jid@domain"
+    calls.webrtc = MagicMock()
+    calls.webrtc.setup_call = AsyncMock()
+    calls.webrtc.start_pipeline = MagicMock()
+    calls.end_call = AsyncMock()
+
+    return calls
+
+
 class TestWebRtc:
     def test_get_payload_types(self, webrtc):
         """The method can identify the correct payload types for video and audio."""
@@ -241,3 +267,107 @@
         monkeypatch.setattr(Gst, "parse_launch", lambda _: None)
         with pytest.raises(exceptions.InternalError):
             await webrtc.setup_call("initiator")
+
+
+class TestCalls:
+
+    @pytest.mark.asyncio
+    async def test_toggle_call_sid_none(self, monkeypatch, calls):
+        """Call is started when there is not sid set."""
+        monkeypatch.setattr(calls.webrtc, "sid", None)
+
+        await calls.toggle_call()
+
+        calls.webrtc.setup_call.assert_called_once_with("initiator")
+        calls.webrtc.start_pipeline.assert_called_once()
+        assert calls.in_call == True
+
+    @pytest.mark.asyncio
+    async def test_toggle_call_sid_set(self, monkeypatch, host, calls):
+        """Call is ended when a sid is set"""
+        monkeypatch.setattr(calls.webrtc, "sid", "test_sid")
+
+        await calls.toggle_call()
+
+        calls.end_call.assert_called_once_with({"reason": "terminated"}, calls.profile)
+        host.a_bridge.call_end.assert_called_once_with("test_sid", "", calls.profile)
+        assert calls.in_call == False
+
+
+    @pytest.mark.asyncio
+    async def test_on_incoming_call_sid_none(self, monkeypatch, host, calls):
+        """Incoming call is accepted if no ongoing call."""
+        monkeypatch.setattr(calls.webrtc, "sid", None)
+        fake_action_id = "fake_action_id"
+        fake_action_data = {"session_id": "test_sid"}
+        fake_profile = "fake_profile"
+
+        await calls.on_incoming_call(fake_action_data, fake_action_id, fake_profile)
+
+        assert calls.in_call == True
+        assert calls.webrtc.sid == "test_sid"
+        host.a_bridge.action_launch.assert_called_once_with(
+            fake_action_id,
+            data_format.serialise({"cancelled": False}),
+            fake_profile
+        )
+
+    @pytest.mark.asyncio
+    async def test_on_incoming_call_sid_set(self, monkeypatch, host, calls):
+        """Incoming call is ignored if there's an ongoing call."""
+        monkeypatch.setattr(calls.webrtc, "sid", "fake_old_sid")
+        fake_action_id = "fake_action_id"
+        fake_action_data = {"session_id": "test_sid_new"}
+        fake_profile = "fake_profile"
+
+        await calls.on_incoming_call(fake_action_data, fake_action_id, fake_profile)
+
+        # Ensuring the state hasn't been changed to True
+        assert calls.in_call == False
+        host.a_bridge.action_launch.assert_not_called()
+
+    @pytest.mark.asyncio
+    async def test_on_call_setup_initiator(self, calls):
+        """Correct method called if role is 'initiator'."""
+        setup_data = {
+            "role": "initiator",
+            "sdp": "fake_sdp"
+        }
+        profile = "fake_profile"
+
+        await calls.on_call_setup(setup_data, profile)
+
+        calls.webrtc.on_accepted_call.assert_called_once_with(setup_data["sdp"], profile)
+
+    @pytest.mark.asyncio
+    async def test_on_call_setup_responder(self, monkeypatch, calls):
+        """Correct method called if role is 'responder'."""
+        monkeypatch.setattr(
+            calls.webrtc,
+            "answer_call",
+            AsyncMock()
+        )
+        setup_data = {
+            "role": "responder",
+            "sdp": "fake_sdp"
+        }
+        profile = "fake_profile"
+
+        await calls.on_call_setup(setup_data, profile)
+
+        calls.webrtc.answer_call.assert_called_once_with(setup_data["sdp"], profile)
+        calls.webrtc.on_accepted_call.assert_not_called()
+
+    @pytest.mark.asyncio
+    async def test_on_call_setup_invalid_role(self, calls):
+        """Nothing is called if role is neither 'initiator' nor 'responder'."""
+        setup_data = {
+            "role": "invalid_role",
+            "sdp": "fake_sdp"
+        }
+        profile = "fake_profile"
+
+        await calls.on_call_setup(setup_data, profile)
+
+        calls.webrtc.answer_call.assert_not_called()
+        calls.webrtc.on_accepted_call.assert_not_called()