def setUp(self):
        # Store an empty widget to parent all the views, and ensure they are deleted correctly
        self.obj = QWidget()
        ConfigService['default.instrument'] = 'MUSR'

        setup_context_for_tests(self)
        self.context.instrument = 'MUSR'
        self.load_file_view = BrowseFileWidgetView(self.obj)
        self.load_run_view = LoadRunWidgetView(self.obj)
        self.load_file_model = BrowseFileWidgetModel(self.loaded_data,
                                                     self.context)
        self.load_run_model = LoadRunWidgetModel(self.loaded_data,
                                                 self.context)

        self.view = LoadWidgetView(parent=self.obj,
                                   load_file_view=self.load_file_view,
                                   load_run_view=self.load_run_view)
        self.presenter = LoadWidgetPresenter(
            self.view, LoadWidgetModel(self.loaded_data, self.context))
        self.presenter.set_load_file_widget(
            BrowseFileWidgetPresenter(self.load_file_view,
                                      self.load_file_model))
        self.presenter.set_load_run_widget(
            LoadRunWidgetPresenter(self.load_run_view, self.load_run_model))

        self.presenter.load_file_widget._view.warning_popup = mock.MagicMock()
        self.presenter.load_run_widget._view.warning_popup = mock.MagicMock()

        self.view.multiple_loading_check.setCheckState(1)
        self.presenter.handle_multiple_files_option_changed()

        self.runs = [15196, 15197]
        self.workspaces = [self.create_fake_workspace(1) for _ in self.runs]
        self.filenames = FileFinder.findRuns(
            'MUSR00015196.nxs, MUSR00015197.nxs')
