1 | import sys |
---|
2 | import unittest |
---|
3 | import platform |
---|
4 | from unittest.mock import MagicMock |
---|
5 | |
---|
6 | from PyQt4 import QtGui |
---|
7 | import matplotlib.pyplot as plt |
---|
8 | from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas |
---|
9 | from matplotlib.backends.backend_qt4agg import NavigationToolbar2QT as NavigationToolbar |
---|
10 | |
---|
11 | ####### TEMP |
---|
12 | import path_prepare |
---|
13 | ####### |
---|
14 | |
---|
15 | from sas.qtgui.Plotting.ScaleProperties import ScaleProperties |
---|
16 | from sas.qtgui.Plotting.WindowTitle import WindowTitle |
---|
17 | from sas.qtgui.Utilities.GuiUtils import * |
---|
18 | import sas.qtgui.Plotting.PlotHelper as PlotHelper |
---|
19 | |
---|
20 | # Tested module |
---|
21 | import sas.qtgui.Plotting.PlotterBase as PlotterBase |
---|
22 | |
---|
23 | if not QtGui.QApplication.instance(): |
---|
24 | app = QtGui.QApplication(sys.argv) |
---|
25 | |
---|
26 | class PlotterBaseTest(unittest.TestCase): |
---|
27 | '''Test the Plotter base class''' |
---|
28 | def setUp(self): |
---|
29 | '''create''' |
---|
30 | class dummy_manager(object): |
---|
31 | def communicator(self): |
---|
32 | return Communicate() |
---|
33 | def perspective(self): |
---|
34 | return MyPerspective() |
---|
35 | |
---|
36 | #PlotterBase.PlotterBase.contextMenuQuickPlot = MagicMock() |
---|
37 | self.plotter = PlotterBase.PlotterBase(None, manager=dummy_manager(), quickplot=True) |
---|
38 | self.isWindows = platform.system=="Windows" |
---|
39 | |
---|
40 | def tearDown(self): |
---|
41 | '''destroy''' |
---|
42 | self.plotter = None |
---|
43 | self.plotter_qp = None |
---|
44 | |
---|
45 | def testDefaults(self): |
---|
46 | """ default method variables values """ |
---|
47 | self.assertIsInstance(self.plotter, QtGui.QWidget) |
---|
48 | self.assertIsInstance(self.plotter.canvas, FigureCanvas) |
---|
49 | self.assertIsInstance(self.plotter.toolbar, NavigationToolbar) |
---|
50 | self.assertIsInstance(self.plotter.properties, ScaleProperties) |
---|
51 | |
---|
52 | self.assertEqual(self.plotter._data, []) |
---|
53 | self.assertEqual(self.plotter._xscale, 'log') |
---|
54 | self.assertEqual(self.plotter._yscale, 'log') |
---|
55 | self.assertEqual(self.plotter.scale, 'linear') |
---|
56 | self.assertFalse(self.plotter.grid_on) |
---|
57 | self.assertEqual(self.plotter.x_label, 'log10(x)') |
---|
58 | self.assertEqual(self.plotter.y_label, 'log10(y)') |
---|
59 | |
---|
60 | def testData(self): |
---|
61 | ''' Test the pure virtual method ''' |
---|
62 | with self.assertRaises(NotImplementedError): |
---|
63 | self.plotter.data=[] |
---|
64 | |
---|
65 | def testContextMenu(self): |
---|
66 | ''' Test the default context menu ''' |
---|
67 | with self.assertRaises(NotImplementedError): |
---|
68 | self.plotter.createContextMenu() |
---|
69 | |
---|
70 | def testClean(self): |
---|
71 | ''' test the graph cleanup ''' |
---|
72 | self.plotter.figure.delaxes = MagicMock() |
---|
73 | self.plotter.clean() |
---|
74 | self.assertTrue(self.plotter.figure.delaxes.called) |
---|
75 | |
---|
76 | def testPlot(self): |
---|
77 | ''' test the pure virtual method ''' |
---|
78 | with self.assertRaises(NotImplementedError): |
---|
79 | self.plotter.plot() |
---|
80 | |
---|
81 | def notestOnCloseEvent(self): |
---|
82 | ''' test the plotter close behaviour ''' |
---|
83 | PlotHelper.deletePlot = MagicMock() |
---|
84 | self.plotter.closeEvent(None) |
---|
85 | self.assertTrue(PlotHelper.deletePlot.called) |
---|
86 | |
---|
87 | def testOnImageSave(self): |
---|
88 | ''' test the workspace save ''' |
---|
89 | self.plotter.toolbar.save_figure = MagicMock() |
---|
90 | self.plotter.onImageSave() |
---|
91 | self.assertTrue(self.plotter.toolbar.save_figure.called) |
---|
92 | |
---|
93 | def testOnImagePrint(self): |
---|
94 | ''' test the workspace print ''' |
---|
95 | QtGui.QPainter.end = MagicMock() |
---|
96 | QtGui.QLabel.render = MagicMock() |
---|
97 | |
---|
98 | # First, let's cancel printing |
---|
99 | QtGui.QPrintDialog.exec_ = MagicMock(return_value=QtGui.QDialog.Rejected) |
---|
100 | self.plotter.onImagePrint() |
---|
101 | self.assertFalse(QtGui.QPainter.end.called) |
---|
102 | self.assertFalse(QtGui.QLabel.render.called) |
---|
103 | |
---|
104 | # Let's print now |
---|
105 | QtGui.QPrintDialog.exec_ = MagicMock(return_value=QtGui.QDialog.Accepted) |
---|
106 | self.plotter.onImagePrint() |
---|
107 | self.assertTrue(QtGui.QPainter.end.called) |
---|
108 | self.assertTrue(QtGui.QLabel.render.called) |
---|
109 | |
---|
110 | def testOnClipboardCopy(self): |
---|
111 | ''' test the workspace screen copy ''' |
---|
112 | QtGui.QClipboard.setPixmap = MagicMock() |
---|
113 | self.plotter.onClipboardCopy() |
---|
114 | self.assertTrue(QtGui.QClipboard.setPixmap.called) |
---|
115 | |
---|
116 | def testOnGridToggle(self): |
---|
117 | ''' test toggling the grid lines ''' |
---|
118 | # Check the toggle |
---|
119 | orig_toggle = self.plotter.grid_on |
---|
120 | |
---|
121 | FigureCanvas.draw_idle = MagicMock() |
---|
122 | self.plotter.onGridToggle() |
---|
123 | |
---|
124 | self.assertTrue(FigureCanvas.draw_idle.called) |
---|
125 | self.assertTrue(self.plotter.grid_on != orig_toggle) |
---|
126 | |
---|
127 | def testDefaultContextMenu(self): |
---|
128 | """ Test the right click default menu """ |
---|
129 | |
---|
130 | self.plotter.defaultContextMenu() |
---|
131 | |
---|
132 | actions = self.plotter.contextMenu.actions() |
---|
133 | self.assertEqual(len(actions), 4) |
---|
134 | |
---|
135 | # Trigger Save Image and make sure the method is called |
---|
136 | self.assertEqual(actions[0].text(), "Save Image") |
---|
137 | self.plotter.toolbar.save_figure = MagicMock() |
---|
138 | actions[0].trigger() |
---|
139 | self.assertTrue(self.plotter.toolbar.save_figure.called) |
---|
140 | |
---|
141 | # Trigger Print Image and make sure the method is called |
---|
142 | self.assertEqual(actions[1].text(), "Print Image") |
---|
143 | QtGui.QPrintDialog.exec_ = MagicMock(return_value=QtGui.QDialog.Rejected) |
---|
144 | actions[1].trigger() |
---|
145 | self.assertTrue(QtGui.QPrintDialog.exec_.called) |
---|
146 | |
---|
147 | # Trigger Copy to Clipboard and make sure the method is called |
---|
148 | self.assertEqual(actions[2].text(), "Copy to Clipboard") |
---|
149 | |
---|
150 | # Spy on cliboard's dataChanged() signal |
---|
151 | if not self.isWindows: |
---|
152 | return |
---|
153 | self.clipboard_called = False |
---|
154 | def done(): |
---|
155 | self.clipboard_called = True |
---|
156 | QtCore.QObject.connect(QtGui.qApp.clipboard(), QtCore.SIGNAL("dataChanged()"), done) |
---|
157 | actions[2].trigger() |
---|
158 | QtGui.qApp.processEvents() |
---|
159 | # Make sure clipboard got updated. |
---|
160 | self.assertTrue(self.clipboard_called) |
---|
161 | |
---|
162 | def testOnWindowsTitle(self): |
---|
163 | """ Test changing the plot title""" |
---|
164 | # Mock the modal dialog's response |
---|
165 | QtGui.QDialog.exec_ = MagicMock(return_value=QtGui.QDialog.Accepted) |
---|
166 | self.plotter.show() |
---|
167 | # Assure the original title is none |
---|
168 | self.assertEqual(self.plotter.windowTitle(), "") |
---|
169 | self.plotter.manager.communicator = MagicMock() |
---|
170 | |
---|
171 | WindowTitle.title = MagicMock(return_value="I am a new title") |
---|
172 | # Change the title |
---|
173 | self.plotter.onWindowsTitle() |
---|
174 | |
---|
175 | self.assertEqual(self.plotter.windowTitle(), "I am a new title") |
---|
176 | |
---|
177 | def testOnMplMouseDown(self): |
---|
178 | """ Test what happens on mouse click down in chart """ |
---|
179 | pass |
---|
180 | |
---|
181 | def testOnMplMouseUp(self): |
---|
182 | """ Test what happens on mouse release in chart """ |
---|
183 | pass |
---|
184 | |
---|
185 | def testOnMplMouseMotion(self): |
---|
186 | """ Test what happens on mouse move in chart """ |
---|
187 | pass |
---|
188 | |
---|
189 | def testOnMplPick(self): |
---|
190 | """ Test what happens on mouse pick in chart """ |
---|
191 | pass |
---|
192 | |
---|
193 | def testOnMplWheel(self): |
---|
194 | """ Test what happens on mouse pick in chart """ |
---|
195 | pass |
---|
196 | |
---|
197 | if __name__ == "__main__": |
---|
198 | unittest.main() |
---|