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