Skip to content

Commit

Permalink
Close and delete Qt widgets that are under test
Browse files Browse the repository at this point in the history
By using qtbot.add_widget on the created widgets they will be
automatically closed and deleted after each test.
  • Loading branch information
matrss committed Jan 12, 2024
1 parent 85acf14 commit 3e95b6e
Show file tree
Hide file tree
Showing 22 changed files with 75 additions and 109 deletions.
12 changes: 8 additions & 4 deletions mslib/msui/msui_mainwindow.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,7 @@ def __init__(self, mscolab_data_dir=None, tutorial_mode=False, *args):
except (ImportError, AttributeError) as error:
logging.debug("AttributeError, ImportError Exception %s", error)

self.force_close = False
self.config_editor = None
self.local_active = True
self.new_flight_track_counter = 0
Expand Down Expand Up @@ -1069,10 +1070,13 @@ def closeEvent(self, event):
Overloads QtGui.QMainWindow.closeEvent(). This method is called if
Qt receives a window close request for our application window.
"""
ret = QtWidgets.QMessageBox.warning(
self, self.tr("Mission Support System"),
self.tr("Do you want to close the Mission Support System application?"),
QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No, QtWidgets.QMessageBox.No)
if self.force_close:
ret = QtWidgets.QMessageBox.Yes
else:
ret = QtWidgets.QMessageBox.warning(
self, self.tr("Mission Support System"),
self.tr("Do you want to close the Mission Support System application?"),
QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No, QtWidgets.QMessageBox.No)

if ret == QtWidgets.QMessageBox.Yes:
if self.mscolab.help_dialog is not None:
Expand Down
2 changes: 0 additions & 2 deletions tests/_test_msui/test_editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ def setup(self, qtbot):
yield
if os.path.exists(self.save_file_name):
os.remove(self.save_file_name)
self.window.hide()
QtWidgets.QApplication.processEvents()

@mock.patch("mslib.msui.editor.get_open_filename", return_value=sample_file)
def test_file_open(self, mockfile):
Expand Down
3 changes: 1 addition & 2 deletions tests/_test_msui/test_kmloverlay_dockwidget.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def setup(self, qtbot):
self.view.map.gcpoints_path = mock.Mock(side_effect=lambda x, y: (x, y))

self.window = kd.KMLOverlayControlWidget(view=self.view)
qtbot.add_widget(self.window)
self.window.show()
QtWidgets.QApplication.processEvents()
QtTest.QTest.qWaitForWindowExposed(self.window)
Expand All @@ -57,8 +58,6 @@ def setup(self, qtbot):
self.window.remove_file()
QtWidgets.QApplication.processEvents()
yield
QtWidgets.QApplication.processEvents()
self.window.close()
if os.path.exists(save_kml):
os.remove(save_kml)

Expand Down
13 changes: 4 additions & 9 deletions tests/_test_msui/test_linearview.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
from mslib.msui import flighttrack as ft
import mslib.msui.linearview as tv
from mslib.msui.mpl_qtwidget import _DEFAULT_SETTINGS_LINEARVIEW
from tests.utils import wait_until_signal
from tests.utils import wait_until_signal, set_force_close

PORTS = list(range(26000, 26500))

Expand All @@ -45,13 +45,11 @@ class Test_MSS_LV_Options_Dialog:
@pytest.fixture(autouse=True)
def setup(self, qtbot):
self.window = tv.MSUI_LV_Options_Dialog(settings=_DEFAULT_SETTINGS_LINEARVIEW)
qtbot.add_widget(self.window)
self.window.show()
QtWidgets.QApplication.processEvents()
QtTest.QTest.qWaitForWindowExposed(self.window)
QtWidgets.QApplication.processEvents()
yield
self.window.hide()
QtWidgets.QApplication.processEvents()

@mock.patch("PyQt5.QtWidgets.QMessageBox")
def test_show(self, mockcrit):
Expand All @@ -73,13 +71,11 @@ def setup(self, qtbot):
0, rows=len(initial_waypoints), waypoints=initial_waypoints)

self.window = tv.MSUILinearViewWindow(model=waypoints_model)
qtbot.add_widget(self.window, before_close_func=set_force_close)
self.window.show()
QtWidgets.QApplication.processEvents()
QtTest.QTest.qWaitForWindowExposed(self.window)
QtWidgets.QApplication.processEvents()
yield
self.window.hide()
QtWidgets.QApplication.processEvents()

@mock.patch("PyQt5.QtWidgets.QMessageBox")
def test_open_wms(self, mockbox):
Expand Down Expand Up @@ -126,6 +122,7 @@ def setup(self, qtbot):
waypoints_model.insertRows(
0, rows=len(initial_waypoints), waypoints=initial_waypoints)
self.window = tv.MSUILinearViewWindow(model=waypoints_model)
qtbot.add_widget(self.window, before_close_func=set_force_close)
self.window.show()
QtWidgets.QApplication.processEvents()
QtTest.QTest.qWait(2000)
Expand All @@ -136,8 +133,6 @@ def setup(self, qtbot):
self.wms_control = self.window.docks[0].widget()
self.wms_control.multilayers.cbWMS_URL.setEditText("")
yield
self.window.hide()
QtWidgets.QApplication.processEvents()
shutil.rmtree(self.tempdir)
self.thread.terminate()

Expand Down
1 change: 1 addition & 0 deletions tests/_test_msui/test_mpl_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ def setup(self, qtbot):
kwargs = {'resolution': 'l', 'area_thresh': 1000.0, 'ax': plt.gca(), 'llcrnrlon': -15.0, 'llcrnrlat': 35.0,
'urcrnrlon': 30.0, 'urcrnrlat': 65.0, 'epsg': '4326'}
self.map = MapCanvas(**kwargs)
qtbot.add_widget(plt.get_current_fig_manager().window)

def test_no_coastsegs(self):
"""
Expand Down
14 changes: 3 additions & 11 deletions tests/_test_msui/test_mscolab.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
from mslib.msui.flighttrack import WaypointsTableModel
from PyQt5 import QtCore, QtTest, QtWidgets
from mslib.utils.config import read_config_file, config_loader, modify_config_file
from tests.utils import mscolab_start_server, create_msui_settings_file, ExceptionMock
from tests.utils import mscolab_start_server, create_msui_settings_file, ExceptionMock, set_force_close
from mslib.msui import msui
from mslib.msui import mscolab
from mslib.mscolab.mscolab import handle_db_reset
Expand All @@ -61,6 +61,7 @@ def setup(self, qtbot):

