annotate tests/unit/frontends/test_webrtc.py @ 4184:50c919dfe61b

plugin XEP-0050: small code quality improvements + add `C.ENTITY_ADMINS` magic key to allow administrators profiles
author Goffi <goffi@goffi.org>
date Sun, 10 Dec 2023 15:08:08 +0100
parents 13dd5660c28f
children dfccc90cacc6
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4140
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/env python3
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
2
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
3 # Libervia: an XMPP client
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
4 # Copyright (C) 2009-2023 Jérôme Poisson (goffi@goffi.org)
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
5
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
6 # This program is free software: you can redistribute it and/or modify
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
7 # it under the terms of the GNU Affero General Public License as published by
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
8 # the Free Software Foundation, either version 3 of the License, or
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
9 # (at your option) any later version.
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
10
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
11 # This program is distributed in the hope that it will be useful,
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
14 # GNU Affero General Public License for more details.
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
15
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
16 # You should have received a copy of the GNU Affero General Public License
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
18 from unittest.mock import AsyncMock, MagicMock
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
19
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
20 from gi.repository import Gst
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
21 import pytest
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
22
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
23 from libervia.backend.core import exceptions
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
24 from libervia.backend.tools.common import data_format
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
25 from libervia.frontends.tools import webrtc as webrtc_mod
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
26
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
27
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
28
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
29 @pytest.fixture
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
30 def host(monkeypatch):
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
31 host = MagicMock()
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
32 host.bridge = AsyncMock()
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
33 host.app.expand = lambda s: s
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
34 return host
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
35
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
36 @pytest.fixture(scope="function")
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
37 def webrtc(host):
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
38 """Fixture for WebRTC instantiation."""
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
39 profile = "test_profile"
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
40 instance = webrtc_mod.WebRTC(host.bridge, profile)
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
41
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
42 instance._set_media_types = MagicMock()
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
43 instance.start_pipeline = MagicMock()
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
44 instance.link_element_or_pad = MagicMock()
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
45 instance.webrtcbin = MagicMock()
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
46 instance.webrtcbin.emit = MagicMock()
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
47
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
48 instance.GstSdp_SDPMessage_new_from_text = MagicMock()
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
49 instance.GstWebRTC_WebRTCSessionDescription_new = MagicMock()
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
50 instance.Gst_Promise_new_with_change_func = MagicMock()
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
51
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
52 return instance
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
53
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
54
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
55 class TestWebRtc:
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
56 def test_get_payload_types(self, webrtc):
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
57 """The method can identify the correct payload types for video and audio."""
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
58 fake_sdpmsg = MagicMock()
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
59 fake_media = MagicMock()
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
60 fake_caps = MagicMock()
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
61 fake_structure = MagicMock()
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
62
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
63 # This side effect will return 'fake_video_encoding' first, then
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
64 # 'fake_audio_encoding'.
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
65 fake_structure.__getitem__.side_effect = [
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
66 "fake_video_encoding",
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
67 "fake_audio_encoding",
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
68 ]
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
69 fake_caps.get_structure.return_value = fake_structure
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
70 fake_media.get_format.side_effect = ["webrtc-datachannel", "10", "20"]
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
71 fake_media.get_caps_from_media.return_value = fake_caps
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
72 fake_sdpmsg.get_media.return_value = fake_media
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
73 fake_sdpmsg.medias_len.return_value = 1
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
74 fake_media.formats_len.return_value = 3
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
75
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
76 result = webrtc.get_payload_types(
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
77 fake_sdpmsg, "fake_video_encoding", "fake_audio_encoding"
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
78 )
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
79 expected_result = {"fake_video_encoding": 10, "fake_audio_encoding": 20}
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
80
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
81 assert result == expected_result
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
82
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
83 def test_on_accepted_call(self, webrtc):
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
84 """The method correctly sets the remote SDP upon acceptance of an outgoing call."""
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
85 sdp_str = "mock_sdp_string"
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
86 profile_str = "test_profile"
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
87
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
88 webrtc.on_accepted_call(sdp_str, profile_str)
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
89
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
90 # remote description must be set
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
91 assert webrtc.webrtcbin.emit.call_count == 1
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
92 assert webrtc.webrtcbin.emit.call_args[0][0] == "set-remote-description"
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
93
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
94 @pytest.mark.asyncio
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
95 async def test_answer_call(self, webrtc, monkeypatch):
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
96 """The method correctly answers an incoming call."""
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
97 mock_setup_call = AsyncMock()
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
98
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
99 def mock_get_payload_types(sdpmsg, video_encoding, audio_encoding):
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
100 return {"VP8": 96, "OPUS": 97}
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
101
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
102 monkeypatch.setattr(webrtc, "setup_call", mock_setup_call)
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
103 monkeypatch.setattr(webrtc, "get_payload_types", mock_get_payload_types)
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
104
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
105 sdp_str = "mock_sdp_string"
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
106 profile_str = "mock_profile"
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
107
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
108 await webrtc.answer_call(sdp_str, profile_str)
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
109
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
110 mock_setup_call.assert_called_once_with("responder", audio_pt=97, video_pt=96)
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
111
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
112 # remote description must be set
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
113 assert webrtc.webrtcbin.emit.call_count == 1
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
114 assert webrtc.webrtcbin.emit.call_args[0][0] == "set-remote-description"
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
115
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
116 def test_on_remote_decodebin_stream_video(self, webrtc, monkeypatch):
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
117 """The method correctly handles video streams from the remote decodebin."""
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
118 mock_pipeline = MagicMock()
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
119 monkeypatch.setattr(webrtc, "pipeline", mock_pipeline)
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
120
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
121 mock_pad = MagicMock()
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
122 mock_caps = MagicMock()
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
123 mock_structure = MagicMock()
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
124
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
125 mock_pad.has_current_caps.return_value = True
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
126 mock_pad.get_current_caps.return_value = mock_caps
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
127 mock_caps.__len__.return_value = 1
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
128 mock_caps.__getitem__.return_value = mock_structure
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
129 mock_structure.get_name.return_value = "video/x-h264"
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
130 # We use non-standard resolution as example to trigger the workaround
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
131 mock_structure.get_int.side_effect = lambda x: MagicMock(
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
132 value=990 if x == "width" else 557
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
133 )
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
134
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
135 webrtc.on_remote_decodebin_stream(None, mock_pad)
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
136
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
137 assert webrtc._remote_video_pad == mock_pad
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
138 mock_pipeline.add.assert_called()
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
139 mock_pad.link.assert_called()
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
140
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
141 def test_on_remote_decodebin_stream_audio(self, webrtc, monkeypatch):
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
142 """The method correctly handles audio streams from the remote decodebin."""
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
143 mock_pipeline = MagicMock()
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
144 monkeypatch.setattr(webrtc, "pipeline", mock_pipeline)
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
145
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
146 mock_pad = MagicMock()
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
147 mock_caps = MagicMock()
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
148 mock_structure = MagicMock()
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
149
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
150 mock_pad.has_current_caps.return_value = True
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
151 mock_pad.get_current_caps.return_value = mock_caps
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
152 mock_caps.__len__.return_value = 1
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
153 mock_caps.__getitem__.return_value = mock_structure
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
154 mock_structure.get_name.return_value = "audio/x-raw"
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
155
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
156 webrtc.on_remote_decodebin_stream(None, mock_pad)
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
157
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
158 mock_pipeline.add.assert_called()
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
159 mock_pad.link.assert_called()
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
160
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
161 @pytest.mark.asyncio
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
162 async def test_setup_call_correct_role(self, host, webrtc, monkeypatch):
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
163 """Roles are set in setup_call."""
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
164 monkeypatch.setattr(Gst, "parse_launch", MagicMock())
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
165 monkeypatch.setattr(data_format, "deserialise", MagicMock(return_value=[]))
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
166
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
167 await webrtc.setup_call("initiator")
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
168 assert webrtc.role == "initiator"
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
169
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
170 await webrtc.setup_call("responder")
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
171 assert webrtc.role == "responder"
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
172
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
173 with pytest.raises(AssertionError):
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
174 await webrtc.setup_call("invalid_role")
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
175
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
176 @pytest.mark.asyncio
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
177 async def test_setup_call_test_mode(self, host, webrtc, monkeypatch):
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
178 """Test mode use fake video and audio in setup_call."""
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
179 monkeypatch.setattr(data_format, "deserialise", MagicMock(return_value=[]))
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
180 monkeypatch.setattr(webrtc, "sources", webrtc_mod.SINKS_TEST)
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
181 await webrtc.setup_call("initiator")
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
182 assert "videotestsrc" in webrtc.gst_pipe_desc
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
183 assert "audiotestsrc" in webrtc.gst_pipe_desc
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
184
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
185 @pytest.mark.asyncio
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
186 async def test_setup_call_normal_mode(self, host, webrtc, monkeypatch):
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
187 """Normal mode use real video and audio in setup_call."""
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
188 monkeypatch.setattr(data_format, "deserialise", MagicMock(return_value=[]))
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
189 monkeypatch.setattr(webrtc, "sources", webrtc_mod.SOURCES_AUTO)
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
190 await webrtc.setup_call("initiator")
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
191 assert "v4l2src" in webrtc.gst_pipe_desc
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
192 assert "pulsesrc" in webrtc.gst_pipe_desc
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
193
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
194 @pytest.mark.asyncio
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
195 async def test_setup_call_with_stun_and_turn(self, host, webrtc, monkeypatch):
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
196 """STUN and TURN server configurations are done in setup_call."""
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
197 mock_pipeline = MagicMock()
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
198 mock_parse_launch = MagicMock()
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
199 mock_parse_launch.return_value = mock_pipeline
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
200 monkeypatch.setattr(Gst, "parse_launch", mock_parse_launch)
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
201
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
202 mock_pipeline.get_by_name.return_value = webrtc.webrtcbin
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
203
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
204 mock_external_disco = [
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
205 {"type": "stun", "transport": "udp", "host": "stun.host", "port": "3478"},
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
206 {
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
207 "type": "turn",
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
208 "transport": "udp",
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
209 "host": "turn.host",
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
210 "port": "3478",
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
211 "username": "user",
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
212 "password": "pass",
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
213 },
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
214 ]
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
215
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
216 monkeypatch.setattr(
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
217 data_format, "deserialise", MagicMock(return_value=mock_external_disco)
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
218 )
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
219
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
220 mock_emit = AsyncMock()
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
221 monkeypatch.setattr(webrtc.webrtcbin, "emit", mock_emit)
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
222
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
223 mock_set_property = AsyncMock()
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
224 monkeypatch.setattr(webrtc.webrtcbin, "set_property", mock_set_property)
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
225
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
226 await webrtc.setup_call("initiator")
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
227
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
228 host.bridge.external_disco_get.assert_called_once_with("", webrtc.profile)
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
229 mock_set_property.assert_any_call("stun-server", "stun://stun.host:3478")
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
230 mock_emit.assert_called_once_with(
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
231 "add-turn-server", "turn://user:pass@turn.host:3478"
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
232 )
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
233
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
234 @pytest.mark.asyncio
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
235 async def test_setup_call_gstreamer_pipeline_failure(self, webrtc, monkeypatch):
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
236 """Test setup_call method handling Gstreamer pipeline failure."""
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
237 monkeypatch.setattr(Gst, "parse_launch", lambda _: None)
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
238 with pytest.raises(exceptions.InternalError):
13dd5660c28f tests (unit/frontends): tests for webrtc implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
239 await webrtc.setup_call("initiator")