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 Mar 21, 2024
1 parent cd0f5fb commit 6e4ed44
Show file tree
Hide file tree
Showing 22 changed files with 76 additions and 81 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
1 change: 0 additions & 1 deletion tests/_test_msui/test_editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ def setup(self, qtbot):
yield
if os.path.exists(self.save_file_name):
os.remove(self.save_file_name)
self.window.hide()

@mock.patch("mslib.msui.editor.get_open_filename", return_value=sample_file)
def test_file_open(self, mockfile):
Expand Down
2 changes: 1 addition & 1 deletion tests/_test_msui/test_kmloverlay_dockwidget.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,13 @@ 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()
QtTest.QTest.qWaitForWindowExposed(self.window)
# start load test
self.window.select_all()
self.window.remove_file()
yield
self.window.close()
if os.path.exists(save_kml):
os.remove(save_kml)

Expand Down
9 changes: 4 additions & 5 deletions tests/_test_msui/test_linearview.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,16 @@
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 set_force_close


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()
QtTest.QTest.qWaitForWindowExposed(self.window)
yield
self.window.hide()

def test_show(self):
pass
Expand All @@ -62,10 +62,9 @@ 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()
QtTest.QTest.qWaitForWindowExposed(self.window)
yield
self.window.hide()

def test_open_wms(self):
self.window.cbTools.currentIndexChanged.emit(1)
Expand Down Expand Up @@ -97,13 +96,13 @@ def setup(self, qtbot, mswms_server):
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()
QtTest.QTest.qWaitForWindowExposed(self.window)
self.window.cbTools.currentIndexChanged.emit(1)
self.wms_control = self.window.docks[0].widget()
self.wms_control.multilayers.cbWMS_URL.setEditText("")
yield
self.window.hide()
shutil.rmtree(self.tempdir)

def query_server(self, qtbot, url):
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
13 changes: 3 additions & 10 deletions tests/_test_msui/test_mscolab.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,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 create_msui_settings_file, ExceptionMock
from tests.utils import create_msui_settings_file, ExceptionMock, set_force_close
from mslib.msui import msui
from mslib.msui import mscolab
from mslib.mscolab.seed import add_user, get_user, add_operation, add_user_to_operation
Expand All @@ -57,6 +57,7 @@ def setup(self, qtbot, mscolab_server):
self.user = get_user(self.userdata[0])

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 @@ -69,8 +70,6 @@ def setup(self, qtbot, mscolab_server):
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()

def test_url_combo(self):
assert self.window.urlCb.count() >= 1
Expand Down Expand Up @@ -279,21 +278,15 @@ def setup(self, qtbot, mscolab_app, mscolab_server):
assert add_user_to_operation(path=self.operation_name3, access_level="collaborator", emailid=self.userdata3[0])

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()

self.total_created_operations = 0
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()

def test_activate_operation(self, qtbot):
self._connect_to_mscolab(qtbot)
Expand Down
9 changes: 3 additions & 6 deletions tests/_test_msui/test_mscolab_admin_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@
See the License for the specific language governing permissions and
limitations under the License.
"""
import mock
import pytest

from mslib.mscolab.conf import mscolab_settings
from PyQt5 import QtCore, QtTest, QtWidgets
from PyQt5 import QtCore, QtTest
from tests.utils import set_force_close
from mslib.msui import mscolab
from mslib.msui import msui
from mslib.mscolab.seed import add_user, get_user, add_operation, add_user_to_operation
Expand Down Expand Up @@ -59,6 +59,7 @@ def setup(self, qtbot, mscolab_server):
assert add_user_to_operation(path="tokyo", emailid=self.userdata[0], access_level="creator")

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,12 +73,8 @@ def setup(self, qtbot, mscolab_server):
QtTest.QTest.qWaitForWindowExposed(self.window)
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()

def test_permission_filter(self):
len_added_users = self.admin_window.modifyUsersTable.rowCount()
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 @@ -33,7 +33,7 @@
from mslib.mscolab.conf import mscolab_settings
from PyQt5 import QtCore, QtTest
from tests.utils import (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.utils.config import modify_config_file
Expand All @@ -45,6 +45,7 @@ def setup(self, qtbot, mscolab_app, mscolab_server):
self.app = mscolab_app
self.url = mscolab_server
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 @@ -57,8 +58,6 @@ def setup(self, qtbot, mscolab_app, mscolab_server):
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()

Expand Down
5 changes: 2 additions & 3 deletions tests/_test_msui/test_mscolab_operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from mslib.mscolab.conf import mscolab_settings
from mslib.mscolab.models import Message
from PyQt5 import QtCore, QtTest, QtWidgets
from tests.utils import set_force_close
from mslib.msui import mscolab
from mslib.msui import msui
from mslib.mscolab.seed import add_user, get_user, add_operation, add_user_to_operation
Expand All @@ -55,6 +56,7 @@ def setup(self, qtbot, mscolab_app, mscolab_server):
assert add_user_to_operation(path=self.operation_name, emailid=self.userdata[0])
self.user = get_user(self.userdata[0])
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 @@ -68,11 +70,8 @@ def setup(self, qtbot, mscolab_app, mscolab_server):
QtTest.QTest.qWaitForWindowExposed(self.window)
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()

def test_send_message(self, qtbot):
self._send_message(qtbot, "**test message**")
Expand Down
4 changes: 2 additions & 2 deletions tests/_test_msui/test_mscolab_version_history.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import pytest
import mock

from tests.utils import set_force_close
from mslib.mscolab.conf import mscolab_settings
from PyQt5 import QtCore, QtTest, QtWidgets
from mslib.msui import mscolab
Expand All @@ -46,6 +47,7 @@ def setup(self, qtbot, mscolab_server):
assert add_user_to_operation(path=self.operation_name, emailid=self.userdata[0])
self.user = get_user(self.userdata[0])
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 @@ -60,8 +62,6 @@ def setup(self, qtbot, mscolab_server):
QtTest.QTest.qWaitForWindowExposed(self.window)
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()

Expand Down
24 changes: 11 additions & 13 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,14 +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()

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

def test_milestone_url(self):
with urlopen(self.window.milestone_url) as f:
Expand All @@ -111,11 +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()

def test_shortcuts_present(self):
# Assert list gets filled properly
Expand Down Expand Up @@ -176,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()
QtTest.QTest.qWaitForWindowExposed(self.window)
Expand All @@ -185,9 +186,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()

def test_no_updater(self):
assert not hasattr(self.window, "updater")
Expand Down
5 changes: 3 additions & 2 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,10 +48,9 @@ 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()
QtTest.QTest.qWaitForWindowExposed(self.window)
yield
self.window.hide()

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
3 changes: 1 addition & 2 deletions tests/_test_msui/test_satellite_dockwidget.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,9 @@ class Test_SatelliteDockWidget:
def setup(self, qtbot):
self.view = mock.Mock()
self.window = sd.SatelliteControlWidget(view=self.view)
qtbot.add_widget(self.window)
self.window.show()
QtTest.QTest.qWaitForWindowExposed(self.window)
yield
self.window.hide()

def test_load(self):
path = os.path.join(os.path.dirname(__file__), "../", "data", "satellite_predictor.txt")
Expand Down
Loading

0 comments on commit 6e4ed44

Please sign in to comment.