From d64ae01f31094e2be216ad0a730130ace1591026 Mon Sep 17 00:00:00 2001 From: Mike Sullivan Date: Thu, 20 Jun 2024 15:56:14 +0100 Subject: [PATCH 1/2] unit test added for spectrum widget roi colour change Co-authored-by: Mike Sullivan Co-authored-by: Ashley Meigh --- .../spectrum_viewer/spectrum_widget.py | 11 ++++++-- .../spectrum_viewer/test/spectrum_test.py | 26 ++++++++++++++++--- 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/mantidimaging/gui/windows/spectrum_viewer/spectrum_widget.py b/mantidimaging/gui/windows/spectrum_viewer/spectrum_widget.py index 77d3032cc71..bc9a8cfe6e3 100644 --- a/mantidimaging/gui/windows/spectrum_viewer/spectrum_widget.py +++ b/mantidimaging/gui/windows/spectrum_viewer/spectrum_widget.py @@ -53,12 +53,19 @@ def __init__(self, name: str, sensible_roi: SensibleROI, *args, **kwargs): def onChangeColor(self): current_color = QColor(*self._colour) - selected_color = QColorDialog.getColor(current_color) - if selected_color.isValid(): + selected_color = self.openColorDialog(current_color) + color_valid = self.check_color_valid(selected_color) + if color_valid: new_color = (selected_color.red(), selected_color.green(), selected_color.blue(), 255) self._colour = new_color self.sig_colour_change.emit(self._name, new_color) + def openColorDialog(self, current_color) -> QColor: + return QColorDialog.getColor(current_color) + + def check_color_valid(self, get_colour) -> bool: + return get_colour.isValid() + def contextMenuEnabled(self): return True diff --git a/mantidimaging/gui/windows/spectrum_viewer/test/spectrum_test.py b/mantidimaging/gui/windows/spectrum_viewer/test/spectrum_test.py index c3ab206689d..7a5254e3e5a 100644 --- a/mantidimaging/gui/windows/spectrum_viewer/test/spectrum_test.py +++ b/mantidimaging/gui/windows/spectrum_viewer/test/spectrum_test.py @@ -5,6 +5,8 @@ import uuid import numpy as np from unittest import mock + +from PyQt5.QtGui import QColor from parameterized import parameterized from pyqtgraph import Point @@ -20,11 +22,27 @@ @start_qapplication class SpectrumROITest(unittest.TestCase): + def setUp(self) -> None: + self.roi = SensibleROI(10, 20, 30, 40) + self.spectrum_roi = SpectrumROI("test_roi", self.roi) + def test_WHEN_initialise_THEN_pos_and_size_correct(self): - roi = SensibleROI(10, 20, 30, 40) - spectrum_roi = SpectrumROI("", roi) - self.assertEqual(spectrum_roi.getState()["pos"], Point(10, 20)) - self.assertEqual(spectrum_roi.getState()["size"], Point(20, 20)) + self.assertEqual(self.spectrum_roi.getState()["pos"], Point(10, 20)) + self.assertEqual(self.spectrum_roi.getState()["size"], Point(20, 20)) + + def test_WHEN_colour_changed_THEN_roi_colour_is_set(self): + self._colour = (1, 2, 58, 255) + self.spectrum_roi.openColorDialog = mock.Mock(return_value=QColor(*self._colour)) + self.spectrum_roi.check_color_valid = mock.Mock(return_value=True) + self.spectrum_roi.onChangeColor() + self.assertEqual(self.spectrum_roi.colour, self._colour) + + def test_WHEN_colour_is_not_valid_THEN_roi_colour_is_unchanged(self): + self._colour = (10, 20, 48, 255) + self.spectrum_roi.openColorDialog = mock.Mock(return_value=QColor(*self._colour)) + self.spectrum_roi.check_color_valid = mock.Mock(return_value=False) + self.spectrum_roi.onChangeColor() + self.assertEqual(self.spectrum_roi.colour, (0, 0, 0, 255)) @mock_versions From a8abd7246c3ff1e9d4137b625ff3fc1d2a6d2fd7 Mon Sep 17 00:00:00 2001 From: Mike Sullivan Date: Fri, 21 Jun 2024 09:31:42 +0100 Subject: [PATCH 2/2] requested changes made --- .../windows/spectrum_viewer/test/spectrum_test.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/mantidimaging/gui/windows/spectrum_viewer/test/spectrum_test.py b/mantidimaging/gui/windows/spectrum_viewer/test/spectrum_test.py index 7a5254e3e5a..0475fcb2bd1 100644 --- a/mantidimaging/gui/windows/spectrum_viewer/test/spectrum_test.py +++ b/mantidimaging/gui/windows/spectrum_viewer/test/spectrum_test.py @@ -31,16 +31,14 @@ def test_WHEN_initialise_THEN_pos_and_size_correct(self): self.assertEqual(self.spectrum_roi.getState()["size"], Point(20, 20)) def test_WHEN_colour_changed_THEN_roi_colour_is_set(self): - self._colour = (1, 2, 58, 255) - self.spectrum_roi.openColorDialog = mock.Mock(return_value=QColor(*self._colour)) - self.spectrum_roi.check_color_valid = mock.Mock(return_value=True) + colour = (1, 2, 58, 255) + self.spectrum_roi.openColorDialog = mock.Mock(return_value=QColor(*colour)) self.spectrum_roi.onChangeColor() - self.assertEqual(self.spectrum_roi.colour, self._colour) + self.assertEqual(self.spectrum_roi.colour, colour) def test_WHEN_colour_is_not_valid_THEN_roi_colour_is_unchanged(self): - self._colour = (10, 20, 48, 255) - self.spectrum_roi.openColorDialog = mock.Mock(return_value=QColor(*self._colour)) - self.spectrum_roi.check_color_valid = mock.Mock(return_value=False) + colour = (10, 20, 480, 255) + self.spectrum_roi.openColorDialog = mock.Mock(return_value=QColor(*colour)) self.spectrum_roi.onChangeColor() self.assertEqual(self.spectrum_roi.colour, (0, 0, 0, 255))