Skip to content

Commit

Permalink
Remove LOAD_DELAY, use wait until conditions
Browse files Browse the repository at this point in the history
Rather that wiat a fixed time, wait until a task such as loading is
complete.
  • Loading branch information
samtygier-stfc committed Aug 24, 2021
1 parent efb9a03 commit dbbaf48
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 8 deletions.
30 changes: 28 additions & 2 deletions mantidimaging/gui/test/gui_system_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import os
from pathlib import Path
from typing import Callable
import unittest
from unittest import mock

Expand All @@ -24,7 +25,6 @@

SHOW_DELAY = 10 # Can be increased to watch tests
SHORT_DELAY = 100
LOAD_DELAY = 5000


@pytest.mark.system
Expand Down Expand Up @@ -58,15 +58,41 @@ def _click_messageBox(cls, button_text: str):
def _close_welcome(self):
self.main_window.welcome_window.view.close()

@classmethod
def _wait_until(cls, test_func: Callable[[], bool], delay=0.1, max_retry=100):
"""
Repeat test_func every delay seconds until is becomes true. Or if max_retry is reached return false.
"""
for _ in range(max_retry):
if test_func():
return True
QTest.qWait(delay * 1000)
raise RuntimeError("_wait_until reach max retries")

@classmethod
def _wait_for_widget_visible(cls, widget_type, delay=0.1, max_retry=100):
for _ in range(max_retry):
for widget in cls.app.topLevelWidgets():
if isinstance(widget, widget_type) and widget.isVisible():
return True
QTest.qWait(delay * 1000)
raise RuntimeError("_wait_for_stack_selector reach max retries")

@mock.patch("mantidimaging.gui.windows.load_dialog.view.MWLoadDialog.select_file")
def _load_data_set(self, mocked_select_file):
mocked_select_file.return_value = LOAD_SAMPLE
initial_stacks = len(self.main_window.presenter.model.get_all_stack_visualisers())

def test_func() -> bool:
current_stacks = len(self.main_window.presenter.model.get_all_stack_visualisers())
return (current_stacks - initial_stacks) >= 5

self.main_window.actionLoadDataset.trigger()
QTest.qWait(SHOW_DELAY)
self.main_window.load_dialogue.presenter.notify(Notification.UPDATE_ALL_FIELDS)
QTest.qWait(SHOW_DELAY)
self.main_window.load_dialogue.accept()
QTest.qWait(LOAD_DELAY)
self._wait_until(test_func)

def _open_operations(self):
self.main_window.actionFilters.trigger()
Expand Down
21 changes: 15 additions & 6 deletions mantidimaging/gui/test/test_gui_system_loading.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@
from unittest import mock

from PyQt5.QtCore import QTimer, QEventLoop
from PyQt5.QtTest import QTest
from PyQt5.QtWidgets import QApplication

from mantidimaging.gui.test.gui_system_base import GuiSystemBase, SHORT_DELAY, LOAD_SAMPLE, LOAD_DELAY
from mantidimaging.gui.test.gui_system_base import GuiSystemBase, SHORT_DELAY, LOAD_SAMPLE
from mantidimaging.gui.widgets.stack_selector_dialog.stack_selector_dialog import StackSelectorDialog


Expand All @@ -20,11 +19,19 @@ def setUp(self) -> None:
@mock.patch("mantidimaging.gui.windows.main.MainWindowView._get_file_name")
def _load_images(self, mocked_select_file):
mocked_select_file.return_value = LOAD_SAMPLE
initial_stacks = len(self.main_window.presenter.model.get_all_stack_visualisers())

self.main_window.actionLoadImages.trigger()
QTest.qWait(LOAD_DELAY)

def test_func() -> bool:
current_stacks = len(self.main_window.presenter.model.get_all_stack_visualisers())
return (current_stacks - initial_stacks) >= 1

self._wait_until(test_func)

@classmethod
def _click_stack_selector(cls):
cls._wait_for_widget_visible(StackSelectorDialog)
for widget in cls.app.topLevelWidgets():
if isinstance(widget, StackSelectorDialog):
for x in range(20):
Expand All @@ -40,15 +47,17 @@ def _click_stack_selector(cls):
def test_load_180(self, mocked_select_file):
path_180 = Path(LOAD_SAMPLE).parents[1] / "180deg" / "IMAT_Flower_180deg_000000.tif"
mocked_select_file.return_value = path_180
self.assertEqual(len(self.main_window.presenter.get_all_stack_visualisers()), 0)
self._load_images()
stacks = self.main_window.presenter.get_all_stack_visualisers()

self.assertEqual(len(stacks), 1)
self.assertEqual(len(self.main_window.presenter.get_all_stack_visualisers()), 1)
self.assertFalse(stacks[0].presenter.images.has_proj180deg())

QTimer.singleShot(SHORT_DELAY * 10, lambda: self._click_stack_selector())
QTimer.singleShot(SHORT_DELAY, lambda: self._click_stack_selector())
self.main_window.actionLoad180deg.trigger()
QTest.qWait(LOAD_DELAY)

self._wait_until(lambda: len(self.main_window.presenter.get_all_stack_visualisers()) == 2)

stacks_after = self.main_window.presenter.get_all_stack_visualisers()
self.assertEqual(len(stacks_after), 2)
Expand Down

0 comments on commit dbbaf48

Please sign in to comment.