Mercurial > libervia-desktop-kivy
annotate tests/unit/test_plugin_calls.py @ 509:f0ce49b360c8
calls: move webrtc code to core:
WebRTC code which can be used in several frontends has been factorized and moved to
common `frontends.tools`. Test have been updated consequently.
rel 426
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 01 Nov 2023 13:41:07 +0100 |
parents | f6b8300e8234 |
children |
rev | line source |
---|---|
500 | 1 #!/usr/bin/env python3 |
2 | |
3 # Libervia: an XMPP client | |
4 # Copyright (C) 2009-2023 Jérôme Poisson (goffi@goffi.org) | |
5 | |
6 # This program is free software: you can redistribute it and/or modify | |
7 # it under the terms of the GNU Affero General Public License as published by | |
8 # the Free Software Foundation, either version 3 of the License, or | |
9 # (at your option) any later version. | |
10 | |
11 # This program is distributed in the hope that it will be useful, | |
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 # GNU Affero General Public License for more details. | |
15 | |
16 # You should have received a copy of the GNU Affero General Public License | |
17 # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
18 | |
19 from unittest.mock import AsyncMock, MagicMock | |
20 | |
21 from libervia.backend.tools.common import data_format | |
22 import pytest | |
23 | |
24 from libervia.desktop_kivy import G | |
25 from libervia.desktop_kivy.plugins import plugin_wid_calls | |
26 | |
27 | |
28 @pytest.fixture | |
29 def host(monkeypatch): | |
30 host = MagicMock() | |
31 host.a_bridge = AsyncMock() | |
507
f6b8300e8234
tests (calls): add tests for the "Calls" class:
Goffi <goffi@goffi.org>
parents:
500
diff
changeset
|
32 host.app.expand = lambda s: s |
500 | 33 monkeypatch.setattr(G, "_host", host, raising=False) |
34 return host | |
35 | |
36 | |
507
f6b8300e8234
tests (calls): add tests for the "Calls" class:
Goffi <goffi@goffi.org>
parents:
500
diff
changeset
|
37 @pytest.fixture |
f6b8300e8234
tests (calls): add tests for the "Calls" class:
Goffi <goffi@goffi.org>
parents:
500
diff
changeset
|
38 def calls(monkeypatch, host): |
f6b8300e8234
tests (calls): add tests for the "Calls" class:
Goffi <goffi@goffi.org>
parents:
500
diff
changeset
|
39 """Fixture for Call UI instantiation.""" |
f6b8300e8234
tests (calls): add tests for the "Calls" class:
Goffi <goffi@goffi.org>
parents:
500
diff
changeset
|
40 for attr in ("header_box", "local_video", "remote_video", "screen_manager"): |
509 | 41 monkeypatch.setattr(plugin_wid_calls.Calls, attr, MagicMock()) |
42 calls = plugin_wid_calls.Calls(host, "test_peer@example.org", ["test_profile"]) | |
507
f6b8300e8234
tests (calls): add tests for the "Calls" class:
Goffi <goffi@goffi.org>
parents:
500
diff
changeset
|
43 calls.jid_selector = MagicMock() |
f6b8300e8234
tests (calls): add tests for the "Calls" class:
Goffi <goffi@goffi.org>
parents:
500
diff
changeset
|
44 calls.header_input = MagicMock() |
f6b8300e8234
tests (calls): add tests for the "Calls" class:
Goffi <goffi@goffi.org>
parents:
500
diff
changeset
|
45 calls.header_input.text = "fake_jid@domain" |
f6b8300e8234
tests (calls): add tests for the "Calls" class:
Goffi <goffi@goffi.org>
parents:
500
diff
changeset
|
46 calls.webrtc = MagicMock() |
f6b8300e8234
tests (calls): add tests for the "Calls" class:
Goffi <goffi@goffi.org>
parents:
500
diff
changeset
|
47 calls.webrtc.setup_call = AsyncMock() |
f6b8300e8234
tests (calls): add tests for the "Calls" class:
Goffi <goffi@goffi.org>
parents:
500
diff
changeset
|
48 calls.webrtc.start_pipeline = MagicMock() |
f6b8300e8234
tests (calls): add tests for the "Calls" class:
Goffi <goffi@goffi.org>
parents:
500
diff
changeset
|
49 calls.end_call = AsyncMock() |
f6b8300e8234
tests (calls): add tests for the "Calls" class:
Goffi <goffi@goffi.org>
parents:
500
diff
changeset
|
50 |
f6b8300e8234
tests (calls): add tests for the "Calls" class:
Goffi <goffi@goffi.org>
parents:
500
diff
changeset
|
51 return calls |
f6b8300e8234
tests (calls): add tests for the "Calls" class:
Goffi <goffi@goffi.org>
parents:
500
diff
changeset
|
52 |
f6b8300e8234
tests (calls): add tests for the "Calls" class:
Goffi <goffi@goffi.org>
parents:
500
diff
changeset
|
53 |
f6b8300e8234
tests (calls): add tests for the "Calls" class:
Goffi <goffi@goffi.org>
parents:
500
diff
changeset
|
54 class TestCalls: |
f6b8300e8234
tests (calls): add tests for the "Calls" class:
Goffi <goffi@goffi.org>
parents:
500
diff
changeset
|
55 @pytest.mark.asyncio |
f6b8300e8234
tests (calls): add tests for the "Calls" class:
Goffi <goffi@goffi.org>
parents:
500
diff
changeset
|
56 async def test_toggle_call_sid_none(self, monkeypatch, calls): |
f6b8300e8234
tests (calls): add tests for the "Calls" class:
Goffi <goffi@goffi.org>
parents:
500
diff
changeset
|
57 """Call is started when there is not sid set.""" |
f6b8300e8234
tests (calls): add tests for the "Calls" class:
Goffi <goffi@goffi.org>
parents:
500
diff
changeset
|
58 monkeypatch.setattr(calls.webrtc, "sid", None) |
f6b8300e8234
tests (calls): add tests for the "Calls" class:
Goffi <goffi@goffi.org>
parents:
500
diff
changeset
|
59 |
f6b8300e8234
tests (calls): add tests for the "Calls" class:
Goffi <goffi@goffi.org>
parents:
500
diff
changeset
|
60 await calls.toggle_call() |
f6b8300e8234
tests (calls): add tests for the "Calls" class:
Goffi <goffi@goffi.org>
parents:
500
diff
changeset
|
61 |
f6b8300e8234
tests (calls): add tests for the "Calls" class:
Goffi <goffi@goffi.org>
parents:
500
diff
changeset
|
62 calls.webrtc.setup_call.assert_called_once_with("initiator") |
f6b8300e8234
tests (calls): add tests for the "Calls" class:
Goffi <goffi@goffi.org>
parents:
500
diff
changeset
|
63 calls.webrtc.start_pipeline.assert_called_once() |
f6b8300e8234
tests (calls): add tests for the "Calls" class:
Goffi <goffi@goffi.org>
parents:
500
diff
changeset
|
64 assert calls.in_call == True |
f6b8300e8234
tests (calls): add tests for the "Calls" class:
Goffi <goffi@goffi.org>
parents:
500
diff
changeset
|
65 |
f6b8300e8234
tests (calls): add tests for the "Calls" class:
Goffi <goffi@goffi.org>
parents:
500
diff
changeset
|
66 @pytest.mark.asyncio |
f6b8300e8234
tests (calls): add tests for the "Calls" class:
Goffi <goffi@goffi.org>
parents:
500
diff
changeset
|
67 async def test_toggle_call_sid_set(self, monkeypatch, host, calls): |
f6b8300e8234
tests (calls): add tests for the "Calls" class:
Goffi <goffi@goffi.org>
parents:
500
diff
changeset
|
68 """Call is ended when a sid is set""" |
f6b8300e8234
tests (calls): add tests for the "Calls" class:
Goffi <goffi@goffi.org>
parents:
500
diff
changeset
|
69 monkeypatch.setattr(calls.webrtc, "sid", "test_sid") |
f6b8300e8234
tests (calls): add tests for the "Calls" class:
Goffi <goffi@goffi.org>
parents:
500
diff
changeset
|
70 |
f6b8300e8234
tests (calls): add tests for the "Calls" class:
Goffi <goffi@goffi.org>
parents:
500
diff
changeset
|
71 await calls.toggle_call() |
f6b8300e8234
tests (calls): add tests for the "Calls" class:
Goffi <goffi@goffi.org>
parents:
500
diff
changeset
|
72 |
f6b8300e8234
tests (calls): add tests for the "Calls" class:
Goffi <goffi@goffi.org>
parents:
500
diff
changeset
|
73 calls.end_call.assert_called_once_with({"reason": "terminated"}, calls.profile) |
f6b8300e8234
tests (calls): add tests for the "Calls" class:
Goffi <goffi@goffi.org>
parents:
500
diff
changeset
|
74 host.a_bridge.call_end.assert_called_once_with("test_sid", "", calls.profile) |
f6b8300e8234
tests (calls): add tests for the "Calls" class:
Goffi <goffi@goffi.org>
parents:
500
diff
changeset
|
75 assert calls.in_call == False |
f6b8300e8234
tests (calls): add tests for the "Calls" class:
Goffi <goffi@goffi.org>
parents:
500
diff
changeset
|
76 |
f6b8300e8234
tests (calls): add tests for the "Calls" class:
Goffi <goffi@goffi.org>
parents:
500
diff
changeset
|
77 @pytest.mark.asyncio |
f6b8300e8234
tests (calls): add tests for the "Calls" class:
Goffi <goffi@goffi.org>
parents:
500
diff
changeset
|
78 async def test_on_incoming_call_sid_none(self, monkeypatch, host, calls): |
f6b8300e8234
tests (calls): add tests for the "Calls" class:
Goffi <goffi@goffi.org>
parents:
500
diff
changeset
|
79 """Incoming call is accepted if no ongoing call.""" |
f6b8300e8234
tests (calls): add tests for the "Calls" class:
Goffi <goffi@goffi.org>
parents:
500
diff
changeset
|
80 monkeypatch.setattr(calls.webrtc, "sid", None) |
f6b8300e8234
tests (calls): add tests for the "Calls" class:
Goffi <goffi@goffi.org>
parents:
500
diff
changeset
|
81 fake_action_id = "fake_action_id" |
f6b8300e8234
tests (calls): add tests for the "Calls" class:
Goffi <goffi@goffi.org>
parents:
500
diff
changeset
|
82 fake_action_data = {"session_id": "test_sid"} |
f6b8300e8234
tests (calls): add tests for the "Calls" class:
Goffi <goffi@goffi.org>
parents:
500
diff
changeset
|
83 fake_profile = "fake_profile" |
f6b8300e8234
tests (calls): add tests for the "Calls" class:
Goffi <goffi@goffi.org>
parents:
500
diff
changeset
|
84 |
f6b8300e8234
tests (calls): add tests for the "Calls" class:
Goffi <goffi@goffi.org>
parents:
500
diff
changeset
|
85 await calls.on_incoming_call(fake_action_data, fake_action_id, fake_profile) |
f6b8300e8234
tests (calls): add tests for the "Calls" class:
Goffi <goffi@goffi.org>
parents:
500
diff
changeset
|
86 |
f6b8300e8234
tests (calls): add tests for the "Calls" class:
Goffi <goffi@goffi.org>
parents:
500
diff
changeset
|
87 assert calls.in_call == True |
f6b8300e8234
tests (calls): add tests for the "Calls" class:
Goffi <goffi@goffi.org>
parents:
500
diff
changeset
|
88 assert calls.webrtc.sid == "test_sid" |
f6b8300e8234
tests (calls): add tests for the "Calls" class:
Goffi <goffi@goffi.org>
parents:
500
diff
changeset
|
89 host.a_bridge.action_launch.assert_called_once_with( |
509 | 90 fake_action_id, data_format.serialise({"cancelled": False}), fake_profile |
507
f6b8300e8234
tests (calls): add tests for the "Calls" class:
Goffi <goffi@goffi.org>
parents:
500
diff
changeset
|
91 ) |
f6b8300e8234
tests (calls): add tests for the "Calls" class:
Goffi <goffi@goffi.org>
parents:
500
diff
changeset
|
92 |
f6b8300e8234
tests (calls): add tests for the "Calls" class:
Goffi <goffi@goffi.org>
parents:
500
diff
changeset
|
93 @pytest.mark.asyncio |
f6b8300e8234
tests (calls): add tests for the "Calls" class:
Goffi <goffi@goffi.org>
parents:
500
diff
changeset
|
94 async def test_on_incoming_call_sid_set(self, monkeypatch, host, calls): |
f6b8300e8234
tests (calls): add tests for the "Calls" class:
Goffi <goffi@goffi.org>
parents:
500
diff
changeset
|
95 """Incoming call is ignored if there's an ongoing call.""" |
f6b8300e8234
tests (calls): add tests for the "Calls" class:
Goffi <goffi@goffi.org>
parents:
500
diff
changeset
|
96 monkeypatch.setattr(calls.webrtc, "sid", "fake_old_sid") |
f6b8300e8234
tests (calls): add tests for the "Calls" class:
Goffi <goffi@goffi.org>
parents:
500
diff
changeset
|
97 fake_action_id = "fake_action_id" |
f6b8300e8234
tests (calls): add tests for the "Calls" class:
Goffi <goffi@goffi.org>
parents:
500
diff
changeset
|
98 fake_action_data = {"session_id": "test_sid_new"} |
f6b8300e8234
tests (calls): add tests for the "Calls" class:
Goffi <goffi@goffi.org>
parents:
500
diff
changeset
|
99 fake_profile = "fake_profile" |
f6b8300e8234
tests (calls): add tests for the "Calls" class:
Goffi <goffi@goffi.org>
parents:
500
diff
changeset
|
100 |
f6b8300e8234
tests (calls): add tests for the "Calls" class:
Goffi <goffi@goffi.org>
parents:
500
diff
changeset
|
101 await calls.on_incoming_call(fake_action_data, fake_action_id, fake_profile) |
f6b8300e8234
tests (calls): add tests for the "Calls" class:
Goffi <goffi@goffi.org>
parents:
500
diff
changeset
|
102 |
f6b8300e8234
tests (calls): add tests for the "Calls" class:
Goffi <goffi@goffi.org>
parents:
500
diff
changeset
|
103 # Ensuring the state hasn't been changed to True |
f6b8300e8234
tests (calls): add tests for the "Calls" class:
Goffi <goffi@goffi.org>
parents:
500
diff
changeset
|
104 assert calls.in_call == False |
f6b8300e8234
tests (calls): add tests for the "Calls" class:
Goffi <goffi@goffi.org>
parents:
500
diff
changeset
|
105 host.a_bridge.action_launch.assert_not_called() |
f6b8300e8234
tests (calls): add tests for the "Calls" class:
Goffi <goffi@goffi.org>
parents:
500
diff
changeset
|
106 |
f6b8300e8234
tests (calls): add tests for the "Calls" class:
Goffi <goffi@goffi.org>
parents:
500
diff
changeset
|
107 @pytest.mark.asyncio |
f6b8300e8234
tests (calls): add tests for the "Calls" class:
Goffi <goffi@goffi.org>
parents:
500
diff
changeset
|
108 async def test_on_call_setup_initiator(self, calls): |
f6b8300e8234
tests (calls): add tests for the "Calls" class:
Goffi <goffi@goffi.org>
parents:
500
diff
changeset
|
109 """Correct method called if role is 'initiator'.""" |
509 | 110 setup_data = {"role": "initiator", "sdp": "fake_sdp"} |
507
f6b8300e8234
tests (calls): add tests for the "Calls" class:
Goffi <goffi@goffi.org>
parents:
500
diff
changeset
|
111 profile = "fake_profile" |
f6b8300e8234
tests (calls): add tests for the "Calls" class:
Goffi <goffi@goffi.org>
parents:
500
diff
changeset
|
112 |
f6b8300e8234
tests (calls): add tests for the "Calls" class:
Goffi <goffi@goffi.org>
parents:
500
diff
changeset
|
113 await calls.on_call_setup(setup_data, profile) |
f6b8300e8234
tests (calls): add tests for the "Calls" class:
Goffi <goffi@goffi.org>
parents:
500
diff
changeset
|
114 |
f6b8300e8234
tests (calls): add tests for the "Calls" class:
Goffi <goffi@goffi.org>
parents:
500
diff
changeset
|
115 calls.webrtc.on_accepted_call.assert_called_once_with(setup_data["sdp"], profile) |
f6b8300e8234
tests (calls): add tests for the "Calls" class:
Goffi <goffi@goffi.org>
parents:
500
diff
changeset
|
116 |
f6b8300e8234
tests (calls): add tests for the "Calls" class:
Goffi <goffi@goffi.org>
parents:
500
diff
changeset
|
117 @pytest.mark.asyncio |
f6b8300e8234
tests (calls): add tests for the "Calls" class:
Goffi <goffi@goffi.org>
parents:
500
diff
changeset
|
118 async def test_on_call_setup_responder(self, monkeypatch, calls): |
f6b8300e8234
tests (calls): add tests for the "Calls" class:
Goffi <goffi@goffi.org>
parents:
500
diff
changeset
|
119 """Correct method called if role is 'responder'.""" |
509 | 120 monkeypatch.setattr(calls.webrtc, "answer_call", AsyncMock()) |
121 setup_data = {"role": "responder", "sdp": "fake_sdp"} | |
507
f6b8300e8234
tests (calls): add tests for the "Calls" class:
Goffi <goffi@goffi.org>
parents:
500
diff
changeset
|
122 profile = "fake_profile" |
f6b8300e8234
tests (calls): add tests for the "Calls" class:
Goffi <goffi@goffi.org>
parents:
500
diff
changeset
|
123 |
f6b8300e8234
tests (calls): add tests for the "Calls" class:
Goffi <goffi@goffi.org>
parents:
500
diff
changeset
|
124 await calls.on_call_setup(setup_data, profile) |
f6b8300e8234
tests (calls): add tests for the "Calls" class:
Goffi <goffi@goffi.org>
parents:
500
diff
changeset
|
125 |
f6b8300e8234
tests (calls): add tests for the "Calls" class:
Goffi <goffi@goffi.org>
parents:
500
diff
changeset
|
126 calls.webrtc.answer_call.assert_called_once_with(setup_data["sdp"], profile) |
f6b8300e8234
tests (calls): add tests for the "Calls" class:
Goffi <goffi@goffi.org>
parents:
500
diff
changeset
|
127 calls.webrtc.on_accepted_call.assert_not_called() |
f6b8300e8234
tests (calls): add tests for the "Calls" class:
Goffi <goffi@goffi.org>
parents:
500
diff
changeset
|
128 |
f6b8300e8234
tests (calls): add tests for the "Calls" class:
Goffi <goffi@goffi.org>
parents:
500
diff
changeset
|
129 @pytest.mark.asyncio |
f6b8300e8234
tests (calls): add tests for the "Calls" class:
Goffi <goffi@goffi.org>
parents:
500
diff
changeset
|
130 async def test_on_call_setup_invalid_role(self, calls): |
f6b8300e8234
tests (calls): add tests for the "Calls" class:
Goffi <goffi@goffi.org>
parents:
500
diff
changeset
|
131 """Nothing is called if role is neither 'initiator' nor 'responder'.""" |
509 | 132 setup_data = {"role": "invalid_role", "sdp": "fake_sdp"} |
507
f6b8300e8234
tests (calls): add tests for the "Calls" class:
Goffi <goffi@goffi.org>
parents:
500
diff
changeset
|
133 profile = "fake_profile" |
f6b8300e8234
tests (calls): add tests for the "Calls" class:
Goffi <goffi@goffi.org>
parents:
500
diff
changeset
|
134 |
f6b8300e8234
tests (calls): add tests for the "Calls" class:
Goffi <goffi@goffi.org>
parents:
500
diff
changeset
|
135 await calls.on_call_setup(setup_data, profile) |
f6b8300e8234
tests (calls): add tests for the "Calls" class:
Goffi <goffi@goffi.org>
parents:
500
diff
changeset
|
136 |
f6b8300e8234
tests (calls): add tests for the "Calls" class:
Goffi <goffi@goffi.org>
parents:
500
diff
changeset
|
137 calls.webrtc.answer_call.assert_not_called() |
f6b8300e8234
tests (calls): add tests for the "Calls" class:
Goffi <goffi@goffi.org>
parents:
500
diff
changeset
|
138 calls.webrtc.on_accepted_call.assert_not_called() |