annotate tests/unit/test_plugin_calls.py @ 514:d78728d7fd6a default tip

plugin wid calls, core: implements WebRTC DataChannel file transfer: - Add a new "file" icon in call UI to send a file via WebRTC. - Handle new preflight mechanism, and WebRTC file transfer. - Native file chooser handling has been moved to new `core.file_chooser` module, and now supports "save" and "dir" modes (based on `plyer`). rel 442
author Goffi <goffi@goffi.org>
date Sat, 06 Apr 2024 13:37:27 +0200
parents f0ce49b360c8
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
500
b0f70be331c5 tests: unit test for "Calls" plugins:
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/env python3
b0f70be331c5 tests: unit test for "Calls" plugins:
Goffi <goffi@goffi.org>
parents:
diff changeset
2
b0f70be331c5 tests: unit test for "Calls" plugins:
Goffi <goffi@goffi.org>
parents:
diff changeset
3 # Libervia: an XMPP client
b0f70be331c5 tests: unit test for "Calls" plugins:
Goffi <goffi@goffi.org>
parents:
diff changeset
4 # Copyright (C) 2009-2023 Jérôme Poisson (goffi@goffi.org)
b0f70be331c5 tests: unit test for "Calls" plugins:
Goffi <goffi@goffi.org>
parents:
diff changeset
5
b0f70be331c5 tests: unit test for "Calls" plugins:
Goffi <goffi@goffi.org>
parents:
diff changeset
6 # This program is free software: you can redistribute it and/or modify
b0f70be331c5 tests: unit test for "Calls" plugins:
Goffi <goffi@goffi.org>
parents:
diff changeset
7 # it under the terms of the GNU Affero General Public License as published by
b0f70be331c5 tests: unit test for "Calls" plugins:
Goffi <goffi@goffi.org>
parents:
diff changeset
8 # the Free Software Foundation, either version 3 of the License, or
b0f70be331c5 tests: unit test for "Calls" plugins:
Goffi <goffi@goffi.org>
parents:
diff changeset
9 # (at your option) any later version.
b0f70be331c5 tests: unit test for "Calls" plugins:
Goffi <goffi@goffi.org>
parents:
diff changeset
10
b0f70be331c5 tests: unit test for "Calls" plugins:
Goffi <goffi@goffi.org>
parents:
diff changeset
11 # This program is distributed in the hope that it will be useful,
b0f70be331c5 tests: unit test for "Calls" plugins:
Goffi <goffi@goffi.org>
parents:
diff changeset
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
b0f70be331c5 tests: unit test for "Calls" plugins:
Goffi <goffi@goffi.org>
parents:
diff changeset
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
b0f70be331c5 tests: unit test for "Calls" plugins:
Goffi <goffi@goffi.org>
parents:
diff changeset
14 # GNU Affero General Public License for more details.
b0f70be331c5 tests: unit test for "Calls" plugins:
Goffi <goffi@goffi.org>
parents:
diff changeset
15
b0f70be331c5 tests: unit test for "Calls" plugins:
Goffi <goffi@goffi.org>
parents:
diff changeset
16 # You should have received a copy of the GNU Affero General Public License
b0f70be331c5 tests: unit test for "Calls" plugins:
Goffi <goffi@goffi.org>
parents:
diff changeset
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
b0f70be331c5 tests: unit test for "Calls" plugins:
Goffi <goffi@goffi.org>
parents:
diff changeset
18
b0f70be331c5 tests: unit test for "Calls" plugins:
Goffi <goffi@goffi.org>
parents:
diff changeset
19 from unittest.mock import AsyncMock, MagicMock
b0f70be331c5 tests: unit test for "Calls" plugins:
Goffi <goffi@goffi.org>
parents:
diff changeset
20
b0f70be331c5 tests: unit test for "Calls" plugins:
Goffi <goffi@goffi.org>
parents:
diff changeset
21 from libervia.backend.tools.common import data_format
b0f70be331c5 tests: unit test for "Calls" plugins:
Goffi <goffi@goffi.org>
parents:
diff changeset
22 import pytest
b0f70be331c5 tests: unit test for "Calls" plugins:
Goffi <goffi@goffi.org>
parents:
diff changeset
23
b0f70be331c5 tests: unit test for "Calls" plugins:
Goffi <goffi@goffi.org>
parents:
diff changeset
24 from libervia.desktop_kivy import G
b0f70be331c5 tests: unit test for "Calls" plugins:
Goffi <goffi@goffi.org>
parents:
diff changeset
25 from libervia.desktop_kivy.plugins import plugin_wid_calls
b0f70be331c5 tests: unit test for "Calls" plugins:
Goffi <goffi@goffi.org>
parents:
diff changeset
26
b0f70be331c5 tests: unit test for "Calls" plugins:
Goffi <goffi@goffi.org>
parents:
diff changeset
27
b0f70be331c5 tests: unit test for "Calls" plugins:
Goffi <goffi@goffi.org>
parents:
diff changeset
28 @pytest.fixture
b0f70be331c5 tests: unit test for "Calls" plugins:
Goffi <goffi@goffi.org>
parents:
diff changeset
29 def host(monkeypatch):
b0f70be331c5 tests: unit test for "Calls" plugins:
Goffi <goffi@goffi.org>
parents:
diff changeset
30 host = MagicMock()
b0f70be331c5 tests: unit test for "Calls" plugins:
Goffi <goffi@goffi.org>
parents:
diff changeset
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
b0f70be331c5 tests: unit test for "Calls" plugins:
Goffi <goffi@goffi.org>
parents:
diff changeset
33 monkeypatch.setattr(G, "_host", host, raising=False)
b0f70be331c5 tests: unit test for "Calls" plugins:
Goffi <goffi@goffi.org>
parents:
diff changeset
34 return host
b0f70be331c5 tests: unit test for "Calls" plugins:
Goffi <goffi@goffi.org>
parents:
diff changeset
35
b0f70be331c5 tests: unit test for "Calls" plugins:
Goffi <goffi@goffi.org>
parents:
diff changeset
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
f0ce49b360c8 calls: move webrtc code to core:
Goffi <goffi@goffi.org>
parents: 507
diff changeset
41 monkeypatch.setattr(plugin_wid_calls.Calls, attr, MagicMock())
f0ce49b360c8 calls: move webrtc code to core:
Goffi <goffi@goffi.org>
parents: 507
diff changeset
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
f0ce49b360c8 calls: move webrtc code to core:
Goffi <goffi@goffi.org>
parents: 507
diff changeset
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
f0ce49b360c8 calls: move webrtc code to core:
Goffi <goffi@goffi.org>
parents: 507
diff changeset
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
f0ce49b360c8 calls: move webrtc code to core:
Goffi <goffi@goffi.org>
parents: 507
diff changeset
120 monkeypatch.setattr(calls.webrtc, "answer_call", AsyncMock())
f0ce49b360c8 calls: move webrtc code to core:
Goffi <goffi@goffi.org>
parents: 507
diff changeset
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
f0ce49b360c8 calls: move webrtc code to core:
Goffi <goffi@goffi.org>
parents: 507
diff changeset
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()