示例#2
0
    def setUp(self):
        # Store an empty widget to parent all the views, and ensure they are deleted correctly
        self.obj = QWidget()

        setup_context_for_tests(self)
        self.context.instrument = 'EMU'
        self.load_file_view = BrowseFileWidgetView(self.obj)
        self.load_run_view = LoadRunWidgetView(self.obj)
        self.load_file_model = BrowseFileWidgetModel(self.loaded_data,
                                                     self.context)
        self.load_run_model = LoadRunWidgetModel(self.loaded_data,
                                                 self.context)

        self.presenter = LoadWidgetPresenter(
            LoadWidgetView(parent=self.obj,
                           load_file_view=self.load_file_view,
                           load_run_view=self.load_run_view),
            LoadWidgetModel(self.loaded_data, self.context))
        self.presenter.set_load_file_widget(
            BrowseFileWidgetPresenter(self.load_file_view,
                                      self.load_file_model))
        self.presenter.set_load_run_widget(
            LoadRunWidgetPresenter(self.load_run_view, self.load_run_model))

        self.filepath = FileFinder.findRuns('MUSR00022725.nxs')[0]

        self.load_patcher = mock.patch(
            'mantidqtinterfaces.Muon.GUI.Common.load_file_widget.model.load_utils.load_workspace_from_filename'
        )
        self.addCleanup(self.load_patcher.stop)
        self.load_mock = self.load_patcher.start()

        self.load_run_patcher = mock.patch(
            'mantidqtinterfaces.Muon.GUI.Common.load_run_widget.load_run_model.load_utils.load_workspace_from_filename'
        )
        self.addCleanup(self.load_run_patcher.stop)
        self.load_run_mock = self.load_run_patcher.start()

        self.mock_workspace = self.create_fake_workspace(1)
        self.mock_loading_from_browse(self.mock_workspace,
                                      r"C:\dir1\dir2\dir3\EMU0001234.nxs",
                                      1234)
        file_utils.get_current_run_filename = mock.Mock(
            return_value=r"C:\dir1\dir2\dir3\EMU0001234.nxs")

        self.presenter.load_file_widget._view.warning_popup = mock.MagicMock()
        self.presenter.load_run_widget._view.warning_popup = mock.MagicMock()

        self.popup_patcher = mock.patch(
            'mantidqtinterfaces.Muon.GUI.Common.thread_model.warning')
        self.addCleanup(self.popup_patcher.stop)
        self.popup_mock = self.popup_patcher.start()

        def setGroupAndPairsToEmptyList(grouping_context):
            grouping_context._groups = []
            grouping_context._pairs = []

        self.group_context.reset_group_and_pairs_to_default = mock.MagicMock(
            side_effect=setGroupAndPairsToEmptyList(self.group_context))
    def setUp(self):

        # Store an empty widget to parent all the views, and ensure they are deleted correctly
        self.obj = QWidget()

        self.popup_patcher = mock.patch('mantidqtinterfaces.Muon.GUI.Common.thread_model.warning')
        self.addCleanup(self.popup_patcher.stop)
        self.popup_mock = self.popup_patcher.start()

        self.load_patcher = mock.patch(
            'mantidqtinterfaces.Muon.GUI.Common.load_file_widget.model.load_utils.load_workspace_from_filename')
        self.addCleanup(self.load_patcher.stop)
        self.load_mock = self.load_patcher.start()

        self.load_run_patcher = mock.patch(
            'mantidqtinterfaces.Muon.GUI.Common.load_run_widget.load_run_model.load_utils.load_workspace_from_filename')
        self.addCleanup(self.load_run_patcher.stop)
        self.load_run_mock = self.load_run_patcher.start()

        setup_context_for_tests(self)
        self.data_context.instrument = 'EMU'
        self.load_file_view = BrowseFileWidgetView(self.obj)
        self.load_run_view = LoadRunWidgetView(self.obj)
        self.load_file_model = BrowseFileWidgetModel(self.loaded_data, self.context)
        self.load_run_model = LoadRunWidgetModel(self.loaded_data, self.context)

        self.model = LoadWidgetModel(self.loaded_data, self.context)
        self.view = LoadWidgetView(parent=self.obj, load_run_view=self.load_run_view,
                                   load_file_view=self.load_file_view)

        self.presenter = LoadWidgetPresenter(view=self.view, model=self.model)
        self.presenter.set_load_file_widget(BrowseFileWidgetPresenter(self.load_file_view, self.load_file_model))
        self.presenter.set_load_run_widget(LoadRunWidgetPresenter(self.load_run_view, self.load_run_model))
        self.presenter.load_run_widget.set_current_instrument('EMU')

        self.presenter.load_file_widget._view.warning_popup = mock.MagicMock()
        self.presenter.load_run_widget._view.warning_popup = mock.MagicMock()

        self.load_file_view.show_file_browser_and_return_selection = mock.Mock(
            return_value=["C:\\dir1\\EMU0001234.nxs"])
        self.workspace_mock = self.create_fake_workspace(1)
        self.load_mock.return_value = (self.workspace_mock, 1234, "C:\\dir1\\EMU0001234.nxs", False)
        self.load_run_mock.return_value = (self.workspace_mock, 1234, "C:\\dir1\\EMU0001234.nxs", False)

        self.presenter.load_file_widget.on_browse_button_clicked()
        self.context.update_current_data = mock.MagicMock(return_value=([], []))
        self.wait_for_thread(self.presenter.load_file_widget._load_thread)

        file_utils.get_current_run_filename = mock.Mock(return_value="EMU0001234.nxs")
示例#4
0
class LoadWidget(object):
    def __init__(self, loaded_data, context, parent):
        # set up the views
        self.load_file_view = BrowseFileWidgetView(parent)
        self.load_run_view = LoadRunWidgetView(parent)
        self.load_widget_view = LoadWidgetView(
            parent=parent,
            load_file_view=self.load_file_view,
            load_run_view=self.load_run_view)
        self.load_widget = LoadWidgetPresenter(
            self.load_widget_view, LoadWidgetModel(loaded_data, context))

        self.file_widget = BrowseFileWidgetPresenter(
            self.load_file_view, BrowseFileWidgetModel(loaded_data, context))
        self.run_widget = LoadRunWidgetPresenter(
            self.load_run_view, LoadRunWidgetModel(loaded_data, context))

        self.load_widget.set_load_file_widget(self.file_widget)
        self.load_widget.set_load_run_widget(self.run_widget)

        self.load_widget.set_current_instrument(
            context.data_context.instrument)

        context.update_view_from_model_notifier.add_subscriber(
            self.load_widget.update_view_from_model_observer)
