From caa78be00da2b9b038613b1151b1674713e97ad8 Mon Sep 17 00:00:00 2001 From: Ken Roberts Date: Fri, 21 Jan 2022 21:30:51 +0000 Subject: [PATCH] ProjectorManager tests upd_add_port listener --- openlp/core/projectors/manager.py | 6 ++-- .../projectors/test_projectormanager.py | 36 +++++++++++++++++++ 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/openlp/core/projectors/manager.py b/openlp/core/projectors/manager.py index 9c488ce81..eb81da5ac 100644 --- a/openlp/core/projectors/manager.py +++ b/openlp/core/projectors/manager.py @@ -34,8 +34,8 @@ from openlp.core.common.registry import Registry, RegistryBase from openlp.core.lib.ui import create_widget_action from openlp.core.projectors import DialogSourceStyle from openlp.core.projectors.constants import E_AUTHENTICATION, E_ERROR, E_NETWORK, E_NOT_CONNECTED, E_SOCKET_TIMEOUT,\ - E_UNKNOWN_SOCKET_ERROR, QSOCKET_STATE, S_CONNECTED, S_CONNECTING, S_COOLDOWN, S_INITIALIZE, S_NOT_CONNECTED, S_OFF,\ - S_ON, S_STANDBY, S_WARMUP, STATUS_CODE, STATUS_MSG + E_UNKNOWN_SOCKET_ERROR, PJLINK_PORT, QSOCKET_STATE, S_CONNECTED, S_CONNECTING, S_COOLDOWN, S_INITIALIZE, \ + S_NOT_CONNECTED, S_OFF, S_ON, S_STANDBY, S_WARMUP, STATUS_CODE, STATUS_MSG from openlp.core.projectors.db import ProjectorDB from openlp.core.projectors.editform import ProjectorEditForm @@ -325,7 +325,7 @@ class ProjectorManager(QtWidgets.QWidget, RegistryBase, UiProjectorManager, LogM self.projector_form.editProjector.connect(self.edit_projector_from_wizard) self.projector_list_widget.itemSelectionChanged.connect(self.update_icons) - def udp_listen_add(self, port): + def udp_listen_add(self, port=PJLINK_PORT): """ Add UDP broadcast listener """ diff --git a/tests/openlp_core/projectors/test_projectormanager.py b/tests/openlp_core/projectors/test_projectormanager.py index 1efdf393d..a126d101c 100644 --- a/tests/openlp_core/projectors/test_projectormanager.py +++ b/tests/openlp_core/projectors/test_projectormanager.py @@ -26,6 +26,7 @@ import logging from unittest.mock import MagicMock, patch +from openlp.core.projectors.constants import PJLINK_PORT from openlp.core.projectors.db import ProjectorDB from openlp.core.projectors.editform import ProjectorEditForm from openlp.core.projectors.manager import ProjectorManager @@ -112,3 +113,38 @@ def test_bootstrap_post_set_up_autostart_projector(projector_manager_nodb, caplo assert caplog.record_tuples[-1] == ('openlp.core.projectors.manager', 10, 'Delaying 1.5 seconds before loading all projectors'), \ "Last log entry should be autoloading entry" + + +def test_udp_listen_add_duplicate_port(projector_manager_nodb, caplog): + """ + Test adding UDP port listener to port already registered + """ + # GIVEN: Initial setup + caplog.set_level(logging.DEBUG) + projector_manager_nodb.pjlink_udp[PJLINK_PORT] = "Something to set index item" + + # WHEN: udp_listen_add is called with duplicate port number + caplog.clear() + projector_manager_nodb.udp_listen_add(port=PJLINK_PORT) + + # THEN: Verify log entry and registry entry not called + assert caplog.record_tuples[0] == ('openlp.core.projectors.manager', 30, + 'UDP Listener for port 4352 already added - skipping') + + +@patch('openlp.core.projectors.manager.Registry') +def test_udp_listen_add_new(mock_registry, projector_manager_nodb, caplog): + """ + Test adding new UDP port listener + """ + # GIVEN: Initial setup + caplog.set_level(logging.DEBUG) + log_entries = [('openlp.core.projectors.manager', 10, 'Adding UDP listener on port 4352'), + ('openlp.core.projectors.pjlink', 10, '(UDP:4352) PJLinkUDP() Initialized')] + # WHEN: Adding new listener + caplog.clear() + projector_manager_nodb.udp_listen_add(port=PJLINK_PORT) + + # THEN: Appropriate listener and log entries + mock_registry.execute.called_with('udp_broadcast_add', port=PJLINK_PORT) + assert caplog.record_tuples == log_entries, 'Invalid log entries'