1 | import sys |
---|
2 | import unittest |
---|
3 | import numpy |
---|
4 | |
---|
5 | from PyQt5 import QtGui, QtWidgets |
---|
6 | from unittest.mock import MagicMock |
---|
7 | import matplotlib as mpl |
---|
8 | |
---|
9 | # set up import paths |
---|
10 | import path_prepare |
---|
11 | |
---|
12 | from sas.qtgui.Plotting.PlotterData import Data2D |
---|
13 | import sas.qtgui.Plotting.Plotter2D as Plotter2D |
---|
14 | from UnitTesting.TestUtils import WarningTestNotImplemented |
---|
15 | from UnitTesting.TestUtils import QtSignalSpy |
---|
16 | |
---|
17 | # Local |
---|
18 | from sas.qtgui.Plotting.ColorMap import ColorMap |
---|
19 | |
---|
20 | if not QtWidgets.QApplication.instance(): |
---|
21 | app = QtWidgets.QApplication(sys.argv) |
---|
22 | |
---|
23 | class ColorMapTest(unittest.TestCase): |
---|
24 | '''Test the ColorMap''' |
---|
25 | def setUp(self): |
---|
26 | '''Create the ColorMap''' |
---|
27 | self.plotter = Plotter2D.Plotter2D(None, quickplot=True) |
---|
28 | |
---|
29 | self.data = Data2D(image=[0.1]*4, |
---|
30 | qx_data=[1.0, 2.0, 3.0, 4.0], |
---|
31 | qy_data=[10.0, 11.0, 12.0, 13.0], |
---|
32 | dqx_data=[0.1, 0.2, 0.3, 0.4], |
---|
33 | dqy_data=[0.1, 0.2, 0.3, 0.4], |
---|
34 | q_data=[1,2,3,4], |
---|
35 | xmin=-1.0, xmax=5.0, |
---|
36 | ymin=-1.0, ymax=15.0, |
---|
37 | zmin=-1.0, zmax=20.0) |
---|
38 | |
---|
39 | self.data.title="Test data" |
---|
40 | self.data.id = 1 |
---|
41 | self.widget = ColorMap(parent=self.plotter, data=self.data) |
---|
42 | |
---|
43 | def tearDown(self): |
---|
44 | '''Destroy the GUI''' |
---|
45 | self.widget.close() |
---|
46 | self.widget = None |
---|
47 | |
---|
48 | def testDefaults(self): |
---|
49 | '''Test the GUI in its default state''' |
---|
50 | self.assertIsInstance(self.widget, QtWidgets.QDialog) |
---|
51 | |
---|
52 | self.assertEqual(self.widget._cmap_orig, "jet") |
---|
53 | self.assertEqual(len(self.widget.all_maps), 160) |
---|
54 | self.assertEqual(len(self.widget.maps), 80) |
---|
55 | self.assertEqual(len(self.widget.rmaps), 80) |
---|
56 | |
---|
57 | self.assertEqual(self.widget.lblWidth.text(), "0") |
---|
58 | self.assertEqual(self.widget.lblHeight.text(), "0") |
---|
59 | self.assertEqual(self.widget.lblQmax.text(), "15.8") |
---|
60 | self.assertEqual(self.widget.lblStopRadius.text(), "-1") |
---|
61 | self.assertFalse(self.widget.chkReverse.isChecked()) |
---|
62 | self.assertEqual(self.widget.cbColorMap.count(), 80) |
---|
63 | self.assertEqual(self.widget.cbColorMap.currentIndex(), 64) |
---|
64 | |
---|
65 | # validators |
---|
66 | self.assertIsInstance(self.widget.txtMinAmplitude.validator(), QtGui.QDoubleValidator) |
---|
67 | self.assertIsInstance(self.widget.txtMaxAmplitude.validator(), QtGui.QDoubleValidator) |
---|
68 | |
---|
69 | # Ranges |
---|
70 | self.assertEqual(self.widget.txtMinAmplitude.text(), "0") |
---|
71 | self.assertEqual(self.widget.txtMaxAmplitude.text(), "100") |
---|
72 | self.assertIsInstance(self.widget.slider, QtWidgets.QSlider) |
---|
73 | |
---|
74 | def testOnReset(self): |
---|
75 | '''Check the dialog reset function''' |
---|
76 | # Set some controls to non-default state |
---|
77 | self.widget.cbColorMap.setCurrentIndex(20) |
---|
78 | self.widget.chkReverse.setChecked(True) |
---|
79 | self.widget.txtMinAmplitude.setText("20.0") |
---|
80 | |
---|
81 | # Reset the widget state |
---|
82 | self.widget.onReset() |
---|
83 | |
---|
84 | # Assure things went back to default |
---|
85 | self.assertEqual(self.widget.cbColorMap.currentIndex(), 20) |
---|
86 | self.assertFalse(self.widget.chkReverse.isChecked()) |
---|
87 | self.assertEqual(self.widget.txtMinAmplitude.text(), "0") |
---|
88 | |
---|
89 | def testOnApply(self): |
---|
90 | '''Check the dialog apply function''' |
---|
91 | # Set some controls to non-default state |
---|
92 | self.widget.show() |
---|
93 | self.widget.cbColorMap.setCurrentIndex(20) # PuRd_r |
---|
94 | self.widget.chkReverse.setChecked(True) |
---|
95 | self.widget.txtMinAmplitude.setText("20.0") |
---|
96 | |
---|
97 | spy_apply = QtSignalSpy(self.widget, self.widget.apply_signal) |
---|
98 | # Reset the widget state |
---|
99 | self.widget.onApply() |
---|
100 | |
---|
101 | # Assure the widget is still up and the signal was sent. |
---|
102 | self.assertTrue(self.widget.isVisible()) |
---|
103 | self.assertEqual(spy_apply.count(), 1) |
---|
104 | self.assertIn('PuRd_r', spy_apply.called()[0]['args'][1]) |
---|
105 | |
---|
106 | def testInitMapCombobox(self): |
---|
107 | '''Test the combo box initializer''' |
---|
108 | # Set a color map from the direct list |
---|
109 | self.widget._cmap = "gnuplot" |
---|
110 | self.widget.initMapCombobox() |
---|
111 | |
---|
112 | # Check the combobox |
---|
113 | self.assertEqual(self.widget.cbColorMap.currentIndex(), 59) |
---|
114 | self.assertFalse(self.widget.chkReverse.isChecked()) |
---|
115 | |
---|
116 | # Set a reversed value |
---|
117 | self.widget._cmap = "hot_r" |
---|
118 | self.widget.initMapCombobox() |
---|
119 | # Check the combobox |
---|
120 | self.assertEqual(self.widget.cbColorMap.currentIndex(), 60) |
---|
121 | self.assertTrue(self.widget.chkReverse.isChecked()) |
---|
122 | |
---|
123 | def testInitRangeSlider(self): |
---|
124 | '''Test the range slider initializer''' |
---|
125 | # Set a color map from the direct list |
---|
126 | self.widget._cmap = "gnuplot" |
---|
127 | self.widget.initRangeSlider() |
---|
128 | |
---|
129 | # Check the values |
---|
130 | self.assertEqual(self.widget.slider.minimum(), 0) |
---|
131 | self.assertEqual(self.widget.slider.maximum(), 100) |
---|
132 | self.assertEqual(self.widget.slider.orientation(), 1) |
---|
133 | |
---|
134 | # Emit new low value |
---|
135 | self.widget.slider.lowValueChanged.emit(5) |
---|
136 | # Assure the widget received changes |
---|
137 | self.assertEqual(self.widget.txtMinAmplitude.text(), "5") |
---|
138 | |
---|
139 | # Emit new high value |
---|
140 | self.widget.slider.highValueChanged.emit(45) |
---|
141 | # Assure the widget received changes |
---|
142 | self.assertEqual(self.widget.txtMinAmplitude.text(), "45") |
---|
143 | |
---|
144 | def testOnMapIndexChange(self): |
---|
145 | '''Test the response to the combo box index change''' |
---|
146 | |
---|
147 | self.widget.canvas.draw = MagicMock() |
---|
148 | mpl.colorbar.ColorbarBase = MagicMock() |
---|
149 | |
---|
150 | # simulate index change |
---|
151 | self.widget.cbColorMap.setCurrentIndex(1) |
---|
152 | |
---|
153 | # Check that draw() got called |
---|
154 | self.assertTrue(self.widget.canvas.draw.called) |
---|
155 | self.assertTrue(mpl.colorbar.ColorbarBase.called) |
---|
156 | |
---|
157 | def testOnColorMapReversed(self): |
---|
158 | '''Test reversing the color map functionality''' |
---|
159 | # Check the defaults |
---|
160 | self.assertEqual(self.widget._cmap, "jet") |
---|
161 | self.widget.cbColorMap.addItems = MagicMock() |
---|
162 | |
---|
163 | # Reverse the choice |
---|
164 | self.widget.onColorMapReversed(True) |
---|
165 | |
---|
166 | # check the behaviour |
---|
167 | self.assertEqual(self.widget._cmap, "jet_r") |
---|
168 | self.assertTrue(self.widget.cbColorMap.addItems.called) |
---|
169 | |
---|
170 | def testOnAmplitudeChange(self): |
---|
171 | '''Check the callback method for responding to changes in textboxes''' |
---|
172 | self.widget.canvas.draw = MagicMock() |
---|
173 | mpl.colors.Normalize = MagicMock() |
---|
174 | mpl.colorbar.ColorbarBase = MagicMock() |
---|
175 | |
---|
176 | self.widget.vmin = 0.0 |
---|
177 | self.widget.vmax = 100.0 |
---|
178 | |
---|
179 | # good values in fields |
---|
180 | self.widget.txtMinAmplitude.setText("1.0") |
---|
181 | self.widget.txtMaxAmplitude.setText("10.0") |
---|
182 | |
---|
183 | self.widget.onAmplitudeChange() |
---|
184 | |
---|
185 | # Check the arguments to Normalize |
---|
186 | mpl.colors.Normalize.assert_called_with(vmin=1.0, vmax=10.0) |
---|
187 | self.assertTrue(self.widget.canvas.draw.called) |
---|
188 | |
---|
189 | # Bad values in fields |
---|
190 | self.widget.txtMinAmplitude.setText("cake") |
---|
191 | self.widget.txtMaxAmplitude.setText("more cake") |
---|
192 | |
---|
193 | self.widget.onAmplitudeChange() |
---|
194 | |
---|
195 | # Check the arguments to Normalize - should be defaults |
---|
196 | mpl.colors.Normalize.assert_called_with(vmin=0.0, vmax=100.0) |
---|
197 | self.assertTrue(self.widget.canvas.draw.called) |
---|
198 | |
---|
199 | |
---|
200 | if __name__ == "__main__": |
---|
201 | unittest.main() |
---|