QtTest.QTest.qWait(500)
self.main_window = msui.MSUIMainWindow(mscolab_data_dir=mscolab_settings.MSCOLAB_DATA_DIR)
qtbot.add_widget(self.main_window, before_close_func=set_force_close)
self.main_window.create_new_flight_track()
self.main_window.show()
self.window = mscolab.MSColab_ConnectDialog(parent=self.main_window, mscolab=self.main_window.mscolab)
Expand All @@ -73,9 +74,6 @@ def setup(self, qtbot):
mslib.utils.auth.del_password_from_keyring(service_name="MSCOLAB", username=email)
yield
self.main_window.mscolab.logout()
self.window.hide()
self.main_window.hide()
QtWidgets.QApplication.processEvents()
self.process.terminate()

def test_url_combo(self):
Expand Down Expand Up @@ -292,19 +290,13 @@ def setup(self, qtbot):

QtTest.QTest.qWait(500)
self.window = msui.MSUIMainWindow(mscolab_data_dir=mscolab_settings.MSCOLAB_DATA_DIR)
qtbot.add_widget(self.window, before_close_func=set_force_close)
self.window.create_new_flight_track()
self.window.show()
yield
self.window.mscolab.logout()
if self.window.mscolab.version_window:
self.window.mscolab.version_window.close()
if self.window.mscolab.conn:
self.window.mscolab.conn.disconnect()
# force close all open views
while self.window.listViews.count() > 0:
self.window.listViews.item(0).window.handle_force_close()
# close all hanging operation option windows
self.window.mscolab.close_external_windows()
self.process.terminate()

def test_activate_operation(self):
Expand Down
8 changes: 2 additions & 6 deletions tests/_test_msui/test_mscolab_admin_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

from mslib.mscolab.conf import mscolab_settings
from PyQt5 import QtCore, QtTest, QtWidgets
from tests.utils import mscolab_start_server
from tests.utils import mscolab_start_server, set_force_close
from mslib.msui import mscolab
from mslib.msui import msui
from mslib.mscolab.mscolab import handle_db_reset
Expand Down Expand Up @@ -69,6 +69,7 @@ def setup(self, qtbot):

QtTest.QTest.qWait(500)
self.window = msui.MSUIMainWindow(mscolab_data_dir=mscolab_settings.MSCOLAB_DATA_DIR)
qtbot.add_widget(self.window, before_close_func=set_force_close)
self.window.create_new_flight_track()
self.window.show()
# connect and login to mscolab
Expand All @@ -84,13 +85,8 @@ def setup(self, qtbot):
QtWidgets.QApplication.processEvents()
yield
self.window.mscolab.logout()
if self.window.mscolab.admin_window:
self.window.mscolab.admin_window.close()
if self.window.mscolab.conn:
self.window.mscolab.conn.disconnect()
with mock.patch("PyQt5.QtWidgets.QMessageBox.warning", return_value=QtWidgets.QMessageBox.Yes):
self.window.close()
QtWidgets.QApplication.processEvents()
self.process.terminate()

