diff --git a/src/aare/gui/threads/axis_video_thread.py b/src/aare/gui/threads/axis_video_thread.py index f488df66..ff5da3f4 100644 --- a/src/aare/gui/threads/axis_video_thread.py +++ b/src/aare/gui/threads/axis_video_thread.py @@ -136,13 +136,21 @@ class VideoThread(QThread): self.running = False self.requestInterruption() - if self.session: + session = self.session + if session is not None: try: - self.session.close() + session.close() except Exception: pass finally: self.session = None - if self.isRunning(): - self.wait(5000) \ No newline at end of file + try: + self.quit() + except Exception: + pass + + try: + self.wait(5000) + except Exception: + pass \ No newline at end of file diff --git a/tests/unit/gui/test_axis_video_thread.py b/tests/unit/gui/test_axis_video_thread.py index 2f3fd9d9..ef2f5951 100644 --- a/tests/unit/gui/test_axis_video_thread.py +++ b/tests/unit/gui/test_axis_video_thread.py @@ -29,13 +29,13 @@ def test_process_buffer_success(video_thread, qtbot): img = np.zeros((10, 10, 3), dtype=np.uint8) _, jpeg_bytes = cv2.imencode('.jpg', img) jpeg_bytes = jpeg_bytes.tobytes() - + boundary = b'--boundary' buffer = b'--boundary\r\nContent-Type: image/jpeg\r\n\r\n' + jpeg_bytes + b'\r\n--boundary' - + with qtbot.waitSignal(video_thread.frame_ready, timeout=1000) as blocker: video_thread._process_buffer(buffer, boundary) - + assert isinstance(blocker.args[0], QImage) assert blocker.args[0].width() == 10 assert blocker.args[0].height() == 10 @@ -43,28 +43,31 @@ def test_process_buffer_success(video_thread, qtbot): def test_process_buffer_invalid_jpeg(video_thread): boundary = b'--boundary' buffer = b'--boundary\r\nContent-Type: image/jpeg\r\n\r\nNOT_A_JPEG\r\n--boundary' - + # Should not emit anything or raise exception video_thread._process_buffer(buffer, boundary) def test_stop(video_thread): - video_thread.session = MagicMock() + mock_session = MagicMock() + video_thread.session = mock_session # Mocking wait and quit to avoid actual thread blocking in test video_thread.quit = MagicMock() video_thread.wait = MagicMock() - + video_thread.stop() + assert video_thread.running is False - video_thread.session.close.assert_called_once() + assert video_thread.session is None + mock_session.close.assert_called_once() video_thread.quit.assert_called_once() - video_thread.wait.assert_called_once() + video_thread.wait.assert_called_once_with(5000) @patch('requests.Session') def test_run_connection_error(mock_session_class, video_thread, qtbot): mock_session = mock_session_class.return_value mock_session.get.side_effect = Exception("Connection Refused") - + with qtbot.waitSignal(video_thread.error_occurred, timeout=1000) as blocker: video_thread.run() - - assert "Unexpected error" in blocker.args[0] + + assert "Unexpected error" in blocker.args[0] \ No newline at end of file diff --git a/tests/unit/gui/test_main_window.py b/tests/unit/gui/test_main_window.py index d3360454..546f7251 100644 --- a/tests/unit/gui/test_main_window.py +++ b/tests/unit/gui/test_main_window.py @@ -9,24 +9,19 @@ def mock_ui_state(): yield mock def test_main_window_init(qtbot, mock_ui_state): - # Mocking many things that MainWindow __init__ tries to do - # Especially things that hit the network or expected files with patch("requests.get") as mock_get, \ patch("aare.gui.main_window.DAQWorker"), \ - patch("aare.gui.main_window.SampleCameraThread"), \ patch("aare.gui.main_window.PredictionSubscriber"), \ patch("aare.gui.main_window.VideoThread"), \ patch("aare.gui.main_window.JFJochDBusClient"), \ patch("aare.gui.main_window.jwt.decode") as mock_jwt: - + mock_get.return_value.status_code = 200 mock_get.return_value.json.return_value = {"status": "ok"} mock_jwt.return_value = {"sub": "testuser", "staff": True, "pgroups": ["p123"], "session": 15} - - # Create a valid-looking fake JWT + fake_token = "header.payload.signature" - - # We need to provide all arguments to MainWindow + win = MainWindow( base_url="http://localhost:5210", token=fake_token, @@ -39,22 +34,21 @@ def test_main_window_init(qtbot, mock_ui_state): ) win.show() qtbot.addWidget(win) - + assert win.windowTitle() == "AareGUI" assert win.isVisible() def test_main_window_mount_view(qtbot, mock_ui_state): with patch("requests.get"), \ patch("aare.gui.main_window.DAQWorker"), \ - patch("aare.gui.main_window.SampleCameraThread"), \ patch("aare.gui.main_window.PredictionSubscriber"), \ patch("aare.gui.main_window.VideoThread"), \ patch("aare.gui.main_window.JFJochDBusClient"), \ patch("aare.gui.main_window.jwt.decode") as mock_jwt: - + mock_jwt.return_value = {"sub": "testuser", "staff": True, "pgroups": ["p123"], "session": 15} fake_token = "header.payload.signature" - + win = MainWindow( base_url=None, token=fake_token, @@ -66,15 +60,12 @@ def test_main_window_mount_view(qtbot, mock_ui_state): gonio_cam_id=None ) qtbot.addWidget(win) - + win.mount_view() - # This just changes some panel visibility, hard to assert without deep inspection - # but we check it doesn't crash def test_mark_user_interaction_reports_backend(qtbot, mock_ui_state): with patch("requests.get"), \ patch("aare.gui.main_window.DAQWorker") as mock_daq_cls, \ - patch("aare.gui.main_window.SampleCameraThread"), \ patch("aare.gui.main_window.PredictionSubscriber"), \ patch("aare.gui.main_window.VideoThread"), \ patch("aare.gui.main_window.JFJochDBusClient"), \ @@ -103,7 +94,6 @@ def test_mark_user_interaction_reports_backend(qtbot, mock_ui_state): def test_automation_activity_refreshes_idle_timestamp(qtbot, mock_ui_state): with patch("requests.get"), \ patch("aare.gui.main_window.DAQWorker"), \ - patch("aare.gui.main_window.SampleCameraThread"), \ patch("aare.gui.main_window.PredictionSubscriber"), \ patch("aare.gui.main_window.VideoThread"), \ patch("aare.gui.main_window.JFJochDBusClient"), \ @@ -191,7 +181,6 @@ def test_automation_activity_refreshes_idle_timestamp(qtbot, mock_ui_state): def test_idle_timeout_closes_when_inactive_and_not_running(qtbot, mock_ui_state): with patch("requests.get"), \ patch("aare.gui.main_window.DAQWorker"), \ - patch("aare.gui.main_window.SampleCameraThread"), \ patch("aare.gui.main_window.PredictionSubscriber"), \ patch("aare.gui.main_window.VideoThread"), \ patch("aare.gui.main_window.JFJochDBusClient"), \ @@ -224,7 +213,6 @@ def test_idle_timeout_closes_when_inactive_and_not_running(qtbot, mock_ui_state) def test_idle_timeout_does_not_close_while_automation_active(qtbot, mock_ui_state): with patch("requests.get"), \ patch("aare.gui.main_window.DAQWorker"), \ - patch("aare.gui.main_window.SampleCameraThread"), \ patch("aare.gui.main_window.PredictionSubscriber"), \ patch("aare.gui.main_window.VideoThread"), \ patch("aare.gui.main_window.JFJochDBusClient"), \