示例#5
0
class LoadRunWidgetPresenterTest(unittest.TestCase):
    def wait_for_thread(self, thread_model):
        if thread_model:
            thread_model._thread.wait()
            QApplication.sendPostedEvents()

    def setUp(self):
        # Store an empty widget to parent all the views, and ensure they are deleted correctly
        self.obj = QWidget()

        setup_context_for_tests(self)
        self.context.instrument = 'EMU'
        self.load_file_view = BrowseFileWidgetView(self.obj)
        self.load_run_view = LoadRunWidgetView(self.obj)
        self.load_file_model = BrowseFileWidgetModel(self.loaded_data, self.context)
        self.load_run_model = LoadRunWidgetModel(self.loaded_data, self.context)

        self.presenter = LoadWidgetPresenter(
            LoadWidgetView(parent=self.obj, load_file_view=self.load_file_view, load_run_view=self.load_run_view),
            LoadWidgetModel(self.loaded_data, self.context))
        self.presenter.set_load_file_widget(BrowseFileWidgetPresenter(self.load_file_view, self.load_file_model))
        self.presenter.set_load_run_widget(LoadRunWidgetPresenter(self.load_run_view, self.load_run_model))

        self.filepath = FileFinder.findRuns('MUSR00022725.nxs')[0]

        self.load_patcher = mock.patch(
            'mantidqtinterfaces.Muon.GUI.Common.load_file_widget.model.load_utils.load_workspace_from_filename')
        self.addCleanup(self.load_patcher.stop)
        self.load_mock = self.load_patcher.start()

        self.load_run_patcher = mock.patch(
            'mantidqtinterfaces.Muon.GUI.Common.load_run_widget.load_run_model.load_utils.load_workspace_from_filename')
        self.addCleanup(self.load_run_patcher.stop)
        self.load_run_mock = self.load_run_patcher.start()

        self.mock_workspace = self.create_fake_workspace(1)
        self.mock_loading_from_browse(self.mock_workspace, r"C:\dir1\dir2\dir3\EMU0001234.nxs", 1234)
        file_utils.get_current_run_filename = mock.Mock(return_value=r"C:\dir1\dir2\dir3\EMU0001234.nxs")

        self.presenter.load_file_widget._view.warning_popup = mock.MagicMock()
        self.presenter.load_run_widget._view.warning_popup = mock.MagicMock()

        self.popup_patcher = mock.patch('mantidqtinterfaces.Muon.GUI.Common.thread_model.warning')
        self.addCleanup(self.popup_patcher.stop)
        self.popup_mock = self.popup_patcher.start()

        def setGroupAndPairsToEmptyList(grouping_context):
            grouping_context._groups = []
            grouping_context._pairs = []
        self.group_context.reset_group_and_pairs_to_default = mock.MagicMock(
            side_effect=setGroupAndPairsToEmptyList(self.group_context))

    def tearDown(self):
        self.obj = None

    def create_fake_workspace(self, name):
        workspace_mock = CreateSampleWorkspace(StoreInADS=False)
        LoadInstrument(Workspace=workspace_mock, InstrumentName='EMU', RewriteSpectraMap=False, StoreInADS=False)

        return {'OutputWorkspace': [MuonWorkspaceWrapper(workspace_mock)], 'MainFieldDirection': 'transverse'}

    def mock_loading_from_browse(self, workspace, filename, run):
        self.load_file_view.show_file_browser_and_return_selection = mock.Mock(
            return_value=[filename])
        self.load_mock.return_value = (workspace, run, filename, False)
        self.load_run_mock.return_value = (workspace, run, filename, False)

    def mock_loading_from_current_run(self, workspace, filename, run):
        file_utils.get_current_run_filename = mock.Mock(return_value=filename)
        self.load_run_mock.return_value = (workspace, run, filename, False)
        self.load_mock.return_value = (workspace, run, filename, False)

    def mock_user_input_single_run(self, workspace, filename, run):
        self.load_run_view.get_run_edit_text = mock.Mock(return_value=str(run))
        self.load_run_mock.return_value = (workspace, run, filename, False)
        self.load_mock.return_value = (workspace, run, filename, False)

    def mock_user_input_single_file(self, workspace, filename, run):
        self.load_run_mock.return_value = (workspace, run, filename, False)
        self.load_mock.return_value = (workspace, run, filename, False)

        self.load_file_view.get_file_edit_text = mock.Mock(
            return_value=filename)

    def mock_disabling_buttons_in_run_and_file_widget(self):
        self.load_run_view.disable_load_buttons = mock.Mock()
        self.load_run_view.enable_load_buttons = mock.Mock()
        self.load_file_view.disable_load_buttons = mock.Mock()
        self.load_file_view.enable_load_buttons = mock.Mock()

    # ------------------------------------------------------------------------------------------------------------------
    # TESTS : The interface should correctly load/display data via the run widget (load current run and user input run)
    # and via the file widget (browse button or user enters file)
    # ------------------------------------------------------------------------------------------------------------------

    def test_that_loading_single_file_via_browse_sets_model_data(self):
        self.presenter.load_file_widget.on_browse_button_clicked()
        self.wait_for_thread(self.presenter.load_file_widget._load_thread)

        self.assertEqual(self.presenter._model.filenames, [r"C:\dir1\dir2\dir3\EMU0001234.nxs"])
        self.assertEqual(self.presenter._model.workspaces, [self.mock_workspace])
        self.assertEqual(self.presenter._model.runs, [[1234]])

    def test_that_loading_single_file_via_browse_sets_run_and_file_view(self):
        self.presenter.load_file_widget.on_browse_button_clicked()
        self.wait_for_thread(self.presenter.load_file_widget._load_thread)

        self.assertEqual(self.load_file_view.get_file_edit_text(), r"C:\dir1\dir2\dir3\EMU0001234.nxs")
        self.assertEqual(self.load_run_view.get_run_edit_text(), "1234")

    def test_that_loading_via_browse_disables_all_load_buttons(self):
        self.mock_disabling_buttons_in_run_and_file_widget()

        self.presenter.load_file_widget.on_browse_button_clicked()
        self.wait_for_thread(self.presenter.load_file_widget._load_thread)

        self.assertEqual(self.load_file_view.disable_load_buttons.call_count, 2)
        self.assertEqual(self.load_file_view.enable_load_buttons.call_count, 2)
        self.assertEqual(self.load_run_view.disable_load_buttons.call_count, 1)
        self.assertEqual(self.load_run_view.enable_load_buttons.call_count, 1)

    def test_that_loading_single_file_via_user_input_file_is_loaded_into_model_correctly(self):
        self.mock_user_input_single_file(self.mock_workspace, r"C:\dir1\dir2\dir3\EMU0001111.nxs", 1111)

        self.presenter.load_file_widget.handle_file_changed_by_user()
        self.wait_for_thread(self.presenter.load_file_widget._load_thread)

        self.assertEqual(self.presenter._model.filenames, [r"C:\dir1\dir2\dir3\EMU0001111.nxs"])
        self.assertEqual(self.presenter._model.workspaces, [self.mock_workspace])
        self.assertEqual(self.presenter._model.runs, [[1111]])

    def test_that_loading_single_file_via_user_input_file_is_displayed_correctly_in_the_interface(self):
        self.mock_user_input_single_file(self.mock_workspace, r"C:\dir1\dir2\dir3\EMU0001111.nxs", 1111)

        self.presenter.load_file_widget.handle_file_changed_by_user()
        self.wait_for_thread(self.presenter.load_file_widget._load_thread)

        self.assertEqual(self.load_file_view.get_file_edit_text(), r"C:\dir1\dir2\dir3\EMU0001111.nxs")
        self.assertEqual(self.load_run_view.get_run_edit_text(), "1111")

    def test_that_loading_via_user_input_file_disables_all_load_buttons(self):
        self.mock_user_input_single_file([1], r"C:\dir1\dir2\dir3\EMU0001111.nxs", 1111)

        self.mock_disabling_buttons_in_run_and_file_widget()

        self.presenter.load_file_widget.handle_file_changed_by_user()
        self.wait_for_thread(self.presenter.load_file_widget._load_thread)

        self.assertEqual(self.load_file_view.disable_load_buttons.call_count, 2)
        self.assertEqual(self.load_file_view.enable_load_buttons.call_count, 2)
        self.assertEqual(self.load_run_view.disable_load_buttons.call_count, 1)
        self.assertEqual(self.load_run_view.enable_load_buttons.call_count, 1)

    def test_that_loading_via_load_current_run_is_loaded_into_model_correctly(self):
        self.mock_loading_from_current_run(self.mock_workspace, "\\\\EMU\\data\\EMU0083420.nxs", 83420)

        self.presenter.load_run_widget.handle_load_current_run()
        self.wait_for_thread(self.presenter.load_run_widget._load_thread)

        self.assertEqual(self.presenter._model.filenames, ["\\\\EMU\\data\\EMU0083420.nxs"])
        self.assertEqual(self.presenter._model.workspaces, [self.mock_workspace])
        self.assertEqual(self.presenter._model.runs, [[83420]])

    def test_that_loading_via_load_current_run_is_displayed_correctly_in_the_interface(self):
        self.mock_loading_from_current_run(self.mock_workspace, "\\\\EMU\\data\\EMU0083420.nxs", 83420)

        self.presenter.load_run_widget.handle_load_current_run()
        self.wait_for_thread(self.presenter.load_run_widget._load_thread)

        self.assertEqual(self.load_file_view.get_file_edit_text(), "\\\\EMU\\data\\EMU0083420.nxs")
        self.assertEqual(self.load_run_view.get_run_edit_text(), "83420")

    def test_that_loading_via_load_current_run_disables_all_load_buttons(self):
        self.mock_loading_from_current_run(self.mock_workspace, "\\\\EMU\\data\\EMU0083420.nxs", 83420)
        self.mock_disabling_buttons_in_run_and_file_widget()

        self.presenter.load_run_widget.handle_load_current_run()
        self.wait_for_thread(self.presenter.load_run_widget._load_thread)

        self.assertEqual(self.load_file_view.disable_load_buttons.call_count, 1)
        self.assertEqual(self.load_file_view.enable_load_buttons.call_count, 1)
        self.assertEqual(self.load_run_view.disable_load_buttons.call_count, 3)
        self.assertEqual(self.load_run_view.enable_load_buttons.call_count, 2)

    def test_that_user_input_run_is_loaded_into_model_correctly(self):
        self.mock_user_input_single_run(self.mock_workspace, "EMU00012345.nxs", 12345)
        self.presenter.load_run_widget.set_current_instrument('EMU')

        self.presenter.load_run_widget.handle_run_changed_by_user()
        self.wait_for_thread(self.presenter.load_run_widget._load_thread)

        self.assertEqual(self.presenter._model.filenames, ["EMU00012345.nxs"])
        self.assertEqual(self.presenter._model.workspaces, [self.mock_workspace])
        self.assertEqual(self.presenter._model.runs, [[12345]])

    def test_that_user_input_run_is_displayed_correctly_in_the_interface(self):
        self.mock_user_input_single_run(self.mock_workspace, "EMU00012345.nxs", 12345)
        self.presenter.load_run_widget.set_current_instrument('EMU')

        self.presenter.load_run_widget.handle_run_changed_by_user()
        self.wait_for_thread(self.presenter.load_run_widget._load_thread)

        self.assertEqual(self.load_file_view.get_file_edit_text(), "EMU00012345.nxs")
        self.assertEqual(self.load_run_view.get_run_edit_text(), "12345")

    def test_that_loading_via_user_input_run_disables_all_load_buttons(self):
        self.mock_user_input_single_run(self.mock_workspace, "EMU00012345.nxs", 12345)
        self.presenter.load_run_widget.set_current_instrument('EMU')

        self.mock_disabling_buttons_in_run_and_file_widget()

        self.presenter.load_run_widget.handle_run_changed_by_user()
        self.wait_for_thread(self.presenter.load_run_widget._load_thread)

        self.assertEqual(self.load_file_view.disable_load_buttons.call_count, 1)
        self.assertEqual(self.load_file_view.enable_load_buttons.call_count, 1)
        self.assertEqual(self.load_run_view.disable_load_buttons.call_count, 3)
        self.assertEqual(self.load_run_view.enable_load_buttons.call_count, 2)
