Exemple #1
0
class LoadFileWidgetViewTest(unittest.TestCase):
    def setUp(self):
        self.view = BrowseFileWidgetView()

    # ------------------------------------------------------------------------------------------------------------------
    # TESTS
    # ------------------------------------------------------------------------------------------------------------------

    def test_view_initialized_with_empty_line_edit(self):
        self.assertEqual(self.view.get_file_edit_text(), "No data loaded")

    def test_reset_text_to_cache_resets_correctly(self):
        text = r"C:\dir1\dir2\EMU00012345.nxs;C:\dir1\dir2\EMU00012345.nxs"
        self.view.set_file_edit(text)
        # User may then overwrite the text in the LineEdit, causing a signal to be sent
        # and the corresponding slot should implement reset_edit_to_cached_value()
        self.view.reset_edit_to_cached_value()
        self.assertEqual(self.view.get_file_edit_text(), text)

    def test_text_clears_from_line_edit_correctly(self):
        text = r"C:\dir1\dir2\EMU00012345.nxs;C:\dir1\dir2\EMU00012345.nxs"
        self.view.set_file_edit(text)
        self.view.clear()
        self.assertEqual(self.view.get_file_edit_text(), "No data loaded")
        self.view.reset_edit_to_cached_value()
        self.assertEqual(self.view.get_file_edit_text(), "No data loaded")

    def test_text_stored_correctly_when_not_visible_in_line_edit(self):
        # This feature is currently unused
        text = r"C:\dir1\dir2\EMU00012345.nxs;C:\dir1\dir2\EMU00012345.nxs"
        self.view.set_file_edit(text, store=True)
        self.assertEqual(self.view.get_file_edit_text(), text)
        self.view.reset_edit_to_cached_value()
        self.assertEqual(self.view.get_file_edit_text(), text)
