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 mock import patch |
---|
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 | from UnitTesting.TestUtils import QtSignalSpy |
---|
16 | |
---|
17 | # Tested module |
---|
18 | import sas.qtgui.Plotter as Plotter |
---|
19 | |
---|
20 | app = QtGui.QApplication(sys.argv) |
---|
21 | |
---|
22 | class PlotterTest(unittest.TestCase): |
---|
23 | '''Test the Plotter 1D class''' |
---|
24 | def setUp(self): |
---|
25 | '''create''' |
---|
26 | |
---|
27 | self.plotter = Plotter.Plotter(None, quickplot=True) |
---|
28 | self.data = Data1D(x=[1.0, 2.0, 3.0], |
---|
29 | y=[10.0, 11.0, 12.0], |
---|
30 | dx=[0.1, 0.2, 0.3], |
---|
31 | dy=[0.1, 0.2, 0.3]) |
---|
32 | self.data.title="Test data" |
---|
33 | self.data.name="Test name" |
---|
34 | self.data.id = 1 |
---|
35 | |
---|
36 | def tearDown(self): |
---|
37 | '''destroy''' |
---|
38 | self.plotter = None |
---|
39 | |
---|
40 | def testDataProperty(self): |
---|
41 | """ Adding data """ |
---|
42 | self.plotter.data = self.data |
---|
43 | |
---|
44 | self.assertEqual(self.plotter.data, self.data) |
---|
45 | self.assertEqual(self.plotter._title, self.data.name) |
---|
46 | self.assertEqual(self.plotter.xLabel, "$()$") |
---|
47 | self.assertEqual(self.plotter.yLabel, "$()$") |
---|
48 | |
---|
49 | def testPlotWithErrors(self): |
---|
50 | """ Look at the plotting with error bars""" |
---|
51 | self.plotter.data = self.data |
---|
52 | self.plotter.show() |
---|
53 | FigureCanvas.draw = MagicMock() |
---|
54 | |
---|
55 | self.plotter.plot(hide_error=False) |
---|
56 | |
---|
57 | self.assertEqual(self.plotter.ax.get_xscale(), 'log') |
---|
58 | self.assertTrue(FigureCanvas.draw.called) |
---|
59 | |
---|
60 | def testPlotWithoutErrors(self): |
---|
61 | """ Look at the plotting without error bars""" |
---|
62 | self.plotter.data = self.data |
---|
63 | self.plotter.show() |
---|
64 | FigureCanvas.draw = MagicMock() |
---|
65 | |
---|
66 | self.plotter.plot(hide_error=True) |
---|
67 | |
---|
68 | self.assertEqual(self.plotter.ax.get_yscale(), 'log') |
---|
69 | self.assertTrue(FigureCanvas.draw.called) |
---|
70 | |
---|
71 | def testCreateContextMenuQuick(self): |
---|
72 | """ Test the right click menu """ |
---|
73 | self.plotter.createContextMenuQuick() |
---|
74 | actions = self.plotter.contextMenu.actions() |
---|
75 | self.assertEqual(len(actions), 7) |
---|
76 | |
---|
77 | # Trigger Save Image and make sure the method is called |
---|
78 | self.assertEqual(actions[0].text(), "Save Image") |
---|
79 | self.plotter.toolbar.save_figure = MagicMock() |
---|
80 | actions[0].trigger() |
---|
81 | self.assertTrue(self.plotter.toolbar.save_figure.called) |
---|
82 | |
---|
83 | # Trigger Print Image and make sure the method is called |
---|
84 | self.assertEqual(actions[1].text(), "Print Image") |
---|
85 | QtGui.QPrintDialog.exec_ = MagicMock(return_value=QtGui.QDialog.Rejected) |
---|
86 | actions[1].trigger() |
---|
87 | self.assertTrue(QtGui.QPrintDialog.exec_.called) |
---|
88 | |
---|
89 | # Trigger Copy to Clipboard and make sure the method is called |
---|
90 | self.assertEqual(actions[2].text(), "Copy to Clipboard") |
---|
91 | |
---|
92 | # Spy on cliboard's dataChanged() signal |
---|
93 | self.clipboard_called = False |
---|
94 | def done(): |
---|
95 | self.clipboard_called = True |
---|
96 | QtCore.QObject.connect(app.clipboard(), QtCore.SIGNAL("dataChanged()"), done) |
---|
97 | actions[2].trigger() |
---|
98 | QtGui.qApp.processEvents() |
---|
99 | # Make sure clipboard got updated. |
---|
100 | #self.assertTrue(self.clipboard_called) |
---|
101 | |
---|
102 | # Trigger Toggle Grid and make sure the method is called |
---|
103 | self.assertEqual(actions[4].text(), "Toggle Grid On/Off") |
---|
104 | self.plotter.ax.grid = MagicMock() |
---|
105 | actions[4].trigger() |
---|
106 | self.assertTrue(self.plotter.ax.grid.called) |
---|
107 | |
---|
108 | # Trigger Change Scale and make sure the method is called |
---|
109 | self.assertEqual(actions[6].text(), "Change Scale") |
---|
110 | self.plotter.properties.exec_ = MagicMock(return_value=QtGui.QDialog.Rejected) |
---|
111 | actions[6].trigger() |
---|
112 | self.assertTrue(self.plotter.properties.exec_.called) |
---|
113 | |
---|
114 | def testXyTransform(self): |
---|
115 | """ Tests the XY transformation and new chart update """ |
---|
116 | self.plotter.plot(self.data) |
---|
117 | |
---|
118 | # Transform the points |
---|
119 | self.plotter.xyTransform(xLabel="x", yLabel="log10(y)") |
---|
120 | |
---|
121 | # Assure new plot has correct labels |
---|
122 | self.assertEqual(self.plotter.ax.get_xlabel(), "$()$") |
---|
123 | self.assertEqual(self.plotter.ax.get_ylabel(), "$()$") |
---|
124 | # ... and scale |
---|
125 | self.assertEqual(self.plotter.xscale, "linear") |
---|
126 | self.assertEqual(self.plotter.yscale, "log") |
---|
127 | # See that just one plot is present |
---|
128 | self.assertEqual(len(self.plotter.plot_dict), 1) |
---|
129 | self.assertEqual(len(self.plotter.ax.collections), 1) |
---|
130 | |
---|
131 | def testAddText(self): |
---|
132 | """ Checks the functionality of adding text to graph """ |
---|
133 | |
---|
134 | self.plotter.plot(self.data) |
---|
135 | self.plotter.x_click = 100.0 |
---|
136 | self.plotter.y_click = 100.0 |
---|
137 | # modify the text edit control |
---|
138 | test_text = "Smoke in cabin" |
---|
139 | test_font = QtGui.QFont("Arial", 16, QtGui.QFont.Bold) |
---|
140 | test_color = "#00FF00" |
---|
141 | self.plotter.addText.textEdit.setText(test_text) |
---|
142 | |
---|
143 | # Return the requested font parameters |
---|
144 | self.plotter.addText.font = MagicMock(return_value = test_font) |
---|
145 | self.plotter.addText.color = MagicMock(return_value = test_color) |
---|
146 | # Return OK from the dialog |
---|
147 | self.plotter.addText.exec_ = MagicMock(return_value = QtGui.QDialog.Accepted) |
---|
148 | # Add text to graph |
---|
149 | self.plotter.onAddText() |
---|
150 | self.plotter.show() |
---|
151 | # Check if the text was added properly |
---|
152 | self.assertEqual(len(self.plotter.textList), 1) |
---|
153 | self.assertEqual(self.plotter.textList[0].get_text(), test_text) |
---|
154 | self.assertEqual(self.plotter.textList[0].get_color(), test_color) |
---|
155 | self.assertEqual(self.plotter.textList[0].get_fontproperties().get_family()[0], 'Arial') |
---|
156 | self.assertEqual(self.plotter.textList[0].get_fontproperties().get_size(), 16) |
---|
157 | |
---|
158 | def testOnRemoveText(self): |
---|
159 | """ Cheks if annotations can be removed from the graph """ |
---|
160 | |
---|
161 | # Add some text |
---|
162 | self.plotter.plot(self.data) |
---|
163 | test_text = "Safety instructions" |
---|
164 | self.plotter.addText.textEdit.setText(test_text) |
---|
165 | # Return OK from the dialog |
---|
166 | self.plotter.addText.exec_ = MagicMock(return_value = QtGui.QDialog.Accepted) |
---|
167 | # Add text to graph |
---|
168 | self.plotter.onAddText() |
---|
169 | self.plotter.show() |
---|
170 | # Check if the text was added properly |
---|
171 | self.assertEqual(len(self.plotter.textList), 1) |
---|
172 | |
---|
173 | # Now, remove the text |
---|
174 | self.plotter.onRemoveText() |
---|
175 | |
---|
176 | # And assure no text is displayed |
---|
177 | self.assertEqual(self.plotter.textList, []) |
---|
178 | |
---|
179 | # Attempt removal on empty and check |
---|
180 | self.plotter.onRemoveText() |
---|
181 | self.assertEqual(self.plotter.textList, []) |
---|
182 | |
---|
183 | def testOnSetGraphRange(self): |
---|
184 | """ Cheks if the graph can be resized for range """ |
---|
185 | new_x = (1,2) |
---|
186 | new_y = (10,11) |
---|
187 | self.plotter.plot(self.data) |
---|
188 | self.plotter.show() |
---|
189 | self.plotter.setRange.exec_ = MagicMock(return_value = QtGui.QDialog.Accepted) |
---|
190 | self.plotter.setRange.xrange = MagicMock(return_value = new_x) |
---|
191 | self.plotter.setRange.yrange = MagicMock(return_value = new_y) |
---|
192 | |
---|
193 | # Call the tested method |
---|
194 | self.plotter.onSetGraphRange() |
---|
195 | # See that ranges changed accordingly |
---|
196 | self.assertEqual(self.plotter.ax.get_xlim(), new_x) |
---|
197 | self.assertEqual(self.plotter.ax.get_ylim(), new_y) |
---|
198 | |
---|
199 | def testOnResetGraphRange(self): |
---|
200 | """ Cheks if the graph can be reset after resizing for range """ |
---|
201 | # New values |
---|
202 | new_x = (1,2) |
---|
203 | new_y = (10,11) |
---|
204 | # define the plot |
---|
205 | self.plotter.plot(self.data) |
---|
206 | self.plotter.show() |
---|
207 | |
---|
208 | # mock setRange methods |
---|
209 | self.plotter.setRange.exec_ = MagicMock(return_value = QtGui.QDialog.Accepted) |
---|
210 | self.plotter.setRange.xrange = MagicMock(return_value = new_x) |
---|
211 | self.plotter.setRange.yrange = MagicMock(return_value = new_y) |
---|
212 | |
---|
213 | # Change the axes range |
---|
214 | self.plotter.onSetGraphRange() |
---|
215 | |
---|
216 | # Now, reset the range back |
---|
217 | self.plotter.onResetGraphRange() |
---|
218 | |
---|
219 | # See that ranges are changed |
---|
220 | self.assertNotEqual(self.plotter.ax.get_xlim(), new_x) |
---|
221 | self.assertNotEqual(self.plotter.ax.get_ylim(), new_y) |
---|
222 | |
---|
223 | def testOnLinearFit(self): |
---|
224 | """ Checks the response to LinearFit call """ |
---|
225 | pass |
---|
226 | |
---|
227 | def testOnRemovePlot(self): |
---|
228 | """ Assure plots get removed when requested """ |
---|
229 | pass |
---|
230 | |
---|
231 | def testRemovePlot(self): |
---|
232 | """ Test plot removal """ |
---|
233 | pass |
---|
234 | |
---|
235 | def testOnToggleHideError(self): |
---|
236 | """ Test the error bar toggle on plots """ |
---|
237 | pass |
---|
238 | |
---|
239 | def testOnFitDisplay(self): |
---|
240 | """ Test the fit line display on the chart """ |
---|
241 | pass |
---|
242 | |
---|
243 | if __name__ == "__main__": |
---|
244 | unittest.main() |
---|