def test_permission_filter(self):
Expand Down
5 changes: 2 additions & 3 deletions tests/_test_msui/test_mscolab_merge_waypoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
from mslib.mscolab.conf import mscolab_settings
from PyQt5 import QtCore, QtTest, QtWidgets
from tests.utils import (mscolab_start_server, mscolab_register_and_login, mscolab_create_operation,
mscolab_delete_all_operations, mscolab_delete_user)
mscolab_delete_all_operations, mscolab_delete_user, set_force_close)
from mslib.msui import mscolab
from mslib.msui import msui
from mslib.mscolab.mscolab import handle_db_reset
Expand All @@ -52,6 +52,7 @@ def setup(self, qtbot):
self.process, self.url, self.app, _, self.cm, self.fm = mscolab_start_server(PORTS)
QtTest.QTest.qWait(500)
self.window = msui.MSUIMainWindow(mscolab_data_dir=mscolab_settings.MSCOLAB_DATA_DIR)
qtbot.add_widget(self.window, before_close_func=set_force_close)
self.window.create_new_flight_track()
self.emailid = '[email protected]'
yield
Expand All @@ -64,8 +65,6 @@ def setup(self, qtbot):
if mss_dir.exists('local_mscolab_data'):
mss_dir.removetree('local_mscolab_data')
assert mss_dir.exists('local_mscolab_data') is False
if self.window.mscolab.version_window:
self.window.mscolab.version_window.close()
if self.window.mscolab.conn:
self.window.mscolab.conn.disconnect()
QtWidgets.QApplication.processEvents()
Expand Down
7 changes: 2 additions & 5 deletions tests/_test_msui/test_mscolab_operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
from mslib.mscolab.conf import mscolab_settings
from mslib.mscolab.models import Message
from PyQt5 import QtCore, QtTest, QtWidgets
from tests.utils import mscolab_start_server
from tests.utils import mscolab_start_server, set_force_close
from mslib.msui import mscolab
from mslib.msui import msui
from mslib.mscolab.mscolab import handle_db_reset
Expand Down Expand Up @@ -63,6 +63,7 @@ def setup(self, qtbot):
self.user = get_user(self.userdata[0])
QtTest.QTest.qWait(500)
self.window = msui.MSUIMainWindow(mscolab_data_dir=mscolab_settings.MSCOLAB_DATA_DIR)
qtbot.add_widget(self.window, before_close_func=set_force_close)
self.window.create_new_flight_track()
self.window.show()
# connect and login to mscolab
Expand All @@ -78,12 +79,8 @@ def setup(self, qtbot):
QtWidgets.QApplication.processEvents()
yield
self.window.mscolab.logout()
if self.window.mscolab.chat_window:
self.window.mscolab.chat_window.hide()
if self.window.mscolab.conn:
self.window.mscolab.conn.disconnect()
self.window.hide()
QtWidgets.QApplication.processEvents()
self.process.terminate()

def test_send_message(self):
Expand Down
5 changes: 2 additions & 3 deletions tests/_test_msui/test_mscolab_version_history.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import pytest
import mock

from tests.utils import mscolab_start_server
from tests.utils import mscolab_start_server, set_force_close
from mslib.mscolab.conf import mscolab_settings
from PyQt5 import QtCore, QtTest, QtWidgets
from mslib.msui import mscolab
Expand Down Expand Up @@ -56,6 +56,7 @@ def setup(self, qtbot):
self.user = get_user(self.userdata[0])
QtTest.QTest.qWait(500)
self.window = msui.MSUIMainWindow(mscolab_data_dir=mscolab_settings.MSCOLAB_DATA_DIR)
qtbot.add_widget(self.window, before_close_func=set_force_close)
self.window.create_new_flight_track()
self.window.show()
# connect and login to mscolab
Expand All @@ -72,8 +73,6 @@ def setup(self, qtbot):
QtWidgets.QApplication.processEvents()
yield
self.window.mscolab.logout()
if self.window.mscolab.version_window:
self.window.mscolab.version_window.close()
if self.window.mscolab.conn:
self.window.mscolab.conn.disconnect()
self.process.terminate()
Expand Down
29 changes: 11 additions & 18 deletions tests/_test_msui/test_msui.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
from tests.constants import ROOT_DIR, POSIX, MSUI_CONFIG_PATH
from mslib.msui import msui
from mslib.msui import msui_mainwindow as msui_mw
from tests.utils import ExceptionMock
from tests.utils import ExceptionMock, set_force_close
from mslib.utils.config import read_config_file