Exemple #2
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)
Exemple #3
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 LoadFileWidgetPresenterMultipleFileModeTest(unittest.TestCase):
    def run_test_with_and_without_threading(test_function):
        def run_twice(self):
            test_function(self)
            self.setUp()
            self.presenter._use_threading = False
            test_function(self)

        return run_twice

    def wait_for_thread(self, thread_model):
        if thread_model:
            thread_model._thread.wait()
            QApplication.sendPostedEvents()

    def setUp(self):
        setup_context_for_tests(self)
        self.data_context.instrument = 'EMU'
        self.view = BrowseFileWidgetView()
        self.model = BrowseFileWidgetModel(self.loaded_data, self.context)

        self.view.disable_load_buttons = mock.Mock()
        self.view.enable_load_buttons = mock.Mock()
        self.view.warning_popup = mock.Mock()

        self.presenter = BrowseFileWidgetPresenter(self.view, self.model)

        patcher = mock.patch('mantidqtinterfaces.Muon.GUI.Common.load_file_widget.model.load_utils.load_workspace_from_filename')
        self.addCleanup(patcher.stop)
        self.load_utils_patcher = patcher.start()
        self.load_utils_patcher.return_value = (self.create_fake_workspace(1), '22222', 'filename')

    def mock_loading_multiple_files_from_browse(self, runs, workspaces, filenames):
        self.view.show_file_browser_and_return_selection = mock.Mock(return_value=filenames)
        psi_data = [False] * len(filenames)
        self.load_utils_patcher.side_effect = zip(workspaces, runs, filenames, psi_data)

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

        return {'OutputWorkspace': [workspace_mock]}

    # ------------------------------------------------------------------------------------------------------------------
    # TESTS : Multiple runs can be selected via browse and entered explicitly using the ";" separator
    # ------------------------------------------------------------------------------------------------------------------

    @run_test_with_and_without_threading
    def test_that_cannot_load_same_file_twice_from_same_browse_even_if_filepaths_are_different(self):
        workspace_1 = self.create_fake_workspace(1)
        workspace_2 = self.create_fake_workspace(2)
        self.view.show_file_browser_and_return_selection = mock.Mock(
            return_value=["C:/dir1/file1.nxs", "C:/dir2/file1.nxs", "C:/dir2/file2.nxs"])
        self.load_utils_patcher.load_workspace_from_filename = mock.Mock(side_effect=zip([workspace_1, workspace_2],
                                                                                         [1234, 1234],
                                                                                         ["C:/dir1/file1.nxs",
                                                                                          "C:/dir2/file2.nxs"],
                                                                                         [False, False]))

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

        self.assertEqual(self.load_utils_patcher.call_count, 2)
        self.load_utils_patcher.assert_any_call("C:/dir1/file1.nxs")
        self.load_utils_patcher.assert_any_call("C:/dir2/file2.nxs")

    @run_test_with_and_without_threading
    def test_that_cannot_load_same_file_twice_from_user_input_even_if_filepaths_are_different(self):
        workspace_1 = self.create_fake_workspace(1)
        workspace_2 = self.create_fake_workspace(2)
        self.load_utils_patcher.load_workspace_from_filename = mock.Mock(side_effect=zip([workspace_1, workspace_2],
                                                                                         [1234, 1234],
                                                                                         ["C:/dir1/file1.nxs",
                                                                                          "C:/dir2/file2.nxs"],
                                                                                         [False, False]))
        self.view.set_file_edit("C:/dir1/file1.nxs;C:/dir2/file1.nxs;C:/dir2/file2.nxs")

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

        self.assertEqual(self.load_utils_patcher.call_count, 2)
        self.load_utils_patcher.assert_any_call("C:/dir1/file1.nxs")
        self.load_utils_patcher.assert_any_call("C:/dir2/file2.nxs")

    @run_test_with_and_without_threading
    def test_that_cannot_browse_and_load_same_run_twice_even_if_filenames_are_different(self):
        workspace_1 = self.create_fake_workspace(1)
        workspace_2 = self.create_fake_workspace(2)
        self.load_utils_patcher.side_effect = zip([workspace_1, workspace_2],
                                                  [1234, 1234],
                                                  ["C:/dir1/file1.nxs", "C:/dir1/file2.nxs"],
                                                  [False, False])
        self.view.show_file_browser_and_return_selection = mock.Mock(
            return_value=["C:/dir1/file1.nxs", "C:/dir1/file2.nxs"])

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

        # Load will take the last occurrence of the run from the list
        self.assertCountEqual(self.model.loaded_filenames, ["C:/dir1/file2.nxs"])
        self.assertCountEqual(self.model.loaded_workspaces, [workspace_2])
        self.assertCountEqual(self.model.loaded_runs, [[1234]])

    #
    @run_test_with_and_without_threading
    def test_that_cannot_input_and_load_same_run_twice_even_if_filenames_are_different(self):
        workspace_1 = self.create_fake_workspace(1)
        workspace_2 = self.create_fake_workspace(2)
        self.load_utils_patcher.side_effect = zip([workspace_1, workspace_2], [1234, 1234],
                                                  ["C:/dir1/file1.nxs", "C:/dir1/file2.nxs"],
                                                  [False, False])
        self.view.set_file_edit("C:/dir1/file1.nxs;C:/dir1/file2.nxs")

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

        # Load will take the last occurrence of the run from the user input
        self.assertCountEqual(self.model.loaded_filenames, ["C:/dir1/file2.nxs"])
        self.assertCountEqual(self.model.loaded_workspaces, [workspace_2])
        self.assertCountEqual(self.model.loaded_runs, [[1234]])

    @run_test_with_and_without_threading
    def test_that_loading_two_files_from_browse_sets_model_and_interface_correctly(self):
        workspace_1 = self.create_fake_workspace(1)
        workspace_2 = self.create_fake_workspace(2)
        self.mock_loading_multiple_files_from_browse([1234, 1235], [workspace_1, workspace_2],
                                                     ["C:/dir1/file1.nxs", "C:/dir2/file2.nxs"])

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

        self.assertEqual(self.model.loaded_filenames, ["C:/dir1/file1.nxs", "C:/dir2/file2.nxs"])
        self.assertEqual(self.model.loaded_workspaces, [workspace_1, workspace_2])
        self.assertEqual(self.model.loaded_runs, [[1234], [1235]])

        self.assertEqual(self.view.get_file_edit_text(), "C:/dir1/file1.nxs;C:/dir2/file2.nxs")

    @run_test_with_and_without_threading
    def test_that_loading_two_files_from_user_input_sets_model_and_interface_correctly(self):
        workspace_1 = self.create_fake_workspace(1)
        workspace_2 = self.create_fake_workspace(2)
        self.load_utils_patcher.side_effect = zip([workspace_1, workspace_2],
                                                  [1234, 1235],
                                                  ["C:/dir1/file1.nxs", "C:/dir2/file2.nxs"],
                                                  [False, False])
        self.view.set_file_edit("C:/dir1/file1.nxs;C:/dir2/file2.nxs")

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

        self.assertEqual(self.model.loaded_filenames, ["C:/dir1/file1.nxs", "C:/dir2/file2.nxs"])
        self.assertEqual(self.model.loaded_workspaces, [workspace_1, workspace_2])
        self.assertEqual(self.model.loaded_runs, [[1234], [1235]])

        self.assertEqual(self.view.get_file_edit_text(), "C:/dir1/file1.nxs;C:/dir2/file2.nxs")

    @run_test_with_and_without_threading
    def test_that_loading_two_files_from_browse_sets_interface_alphabetically(self):
        workspace_1 = self.create_fake_workspace(1)
        workspace_2 = self.create_fake_workspace(2)
        self.mock_loading_multiple_files_from_browse([1234, 1235], [workspace_1, workspace_2],
                                                     ["C:/dir1/file2.nxs", "C:/dir1/file1.nxs"])

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

        self.assertEqual(self.view.get_file_edit_text(), "C:/dir1/file1.nxs;C:/dir1/file2.nxs")

    @run_test_with_and_without_threading
    def test_that_loading_two_files_from_user_input_sets_interface_alphabetically(self):
        workspace_1 = self.create_fake_workspace(1)
        workspace_2 = self.create_fake_workspace(2)
        self.load_utils_patcher.side_effect = zip([workspace_2, workspace_1], [1235, 1234],
                                                  ["C:/dir1/file2.nxs", "C:/dir1/file1.nxs"],
                                                  [False, False])
        self.view.set_file_edit("C:/dir1/file2.nxs;C:/dir1/file1.nxs")

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

        self.assertEqual(self.view.get_file_edit_text(), "C:/dir1/file1.nxs;C:/dir1/file2.nxs")

    @run_test_with_and_without_threading
    def test_that_browse_allows_loading_of_additional_files(self):
        workspace_1 = self.create_fake_workspace(1)
        workspace_2 = self.create_fake_workspace(2)
        workspace_3 = self.create_fake_workspace(3)
        self.mock_loading_multiple_files_from_browse([1234, 1235], [workspace_1, workspace_2],
                                                     ["C:/dir1/file1.nxs", "C:/dir2/file2.nxs"])

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

        self.mock_loading_multiple_files_from_browse([1236], [workspace_3], ["C:/dir1/file3.nxs"])

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

        self.assertCountEqual(self.model.loaded_filenames,
                              ["C:/dir1/file1.nxs", "C:/dir2/file2.nxs", "C:/dir1/file3.nxs"])
        self.assertCountEqual(self.model.loaded_workspaces, [workspace_1, workspace_2, workspace_3])
        self.assertCountEqual(self.model.loaded_runs, [[1234], [1235], [1236]])

        self.assertEqual(self.view.get_file_edit_text(), "C:/dir1/file3.nxs")

    @run_test_with_and_without_threading
    def test_that_loading_an_already_loaded_run_from_different_file_overwrites(self):
        workspace_1 = self.create_fake_workspace(1)
        workspace_2 = self.create_fake_workspace(2)
        workspace_3 = self.create_fake_workspace(3)
        self.mock_loading_multiple_files_from_browse([1234, 1235], [workspace_1, workspace_2],
                                                     ["C:/dir1/file1.nxs", "C:/dir2/file2.nxs"])

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

        # only checks runs, so can have a different file/workspace (this is why overwriting is
        # the most useful behaviour in this situation).
        self.mock_loading_multiple_files_from_browse([1234], [workspace_3], ["C:/dir2/file1.nxs"])

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

        self.assertCountEqual(self.model.loaded_filenames, ["C:/dir2/file1.nxs", "C:/dir2/file2.nxs"])
        self.assertCountEqual(self.model.loaded_workspaces, [workspace_3, workspace_2])
        self.assertCountEqual(self.model.loaded_runs, [[1234], [1235]])

        self.assertEqual(self.view.get_file_edit_text(), "C:/dir2/file1.nxs")
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()