0
0
mirror of https://github.com/bec-project/bec_widgets.git synced 2025-07-14 03:31:50 +02:00
This commit is contained in:
2025-03-27 21:42:19 +01:00
committed by wyzula-jan
parent 1d98aed46e
commit 705e819352
3 changed files with 52 additions and 30 deletions

View File

@ -68,7 +68,6 @@ class BECApplication:
config: The ServiceConfig instance to use for configuration.
gui_id: The unique identifier for this application.
"""
self.app.gui_id = gui_id or BECApplication.generate_unique_identifier()
self.app.dispatcher = BECDispatcher(client=client, config=config)
self.app.rpc_register = RPCRegister()
@ -82,6 +81,7 @@ class BECApplication:
)
self.app.is_bec_app = True
self.app.aboutToQuit.connect(self.shutdown)
self.setup_bec_icon()
@ -91,7 +91,7 @@ class BECApplication:
def __getattr__(self, name: str) -> Any:
if hasattr(self.app, name):
return getattr(self.app, name)
return super().__getattr__(name)
return super().__getattribute__(name)
def __new__(cls, *args, **kwargs) -> BECApplication:
if not hasattr(cls, "_instance"):
@ -222,4 +222,12 @@ class BECApplication:
def shutdown(self):
self.dispatcher.disconnect_all()
self.cli_server.shutdown()
super().shutdown()
self.rpc_register.reset_singleton()
delattr(self.app, "gui_id")
delattr(self.app, "dispatcher")
delattr(self.app, "rpc_register")
delattr(self.app, "client")
delattr(self.app, "is_bec_app")
delattr(self.app, "cli_server")
self._initialized = False
self._instance = None

View File

@ -7,6 +7,7 @@ from qtpy.QtWidgets import QApplication
from bec_widgets.cli.rpc.rpc_register import RPCRegister
from bec_widgets.utils import bec_dispatcher as bec_dispatcher_module
from bec_widgets.utils import error_popups
from bec_widgets.utils.bec_qapp import BECApplication
@pytest.hookimpl(tryfirst=True, hookwrapper=True)
@ -28,9 +29,11 @@ def qapplication(qtbot, request, testable_qtimer_class): # pylint: disable=unus
print("Test failed, skipping cleanup checks")
return
qapp = BECApplication()
qapp.shutdown()
testable_qtimer_class.check_all_stopped(qtbot)
qapp = QApplication.instance()
qapp.processEvents()
if hasattr(qapp, "os_listener") and qapp.os_listener:
qapp.removeEventFilter(qapp.os_listener)

View File

@ -1,45 +1,56 @@
import argparse
from unittest import mock
import pytest
from bec_lib.service_config import ServiceConfig
from bec_widgets.cli.server import _start_server
from bec_widgets.widgets.containers.dock import BECDockArea
from bec_widgets.cli.server import GUIServer
@pytest.fixture
def mocked_cli_server():
with mock.patch("bec_widgets.cli.server.BECWidgetsCLIServer") as mock_server:
with mock.patch("bec_widgets.cli.server.ServiceConfig") as mock_config:
with mock.patch("bec_lib.logger.bec_logger.configure") as mock_logger:
yield mock_server, mock_config, mock_logger
def gui_server():
args = argparse.Namespace(
config=None, id="gui_id", gui_class="LaunchWindow", gui_class_id="bec", hide=False
)
return GUIServer(args=args)
def test_rpc_server_start_server_without_service_config(mocked_cli_server):
def test_gui_server_start_server_without_service_config(gui_server):
"""
Test that the server is started with the correct arguments.
"""
mock_server, mock_config, _ = mocked_cli_server
assert gui_server.config is None
assert gui_server.gui_id == "gui_id"
assert gui_server.gui_class == "LaunchWindow"
assert gui_server.gui_class_id == "bec"
assert gui_server.hide is False
_start_server("gui_id", BECDockArea, config=None)
mock_server.assert_called_once_with(
gui_id="gui_id", config=mock_config(), gui_class=BECDockArea, gui_class_id="bec"
)
def test_gui_server_get_service_config(gui_server):
"""
Test that the server is started with the correct arguments.
"""
assert gui_server._get_service_config().config is ServiceConfig().config
@pytest.mark.parametrize(
"config, call_config",
"connections, hide",
[
("/path/to/config.yml", {"config_path": "/path/to/config.yml"}),
({"key": "value"}, {"config": {"key": "value"}}),
({}, False),
({"launcher": mock.MagicMock()}, False),
({"launcher": mock.MagicMock(), "dock_area": mock.MagicMock()}, True),
],
)
def test_rpc_server_start_server_with_service_config(mocked_cli_server, config, call_config):
"""
Test that the server is started with the correct arguments.
"""
mock_server, mock_config, _ = mocked_cli_server
config = mock_config(**call_config)
_start_server("gui_id", BECDockArea, config=config)
mock_server.assert_called_once_with(
gui_id="gui_id", config=config, gui_class=BECDockArea, gui_class_id="bec"
)
def test_gui_server_turns_off_the_lights(gui_server, connections, hide):
with mock.patch.object(gui_server, "launcher_window") as mock_launcher_window:
with mock.patch.object(gui_server, "app") as mock_app:
gui_server._turn_off_the_lights(connections)
if not hide:
mock_launcher_window.show.assert_called_once()
mock_launcher_window.activateWindow.assert_called_once()
mock_launcher_window.raise_.assert_called_once()
mock_app.setQuitOnLastWindowClosed.assert_called_once_with(True)
else:
mock_launcher_window.hide.assert_called_once()
mock_app.setQuitOnLastWindowClosed.assert_called_once_with(False)