From 419b0fb025b6aa44d8cc889e511f9a936a190936 Mon Sep 17 00:00:00 2001 From: Ian Bush <bush@ill.fr> Date: Tue, 29 May 2018 14:56:55 +0200 Subject: [PATCH] Refs #22477 Added tests for the model --- .../workbench/plotting/currentfigure.py | 12 +-- qt/python/CMakeLists.txt | 1 + .../mantidqt/widgets/plotselector/model.py | 7 +- .../widgets/plotselector/presenter.py | 22 ++--- .../mantidqt/widgets/plotselector/widget.py | 1 - .../widgets/test/test_plotselector_model.py | 83 +++++++++++++++++++ .../test/test_plotselector_presenter.py | 5 +- 7 files changed, 104 insertions(+), 27 deletions(-) create mode 100644 qt/python/mantidqt/widgets/test/test_plotselector_model.py diff --git a/qt/applications/workbench/workbench/plotting/currentfigure.py b/qt/applications/workbench/workbench/plotting/currentfigure.py index aad2d5a13b1..b80e9c3dc4b 100644 --- a/qt/applications/workbench/workbench/plotting/currentfigure.py +++ b/qt/applications/workbench/workbench/plotting/currentfigure.py @@ -162,12 +162,14 @@ class CurrentFigure(object): for num, figure_manager in cls.figs.items(): if figure_manager.get_window_title() == figure_title: return num + return None @classmethod def get_figure_manager_from_name(cls, figure_title): for figure_manager in cls.figs.values(): if figure_manager.get_window_title() == figure_title: return figure_manager + return None @classmethod def set_hold(cls, manager): @@ -175,16 +177,6 @@ class CurrentFigure(object): if cls._active == manager: cls._active = None - @classmethod - def bring_to_front_by_name(cls, figure_title): - """ - Make the figure corresponding to figure_title come to the top. - """ - for figure_manager in cls.figs.values(): - if figure_manager.get_window_title() == figure_title: - figure_manager.show() - break - # ---------------------- Observer methods --------------------- # This is currently very simple as the only observer is # permanently registered to this class. diff --git a/qt/python/CMakeLists.txt b/qt/python/CMakeLists.txt index 2f2c4319ea2..cb0356b77e1 100644 --- a/qt/python/CMakeLists.txt +++ b/qt/python/CMakeLists.txt @@ -84,6 +84,7 @@ endif () mantidqt/widgets/test/test_algorithmselector.py mantidqt/widgets/test/test_jupyterconsole.py mantidqt/widgets/test/test_messagedisplay.py + mantidqt/widgets/test/test_plotselector_model.py mantidqt/widgets/test/test_plotselector_presenter.py ) diff --git a/qt/python/mantidqt/widgets/plotselector/model.py b/qt/python/mantidqt/widgets/plotselector/model.py index d54166b0714..79175317f7c 100644 --- a/qt/python/mantidqt/widgets/plotselector/model.py +++ b/qt/python/mantidqt/widgets/plotselector/model.py @@ -45,8 +45,11 @@ class PlotSelectorModel(object): self.presenter.update_plot_list() def make_plot_active(self, plot_name): - self.CurrentFigure.bring_to_front_by_name(plot_name) + figure_manager = self.CurrentFigure.get_figure_manager_from_name(plot_name) + if figure_manager is not None: + figure_manager.show() def close_plot(self, plot_name): figure_number_to_close = self.CurrentFigure.get_figure_number_from_name(plot_name) - self.CurrentFigure.destroy(figure_number_to_close) + if figure_number_to_close is not None: + self.CurrentFigure.destroy(figure_number_to_close) diff --git a/qt/python/mantidqt/widgets/plotselector/presenter.py b/qt/python/mantidqt/widgets/plotselector/presenter.py index 03692952618..3a103477e6a 100644 --- a/qt/python/mantidqt/widgets/plotselector/presenter.py +++ b/qt/python/mantidqt/widgets/plotselector/presenter.py @@ -25,12 +25,12 @@ class PlotSelectorPresenter(object): Presents (controls) a plot selector view. This UI element allows the user to select and make active a plot. """ - def __init__(self, current_figure_class, view = None, model = None): + def __init__(self, current_figure_class, widget = None, model = None): # Create model and view, or accept mocked versions - if view is None: - self.view = PlotSelectorWidget(self) + if widget is None: + self.widget = PlotSelectorWidget(self) else: - self.view = view + self.widget = widget if model is None: self.model = PlotSelectorModel(self, current_figure_class) else: @@ -41,16 +41,16 @@ class PlotSelectorPresenter(object): def update_plot_list(self): self.model.update_plot_list() - filter_text = self.view.get_filter_text() + filter_text = self.widget.get_filter_text() if not filter_text: - self.view.set_plot_list(self.model.plot_list) + self.widget.set_plot_list(self.model.plot_list) else: self._filter_plot_list_by_string(filter_text) # ------------------------ Plot Closing ------------------------- def close_button_clicked(self): - selected_plots = self.view.get_all_selected_plot_names() + selected_plots = self.widget.get_all_selected_plot_names() self._close_plots(selected_plots) def _close_plots(self, list_of_plots): @@ -63,23 +63,23 @@ class PlotSelectorPresenter(object): # ----------------------- Plot Filtering ------------------------ def filter_text_changed(self): - filter_text = self.view.get_filter_text() + filter_text = self.widget.get_filter_text() self._filter_plot_list_by_string(filter_text) def _filter_plot_list_by_string(self, filter_text): if not filter_text: - self.view.set_plot_list(self.model.plot_list) + self.widget.set_plot_list(self.model.plot_list) else: filtered_plot_list = [] for plot_name in self.model.plot_list: if filter_text.lower() in plot_name.lower(): filtered_plot_list.append(plot_name) - self.view.set_plot_list(filtered_plot_list) + self.widget.set_plot_list(filtered_plot_list) # ----------------------- Plot Selection ------------------------ def list_double_clicked(self): - plot_name = self.view.get_currently_selected_plot_name() + plot_name = self.widget.get_currently_selected_plot_name() self.make_plot_active(plot_name) def make_plot_active(self, plot_name): diff --git a/qt/python/mantidqt/widgets/plotselector/widget.py b/qt/python/mantidqt/widgets/plotselector/widget.py index 49e967f42c1..920f4e7a008 100644 --- a/qt/python/mantidqt/widgets/plotselector/widget.py +++ b/qt/python/mantidqt/widgets/plotselector/widget.py @@ -104,4 +104,3 @@ class PlotSelectorWidget(QWidget): def get_filter_text(self): return self.filter_box.text() - diff --git a/qt/python/mantidqt/widgets/test/test_plotselector_model.py b/qt/python/mantidqt/widgets/test/test_plotselector_model.py new file mode 100644 index 00000000000..53febfebe0a --- /dev/null +++ b/qt/python/mantidqt/widgets/test/test_plotselector_model.py @@ -0,0 +1,83 @@ +# This file is part of the mantid workbench. +# +# Copyright (C) 2018 mantidproject +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. +from __future__ import absolute_import, division, print_function + +from mantidqt.widgets.plotselector.model import PlotSelectorModel +from mantidqt.widgets.plotselector.presenter import PlotSelectorPresenter + +import unittest +try: + from unittest import mock +except ImportError: + import mock + + +class PlotSelectorModelTest(unittest.TestCase): + + def side_effects_manager(self, plot_name): + if plot_name == "Plot1": + return self.figure_manager + return None + + def side_effects_number(self, plot_name): + if plot_name == "Plot1": + return 42 + return None + + def setUp(self): + self.presenter = mock.Mock(spec=PlotSelectorPresenter) + + self.figure_manager = mock.Mock() + self.figure_manager.show = mock.Mock() + + self.current_figure = mock.Mock() + self.current_figure.add_observer = mock.Mock() + self.current_figure.get_figure_manager_from_name = mock.Mock(side_effect=self.side_effects_manager) + self.current_figure.get_figure_number_from_name = mock.Mock(side_effect=self.side_effects_number) + self.current_figure.destroy = mock.Mock() + + self.model = PlotSelectorModel(self.presenter, self.current_figure) + self.model.plot_list = ["Plot1", "Plot2"] + + def test_observer_added_during_setup(self): + self.assertEqual(self.current_figure.add_observer.call_count, 1) + + def test_notify_calls_update_in_presenter(self): + self.model.notify() + self.assertEqual(self.presenter.update_plot_list.call_count, 1) + self.model.notify() + self.assertEqual(self.presenter.update_plot_list.call_count, 2) + + def test_make_plot_active_calls_current_figure(self): + self.model.make_plot_active("Plot1") + self.assertEqual(self.figure_manager.show.call_count, 1) + + def test_make_plot_active_for_invalid_name_does_nothing(self): + self.model.make_plot_active("NotAPlot") + self.figure_manager.show.assert_not_called() + + def test_close_plot_calls_destroy_in_current_figure(self): + self.model.close_plot("Plot1") + self.current_figure.destroy.assert_called_once_with(42) + + def test_close_plot_for_invalid_name_does_noting(self): + self.model.close_plot("NotAPlot") + self.current_figure.destroy.assert_not_called() + + +if __name__ == '__main__': + unittest.main() diff --git a/qt/python/mantidqt/widgets/test/test_plotselector_presenter.py b/qt/python/mantidqt/widgets/test/test_plotselector_presenter.py index 2525f10995c..d3a67643a58 100644 --- a/qt/python/mantidqt/widgets/test/test_plotselector_presenter.py +++ b/qt/python/mantidqt/widgets/test/test_plotselector_presenter.py @@ -16,15 +16,14 @@ # along with this program. If not, see <http://www.gnu.org/licenses/>. from __future__ import absolute_import, division, print_function -import sys from mantidqt.widgets.plotselector.model import PlotSelectorModel from mantidqt.widgets.plotselector.presenter import PlotSelectorPresenter from mantidqt.widgets.plotselector.widget import PlotSelectorWidget import unittest -if sys.version_info.major == 3: +try: from unittest import mock -else: +except ImportError: import mock -- GitLab