示例#6
0
class LoadRunWidgetPresenterLoadFailTest(unittest.TestCase):
    def wait_for_thread(self, thread_model):
        if thread_model and thread_model.worker:
            while thread_model.worker.is_alive():
                time.sleep(0.1)
            QApplication.sendPostedEvents()

    def create_fake_workspace(self, name):
        workspace_mock = CreateSampleWorkspace(StoreInADS=False)
        LoadInstrument(Workspace=workspace_mock,
                       InstrumentName='EMU',
                       RewriteSpectraMap=False,
                       StoreInADS=False)

        return {
            'OutputWorkspace': [MuonWorkspaceWrapper(workspace_mock)],
            'MainFieldDirection': 'transverse'
        }

    def setUp(self):

        # Store an empty widget to parent all the views, and ensure they are deleted correctly
        self.obj = QWidget()

        self.popup_patcher = mock.patch(
            'mantidqtinterfaces.Muon.GUI.Common.thread_model.warning')
        self.addCleanup(self.popup_patcher.stop)
        self.popup_mock = self.popup_patcher.start()

        self.load_patcher = mock.patch(
            'mantidqtinterfaces.Muon.GUI.Common.load_file_widget.model.load_utils.load_workspace_from_filename'
        )
        self.addCleanup(self.load_patcher.stop)
        self.load_mock = self.load_patcher.start()

        self.load_run_patcher = mock.patch(
            'mantidqtinterfaces.Muon.GUI.Common.load_run_widget.load_run_model.load_utils.load_workspace_from_filename'
        )
        self.addCleanup(self.load_run_patcher.stop)
        self.load_run_mock = self.load_run_patcher.start()

        setup_context_for_tests(self)
        self.data_context.instrument = 'EMU'
        self.load_file_view = BrowseFileWidgetView(self.obj)
        self.load_run_view = LoadRunWidgetView(self.obj)
        self.load_file_model = BrowseFileWidgetModel(self.loaded_data,
                                                     self.context)
        self.load_run_model = LoadRunWidgetModel(self.loaded_data,
                                                 self.context)

        self.model = LoadWidgetModel(self.loaded_data, self.context)
        self.view = LoadWidgetView(parent=self.obj,
                                   load_run_view=self.load_run_view,
                                   load_file_view=self.load_file_view)

        self.presenter = LoadWidgetPresenter(view=self.view, model=self.model)
        self.presenter.set_load_file_widget(
            BrowseFileWidgetPresenter(self.load_file_view,
                                      self.load_file_model))
        self.presenter.set_load_run_widget(
            LoadRunWidgetPresenter(self.load_run_view, self.load_run_model))
        self.presenter.load_run_widget.set_current_instrument('EMU')

        self.presenter.load_file_widget._view.warning_popup = mock.MagicMock()
        self.presenter.load_run_widget._view.warning_popup = mock.MagicMock()

        self.load_file_view.show_file_browser_and_return_selection = mock.Mock(
            return_value=["C:\\dir1\\EMU0001234.nxs"])
        self.workspace_mock = self.create_fake_workspace(1)
        self.load_mock.return_value = (self.workspace_mock, 1234,
                                       "C:\\dir1\\EMU0001234.nxs", False)
        self.load_run_mock.return_value = (self.workspace_mock, 1234,
                                           "C:\\dir1\\EMU0001234.nxs", False)

        self.presenter.load_file_widget.on_browse_button_clicked()
        self.context.update_current_data = mock.MagicMock(return_value=([],
                                                                        []))
        self.wait_for_thread(self.presenter.load_file_widget._load_thread)

        file_utils.get_current_run_filename = mock.Mock(
            return_value="EMU0001234.nxs")

    def tearDown(self):
        self.obj = None

    def load_failure(self):
        raise ValueError("Error text")

    def mock_loading_to_throw(self):
        self.load_mock.side_effect = self.load_failure
        self.load_run_mock.side_effect = self.load_failure

    def mock_disabling_buttons_in_run_and_file_widget(self):
        self.load_run_view.disable_load_buttons = mock.Mock()
        self.load_run_view.enable_load_buttons = mock.Mock()
        self.load_file_view.disable_load_buttons = mock.Mock()
        self.load_file_view.enable_load_buttons = mock.Mock()

    def assert_model_unchanged(self):
        self.assertEqual(self.presenter._model.filenames,
                         ["C:\\dir1\\EMU0001234.nxs"])
        self.assertEqual(self.presenter._model.workspaces,
                         [self.workspace_mock])
        self.assertEqual(self.presenter._model.runs, [[1234]])

    def assert_interface_unchanged(self):
        self.assertEqual(self.load_file_view.get_file_edit_text(),
                         "C:\\dir1\\EMU0001234.nxs")
        self.assertEqual(self.load_run_view.get_run_edit_text(), "1234")

    # ------------------------------------------------------------------------------------------------------------------
    # TESTS : The interface should always revert to its previous state if a load fails from anywhere in the widget
    # and a warning should be shown to the user.
    # ------------------------------------------------------------------------------------------------------------------

    def test_that_if_load_fails_from_browse_that_model_and_interface_are_unchanged_from_previous_state(
            self):
        self.presenter.load_file_widget.on_browse_button_clicked()
        self.wait_for_thread(self.presenter.load_file_widget._load_thread)

        self.assert_model_unchanged()
        self.assert_interface_unchanged()

    def test_that_if_load_fails_from_user_file_entry_that_model_and_interface_are_unchanged_from_previous_state(
            self):
        self.presenter.load_file_widget.handle_file_changed_by_user()
        self.wait_for_thread(self.presenter.load_file_widget._load_thread)

        self.assert_model_unchanged()
        self.assert_interface_unchanged()

    def test_that_if_load_fails_from_current_run_that_model_and_interface_are_unchanged_from_previous_state(
            self):
        self.presenter.load_run_widget.handle_load_current_run()
        self.wait_for_thread(self.presenter.load_run_widget._load_thread)

        self.assert_model_unchanged()
        self.assert_interface_unchanged()

    def test_that_if_load_fails_from_user_run_entry_that_model_and_interface_are_unchanged_from_previous_state(
            self):
        self.presenter.load_run_widget.handle_run_changed_by_user()
        self.wait_for_thread(self.presenter.load_run_widget._load_thread)

        self.assert_model_unchanged()
        self.assert_interface_unchanged()

    def test_that_if_load_fails_from_browse_that_warning_is_displayed(self):
        self.mock_loading_to_throw()
        self.presenter.load_file_widget.on_browse_button_clicked()
        self.wait_for_thread(self.presenter.load_file_widget._load_thread)

        self.assertEqual(
            self.presenter.load_file_widget._view.warning_popup.call_count, 1)

    def test_that_if_load_fails_from_user_file_entry_that_warning_is_displayed(
            self):
        self.mock_loading_to_throw()
        self.presenter.load_file_widget.handle_file_changed_by_user()
        self.wait_for_thread(self.presenter.load_file_widget._load_thread)

        self.assertEqual(
            self.presenter.load_file_widget._view.warning_popup.call_count, 1)

    def test_that_if_load_fails_from_current_run_that_warning_is_displayed(
            self):
        self.mock_loading_to_throw()
        self.presenter.load_run_widget.handle_load_current_run()
        self.wait_for_thread(self.presenter.load_run_widget._load_thread)

        self.assertEqual(
            self.presenter.load_run_widget._view.warning_popup.call_count, 1)

    def test_that_if_load_fails_from_user_run_entry_that_warning_is_displayed(
            self):
        self.mock_loading_to_throw()
        self.presenter.load_run_widget._view.run_edit.setText('1239')
        self.presenter.load_run_widget.handle_run_changed_by_user()
        self.wait_for_thread(self.presenter.load_run_widget._load_thread)

        self.assertEqual(
            self.presenter.load_run_widget._view.warning_popup.call_count, 1)
