1 | import sys |
---|
2 | import unittest |
---|
3 | import platform |
---|
4 | from unittest.mock import MagicMock |
---|
5 | |
---|
6 | from PyQt5 import QtGui, QtWidgets, QtPrintSupport |
---|
7 | import matplotlib.pyplot as plt |
---|
8 | from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas |
---|
9 | from matplotlib.backends.backend_qt5agg 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 QtWidgets.QApplication.instance(): |
---|
24 | app = QtWidgets.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, QtWidgets.QWidget) |
---|
48 | self.assertIsInstance(self.plotter.canvas, FigureCanvas) |
---|
49 | self.assertIsInstance(self.plotter.properties, ScaleProperties) |
---|
50 | |
---|
51 | self.assertEqual(self.plotter._data, []) |
---|
52 | self.assertEqual(self.plotter._xscale, 'log') |
---|
53 | self.assertEqual(self.plotter._yscale, 'log') |
---|
54 | self.assertEqual(self.plotter.scale, 'linear') |
---|
55 | self.assertFalse(self.plotter.grid_on) |
---|
56 | self.assertEqual(self.plotter.x_label, 'log10(x)') |
---|
57 | self.assertEqual(self.plotter.y_label, 'log10(y)') |
---|
58 | |
---|
59 | def testData(self): |
---|
60 | ''' Test the pure virtual method ''' |
---|
61 | with self.assertRaises(NotImplementedError): |
---|
62 | self.plotter.data=[] |
---|
63 | |
---|
64 | def testContextMenu(self): |
---|
65 | ''' Test the default context menu ''' |
---|
66 | with self.assertRaises(NotImplementedError): |
---|
67 | self.plotter.createContextMenu() |
---|
68 | |
---|
69 | def testClean(self): |
---|
70 | ''' test the graph cleanup ''' |
---|
71 | self.plotter.figure.delaxes = MagicMock() |
---|
72 | self.plotter.clean() |
---|
73 | self.assertTrue(self.plotter.figure.delaxes.called) |
---|
74 | |
---|
75 | def testPlot(self): |
---|
76 | ''' test the pure virtual method ''' |
---|
77 | with self.assertRaises(NotImplementedError): |
---|
78 | self.plotter.plot() |
---|
79 | |
---|
80 | def notestOnCloseEvent(self): |
---|
81 | ''' test the plotter close behaviour ''' |
---|
82 | PlotHelper.deletePlot = MagicMock() |
---|
83 | self.plotter.closeEvent(None) |
---|
84 | self.assertTrue(PlotHelper.deletePlot.called) |
---|
85 | |
---|
86 | def notestOnImagePrint(self): |
---|
87 | ''' test the workspace print ''' |
---|
88 | QtGui.QPainter.end = MagicMock() |
---|
89 | QtWidgets.QLabel.render = MagicMock() |
---|
90 | |
---|
91 | # First, let's cancel printing |
---|
92 | QtPrintSupport.QPrintDialog.exec_ = MagicMock(return_value=QtWidgets.QDialog.Rejected) |
---|
93 | self.plotter.onImagePrint() |
---|
94 | self.assertFalse(QtGui.QPainter.end.called) |
---|
95 | self.assertFalse(QtWidgets.QLabel.render.called) |
---|
96 | |
---|
97 | # Let's print now |
---|
98 | QtPrintSupport.QPrintDialog.exec_ = MagicMock(return_value=QtWidgets.QDialog.Accepted) |
---|
99 | self.plotter.onImagePrint() |
---|
100 | self.assertTrue(QtGui.QPainter.end.called) |
---|
101 | self.assertTrue(QtWidgets.QLabel.render.called) |
---|
102 | |
---|
103 | def testOnClipboardCopy(self): |
---|
104 | ''' test the workspace screen copy ''' |
---|
105 | QtGui.QClipboard.setPixmap = MagicMock() |
---|
106 | self.plotter.onClipboardCopy() |
---|
107 | self.assertTrue(QtGui.QClipboard.setPixmap.called) |
---|
108 | |
---|
109 | def testOnGridToggle(self): |
---|
110 | ''' test toggling the grid lines ''' |
---|
111 | # Check the toggle |
---|
112 | orig_toggle = self.plotter.grid_on |
---|
113 | |
---|
114 | FigureCanvas.draw_idle = MagicMock() |
---|
115 | self.plotter.onGridToggle() |
---|
116 | |
---|
117 | self.assertTrue(FigureCanvas.draw_idle.called) |
---|
118 | self.assertTrue(self.plotter.grid_on != orig_toggle) |
---|
119 | |
---|
120 | def testDefaultContextMenu(self): |
---|
121 | """ Test the right click default menu """ |
---|
122 | |
---|
123 | self.plotter.defaultContextMenu() |
---|
124 | |
---|
125 | actions = self.plotter.contextMenu.actions() |
---|
126 | self.assertEqual(len(actions), 4) |
---|
127 | |
---|
128 | # Trigger Print Image and make sure the method is called |
---|
129 | self.assertEqual(actions[1].text(), "Print Image") |
---|
130 | QtPrintSupport.QPrintDialog.exec_ = MagicMock(return_value=QtWidgets.QDialog.Rejected) |
---|
131 | actions[1].trigger() |
---|
132 | self.assertTrue(QtPrintSupport.QPrintDialog.exec_.called) |
---|
133 | |
---|
134 | # Trigger Copy to Clipboard and make sure the method is called |
---|
135 | self.assertEqual(actions[2].text(), "Copy to Clipboard") |
---|
136 | |
---|
137 | # Spy on cliboard's dataChanged() signal |
---|
138 | if not self.isWindows: |
---|
139 | return |
---|
140 | self.clipboard_called = False |
---|
141 | def done(): |
---|
142 | self.clipboard_called = True |
---|
143 | QtCore.QObject.connect(QtWidgets.qApp.clipboard(), QtCore.SIGNAL("dataChanged()"), done) |
---|
144 | actions[2].trigger() |
---|
145 | QtWidgets.qApp.processEvents() |
---|
146 | # Make sure clipboard got updated. |
---|
147 | self.assertTrue(self.clipboard_called) |
---|
148 | |
---|
149 | ## Trigger toggle navigation bar and make sure the method is called |
---|
150 | #self.assertEqual(actions[4].text(), "Toggle Navigation Menu") |
---|
151 | #isShown = self.plotter.toolbar.isVisible() |
---|
152 | #self.assertTrue(isShow) |
---|
153 | #actions[4].trigger() |
---|
154 | #isShown = self.plotter.toolbar.isVisible() |
---|
155 | #self.assertFalse(isShow) |
---|
156 | #actions[4].trigger() |
---|
157 | #isShown = self.plotter.toolbar.isVisible() |
---|
158 | #self.assertTrue(isShow) |
---|
159 | |
---|
160 | |
---|
161 | def testOnWindowsTitle(self): |
---|
162 | """ Test changing the plot title""" |
---|
163 | # Mock the modal dialog's response |
---|
164 | QtWidgets.QDialog.exec_ = MagicMock(return_value=QtWidgets.QDialog.Accepted) |
---|
165 | self.plotter.show() |
---|
166 | # Assure the original title is none |
---|
167 | self.assertEqual(self.plotter.windowTitle(), "") |
---|
168 | self.plotter.manager.communicator = MagicMock() |
---|
169 | |
---|
170 | WindowTitle.title = MagicMock(return_value="I am a new title") |
---|
171 | # Change the title |
---|
172 | self.plotter.onWindowsTitle() |
---|
173 | |
---|
174 | self.assertEqual(self.plotter.windowTitle(), "I am a new title") |
---|
175 | |
---|
176 | def testOnMplMouseDown(self): |
---|
177 | """ Test what happens on mouse click down in chart """ |
---|
178 | pass |
---|
179 | |
---|
180 | def testOnMplMouseUp(self): |
---|
181 | """ Test what happens on mouse release in chart """ |
---|
182 | pass |
---|
183 | |
---|
184 | def testOnMplMouseMotion(self): |
---|
185 | """ Test what happens on mouse move in chart """ |
---|
186 | pass |
---|
187 | |
---|
188 | def testOnMplPick(self): |
---|
189 | """ Test what happens on mouse pick in chart """ |
---|
190 | pass |
---|
191 | |
---|
192 | def testOnMplWheel(self): |
---|
193 | """ Test what happens on mouse pick in chart """ |
---|
194 | pass |
---|
195 | |
---|
196 | if __name__ == "__main__": |
---|
197 | unittest.main() |
---|