1 | import sys |
---|
2 | import unittest |
---|
3 | |
---|
4 | from PyQt4 import QtGui |
---|
5 | from PyQt4 import QtCore |
---|
6 | from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas |
---|
7 | from mock import MagicMock |
---|
8 | from mpl_toolkits.mplot3d import Axes3D |
---|
9 | |
---|
10 | ####### TEMP |
---|
11 | import path_prepare |
---|
12 | ####### |
---|
13 | from sas.sasgui.guiframe.dataFitting import Data1D |
---|
14 | from sas.sasgui.guiframe.dataFitting import Data2D |
---|
15 | |
---|
16 | # Tested module |
---|
17 | import sas.qtgui.Plotter2D as Plotter2D |
---|
18 | |
---|
19 | app = QtGui.QApplication(sys.argv) |
---|
20 | |
---|
21 | class Plotter2DTest(unittest.TestCase): |
---|
22 | '''Test the Plotter 2D class''' |
---|
23 | def setUp(self): |
---|
24 | '''create''' |
---|
25 | self.plotter = Plotter2D.Plotter2D(None, quickplot=True) |
---|
26 | |
---|
27 | self.data = Data2D(image=[0.1]*4, |
---|
28 | qx_data=[1.0, 2.0, 3.0, 4.0], |
---|
29 | qy_data=[10.0, 11.0, 12.0, 13.0], |
---|
30 | dqx_data=[0.1, 0.2, 0.3, 0.4], |
---|
31 | dqy_data=[0.1, 0.2, 0.3, 0.4], |
---|
32 | q_data=[1,2,3,4], |
---|
33 | xmin=-1.0, xmax=5.0, |
---|
34 | ymin=-1.0, ymax=15.0, |
---|
35 | zmin=-1.0, zmax=20.0) |
---|
36 | |
---|
37 | self.data.title="Test data" |
---|
38 | self.data.id = 1 |
---|
39 | |
---|
40 | def tearDown(self): |
---|
41 | '''destroy''' |
---|
42 | self.plotter = None |
---|
43 | |
---|
44 | def testDataProperty(self): |
---|
45 | """ Adding data """ |
---|
46 | self.plotter.data = self.data |
---|
47 | |
---|
48 | self.assertEqual(self.plotter.data, self.data) |
---|
49 | self.assertEqual(self.plotter._title, self.data.title) |
---|
50 | self.assertEqual(self.plotter.xLabel, "$\\rm{Q_{x}}(A^{-1})$") |
---|
51 | self.assertEqual(self.plotter.yLabel, "$\\rm{Q_{y}}(A^{-1})$") |
---|
52 | |
---|
53 | def testPlot(self): |
---|
54 | """ Look at the plotting """ |
---|
55 | self.plotter.data = self.data |
---|
56 | self.plotter.show() |
---|
57 | FigureCanvas.draw_idle = MagicMock() |
---|
58 | |
---|
59 | self.plotter.plot() |
---|
60 | |
---|
61 | self.assertTrue(FigureCanvas.draw_idle.called) |
---|
62 | |
---|
63 | def testOnToggleScale(self): |
---|
64 | """ Respond to the event by replotting """ |
---|
65 | self.plotter.data = self.data |
---|
66 | self.plotter.show() |
---|
67 | FigureCanvas.draw_idle = MagicMock() |
---|
68 | |
---|
69 | self.plotter.onToggleScale(None) |
---|
70 | |
---|
71 | self.assertTrue(FigureCanvas.draw_idle.called) |
---|
72 | |
---|
73 | def testContextMenuQuickPlot(self): |
---|
74 | """ Test the right click menu """ |
---|
75 | self.plotter.data = self.data |
---|
76 | actions = self.plotter.contextMenu.actions() |
---|
77 | self.assertEqual(len(actions), 7) |
---|
78 | |
---|
79 | # Trigger Save Image and make sure the method is called |
---|
80 | self.assertEqual(actions[0].text(), "Save Image") |
---|
81 | self.plotter.toolbar.save_figure = MagicMock() |
---|
82 | actions[0].trigger() |
---|
83 | self.assertTrue(self.plotter.toolbar.save_figure.called) |
---|
84 | |
---|
85 | # Trigger Print Image and make sure the method is called |
---|
86 | self.assertEqual(actions[1].text(), "Print Image") |
---|
87 | QtGui.QPrintDialog.exec_ = MagicMock(return_value=QtGui.QDialog.Rejected) |
---|
88 | actions[1].trigger() |
---|
89 | self.assertTrue(QtGui.QPrintDialog.exec_.called) |
---|
90 | |
---|
91 | # Trigger Copy to Clipboard and make sure the method is called |
---|
92 | self.assertEqual(actions[2].text(), "Copy to Clipboard") |
---|
93 | |
---|
94 | # Spy on cliboard's dataChanged() signal |
---|
95 | self.clipboard_called = False |
---|
96 | def done(): |
---|
97 | self.clipboard_called = True |
---|
98 | QtCore.QObject.connect(app.clipboard(), QtCore.SIGNAL("dataChanged()"), done) |
---|
99 | actions[2].trigger() |
---|
100 | QtGui.qApp.processEvents() |
---|
101 | # Make sure clipboard got updated. |
---|
102 | #self.assertTrue(self.clipboard_called) |
---|
103 | |
---|
104 | # Trigger Toggle Grid and make sure the method is called |
---|
105 | self.assertEqual(actions[4].text(), "Toggle Grid On/Off") |
---|
106 | self.plotter.ax.grid = MagicMock() |
---|
107 | actions[4].trigger() |
---|
108 | self.assertTrue(self.plotter.ax.grid.called) |
---|
109 | |
---|
110 | # Trigger Change Scale and make sure the method is called |
---|
111 | self.assertEqual(actions[6].text(), "Toggle Linear/Log Scale") |
---|
112 | FigureCanvas.draw_idle = MagicMock() |
---|
113 | actions[6].trigger() |
---|
114 | self.assertTrue(FigureCanvas.draw_idle.called) |
---|
115 | |
---|
116 | def testShowNoPlot(self): |
---|
117 | """ Test the plot rendering and generation """ |
---|
118 | |
---|
119 | FigureCanvas.draw_idle = MagicMock() |
---|
120 | FigureCanvas.draw = MagicMock() |
---|
121 | |
---|
122 | # Test early return on no data |
---|
123 | self.plotter.showPlot(data=None, |
---|
124 | qx_data=self.data.qx_data, |
---|
125 | qy_data=self.data.qy_data, |
---|
126 | xmin=self.data.xmin, |
---|
127 | xmax=self.data.xmax, |
---|
128 | ymin=self.data.ymin, ymax=self.data.ymax, |
---|
129 | cmap=None, zmin=None, |
---|
130 | zmax=None) |
---|
131 | |
---|
132 | self.assertFalse(FigureCanvas.draw_idle.called) |
---|
133 | self.assertFalse(FigureCanvas.draw.called) |
---|
134 | |
---|
135 | def testShow3DPlot(self): |
---|
136 | """ Test the 3Dplot rendering and generation """ |
---|
137 | # Test 3D printout |
---|
138 | FigureCanvas.draw = MagicMock() |
---|
139 | Axes3D.plot_surface = MagicMock() |
---|
140 | self.plotter.figure.colorbar = MagicMock() |
---|
141 | |
---|
142 | self.plotter.dimension = 3 |
---|
143 | self.plotter.data = self.data |
---|
144 | self.plotter.showPlot(data=self.plotter.data.data, |
---|
145 | qx_data=self.data.qx_data, |
---|
146 | qy_data=self.data.qy_data, |
---|
147 | xmin=self.data.xmin, |
---|
148 | xmax=self.data.xmax, |
---|
149 | ymin=self.data.ymin, ymax=self.data.ymax, |
---|
150 | cmap=None, zmin=None, |
---|
151 | zmax=None) |
---|
152 | self.assertTrue(Axes3D.plot_surface.called) |
---|
153 | self.assertTrue(FigureCanvas.draw.called) |
---|
154 | |
---|
155 | def testShow2DPlot(self): |
---|
156 | """ Test the 2Dplot rendering and generation """ |
---|
157 | # Test 2D printout |
---|
158 | FigureCanvas.draw_idle = MagicMock() |
---|
159 | self.plotter.figure.colorbar = MagicMock() |
---|
160 | |
---|
161 | self.plotter.dimension = 2 |
---|
162 | self.plotter.data = self.data |
---|
163 | self.plotter.showPlot(data=self.plotter.data.data, |
---|
164 | qx_data=self.data.qx_data, |
---|
165 | qy_data=self.data.qy_data, |
---|
166 | xmin=self.data.xmin, |
---|
167 | xmax=self.data.xmax, |
---|
168 | ymin=self.data.ymin, ymax=self.data.ymax, |
---|
169 | cmap=None, zmin=None, |
---|
170 | zmax=None) |
---|
171 | self.assertTrue(FigureCanvas.draw_idle.called) |
---|
172 | |
---|
173 | |
---|
174 | if __name__ == "__main__": |
---|
175 | unittest.main() |
---|