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