def __execute_test_save(self, project_dir):
        self.addCleanup(lambda: DeleteDirectoryIfExisting(project_dir))
        DeleteDirectoryIfExisting(project_dir) # remove potential leftovers

        controller = PluginController()

        with patch.object(controller._project_path_handler, 'GetSavePath', return_value=project_dir) as patch_fct:
            controller._Save()
            self.assertTrue(project_dir.is_dir())
            self.assertGreater(len(listdir(project_dir)), 0) # make sure sth was created
    def test_Open_aborted(self):
        controller = PluginController()

        with patch.object(controller._project_path_handler, 'GetOpenPath', return_value=Path(".")) as patch_fct_get_open_path:
            with patch.object(controller._project_manager, 'OpenProject', return_value=True) as patch_fct_open_proj:
                with self.assertLogs('kratos_salome_plugin.gui.plugin_controller', level='INFO') as cm:
                    controller._Open()
                    self.assertEqual(len(cm.output), 1)
                    self.assertEqual(cm.output[0], 'INFO:kratos_salome_plugin.gui.plugin_controller:Opening was aborted')

                self.assertEqual(patch_fct_get_open_path.call_count, 1)
                self.assertEqual(patch_fct_open_proj.call_count, 0)
    def test_Open_invalid_folder(self):
        controller = PluginController()

        with patch(_QFileDialog_patch+'getExistingDirectory', return_value=str("random_folder")) as patch_fct:
            with patch.object(controller._project_manager, 'OpenProject', return_value=True) as patch_fct_open_proj:
                with self.assertLogs('kratos_salome_plugin.gui.plugin_controller', level='INFO') as cm:
                    controller._Open()
                    self.assertEqual(len(cm.output), 1)
                    self.assertEqual(cm.output[0], 'WARNING:kratos_salome_plugin.gui.plugin_controller:User input error while opening project: Invalid project folder selected, must end with ".ksp"!')

                self.assertEqual(patch_fct.call_count, 1)
                self.assertEqual(patch_fct_open_proj.call_count, 0)
    def test_Open_failed(self):
        controller = PluginController()

        project_dir = Path("controller_open_project_failed.ksp")

        with patch.object(controller._project_path_handler, 'GetOpenPath', return_value=project_dir) as patch_fct_get_open_path:
            with patch.object(controller._project_manager, 'OpenProject', return_value=False) as patch_fct_open_proj:
                with self.assertLogs('kratos_salome_plugin.gui.plugin_controller', level='INFO') as cm:
                    controller._Open()
                    self.assertEqual(len(cm.output), 1)
                    self.assertEqual(cm.output[0], 'CRITICAL:kratos_salome_plugin.gui.plugin_controller:Failed to open project from "{}"!'.format(project_dir))

                self.assertEqual(patch_fct_get_open_path.call_count, 1)
                self.assertEqual(patch_fct_open_proj.call_count, 1)
    def test_New(self):
        controller = PluginController()

        controller._previous_save_path = Path("some/path")

        initial_project_manager = controller._project_manager
        initial_project_path_handler = controller._project_path_handler

        controller._New()

        # make sure things were cleaned properly
        self.assertIsNone(controller._previous_save_path)
        self.assertIsNot(initial_project_manager, controller._project_manager)
        self.assertIsNot(initial_project_path_handler, controller._project_path_handler)
    def test_Close(self):
        controller = PluginController()
        controller._main_window.ShowOnTop()

        self.assertFalse(controller._main_window.isMinimized())
        self.assertTrue(controller._main_window.isVisible())
        self.assertFalse(controller._main_window.isHidden())
        self.assertEqual(controller._main_window.windowState(), Qt.WindowNoState)

        controller._Close()

        self.assertFalse(controller._main_window.isMinimized())
        self.assertFalse(controller._main_window.isVisible())
        self.assertTrue(controller._main_window.isHidden())
        self.assertEqual(controller._main_window.windowState(), Qt.WindowNoState)
    def test_main_window_active_window(self):
        # setting initial state
        active_window.ACTIVE_WINDOW = None

        controller = PluginController()

        self.assertIs(active_window.ACTIVE_WINDOW, controller._main_window)
    def test_SaveAndReOpen(self):
        # imported here due to patching issues
        from kratos_salome_plugin.salome_study_utilities import GetNumberOfObjectsInStudy, ResetStudy

        project_dir = Path("controller_save_open_project_salome.ksp")

        initial_num_objs = GetNumberOfObjectsInStudy()

        self.__execute_test_save(project_dir)

        ResetStudy()

        controller = PluginController()

        with patch.object(controller._project_path_handler, 'GetOpenPath', return_value=project_dir) as patch_fct:
            controller._Open()

            self.assertEqual(GetNumberOfObjectsInStudy(), initial_num_objs)
    def test_main_window_reopen(self):
        controller = PluginController()

        orig_obj = controller._main_window

        controller._main_window.close()

        controller._main_window.ShowOnTop()

        self.assertIs(orig_obj, controller._main_window)
    def test_Save_failed(self, mock_save_study, mock_version):
        project_dir = Path("controller_save_project_failed.ksp")

        controller = PluginController()

        controller._previous_save_path = project_dir

        with patch.object(controller._project_path_handler, 'GetSavePath', return_value=project_dir) as patch_fct_get_save_path:
            with patch.object(controller._project_manager, 'SaveProject', return_value=False) as patch_fct_save_proj:
                with self.assertLogs('kratos_salome_plugin.gui.plugin_controller', level='INFO') as cm:
                    controller._Save()
                    self.assertEqual(len(cm.output), 2)
                    self.assertEqual(cm.output[0], 'INFO:kratos_salome_plugin.gui.plugin_controller:Saving project with previous save path ...')
                    self.assertEqual(cm.output[1], 'CRITICAL:kratos_salome_plugin.gui.plugin_controller:Failed to save project under "{}"!'.format(project_dir))

                self.assertEqual(patch_fct_get_save_path.call_count, 0)
                self.assertEqual(patch_fct_save_proj.call_count, 1)
                self.assertFalse(project_dir.is_dir())
                self.assertEqual(controller._previous_save_path, project_dir)
    def test_Save_aborted(self, mock_save_study, mock_version):
        project_dir = Path("controller_save_project_aborted.ksp")

        controller = PluginController()

        self.assertIsNone(controller._previous_save_path)

        with patch.object(controller._project_manager, 'SaveProject') as patch_fct_save_project:
            with patch.object(controller._main_window, 'StatusBarWarning') as patch_fct_status_bar:
                with patch(_QFileDialog_patch+'getSaveFileName', return_value=("",0)) as patch_fct:
                    with self.assertLogs('kratos_salome_plugin.gui.plugin_controller', level='INFO') as cm:
                        controller._Save()
                        self.assertEqual(len(cm.output), 2)
                        self.assertEqual(cm.output[0], 'INFO:kratos_salome_plugin.gui.plugin_controller:Saving project as ...')
                        self.assertEqual(cm.output[1], 'INFO:kratos_salome_plugin.gui.plugin_controller:Saving was aborted')

                    self.assertEqual(patch_fct_save_project.call_count, 0)
                    self.assertEqual(patch_fct_status_bar.call_count, 1)
                    self.assertEqual(patch_fct.call_count, 1)
                    self.assertFalse(project_dir.is_dir())
                    self.assertIsNone(controller._previous_save_path)
    def test_SaveAs(self, mock_save_study, mock_version):
        project_dir = Path("controller_save_project_as.ksp")

        self.addCleanup(lambda: DeleteDirectoryIfExisting(project_dir))
        DeleteDirectoryIfExisting(project_dir) # remove potential leftovers

        controller = PluginController()

        self.assertIsNone(controller._previous_save_path)

        with patch.object(controller._main_window, 'StatusBarInfo') as patch_fct_status_bar:
            with patch.object(controller._project_path_handler, 'GetSavePath', return_value=project_dir) as patch_fct:
                with self.assertLogs('kratos_salome_plugin.gui.plugin_controller', level='INFO') as cm:
                    controller._SaveAs()
                    self.assertEqual(len(cm.output), 2)
                    self.assertEqual(cm.output[0], 'INFO:kratos_salome_plugin.gui.plugin_controller:Saving project as ...')
                    self.assertEqual(cm.output[1], 'INFO:kratos_salome_plugin.gui.plugin_controller:Saved project under "{}"'.format(project_dir))

                self.assertEqual(patch_fct_status_bar.call_count, 1)
                self.assertEqual(patch_fct.call_count, 1)
                self.assertTrue(project_dir.is_dir())
                num_files_after_first_save = len(listdir(project_dir))
                self.assertGreater(num_files_after_first_save, 0)

                self.assertEqual(controller._previous_save_path, project_dir)

                # calling it a second time should ask again for the save-path
                controller._SaveAs()
                self.assertEqual(patch_fct_status_bar.call_count, 2)
                self.assertEqual(patch_fct.call_count, 2)
                self.assertTrue(project_dir.is_dir())
                self.assertEqual(num_files_after_first_save, len(listdir(project_dir))) # make sure not more files are created

                self.assertEqual(controller._previous_save_path, project_dir)
    def test_Save_second_save(self, mock_save_study, mock_version):
        project_dir = Path("controller_save_project_second.ksp")

        self.addCleanup(lambda: DeleteDirectoryIfExisting(project_dir))
        DeleteDirectoryIfExisting(project_dir) # remove potential leftovers

        controller = PluginController()

        controller._previous_save_path = project_dir

        with patch.object(controller._main_window, 'StatusBarInfo') as patch_fct_status_bar:
            with patch.object(controller._project_path_handler, 'GetSavePath', return_value=project_dir) as patch_fct:
                with self.assertLogs('kratos_salome_plugin.gui.plugin_controller', level='INFO') as cm:
                    controller._Save()
                    self.assertEqual(len(cm.output), 2)
                    self.assertEqual(cm.output[0], 'INFO:kratos_salome_plugin.gui.plugin_controller:Saving project with previous save path ...')
                    self.assertEqual(cm.output[1], 'INFO:kratos_salome_plugin.gui.plugin_controller:Saved project under "{}"'.format(project_dir))

                self.assertEqual(patch_fct_status_bar.call_count, 1)
                self.assertEqual(patch_fct.call_count, 0) # should not be called as previous save path is used
                self.assertTrue(project_dir.is_dir())

                self.assertEqual(controller._previous_save_path, project_dir)
 def test_kratos_load_application(self):
     with patch.object(PluginController, '_LoadApplication') as patch_fct:
         controller = PluginController()
         controller._main_window.actionLoad_Application.trigger()
         self.assertEqual(patch_fct.call_count, 1)
 def test_kratos_groups(self):
     with patch.object(PluginController, '_Groups') as patch_fct:
         controller = PluginController()
         controller._main_window.actionGroups.trigger()
         self.assertEqual(patch_fct.call_count, 1)
 def test_file_close(self):
     with patch.object(PluginController, '_Close') as patch_fct:
         controller = PluginController()
         controller._main_window.actionClose.trigger()
         self.assertEqual(patch_fct.call_count, 1)
 def test_file_settings(self):
     with patch.object(PluginController, '_Settings') as patch_fct:
         controller = PluginController()
         controller._main_window.actionSettings.trigger()
         self.assertEqual(patch_fct.call_count, 1)
 def test_file_open_button(self):
     with patch.object(PluginController, '_Open') as patch_fct:
         controller = PluginController()
         QTest.mouseClick(controller._main_window.pushButton_Open, Qt.LeftButton)
         self.assertEqual(patch_fct.call_count, 1)
 def test_help_website(self):
     with patch('kratos_salome_plugin.gui.plugin_controller.webbrowser') as patch_fct:
         controller = PluginController()
         controller._main_window.actionWebsite.trigger()
         self.assertEqual(patch_fct.open.call_count, 1)
 def test_help_about(self):
     with patch('kratos_salome_plugin.gui.plugin_controller.ShowAbout') as patch_fct:
         controller = PluginController()
         controller._main_window.actionAbout.trigger()
         self.assertEqual(patch_fct.call_count, 1)
 def test_kratos_load_application_button(self):
     with patch.object(PluginController, '_LoadApplication') as patch_fct:
         controller = PluginController()
         QTest.mouseClick(controller._main_window.pushButton_Load_Application, Qt.LeftButton)
         self.assertEqual(patch_fct.call_count, 1)
 def test_kratos_import_mdpa(self):
     with patch.object(PluginController, '_ImportMdpa') as patch_fct:
         controller = PluginController()
         controller._main_window.actionImport_MDPA.trigger()
         self.assertEqual(patch_fct.call_count, 1)
