1 | import sys |
---|
2 | import unittest |
---|
3 | import numpy |
---|
4 | |
---|
5 | from PyQt4 import QtGui |
---|
6 | from PyQt4 import QtCore |
---|
7 | from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas |
---|
8 | from mock import MagicMock |
---|
9 | from mpl_toolkits.mplot3d import Axes3D |
---|
10 | |
---|
11 | ####### TEMP |
---|
12 | import path_prepare |
---|
13 | ####### |
---|
14 | from sas.qtgui.Plotting.PlotterData import Data1D |
---|
15 | from sas.qtgui.Plotting.PlotterData import Data2D |
---|
16 | from UnitTesting.TestUtils import WarningTestNotImplemented |
---|
17 | |
---|
18 | # Tested module |
---|
19 | import sas.qtgui.Plotting.Plotter2D as Plotter2D |
---|
20 | |
---|
21 | app = QtGui.QApplication(sys.argv) |
---|
22 | |
---|
23 | class Plotter2DTest(unittest.TestCase): |
---|
24 | '''Test the Plotter 2D class''' |
---|
25 | def setUp(self): |
---|
26 | '''create''' |
---|
27 | class dummy_manager(object): |
---|
28 | def communicator(self): |
---|
29 | return Communicate() |
---|
30 | def perspective(self): |
---|
31 | return MyPerspective() |
---|
32 | def workspace(self): |
---|
33 | return None |
---|
34 | |
---|
35 | self.plotter = Plotter2D.Plotter2D(parent=dummy_manager(), quickplot=True) |
---|
36 | |
---|
37 | self.data = Data2D(image=[0.1]*4, |
---|
38 | qx_data=[1.0, 2.0, 3.0, 4.0], |
---|
39 | qy_data=[10.0, 11.0, 12.0, 13.0], |
---|
40 | dqx_data=[0.1, 0.2, 0.3, 0.4], |
---|
41 | dqy_data=[0.1, 0.2, 0.3, 0.4], |
---|
42 | q_data=[1,2,3,4], |
---|
43 | xmin=-1.0, xmax=5.0, |
---|
44 | ymin=-1.0, ymax=15.0, |
---|
45 | zmin=-1.0, zmax=20.0) |
---|
46 | |
---|
47 | self.data.title="Test data" |
---|
48 | self.data.id = 1 |
---|
49 | |
---|
50 | def tearDown(self): |
---|
51 | '''destroy''' |
---|
52 | self.plotter = None |
---|
53 | |
---|
54 | def testDataProperty(self): |
---|
55 | """ Adding data """ |
---|
56 | self.plotter.data = self.data |
---|
57 | |
---|
58 | self.assertEqual(self.plotter.data, self.data) |
---|
59 | self.assertEqual(self.plotter._title, self.data.title) |
---|
60 | self.assertEqual(self.plotter.xLabel, "$\\rm{Q_{x}}(A^{-1})$") |
---|
61 | self.assertEqual(self.plotter.yLabel, "$\\rm{Q_{y}}(A^{-1})$") |
---|
62 | |
---|
63 | def testPlot(self): |
---|
64 | """ Look at the plotting """ |
---|
65 | self.plotter.data = self.data |
---|
66 | self.plotter.show() |
---|
67 | FigureCanvas.draw_idle = MagicMock() |
---|
68 | |
---|
69 | self.plotter.plot() |
---|
70 | |
---|
71 | self.assertTrue(FigureCanvas.draw_idle.called) |
---|
72 | |
---|
73 | def testCalculateDepth(self): |
---|
74 | ''' Test the depth calculator ''' |
---|
75 | self.plotter.data = self.data |
---|
76 | |
---|
77 | # Default, log scale |
---|
78 | depth = self.plotter.calculateDepth() |
---|
79 | self.assertEqual(depth, (0.1, 1.e20)) |
---|
80 | |
---|
81 | # Change the scale to linear |
---|
82 | self.plotter.scale = 'linear' |
---|
83 | depth = self.plotter.calculateDepth() |
---|
84 | self.assertEqual(depth[0], -32.) |
---|
85 | self.assertAlmostEqual(depth[1], 1.30103, 5) |
---|
86 | |
---|
87 | def testOnColorMap(self): |
---|
88 | ''' Respond to the color map event ''' |
---|
89 | self.plotter.data = self.data |
---|
90 | self.plotter.plot() |
---|
91 | self.plotter.show() |
---|
92 | |
---|
93 | QtGui.QDialog.exec_ = MagicMock(return_value=QtGui.QDialog.Accepted) |
---|
94 | |
---|
95 | # Just this one plot |
---|
96 | self.plotter.onColorMap() |
---|
97 | |
---|
98 | # Check that exec_ got called |
---|
99 | self.assertTrue(QtGui.QDialog.exec_.called) |
---|
100 | |
---|
101 | self.assertEqual(self.plotter.cmap, "jet") |
---|
102 | self.assertAlmostEqual(self.plotter.vmin, 0.1, 6) |
---|
103 | self.assertAlmostEqual(self.plotter.vmax, 1e+20, 6) |
---|
104 | |
---|
105 | def testOnToggleScale(self): |
---|
106 | """ Respond to the event by replotting """ |
---|
107 | self.plotter.data = self.data |
---|
108 | self.plotter.show() |
---|
109 | FigureCanvas.draw_idle = MagicMock() |
---|
110 | |
---|
111 | self.plotter.onToggleScale(None) |
---|
112 | |
---|
113 | self.assertTrue(FigureCanvas.draw_idle.called) |
---|
114 | |
---|
115 | def testOnBoxSum(self): |
---|
116 | """ Test the box sum display and functionality """ |
---|
117 | |
---|
118 | # hacky way to make things work in manipulations._sum |
---|
119 | self.data.detector = [1] |
---|
120 | self.data.err_data = numpy.array([0.0, 0.0, 0.1, 0.0]) |
---|
121 | self.plotter.data = self.data |
---|
122 | self.plotter.show() |
---|
123 | |
---|
124 | # Mock the main window |
---|
125 | self.plotter.manager.parent = MagicMock() |
---|
126 | |
---|
127 | # Call the main tested method |
---|
128 | self.plotter.onBoxSum() |
---|
129 | |
---|
130 | # Test various properties |
---|
131 | self.assertIsInstance(self.plotter.slicer.model(), QtGui.QStandardItemModel) |
---|
132 | self.assertTrue(self.plotter.boxwidget.isVisible()) |
---|
133 | self.assertIsInstance(self.plotter.boxwidget.model, QtGui.QStandardItemModel) |
---|
134 | |
---|
135 | def testContextMenuQuickPlot(self): |
---|
136 | """ Test the right click menu """ |
---|
137 | self.plotter.data = self.data |
---|
138 | self.plotter.createContextMenuQuick() |
---|
139 | actions = self.plotter.contextMenu.actions() |
---|
140 | self.assertEqual(len(actions), 7) |
---|
141 | |
---|
142 | # Trigger Save Image and make sure the method is called |
---|
143 | self.assertEqual(actions[0].text(), "Save Image") |
---|
144 | self.plotter.toolbar.save_figure = MagicMock() |
---|
145 | actions[0].trigger() |
---|
146 | self.assertTrue(self.plotter.toolbar.save_figure.called) |
---|
147 | |
---|
148 | # Trigger Print Image and make sure the method is called |
---|
149 | self.assertEqual(actions[1].text(), "Print Image") |
---|
150 | QtGui.QPrintDialog.exec_ = MagicMock(return_value=QtGui.QDialog.Rejected) |
---|
151 | actions[1].trigger() |
---|
152 | self.assertTrue(QtGui.QPrintDialog.exec_.called) |
---|
153 | |
---|
154 | # Trigger Copy to Clipboard and make sure the method is called |
---|
155 | self.assertEqual(actions[2].text(), "Copy to Clipboard") |
---|
156 | |
---|
157 | # Spy on cliboard's dataChanged() signal |
---|
158 | self.clipboard_called = False |
---|
159 | def done(): |
---|
160 | self.clipboard_called = True |
---|
161 | QtCore.QObject.connect(app.clipboard(), QtCore.SIGNAL("dataChanged()"), done) |
---|
162 | actions[2].trigger() |
---|
163 | QtGui.qApp.processEvents() |
---|
164 | # Make sure clipboard got updated. |
---|
165 | #self.assertTrue(self.clipboard_called) |
---|
166 | |
---|
167 | # Trigger Toggle Grid and make sure the method is called |
---|
168 | self.assertEqual(actions[4].text(), "Toggle Grid On/Off") |
---|
169 | self.plotter.ax.grid = MagicMock() |
---|
170 | actions[4].trigger() |
---|
171 | self.assertTrue(self.plotter.ax.grid.called) |
---|
172 | |
---|
173 | # Trigger Change Scale and make sure the method is called |
---|
174 | self.assertEqual(actions[6].text(), "Toggle Linear/Log Scale") |
---|
175 | FigureCanvas.draw_idle = MagicMock() |
---|
176 | actions[6].trigger() |
---|
177 | self.assertTrue(FigureCanvas.draw_idle.called) |
---|
178 | |
---|
179 | def testShowNoPlot(self): |
---|
180 | """ Test the plot rendering and generation """ |
---|
181 | |
---|
182 | FigureCanvas.draw_idle = MagicMock() |
---|
183 | FigureCanvas.draw = MagicMock() |
---|
184 | |
---|
185 | # Test early return on no data |
---|
186 | self.plotter.showPlot(data=None, |
---|
187 | qx_data=self.data.qx_data, |
---|
188 | qy_data=self.data.qy_data, |
---|
189 | xmin=self.data.xmin, |
---|
190 | xmax=self.data.xmax, |
---|
191 | ymin=self.data.ymin, ymax=self.data.ymax, |
---|
192 | cmap=None, zmin=None, |
---|
193 | zmax=None) |
---|
194 | |
---|
195 | self.assertFalse(FigureCanvas.draw_idle.called) |
---|
196 | self.assertFalse(FigureCanvas.draw.called) |
---|
197 | |
---|
198 | def testShow3DPlot(self): |
---|
199 | """ Test the 3Dplot rendering and generation """ |
---|
200 | # Test 3D printout |
---|
201 | FigureCanvas.draw = MagicMock() |
---|
202 | Axes3D.plot_surface = MagicMock() |
---|
203 | self.plotter.figure.colorbar = MagicMock() |
---|
204 | |
---|
205 | self.plotter.dimension = 3 |
---|
206 | self.plotter.data = self.data |
---|
207 | self.plotter.showPlot(data=self.plotter.data.data, |
---|
208 | qx_data=self.data.qx_data, |
---|
209 | qy_data=self.data.qy_data, |
---|
210 | xmin=self.data.xmin, |
---|
211 | xmax=self.data.xmax, |
---|
212 | ymin=self.data.ymin, ymax=self.data.ymax, |
---|
213 | cmap=None, zmin=None, |
---|
214 | zmax=None) |
---|
215 | self.assertTrue(Axes3D.plot_surface.called) |
---|
216 | self.assertTrue(FigureCanvas.draw.called) |
---|
217 | |
---|
218 | def testShow2DPlot(self): |
---|
219 | """ Test the 2Dplot rendering and generation """ |
---|
220 | # Test 2D printout |
---|
221 | FigureCanvas.draw_idle = MagicMock() |
---|
222 | self.plotter.figure.colorbar = MagicMock() |
---|
223 | |
---|
224 | self.plotter.dimension = 2 |
---|
225 | self.plotter.data = self.data |
---|
226 | self.plotter.showPlot(data=self.plotter.data.data, |
---|
227 | qx_data=self.data.qx_data, |
---|
228 | qy_data=self.data.qy_data, |
---|
229 | xmin=self.data.xmin, |
---|
230 | xmax=self.data.xmax, |
---|
231 | ymin=self.data.ymin, ymax=self.data.ymax, |
---|
232 | cmap=None, zmin=None, |
---|
233 | zmax=None) |
---|
234 | self.assertTrue(FigureCanvas.draw_idle.called) |
---|
235 | |
---|
236 | |
---|
237 | if __name__ == "__main__": |
---|
238 | unittest.main() |
---|