Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unit test added for spectrum widget roi colour change #2233

Merged
merged 2 commits into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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