class LoadRunWidgetPresenterMultipleFileTest(unittest.TestCase):
    def wait_for_thread(self, thread_model):
        if thread_model and thread_model.worker:
            while thread_model.worker.is_alive():
                QApplication.sendPostedEvents()
                time.sleep(0.1)
            QApplication.sendPostedEvents()

    def setUp(self):
        # Store an empty widget to parent all the views, and ensure they are deleted correctly
        self.obj = QWidget()
        ConfigService['default.instrument'] = 'MUSR'

        setup_context_for_tests(self)
        self.context.instrument = 'MUSR'
        self.load_file_view = BrowseFileWidgetView(self.obj)
        self.load_run_view = LoadRunWidgetView(self.obj)
        self.load_file_model = BrowseFileWidgetModel(self.loaded_data,
                                                     self.context)
        self.load_run_model = LoadRunWidgetModel(self.loaded_data,
                                                 self.context)

        self.view = LoadWidgetView(parent=self.obj,
                                   load_file_view=self.load_file_view,
                                   load_run_view=self.load_run_view)
        self.presenter = LoadWidgetPresenter(
            self.view, LoadWidgetModel(self.loaded_data, self.context))
        self.presenter.set_load_file_widget(
            BrowseFileWidgetPresenter(self.load_file_view,
                                      self.load_file_model))
        self.presenter.set_load_run_widget(
            LoadRunWidgetPresenter(self.load_run_view, self.load_run_model))

        self.presenter.load_file_widget._view.warning_popup = mock.MagicMock()
        self.presenter.load_run_widget._view.warning_popup = mock.MagicMock()

        self.view.multiple_loading_check.setCheckState(1)
        self.presenter.handle_multiple_files_option_changed()

        self.runs = [15196, 15197]
        self.workspaces = [self.create_fake_workspace(1) for _ in self.runs]
        self.filenames = FileFinder.findRuns(
            'MUSR00015196.nxs, MUSR00015197.nxs')

    def tearDown(self):
        self.obj = None

    def assert_model_contains_correct_loaded_data(self):
        # use sorted due to threads finishing at different times
        self.assertEqual(sorted(self.presenter._model.filenames), ['Co-added'])
        self.assertEqual(sorted(self.presenter._model.runs),
                         [sorted(self.runs)])

    def assert_interface_contains_correct_runs_and_files(self):
        self.assertEqual(self.load_file_view.get_file_edit_text(),
                         ";".join(['Co-added']))
        self.assertEqual(self.load_run_view.get_run_edit_text(), "15196-15197")

    def create_fake_workspace(self, name):
        workspace_mock = mock.MagicMock()
        instrument_mock = mock.MagicMock()
        instrument_mock.getName.return_value = 'MUSR'
        workspace_mock.workspace.getInstrument.return_value = instrument_mock

        return {'OutputWorkspace': [workspace_mock]}

    # ------------------------------------------------------------------------------------------------------------------
    # TESTS : The loading of multiple files is supported by the widget
    # ------------------------------------------------------------------------------------------------------------------

    def test_that_loading_multiple_files_via_browse_sets_model_and_interface_correctly(
            self):
        self.load_file_view.show_file_browser_and_return_selection = mock.Mock(
            return_value=self.filenames)

        self.presenter.load_file_widget.on_browse_button_clicked()
        self.wait_for_thread(self.presenter.load_file_widget._load_thread)

        self.assert_model_contains_correct_loaded_data()
        self.assert_interface_contains_correct_runs_and_files()

    def test_that_loading_multiple_files_via_user_input_files_sets_model_and_interface_correctly(
            self):
        self.load_file_view.file_path_edit.setText(';'.join(self.filenames))

        self.load_file_view.file_path_edit.returnPressed.emit()
        self.wait_for_thread(self.presenter.load_file_widget._load_thread)

        self.assert_model_contains_correct_loaded_data()
        self.assert_interface_contains_correct_runs_and_files()

    def test_that_loading_multiple_files_via_user_input_runs_sets_model_and_interface_correctly(
            self):
        self.load_run_view.run_edit.setText("15196-97")

        self.load_run_view.run_edit.returnPressed.emit()
        self.wait_for_thread(self.presenter.load_run_widget._load_thread)

        self.assert_model_contains_correct_loaded_data()
        self.assert_interface_contains_correct_runs_and_files()