Example #1
0
def execute():
    # all data will be row-major, so this needs to be specified as the default is col-major
    pyqtgraph.setConfigOptions(imageAxisOrder="row-major")

    # create the GUI event loop
    q_application = QApplication(sys.argv)
    q_application.setApplicationName("Mantid Imaging")
    application_window = MainWindowView()

    sys.excepthook = lambda exc_type, exc_value, exc_traceback: application_window.uncaught_exception(
        "".join(traceback.format_exception_only(exc_type, exc_value)), "".join(
            traceback.format_exception(exc_type, exc_value, exc_traceback)))

    def dont_let_qt_shutdown_while_debugging(type, value, tback):
        # log the exception here
        logging.getLogger(__name__).error(
            f"Exception {type} encountered:\n{traceback.format_exception(type, value, tback)}"
        )
        # then call the default handler
        sys.__excepthook__(type, value, tback)

    if os.environ.get("PYDEVD_LOAD_VALUES_ASYNC", False):
        sys.excepthook = dont_let_qt_shutdown_while_debugging
    application_window.show()

    return sys.exit(q_application.exec_())
Example #2
0
 def setUp(self):
     # mock the view so it has the same methods
     with mock.patch(
             'mantidimaging.gui.windows.main.view.check_version_and_label'
     ) as mock_find_latest_version:
         self.window = MainWindowView()
         mock_find_latest_version.assert_called_once()
     self.window.remove_stack = mock.Mock()
     self.dock, self.view, self.test_data = self._add_stack_visualiser()
Example #3
0
 def setUp(self) -> None:
     with mock.patch(
             "mantidimaging.gui.windows.main.view.WelcomeScreenPresenter"):
         with mock.patch(
                 "mantidimaging.gui.windows.main.view.CommandLineArguments"
         ) as command_line_args:
             command_line_args.return_value.path.return_value = ""
             command_line_args.return_value.operation.return_value = ""
             command_line_args.return_value.recon.return_value = False
             self.view = MainWindowView()
     self.presenter = mock.MagicMock()
     self.view.presenter = self.presenter
Example #4
0
    def setUp(self):
        # mock the view so it has the same methods
        with mock.patch(
                'mantidimaging.gui.windows.main.view.find_if_latest_version'
        ) as mock_find_latest_version:
            self.window = MainWindowView()
            mock_find_latest_version.assert_called_once()
        self.window.remove_stack = mock.Mock()

        self.dock = QDockWidget()
        self.dock.setWindowTitle("Potatoes")

        self.test_data = th.generate_images(automatic_free=False)
        self.view = StackVisualiserView(self.window, self.dock, self.test_data)
        self.dock.setWidget(self.view)
        self.window.addDockWidget(QtCore.Qt.TopDockWidgetArea, self.dock)
Example #5
0
 def test_command_line_dont_show_recon_window(self, recon_window,
                                              welcome_screen_presenter,
                                              command_line_args):
     command_line_args.return_value.path.return_value = ""
     command_line_args.return_value.recon.return_value = False
     command_line_args.return_value.operation.return_value = ""
     MainWindowView()
     recon_window.assert_not_called()
Example #6
0
 def test_command_line_show_recon_window(self, recon_window,
                                         welcome_screen_presenter,
                                         command_line_args):
     command_line_args.return_value.path.return_value = ""
     command_line_args.return_value.recon.return_value = True
     command_line_args.return_value.operation.return_value = ""
     view = MainWindowView()
     recon_window.assert_called_once_with(view)
Example #7
0
 def test_command_line_no_path_argument_set(self, main_window_presenter,
                                            welcome_screen_presenter,
                                            command_line_args):
     command_line_args.return_value.path.return_value = ""
     command_line_args.return_value.recon.return_value = False
     command_line_args.return_value.operation.return_value = ""
     MainWindowView()
     main_window_presenter.return_value.load_stacks_from_folder.assert_not_called(
     )
Example #8
0
 def test_load_path_from_command_line(self, main_window_presenter,
                                      welcome_screen_presenter,
                                      command_line_args):
     command_line_args.return_value.path.return_value = test_path = "./"
     command_line_args.return_value.recon.return_value = False
     command_line_args.return_value.operation.return_value = ""
     MainWindowView()
     main_window_presenter.return_value.load_stacks_from_folder.assert_called_once_with(
         test_path)
Example #9
0
    def test_command_line_show_filters_window(self, main_window_presenter,
                                              welcome_screen_presenter,
                                              command_line_args):
        command_line_args.return_value.path.return_value = ""
        command_line_args.return_value.recon.return_value = False
        command_line_args.return_value.operation.return_value = command_line_filter = "Median"

        MainWindowView()
        main_window_presenter.return_value.show_operation.assert_called_once_with(
            command_line_filter)
Example #10
0
class StackVisualiserViewTest(unittest.TestCase):
    test_data: Images
    window: MainWindowView

    def setUp(self):
        with mock.patch("mantidimaging.gui.windows.main.view.WelcomeScreenPresenter"):
            self.window = MainWindowView()
        self.view, self.test_data = self._add_stack_visualiser()

    def _add_stack_visualiser(self) -> Tuple[StackVisualiserView, Images]:
        test_data = th.generate_images()
        test_data.name = "Test Data"
        self.window.create_new_stack(test_data)
        view = self.window.get_stack_with_images(test_data)
        return view, test_data

    def test_name(self):
        title = "Potatoes"
        self.view.setWindowTitle(title)
        self.assertEqual(title, self.view.name)

    def _roi_updated_callback(self, roi):
        self.assertIsInstance(roi, SensibleROI)

        self.assertEqual(roi.left, 1)
        self.assertEqual(roi.top, 2)
        self.assertEqual(roi.right, 3)
        self.assertEqual(roi.bottom, 4)

        self.roi_callback_was_called = True

    def test_roi_changed_callback(self):
        self.roi_callback_was_called = False
        self.view.roi_updated.connect(self._roi_updated_callback)

        self.view.roi_changed_callback(SensibleROI(1, 2, 3, 4))

        self.assertTrue(self.roi_callback_was_called)
