Skip to content

Commit

Permalink
unit test added for spectrum widget roi colour change (#2233)
Browse files Browse the repository at this point in the history
  • Loading branch information
samtygier-stfc authored Jun 24, 2024
2 parents f0433e5 + a8abd72 commit dd2a13f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
11 changes: 9 additions & 2 deletions mantidimaging/gui/windows/spectrum_viewer/spectrum_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
24 changes: 20 additions & 4 deletions mantidimaging/gui/windows/spectrum_viewer/test/spectrum_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -20,11 +22,25 @@
@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):
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, colour)

def test_WHEN_colour_is_not_valid_THEN_roi_colour_is_unchanged(self):
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))


@mock_versions
Expand Down

0 comments on commit dd2a13f

Please sign in to comment.