56 lines
2.0 KiB
Python
56 lines
2.0 KiB
Python
import pytest
|
|
from unittest.mock import MagicMock, patch
|
|
from src.ui.main_window import MainWindow
|
|
|
|
def test_on_tick_updates_ui_and_publishes(qtbot):
|
|
with patch("src.ui.main_window.ConnectionManager") as mock_conn_mgr_cls, \
|
|
patch("src.ui.main_window.OsdGenerator") as mock_osd_cls:
|
|
|
|
# Setup mocks
|
|
mock_conn_instance = mock_conn_mgr_cls.return_value
|
|
mock_conn_instance.is_connected.return_value = True
|
|
|
|
mock_osd_instance = mock_osd_cls.return_value
|
|
mock_osd_instance.get_osd_data.return_value = {
|
|
"latitude": 22.0, "longitude": 113.0, "height": 10.0,
|
|
"battery_percent": 90, "attitude_pitch": 1.0,
|
|
"attitude_roll": 2.0, "attitude_yaw": 3.0,
|
|
"horizontal_speed": 0, "vertical_speed": 0
|
|
}
|
|
|
|
window = MainWindow()
|
|
qtbot.addWidget(window)
|
|
|
|
# Force a tick
|
|
window._on_tick()
|
|
|
|
# Check UI updates
|
|
assert window.lbl_latitude.text() == "22.0"
|
|
assert window.lbl_altitude.text() == "10.00 m"
|
|
|
|
# Check Publish
|
|
mock_conn_instance.client.publish.assert_called()
|
|
args = mock_conn_instance.client.publish.call_args
|
|
# Topic should contain SN (default 1581F4BM12345)
|
|
assert "sys/product/1581F4BM12345/osd" in args[0][0]
|
|
# Payload should contain json data
|
|
assert "22.0" in args[0][1]
|
|
|
|
def test_connect_button_toggle(qtbot):
|
|
with patch("src.ui.main_window.ConnectionManager") as mock_conn_mgr_cls:
|
|
mock_conn_instance = mock_conn_mgr_cls.return_value
|
|
mock_conn_instance.is_connected.side_effect = [False, True] # First call False, then True
|
|
|
|
window = MainWindow()
|
|
qtbot.addWidget(window)
|
|
|
|
# Initial state
|
|
assert window.btn_connect.text() == "Connect"
|
|
|
|
# Click connect
|
|
window.btn_connect.click()
|
|
|
|
# Should call connect
|
|
mock_conn_instance.connect.assert_called()
|
|
assert window.btn_connect.text() == "Disconnect"
|