Example #11
0
 def setUp(self) -> None:
     with mock.patch("mantidimaging.gui.windows.main.view.WelcomeScreenPresenter"):
         self.main_window = MainWindowView()
     self.view = ReconstructWindowView(self.main_window)
     self.view.presenter = self.presenter = mock.Mock()
     self.view.image_view = self.image_view = mock.Mock()
     self.view.tableView = self.tableView = mock.Mock()
     self.view.resultCor = self.resultCor = mock.Mock()
     self.view.resultTilt = self.resultTilt = mock.Mock()
     self.view.resultSlope = self.resultSlope = mock.Mock()
     self.view.numIter = self.numIter = mock.Mock()
     self.view.pixelSize = self.pixelSize = mock.Mock()
     self.view.alphaSpinbox = self.alpha = mock.Mock()
     self.view.algorithmName = self.algorithmName = mock.Mock()
     self.view.filterName = self.filterName = mock.Mock()
     self.view.maxProjAngle = self.maxProjAngle = mock.Mock()
     self.view.autoFindMethod = self.autoFindMethod = mock.Mock()
Example #12
0
class MainWindowViewTest(unittest.TestCase):
    def setUp(self) -> None:
        with mock.patch(
                "mantidimaging.gui.windows.main.view.WelcomeScreenPresenter"):
            with mock.patch(
                    "mantidimaging.gui.windows.main.view.CommandLineArguments"
            ) as command_line_args:
                command_line_args.return_value.path.return_value = ""
                command_line_args.return_value.operation.return_value = ""
                command_line_args.return_value.recon.return_value = False
                self.view = MainWindowView()
        self.presenter = mock.MagicMock()
        self.view.presenter = self.presenter

    def test_execute_save(self):
        self.view.execute_save()

        self.presenter.notify.assert_called_once_with(PresNotification.SAVE)

    def test_find_images_stack_title(self):
        images = mock.MagicMock()
        self.presenter.get_stack_with_images = mock.MagicMock()

        return_value = self.view.find_images_stack_title(images)

        self.presenter.get_stack_with_images.assert_called_once_with(images)
        self.assertEqual(
            return_value,
            self.presenter.get_stack_with_images.return_value.name)

    @mock.patch("mantidimaging.gui.windows.main.view.DatasetSelectorDialog")
    @mock.patch(
        "mantidimaging.gui.windows.main.view.QFileDialog.getOpenFileName")
    def test_load_180_deg_dialog(self, get_open_file_name: mock.Mock,
                                 dataset_selector_dialog: mock.Mock):
        dataset_selector_dialog.return_value.exec.return_value = QDialog.DialogCode.Accepted
        dataset_id = "dataset-id"
        dataset_selector_dialog.return_value.selected_dataset = dataset_id
        selected_file = "~/home/test/directory/selected_file.tif"
        get_open_file_name.return_value = (selected_file, None)
        _180_dataset = mock.MagicMock()
        self.presenter.add_180_deg_to_dataset.return_value = _180_dataset
        self.view.create_new_180_stack = mock.MagicMock()  # type: ignore

        self.view.load_180_deg_dialog()

        dataset_selector_dialog.assert_called_once_with(
            main_window=self.view, title='Dataset Selector')
        get_open_file_name.assert_called_once_with(
            caption="180 Degree Image",
            filter="Image File (*.tif *.tiff);;All (*.*)",
            initialFilter="Image File (*.tif *.tiff)")
        self.presenter.add_180_deg_to_dataset.assert_called_once_with(
            dataset_id=dataset_id, _180_deg_file=selected_file)
        self.view.create_new_180_stack.assert_called_once_with(_180_dataset)

    def test_execute_load(self):
        self.view.execute_load()

        self.presenter.notify.assert_called_once_with(PresNotification.LOAD)

    def test_execute_nexus_load(self):
        self.view.execute_nexus_load()
        self.presenter.notify.assert_called_once_with(
            PresNotification.NEXUS_LOAD)

    @mock.patch("mantidimaging.gui.windows.main.view.MWLoadDialog")
    def test_show_load_dialogue(self, mock_load: mock.Mock):
        self.view.show_load_dialogue()

        mock_load.assert_called_once_with(self.view)
        mock_load.return_value.show.assert_called_once_with()

    @mock.patch("mantidimaging.gui.windows.main.view.WizardPresenter")
    def test_show_wizard(self, mock_wizard: mock.Mock):
        self.view.show_wizard()

        mock_wizard.assert_called_once_with(self.view)
        mock_wizard.return_value.show.assert_called_once_with()

    @mock.patch("mantidimaging.gui.windows.main.view.ReconstructWindowView")
    def test_show_recon_window(self, mock_recon: mock.Mock):
        self.view.show_recon_window()

        mock_recon.assert_called_once_with(self.view)
        mock_recon.return_value.show.assert_called_once_with()
        mock_recon.return_value.activateWindow.assert_not_called()
        mock_recon.return_value.raise_.assert_not_called()

        self.view.show_recon_window()
        mock_recon.assert_called_once_with(self.view)
        mock_recon.return_value.activateWindow.assert_called_once_with()
        mock_recon.return_value.raise_.assert_called_once_with()

    @mock.patch("mantidimaging.gui.windows.main.view.FiltersWindowView")
    def test_show_filters_window(self, mock_filters: mock.Mock):
        self.view.show_filters_window()

        mock_filters.assert_called_once_with(self.view)
        mock_filters.return_value.show.assert_called_once_with()
        mock_filters.return_value.activateWindow.assert_not_called()
        mock_filters.return_value.raise_.assert_not_called()

        self.view.show_filters_window()
        mock_filters.assert_called_once_with(self.view)
        mock_filters.return_value.activateWindow.assert_called_once_with()
        mock_filters.return_value.raise_.assert_called_once_with()

    def test_create_new_stack(self):
        images = generate_images()
        self.view.create_new_stack(images)

        self.presenter.create_new_stack.assert_called_once_with(images)

    def test_update_stack_with_images(self):
        images = generate_images()
        self.view.update_stack_with_images(images)

        self.presenter.update_stack_with_images.assert_called_once_with(images)

    def test_rename_stack(self):
        self.view.rename_stack("apples", "oranges")

        self.presenter.notify.assert_called_once_with(
            PresNotification.RENAME_STACK,
            current_name="apples",
            new_name="oranges")

    @mock.patch("mantidimaging.gui.windows.main.view.getLogger")
    @mock.patch("mantidimaging.gui.windows.main.view.QMessageBox")
    def test_uncaught_exception(self, mock_qtmessagebox, mock_getlogger):
        self.view.uncaught_exception("user-error", "log-error")

        mock_qtmessagebox.critical.assert_called_once_with(
            self.view, self.view.UNCAUGHT_EXCEPTION, "user-error")
        mock_getlogger.return_value.error.assert_called_once_with("log-error")

    @mock.patch("mantidimaging.gui.windows.main.view.WelcomeScreenPresenter")
    def test_show_about(self, mock_welcomescreen: mock.Mock):
        self.view.show_about()
        mock_welcomescreen.assert_called_once_with(self.view)

    @mock.patch("mantidimaging.gui.windows.main.view.QDesktopServices")
    def test_open_online_documentation(self, mock_qtdeskserv: mock.Mock):
        self.view.open_online_documentation()
        mock_qtdeskserv.openUrl.assert_called_once()

    @mock.patch.multiple("mantidimaging.gui.windows.main.view.MainWindowView",
                         setCentralWidget=DEFAULT,
                         addDockWidget=DEFAULT)
    @mock.patch("mantidimaging.gui.windows.main.view.StackVisualiserView")
    def test_create_stack_window(self,
                                 mock_sv: mock.Mock,
                                 setCentralWidget: Mock = Mock(),
                                 addDockWidget: Mock = Mock()):
        images = generate_images()
        position = "test_position"
        floating = False

        self.view.splitter = splitter_mock = mock.Mock()

        self.view.create_stack_window(images,
                                      position=position,
                                      floating=floating)

        mock_sv.assert_called_once_with(self.view, images)
        dock = mock_sv.return_value
        setCentralWidget.assert_called_once_with(splitter_mock)
        addDockWidget.assert_called_once_with(position, dock)

        splitter_mock.addWidget.assert_called_once_with(mock_sv.return_value)

        dock.setFloating.assert_called_once_with(floating)

    @mock.patch("mantidimaging.gui.windows.main.view.QMessageBox")
    @mock.patch("mantidimaging.gui.windows.main.view.ProjectionAngleFileParser"
                )
    @mock.patch("mantidimaging.gui.windows.main.view.StackSelectorDialog")
    @mock.patch(
        "mantidimaging.gui.windows.main.view.QFileDialog.getOpenFileName")
    def test_load_projection_angles(self, getOpenFileName: mock.Mock,
                                    StackSelectorDialog: mock.Mock,
                                    ProjectionAngleFileParser: Mock,
                                    QMessageBox: Mock):
        StackSelectorDialog.return_value.exec.return_value = QDialog.DialogCode.Accepted
        selected_stack = "selected_stack"
        StackSelectorDialog.return_value.selected_stack = selected_stack

        selected_file = "~/home/test/directory/selected_file.txt"
        getOpenFileName.return_value = (selected_file, None)

        proj_angles = ProjectionAngles(np.arange(0, 10))
        ProjectionAngleFileParser.return_value.get_projection_angles.return_value = proj_angles

        self.view.load_projection_angles()

        StackSelectorDialog.assert_called_once_with(
            main_window=self.view,
            title='Stack Selector',
            message=self.view.LOAD_PROJECTION_ANGLES_DIALOG_MESSAGE)
        getOpenFileName.assert_called_once_with(
            caption=self.view.LOAD_PROJECTION_ANGLES_FILE_DIALOG_CAPTION,
            filter="All (*.*)")

        self.presenter.add_projection_angles_to_sample.assert_called_once_with(
            selected_stack, proj_angles)
        QMessageBox.information.assert_called_once()

    def test_update_shortcuts_with_presenter_with_one_or_more_stacks(self):
        self.presenter.stacks = {"a": mock.Mock(), "b": mock.Mock()}

        self._update_shortcuts_test(False, True)
        self._update_shortcuts_test(True, True)

    def test_update_shortcuts_with_presenter_with_no_stacks(self):
        self.presenter.stacks = dict()

        self._update_shortcuts_test(False, False)
        self._update_shortcuts_test(True, False)

    def _update_shortcuts_test(self, original_state, expected_state):
        self.view.actionSave.setEnabled(original_state)
        self.view.actionSampleLoadLog.setEnabled(original_state)
        self.view.actionLoad180deg.setEnabled(original_state)
        self.view.actionLoadProjectionAngles.setEnabled(original_state)
        self.view.menuWorkflow.setEnabled(original_state)
        self.view.menuImage.setEnabled(original_state)

        self.view.update_shortcuts()

        self.assertEqual(expected_state, self.view.actionSave.isEnabled())
        self.assertEqual(expected_state,
                         self.view.actionSampleLoadLog.isEnabled())
        self.assertEqual(expected_state,
                         self.view.actionLoad180deg.isEnabled())
        self.assertEqual(expected_state,
                         self.view.actionLoadProjectionAngles.isEnabled())
        self.assertEqual(expected_state, self.view.menuWorkflow.isEnabled())
        self.assertEqual(expected_state, self.view.menuImage.isEnabled())

    @mock.patch("mantidimaging.gui.windows.main.view.populate_menu")
    def test_populate_image_menu_with_no_stack(self, populate_menu):
        self.view.menuImage = mock.MagicMock()
        self.view.current_showing_stack = mock.MagicMock(return_value=None)

        self.view.populate_image_menu()

        populate_menu.assert_not_called()
        self.view.menuImage.addAction.assert_called_once_with(
            "No stack loaded!")

    @mock.patch("mantidimaging.gui.windows.main.view.populate_menu")
    def test_populate_image_menu_with_stack_and_actions(self, populate_menu):
        self.view.menuImage = mock.MagicMock()
        stack = mock.MagicMock()
        actions = mock.MagicMock()
        stack.actions = actions
        self.view.current_showing_stack = mock.MagicMock(return_value=stack)

        self.view.populate_image_menu()

        populate_menu.assert_called_once_with(self.view.menuImage, actions)
        self.view.menuImage.addAction.assert_not_called()

    @mock.patch("mantidimaging.gui.windows.main.view.StackVisualiserView")
    def test_current_showing_stack_with_stack_with_visible(
            self, stack_visualiser_view):
        stack = mock.MagicMock()
        stack.visibleRegion.return_value.isEmpty.return_value = False
        self.view.findChildren = mock.MagicMock(return_value=[stack])

        current_stack = self.view.current_showing_stack()

        self.assertEqual(stack, current_stack)
        stack.visibleRegion.assert_called_once()
        stack.visibleRegion.return_value.isEmpty.assert_called_once()
        self.view.findChildren.assert_called_once_with(stack_visualiser_view)

    @mock.patch("mantidimaging.gui.windows.main.view.StackVisualiserView")
    def test_current_showing_stack_with_stack_not_visible(
            self, stack_visualiser_view):
        stack = mock.MagicMock()
        stack.visibleRegion.return_value.isEmpty.return_value = True
        self.view.findChildren = mock.MagicMock(return_value=[stack])

        current_stack = self.view.current_showing_stack()

        self.assertEqual(None, current_stack)
        stack.visibleRegion.assert_called_once()
        stack.visibleRegion.return_value.isEmpty.assert_called_once()
        self.view.findChildren.assert_called_once_with(stack_visualiser_view)

    @mock.patch("mantidimaging.gui.windows.main.view.StackVisualiserView")
    def test_current_showing_stack_no_stack(self, stack_visualiser_view):
        stack = mock.MagicMock()
        self.view.findChildren = mock.MagicMock(return_value=[])

        current_stack = self.view.current_showing_stack()

        self.assertEqual(None, current_stack)
        stack.visibleRegion.assert_not_called()
        stack.visibleRegion.return_value.isEmpty.assert_not_called()
        self.view.findChildren.assert_called_once_with(stack_visualiser_view)

    def test_get_images_from_stack_uuid(self):
        uuid = uuid4()
        images = mock.MagicMock()
        self.presenter.get_stack_visualiser.return_value.presenter.images = images

        return_value = self.view.get_images_from_stack_uuid(uuid)

        self.presenter.get_stack_visualiser.assert_called_once_with(uuid)
        self.assertEqual(images, return_value)

    def test_load_image_stack(self):
        selected_file = "file_name"
        self.view._get_file_name = mock.MagicMock(return_value=selected_file)

        self.view.load_image_stack()

        self.presenter.load_image_stack.assert_called_once_with(selected_file)
        self.view._get_file_name.assert_called_once_with(
            "Image", "Image File (*.tif *.tiff)")

    def test_show_nexus_load_dialog_calls_show(self):
        self.view._get_file_name = mock.MagicMock()
        with mock.patch("mantidimaging.gui.windows.main.view.NexusLoadDialog"
                        ) as nexus_load_dialog:
            self.view.show_load_nexus_dialog()
        nexus_load_dialog.assert_called_once()
        nexus_load_dialog.return_value.show.assert_called_once()

    @mock.patch("mantidimaging.gui.windows.main.view.CommandLineArguments")
    @mock.patch("mantidimaging.gui.windows.main.view.WelcomeScreenPresenter")
    @mock.patch("mantidimaging.gui.windows.main.view.MainWindowPresenter")
    def test_load_path_from_command_line(self, main_window_presenter,
                                         welcome_screen_presenter,
                                         command_line_args):
        command_line_args.return_value.path.return_value = test_path = "./"
        command_line_args.return_value.recon.return_value = False
        command_line_args.return_value.operation.return_value = ""
        MainWindowView()
        main_window_presenter.return_value.load_stacks_from_folder.assert_called_once_with(
            test_path)

    @mock.patch("mantidimaging.gui.windows.main.view.CommandLineArguments")
    @mock.patch("mantidimaging.gui.windows.main.view.WelcomeScreenPresenter")
    @mock.patch("mantidimaging.gui.windows.main.view.MainWindowPresenter")
    def test_command_line_no_path_argument_set(self, main_window_presenter,
                                               welcome_screen_presenter,
                                               command_line_args):
        command_line_args.return_value.path.return_value = ""
        command_line_args.return_value.recon.return_value = False
        command_line_args.return_value.operation.return_value = ""
        MainWindowView()
        main_window_presenter.return_value.load_stacks_from_folder.assert_not_called(
        )

    @mock.patch("mantidimaging.gui.windows.main.view.CommandLineArguments")
    @mock.patch("mantidimaging.gui.windows.main.view.WelcomeScreenPresenter")
    @mock.patch("mantidimaging.gui.windows.main.view.MainWindowPresenter")
    def test_command_line_show_filters_window(self, main_window_presenter,
                                              welcome_screen_presenter,
                                              command_line_args):
        command_line_args.return_value.path.return_value = ""
        command_line_args.return_value.recon.return_value = False
        command_line_args.return_value.operation.return_value = command_line_filter = "Median"

        MainWindowView()
        main_window_presenter.return_value.show_operation.assert_called_once_with(
            command_line_filter)

    @mock.patch("mantidimaging.gui.windows.main.view.CommandLineArguments")
    @mock.patch("mantidimaging.gui.windows.main.view.WelcomeScreenPresenter")
    @mock.patch("mantidimaging.gui.windows.main.view.FiltersWindowView")
    def test_command_line_dont_show_filters_window(self, filters_window,
                                                   welcome_screen_presenter,
                                                   command_line_args):
        command_line_args.return_value.path.return_value = ""
        command_line_args.return_value.recon.return_value = False
        command_line_args.return_value.operation.return_value = ""
        MainWindowView()
        filters_window.assert_not_called()

    @mock.patch("mantidimaging.gui.windows.main.view.CommandLineArguments")
    @mock.patch("mantidimaging.gui.windows.main.view.WelcomeScreenPresenter")
    @mock.patch("mantidimaging.gui.windows.main.view.ReconstructWindowView")
    def test_command_line_show_recon_window(self, recon_window,
                                            welcome_screen_presenter,
                                            command_line_args):
        command_line_args.return_value.path.return_value = ""
        command_line_args.return_value.recon.return_value = True
        command_line_args.return_value.operation.return_value = ""
        view = MainWindowView()
        recon_window.assert_called_once_with(view)

    @mock.patch("mantidimaging.gui.windows.main.view.CommandLineArguments")
    @mock.patch("mantidimaging.gui.windows.main.view.WelcomeScreenPresenter")
    @mock.patch("mantidimaging.gui.windows.main.view.ReconstructWindowView")
    def test_command_line_dont_show_recon_window(self, recon_window,
                                                 welcome_screen_presenter,
                                                 command_line_args):
        command_line_args.return_value.path.return_value = ""
        command_line_args.return_value.recon.return_value = False
        command_line_args.return_value.operation.return_value = ""
        MainWindowView()
        recon_window.assert_not_called()