Пример #23
0
def InitializePlugin(context):
    """Main function for initializing / opening the plugin
    This is called each time the user opens the KratosMultiphysics plugin in Salome
    """

    ### for development/debugging
    reinitialize_every_time = False  # default value: False # CONFIG

    # python imports
    import logging
    logger = logging.getLogger(__name__)

    # plugin imports
    from kratos_salome_plugin.gui.plugin_controller import PluginController
    import kratos_salome_plugin.gui.active_window as active_window
    import kratos_salome_plugin.version as plugin_version
    from kratos_salome_plugin import salome_utilities
    from kratos_salome_plugin.reload_modules import ReloadModules

    # salome imports
    import qtsalome

    # qt imports
    from PyQt5.QtWidgets import QMessageBox

    ### initializing the plugin ###
    logger.info("")
    logger.info("Starting to initialize plugin")

    # logging configuration
    logger.info('Reinitialize Plugin every time: %s', reinitialize_every_time)

    if reinitialize_every_time:
        ReloadModules()

    global VERSION_CHECKS_PERFORMED
    if 'VERSION_CHECKS_PERFORMED' not in globals():
        # doing the version check only once per session and not every time the plugin is reopened
        VERSION_CHECKS_PERFORMED = 1
        # check version of py-qt
        expected_qt_version = 5
        if not qtsalome.QT_SALOME_VERSION == expected_qt_version:
            logger.warning('The version of PyQt has changed, from %d to %d!',
                           expected_qt_version, qtsalome.QT_SALOME_VERSION)

        # check if version of salome is among the checked versions
        salome_versions = salome_utilities.GetVersions()
        if salome_versions not in plugin_version.TESTED_SALOME_VERSIONS:
            msg = 'This Plugin is not tested with this version of Salome ({}.{}.{}).\n'.format(
                *salome_versions)
            msg += 'The tested versions are:'
            for v in plugin_version.TESTED_SALOME_VERSIONS:
                msg += '\n    {}.{}.{}'.format(*v)
            QMessageBox.warning(None, 'Untested Salome Version', msg)

    global PLUGIN_CONTROLLER
    if 'PLUGIN_CONTROLLER' not in globals() or reinitialize_every_time:
        # initialize only once the PluginController
        PLUGIN_CONTROLLER = PluginController()

    active_window.ACTIVE_WINDOW.ShowOnTop()

    logger.info("Successfully initialized plugin")