Expand Down Expand Up @@ -70,15 +70,15 @@ class Test_MSS_TutorialMode:
def setup(self, qapp, qtbot):
qapp.setApplicationDisplayName("MSUI")
self.main_window = msui_mw.MSUIMainWindow(tutorial_mode=True)
qtbot.add_widget(self.main_window, before_close_func=set_force_close)
with qtbot.wait_active(self.main_window):
self.main_window.show()
self.main_window.activateWindow()
self.main_window.create_new_flight_track()
self.main_window.show()
self.main_window.shortcuts_dlg = msui_mw.MSUI_ShortcutsDialog(
tutorial_mode=True)
self.main_window.show_shortcuts(search_mode=True)
self.tutorial_dir = fs.path.combine(MSUI_CONFIG_PATH, 'tutorial_images')
yield
self.main_window.hide()
QtWidgets.QApplication.processEvents()

def test_tutorial_dir(self):
dir_name, name = fs.path.split(self.tutorial_dir)
Expand All @@ -98,9 +98,7 @@ class Test_MSS_AboutDialog:
@pytest.fixture(autouse=True)
def setup(self, qtbot):
self.window = msui_mw.MSUI_AboutDialog()
yield
self.window.hide()
QtWidgets.QApplication.processEvents()
qtbot.add_widget(self.window)

def test_milestone_url(self):
with urlopen(self.window.milestone_url) as f:
Expand All @@ -113,12 +111,11 @@ class Test_MSS_ShortcutDialog:
@pytest.fixture(autouse=True)
def setup(self, qtbot):
self.main_window = msui_mw.MSUIMainWindow()
self.main_window.show()
qtbot.add_widget(self.main_window, before_close_func=set_force_close)
with qtbot.wait_active(self.main_window):
self.main_window.show()
self.main_window.activateWindow()
self.shortcuts = msui_mw.MSUI_ShortcutsDialog()
yield
self.shortcuts.hide()
self.main_window.hide()
QtWidgets.QApplication.processEvents()

def test_shortcuts_present(self):
# Assert list gets filled properly
Expand Down Expand Up @@ -179,6 +176,7 @@ def setup(self, qtbot):
'data/')

self.window = msui.MSUIMainWindow()
qtbot.add_widget(self.window, before_close_func=set_force_close)
self.window.create_new_flight_track()
self.window.show()
QtWidgets.QApplication.processEvents()
Expand All @@ -190,10 +188,6 @@ def setup(self, qtbot):
'empty_msui_settings.json',
)
read_config_file(path=config_file)
for i in range(self.window.listViews.count()):
self.window.listViews.item(i).window.hide()
self.window.hide()
QtWidgets.QApplication.processEvents()

def test_no_updater(self):
assert not hasattr(self.window, "updater")
Expand Down Expand Up @@ -254,7 +248,6 @@ def test_open_config(self, mockbox):
pytest.skip("To be done")
self.window.actionConfigurationEditor.trigger()
QtWidgets.QApplication.processEvents()
self.window.config_editor.close()
assert mockbox.critical.call_count == 0

@mock.patch("PyQt5.QtWidgets.QMessageBox")
Expand Down
6 changes: 3 additions & 3 deletions tests/_test_msui/test_multiple_flightpath_dockwidget.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@
from mslib.msui import msui
from mslib.msui.multiple_flightpath_dockwidget import MultipleFlightpathControlWidget
from mslib.msui import flighttrack as ft
from tests.utils import set_force_close
import mslib.msui.topview as tv


class Test_MultipleFlightpathControlWidget:
@pytest.fixture(autouse=True)
def setup(self, qtbot):
self.window = msui.MSUIMainWindow()
qtbot.add_widget(self.window, before_close_func=set_force_close)
self.window.create_new_flight_track()

self.window.actionNewFlightTrack.trigger()
Expand All @@ -46,13 +48,11 @@ def setup(self, qtbot):
0, rows=len(initial_waypoints), waypoints=initial_waypoints)

self.widget = tv.MSUITopViewWindow(model=self.waypoints_model, mainwindow=self.window)
qtbot.add_widget(self.widget, before_close_func=set_force_close)
self.window.show()
QtWidgets.QApplication.processEvents()
QtTest.QTest.qWaitForWindowExposed(self.window)
QtWidgets.QApplication.processEvents()
yield
self.window.hide()
QtWidgets.QApplication.processEvents()

def test_initialization(self):
widget = MultipleFlightpathControlWidget(parent=self.widget,
Expand Down
1 change: 1 addition & 0 deletions tests/_test_msui/test_remotesensing.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ def setup(self, qtbot):
datetime.datetime(2023, 4, 15, 11, 18, 27, 735581)]
self.solar_type = ('sun', 'total (horizon)')
self.remote_widget = RemoteSensingControlWidget(view=self.view)
qtbot.add_widget(self.remote_widget)

@pytest.mark.parametrize(
"lon0, lat0, h0, lon1, lat1, h1, obs_azi, expected",
Expand Down
Loading

0 comments on commit 3e95b6e

Please sign in to comment.