Example #13
0
def setup_application():
    q_application = QApplication(sys.argv)
    q_application.setApplicationName("Mantid Imaging")
    q_application.setOrganizationName("mantidproject")
    q_application.setOrganizationDomain("mantidproject.org")
    return q_application, MainWindowView()
 def setUp(self) -> None:
     with mock.patch(
             "mantidimaging.gui.windows.main.view.WelcomeScreenPresenter"):
         self.view = MainWindowView()
     self.presenter = mock.MagicMock()
     self.view.presenter = self.presenter
Example #15
0
 def setUp(self):
     # mock the view so it has the same methods
     with mock.patch(
             "mantidimaging.gui.windows.main.view.WelcomeScreenPresenter"):
         self.main_window = MainWindowView()
     self.window = FiltersWindowView(self.main_window)
class GuiSystemBase(unittest.TestCase):
    app: QApplication

    def setUp(self) -> None:
        self.main_window = MainWindowView()
        self.main_window.show()
        QTest.qWait(SHORT_DELAY)

    def tearDown(self) -> None:
        QTimer.singleShot(SHORT_DELAY, lambda: self._click_messageBox("Yes"))
        self.main_window.close()
        QTest.qWait(SHORT_DELAY)

    @classmethod
    def _click_messageBox(cls, button_text: str):
        """Needs to be queued with QTimer.singleShot before triggering the message box"""
        for widget in cls.app.topLevelWidgets():
            if isinstance(widget, QMessageBox) and widget.isVisible():
                for button in widget.buttons():
                    if button.text().replace("&", "") == button_text:
                        QTest.mouseClick(button, Qt.LeftButton)
                        return
                button_texts = [button.text() for button in widget.buttons()]
                raise ValueError(
                    f"Could not find button '{button_text}' in {button_texts}.\n"
                    f"Message box: {widget.windowTitle()} {widget.text()}")

    @classmethod
    def _click_InputDialog(cls, set_int: Optional[int] = None):
        """Needs to be queued with QTimer.singleShot before triggering the message box"""
        for widget in cls.app.topLevelWidgets():
            if isinstance(widget, QInputDialog) and widget.isVisible():
                if set_int:
                    widget.setIntValue(set_int)
                QTest.qWait(SHORT_DELAY)
                widget.accept()

    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(int(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.get_active_stack_visualisers())

        def test_func() -> bool:
            current_stacks = len(
                self.main_window.presenter.get_active_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()
        self._wait_until(test_func, max_retry=600)

    def _open_operations(self):
        self.main_window.actionFilters.trigger()

    def _open_reconstruction(self):
        self.main_window.actionRecon.trigger()

    def _close_stack_tabs(self):
        while self.main_window.dataset_tree_widget.topLevelItemCount():
            self.main_window.dataset_tree_widget.topLevelItem(0).setSelected(
                True)
            self.main_window._delete_container()
class MainWindowViewTest(unittest.TestCase):
    def setUp(self) -> None:
        with mock.patch(
                "mantidimaging.gui.windows.main.view.WelcomeScreenPresenter"):
            self.view = MainWindowView()
        self.presenter = mock.MagicMock()
        self.view.presenter = self.presenter

    def test_execute_save(self):
        self.view.execute_save()

        self.presenter.notify.assert_called_once_with(PresNotification.SAVE)

    def test_find_images_stack_title(self):
        images = mock.MagicMock()
        self.presenter.get_stack_with_images = mock.MagicMock()

        return_value = self.view.find_images_stack_title(images)

        self.presenter.get_stack_with_images.assert_called_once_with(images)
        self.assertEqual(
            return_value,
            self.presenter.get_stack_with_images.return_value.name)

    @mock.patch("mantidimaging.gui.windows.main.view.StackSelectorDialog")
    @mock.patch(
        "mantidimaging.gui.windows.main.view.QFileDialog.getOpenFileName")
    def test_load_180_deg_dialog(self, get_open_file_name: mock.Mock,
                                 stack_selector_dialog: mock.Mock):
        stack_selector_dialog.return_value.exec.return_value = QDialog.Accepted
        selected_stack = "selected_stack"
        stack_selector_dialog.return_value.selected_stack = selected_stack
        selected_file = "~/home/test/directory/selected_file.tif"
        get_open_file_name.return_value = (selected_file, None)
        _180_dataset = mock.MagicMock()
        self.presenter.add_180_deg_to_sample.return_value = _180_dataset
        self.view.create_new_stack = mock.MagicMock()  # type: ignore
        selected_filename = "selected_file.tif"
        self.presenter.create_stack_name = mock.MagicMock(
            return_value=selected_filename)

        self.view.load_180_deg_dialog()

        stack_selector_dialog.assert_called_once_with(
            main_window=self.view,
            title='Stack Selector',
            message='Which stack is the 180 degree projection being loaded '
            'for?')
        get_open_file_name.assert_called_once_with(
            caption="180 Degree Image",
            filter="Image File (*.tif *.tiff);;All (*.*)",
            initialFilter="Image File (*.tif *.tiff)")
        self.presenter.add_180_deg_to_sample.assert_called_once_with(
            stack_name=selected_stack, _180_deg_file=selected_file)
        self.presenter.create_stack_name.assert_called_once_with(selected_file)
        self.view.create_new_stack.assert_called_once_with(
            _180_dataset, selected_filename)

    def test_execute_load(self):
        self.view.execute_load()

        self.presenter.notify.assert_called_once_with(PresNotification.LOAD)

    @mock.patch("mantidimaging.gui.windows.main.view.MWLoadDialog")
    def test_show_load_dialogue(self, mock_load: mock.Mock):
        self.view.show_load_dialogue()

        mock_load.assert_called_once_with(self.view)
        mock_load.return_value.show.assert_called_once_with()

    @mock.patch("mantidimaging.gui.windows.main.view.ReconstructWindowView")
    def test_show_recon_window(self, mock_recon: mock.Mock):
        self.view.show_recon_window()

        mock_recon.assert_called_once_with(self.view)
        mock_recon.return_value.show.assert_called_once_with()
        mock_recon.return_value.activateWindow.assert_not_called()
        mock_recon.return_value.raise_.assert_not_called()

        self.view.show_recon_window()
        mock_recon.assert_called_once_with(self.view)
        mock_recon.return_value.activateWindow.assert_called_once_with()
        mock_recon.return_value.raise_.assert_called_once_with()

    @mock.patch("mantidimaging.gui.windows.main.view.FiltersWindowView")
    def test_show_filters_window(self, mock_filters: mock.Mock):
        self.view.show_filters_window()

        mock_filters.assert_called_once_with(self.view)
        mock_filters.return_value.show.assert_called_once_with()
        mock_filters.return_value.activateWindow.assert_not_called()
        mock_filters.return_value.raise_.assert_not_called()

        self.view.show_filters_window()
        mock_filters.assert_called_once_with(self.view)
        mock_filters.return_value.activateWindow.assert_called_once_with()
        mock_filters.return_value.raise_.assert_called_once_with()

    def test_create_new_stack(self):
        images = generate_images()
        self.view.create_new_stack(images, "Test Title")

        self.presenter.create_new_stack.assert_called_once_with(
            images, "Test Title")

    def test_update_stack_with_images(self):
        images = generate_images()
        self.view.update_stack_with_images(images)

        self.presenter.update_stack_with_images.assert_called_once_with(images)

    def test_remove_stack(self):
        fake_stack_vis = mock.Mock()
        fake_stack_vis.uuid = "test-uuid"
        self.view.remove_stack(fake_stack_vis)

        self.presenter.notify.assert_called_once_with(
            PresNotification.REMOVE_STACK, uuid="test-uuid")

    def test_rename_stack(self):
        self.view.rename_stack("apples", "oranges")

        self.presenter.notify.assert_called_once_with(
            PresNotification.RENAME_STACK,
            current_name="apples",
            new_name="oranges")

    @mock.patch("mantidimaging.gui.windows.main.view.getLogger")
    @mock.patch("mantidimaging.gui.windows.main.view.QtWidgets")
    def test_uncaught_exception(self, mock_qtwidgets, mock_getlogger):
        self.view.uncaught_exception("user-error", "log-error")

        mock_qtwidgets.QMessageBox.critical.assert_called_once_with(
            self.view, self.view.UNCAUGHT_EXCEPTION, "user-error")
        mock_getlogger.return_value.error.assert_called_once_with("log-error")

    @mock.patch("mantidimaging.gui.windows.main.view.WelcomeScreenPresenter")
    def test_show_about(self, mock_welcomescreen: mock.Mock):
        self.view.show_about()
        mock_welcomescreen.assert_called_once()

    @mock.patch("mantidimaging.gui.windows.main.view.QtGui")
    def test_open_online_documentation(self, mock_qtgui: mock.Mock):
        self.view.open_online_documentation()
        mock_qtgui.QDesktopServices.openUrl.assert_called_once()

    @mock.patch.multiple("mantidimaging.gui.windows.main.view.MainWindowView",
                         setCentralWidget=DEFAULT,
                         addDockWidget=DEFAULT)
    @mock.patch("mantidimaging.gui.windows.main.view.StackVisualiserView")
    @mock.patch("mantidimaging.gui.windows.main.view.QDockWidget")
    def test_create_stack_window(self,
                                 mock_dock: mock.Mock,
                                 mock_sv: mock.Mock,
                                 setCentralWidget: Mock = Mock(),
                                 addDockWidget: Mock = Mock()):
        images = generate_images()
        title = "test_title"
        position = "test_position"
        floating = False

        self.view.create_stack_window(images,
                                      title,
                                      position=position,
                                      floating=floating)

        mock_dock.assert_called_once_with(title, self.view)
        dock = mock_dock.return_value
        setCentralWidget.assert_called_once_with(dock)
        addDockWidget.assert_called_once_with(position, dock)

        mock_sv.assert_called_once_with(self.view, dock, images)
        dock.setWidget.assert_called_once_with(mock_sv.return_value)
        dock.setFloating.assert_called_once_with(floating)

    @mock.patch("mantidimaging.gui.windows.main.view.QMessageBox")
    @mock.patch("mantidimaging.gui.windows.main.view.ProjectionAngleFileParser"
                )
    @mock.patch("mantidimaging.gui.windows.main.view.StackSelectorDialog")
    @mock.patch(
        "mantidimaging.gui.windows.main.view.QFileDialog.getOpenFileName")
    def test_load_projection_angles(self, getOpenFileName: mock.Mock,
                                    StackSelectorDialog: mock.Mock,
                                    ProjectionAngleFileParser: Mock,
                                    QMessageBox: Mock):
        StackSelectorDialog.return_value.exec.return_value = QDialog.Accepted
        selected_stack = "selected_stack"
        StackSelectorDialog.return_value.selected_stack = selected_stack

        selected_file = "~/home/test/directory/selected_file.txt"
        getOpenFileName.return_value = (selected_file, None)

        proj_angles = ProjectionAngles(np.arange(0, 10))
        ProjectionAngleFileParser.return_value.get_projection_angles.return_value = proj_angles

        self.view.load_projection_angles()

        StackSelectorDialog.assert_called_once_with(
            main_window=self.view,
            title='Stack Selector',
            message=self.view.LOAD_PROJECTION_ANGLES_DIALOG_MESSAGE)
        getOpenFileName.assert_called_once_with(
            caption=self.view.LOAD_PROJECTION_ANGLES_FILE_DIALOG_CAPTION,
            filter="All (*.*)")

        self.presenter.add_projection_angles_to_sample.assert_called_once_with(
            selected_stack, proj_angles)
        QMessageBox.information.assert_called_once()

    def test_update_shortcuts_with_presenter_with_one_or_more_stacks(self):
        self.presenter.stack_names = ["1", "2"]

        self._update_shortcuts_test(False, True)
        self._update_shortcuts_test(True, True)

    def test_update_shortcuts_with_presenter_with_no_stacks(self):
        self.presenter.stack_names = []

        self._update_shortcuts_test(False, False)
        self._update_shortcuts_test(True, False)

    def _update_shortcuts_test(self, original_state, expected_state):
        self.view.actionSave.setEnabled(original_state)
        self.view.actionSampleLoadLog.setEnabled(original_state)
        self.view.actionLoad180deg.setEnabled(original_state)
        self.view.actionLoadProjectionAngles.setEnabled(original_state)
        self.view.menuWorkflow.setEnabled(original_state)
        self.view.menuImage.setEnabled(original_state)

        self.view.update_shortcuts()

        self.assertEqual(expected_state, self.view.actionSave.isEnabled())
        self.assertEqual(expected_state,
                         self.view.actionSampleLoadLog.isEnabled())
        self.assertEqual(expected_state,
                         self.view.actionLoad180deg.isEnabled())
        self.assertEqual(expected_state,
                         self.view.actionLoadProjectionAngles.isEnabled())
        self.assertEqual(expected_state, self.view.menuWorkflow.isEnabled())
        self.assertEqual(expected_state, self.view.menuImage.isEnabled())

    @mock.patch("mantidimaging.gui.windows.main.view.populate_menu")
    def test_populate_image_menu_with_no_stack(self, populate_menu):
        self.view.menuImage = mock.MagicMock()
        self.view.current_showing_stack = mock.MagicMock(return_value=None)

        self.view.populate_image_menu()

        populate_menu.assert_not_called()
        self.view.menuImage.addAction.assert_called_once_with(
            "No stack loaded!")

    @mock.patch("mantidimaging.gui.windows.main.view.populate_menu")
    def test_populate_image_menu_with_stack_and_actions(self, populate_menu):
        self.view.menuImage = mock.MagicMock()
        stack = mock.MagicMock()
        actions = mock.MagicMock()
        stack.actions = actions
        self.view.current_showing_stack = mock.MagicMock(return_value=stack)

        self.view.populate_image_menu()

        populate_menu.assert_called_once_with(self.view.menuImage, actions)
        self.view.menuImage.addAction.assert_not_called()

    @mock.patch("mantidimaging.gui.windows.main.view.StackVisualiserView")
    def test_current_showing_stack_with_stack_with_visible(
            self, stack_visualiser_view):
        stack = mock.MagicMock()
        stack.visibleRegion.return_value.isEmpty.return_value = False
        self.view.findChildren = mock.MagicMock(return_value=[stack])

        current_stack = self.view.current_showing_stack()

        self.assertEqual(stack, current_stack)
        stack.visibleRegion.assert_called_once()
        stack.visibleRegion.return_value.isEmpty.assert_called_once()
        self.view.findChildren.assert_called_once_with(stack_visualiser_view)

    @mock.patch("mantidimaging.gui.windows.main.view.StackVisualiserView")
    def test_current_showing_stack_with_stack_not_visible(
            self, stack_visualiser_view):
        stack = mock.MagicMock()
        stack.visibleRegion.return_value.isEmpty.return_value = True
        self.view.findChildren = mock.MagicMock(return_value=[stack])

        current_stack = self.view.current_showing_stack()

        self.assertEqual(None, current_stack)
        stack.visibleRegion.assert_called_once()
        stack.visibleRegion.return_value.isEmpty.assert_called_once()
        self.view.findChildren.assert_called_once_with(stack_visualiser_view)

    @mock.patch("mantidimaging.gui.windows.main.view.StackVisualiserView")
    def test_current_showing_stack_no_stack(self, stack_visualiser_view):
        stack = mock.MagicMock()
        self.view.findChildren = mock.MagicMock(return_value=[])

        current_stack = self.view.current_showing_stack()

        self.assertEqual(None, current_stack)
        stack.visibleRegion.assert_not_called()
        stack.visibleRegion.return_value.isEmpty.assert_not_called()
        self.view.findChildren.assert_called_once_with(stack_visualiser_view)

    def test_get_images_from_stack_uuid(self):
        uuid = uuid4()
        images = mock.MagicMock()
        self.presenter.get_stack_visualiser.return_value.presenter.images = images

        return_value = self.view.get_images_from_stack_uuid(uuid)

        self.presenter.get_stack_visualiser.assert_called_once_with(uuid)
        self.assertEqual(images, return_value)

    def test_load_image_stack(self):
        selected_file = "file_name"
        self.view._get_file_name = mock.MagicMock(return_value=selected_file)

        self.view.load_image_stack()

        self.presenter.load_image_stack.assert_called_once_with(selected_file)
        self.view._get_file_name.assert_called_once_with(
            "Image", "Image File (*.tif *.tiff)")
 def setUp(self) -> None:
     self.main_window = MainWindowView()
     self.main_window.show()
     QTest.qWait(SHORT_DELAY)
Example #19
0
class StackVisualiserViewTest(unittest.TestCase):
    test_data: Images

    def __init__(self, *args, **kwargs):
        super(StackVisualiserViewTest, self).__init__(*args, **kwargs)

    def tearDown(self) -> None:
        try:
            self.test_data.free_memory()
        except FileNotFoundError:
            pass

    def setUp(self):
        # mock the view so it has the same methods
        with mock.patch(
                'mantidimaging.gui.windows.main.view.find_if_latest_version'
        ) as mock_find_latest_version:
            self.window = MainWindowView()
            mock_find_latest_version.assert_called_once()
        self.window.remove_stack = mock.Mock()

        self.dock = QDockWidget()
        self.dock.setWindowTitle("Potatoes")

        self.test_data = th.generate_images(automatic_free=False)
        self.view = StackVisualiserView(self.window, self.dock, self.test_data)
        self.dock.setWidget(self.view)
        self.window.addDockWidget(QtCore.Qt.TopDockWidgetArea, self.dock)

    def test_name(self):
        title = "Potatoes"
        self.dock.setWindowTitle(title)
        self.assertEqual(title, self.view.name)

    def test_closeEvent_deletes_images(self):
        self.dock.setFloating = mock.Mock()
        self.dock.deleteLater = mock.Mock()

        self.view.close()

        self.dock.setFloating.assert_called_once_with(False)
        self.dock.deleteLater.assert_called_once_with()
        self.assertEqual(None, self.view.presenter.images)
        self.window.remove_stack.assert_called_once_with(self.view)

    def _roi_updated_callback(self, roi):
        self.assertIsInstance(roi, SensibleROI)

        self.assertEqual(roi.left, 1)
        self.assertEqual(roi.top, 2)
        self.assertEqual(roi.right, 3)
        self.assertEqual(roi.bottom, 4)

        self.roi_callback_was_called = True

    def test_roi_changed_callback(self):
        self.roi_callback_was_called = False
        self.view.roi_updated.connect(self._roi_updated_callback)

        self.view.roi_changed_callback(SensibleROI(1, 2, 3, 4))

        self.assertTrue(self.roi_callback_was_called)
Example #20
0
 def setUp(self):
     # mock the view so it has the same methods
     with mock.patch('mantidimaging.gui.windows.main.view.check_version_and_label'):
         self.main_window = MainWindowView()
     self.window = FiltersWindowView(self.main_window)
Example #21
0
class StackVisualiserViewTest(unittest.TestCase):
    test_data: Images
    window: MainWindowView

    def __init__(self, *args, **kwargs):
        super(StackVisualiserViewTest, self).__init__(*args, **kwargs)

    def tearDown(self) -> None:
        try:
            self.test_data.free_memory()
        except FileNotFoundError:
            pass
        self.view = None
        self.window = None  # type: ignore[assignment]
        self.dock = None

    def setUp(self):
        # mock the view so it has the same methods
        with mock.patch(
                'mantidimaging.gui.windows.main.view.check_version_and_label'
        ) as mock_find_latest_version:
            self.window = MainWindowView()
            mock_find_latest_version.assert_called_once()
        self.window.remove_stack = mock.Mock()
        self.dock, self.view, self.test_data = self._add_stack_visualiser()

    def _add_stack_visualiser(
            self) -> Tuple[QDockWidget, StackVisualiserView, Images]:
        test_data = th.generate_images(automatic_free=False)
        self.window.create_new_stack(test_data, "Test Data")
        view = self.window.get_stack_with_images(test_data)
        return view.dock, view, test_data

    def test_name(self):
        title = "Potatoes"
        self.dock.setWindowTitle(title)
        self.assertEqual(title, self.view.name)

    def test_closeEvent_deletes_images(self):
        self.dock.setFloating = mock.Mock()
        self.dock.deleteLater = mock.Mock()

        self.view.close()

        self.dock.setFloating.assert_called_once_with(False)
        self.dock.deleteLater.assert_called_once_with()
        self.assertEqual(None, self.view.presenter.images)
        self.window.remove_stack.assert_called_once_with(self.view)

    @mock.patch(
        "mantidimaging.gui.windows.main.view.StackVisualiserView.ask_confirmation",
        return_value=True)
    def test_closeEvent_deletes_images_with_proj180_user_accepts(
            self, ask_confirmation_mock: Mock):
        p180_dock, p180_view, images = self._add_stack_visualiser()
        self.test_data.proj180deg = images

        p180_dock.setFloating = mock.Mock()  # type: ignore[assignment]
        p180_dock.deleteLater = mock.Mock()  # type: ignore[assignment]

        p180_view.close()

        ask_confirmation_mock.assert_called_once()

        # proj180 has been cleared from the stack referencing it
        self.assertFalse(self.test_data.has_proj180deg())

        p180_dock.setFloating.assert_called_once_with(False)
        p180_dock.deleteLater.assert_called_once_with()
        self.assertIsNone(p180_view.presenter.images)
        self.window.remove_stack.assert_called_once_with(
            p180_view)  # type: ignore[attr-defined]

    @mock.patch(
        "mantidimaging.gui.windows.main.view.StackVisualiserView.ask_confirmation",
        return_value=False)
    def test_closeEvent_deletes_images_with_proj180_user_declined(
            self, ask_confirmation_mock: Mock):
        p180_dock, p180_view, images = self._add_stack_visualiser()
        self.test_data.proj180deg = images

        p180_dock.setFloating = mock.Mock()  # type: ignore[assignment]
        p180_dock.deleteLater = mock.Mock()  # type: ignore[assignment]

        p180_view.close()

        ask_confirmation_mock.assert_called_once()

        # proj180 has been cleared from the stack referencing it
        self.assertTrue(self.test_data.has_proj180deg())

        p180_dock.setFloating.assert_not_called()
        p180_dock.deleteLater.assert_not_called()
        self.assertIsNotNone(p180_view.presenter.images)
        self.window.remove_stack.assert_not_called(
        )  # type: ignore[attr-defined]

    def _roi_updated_callback(self, roi):
        self.assertIsInstance(roi, SensibleROI)

        self.assertEqual(roi.left, 1)
        self.assertEqual(roi.top, 2)
        self.assertEqual(roi.right, 3)
        self.assertEqual(roi.bottom, 4)

        self.roi_callback_was_called = True

    def test_roi_changed_callback(self):
        self.roi_callback_was_called = False
        self.view.roi_updated.connect(self._roi_updated_callback)

        self.view.roi_changed_callback(SensibleROI(1, 2, 3, 4))

        self.assertTrue(self.roi_callback_was_called)
Example #22
0
 def setUp(self) -> None:
     with mock.patch("mantidimaging.gui.windows.main.view.WelcomeScreenPresenter"):
         self.main_window = MainWindowView()
     self.view = WizardView(self.main_window, mock.Mock())
Example #23
0
 def setUp(self):
     with mock.patch("mantidimaging.gui.windows.main.view.WelcomeScreenPresenter"):
         self.window = MainWindowView()
     self.view, self.test_data = self._add_stack_visualiser()
Example #24
0
 def setUp(self) -> None:
     with mock.patch("mantidimaging.gui.windows.main.view.check_version_and_label") as check_version_and_label:
         self.view = MainWindowView()
         self.presenter = mock.MagicMock()
         self.view.presenter = self.presenter
         self.check_version_and_label = check_version_and_label
Example #25
0
 def start_imaging(self):
     self.imaging = MainWindowView(open_dialogs=False)
     self.imaging.ask_to_use_closest_to_180 = mock.Mock(return_value=False)
     self.imaging.show()
     QApplication.processEvents()
Example #26
0
class EyesManager:
    def __init__(self, application_name="Mantid Imaging", test_name=None):
        self.application_name = application_name
        self.eyes = Eyes()
        self.eyes.match_level = MatchLevel.CONTENT
        self.image_directory = None
        self.imaging = None
        if test_name is None:
            test_name = self.application_name + " Tests"
        self.test_name = test_name

    def set_match_level(self, level: MatchLevel):
        self.eyes.match_level = level

    def set_batch(self, batch_id):
        batch_info = BatchInfo()
        batch_info.name = self.test_name
        batch_info.id = batch_id
        self.eyes.batch = batch_info

    def abort(self):
        self.eyes.abort_if_not_closed()

    def check_target(self, widget: QWidget = None):
        test_file_name = os.path.basename(inspect.stack()[2][1])
        test_method_name = inspect.stack()[2][3]
        test_image_name = test_file_name.rpartition(
            ".")[0] + "_" + test_method_name

        image = self._take_screenshot(widget=widget,
                                      image_name=test_image_name)

        if self.eyes.api_key == "local":
            return

        if not self.eyes.is_open:
            self.eyes.open(self.application_name,
                           test_file_name,
                           dimension={
                               'width': VIEWPORT_WIDTH,
                               'height': VIEWPORT_HEIGHT
                           })
        self.eyes.check_image(image, test_method_name)

    def close_imaging(self):
        self.imaging.close()

    def start_imaging(self):
        self.imaging = MainWindowView(open_dialogs=False)
        self.imaging.ask_to_use_closest_to_180 = mock.Mock(return_value=False)
        self.imaging.show()
        QApplication.processEvents()

    def _take_screenshot(self, widget: QWidget = None, image_name=None):
        """
        :param widget: Widget to take screen shot of or main window if None.
        :param image_name: File name for screenshot
        :return: Will return the path to the saved image, or None if failed.
        """
        if self.image_directory is None:
            directory = mkdtemp()
        else:
            directory = self.image_directory

        if widget is None and self.imaging is not None:
            widget = self.imaging

        if not isinstance(widget, QWidget):
            raise ValueError("widget is not a QWidget")

        time.sleep(0.2)
        QApplication.processEvents()
        window_image = widget.grab()

        if image_name is None:
            image_name = str(uuid4())

        file_path = os.path.join(directory, image_name) + ".png"

        if window_image.save(file_path, "PNG"):
            return file_path
        else:
            raise IOError("Failed to save", file_path)

    def close_eyes(self):
        if self.eyes.is_open:
            self.eyes.close()