[3b7b218] | 1 | import sys |
---|
| 2 | import unittest |
---|
[b2a5042] | 3 | import platform |
---|
[c4e5400] | 4 | from mock import patch, MagicMock |
---|
[3b7b218] | 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 | |
---|
[83eb5208] | 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 |
---|
[3b7b218] | 19 | |
---|
| 20 | # Tested module |
---|
[83eb5208] | 21 | import sas.qtgui.Plotting.PlotterBase as PlotterBase |
---|
[3b7b218] | 22 | |
---|
[464cd07] | 23 | if not QtGui.QApplication.instance(): |
---|
| 24 | app = QtGui.QApplication(sys.argv) |
---|
[3b7b218] | 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 | |
---|
[aadf0af1] | 36 | #PlotterBase.PlotterBase.contextMenuQuickPlot = MagicMock() |
---|
[3b7b218] | 37 | self.plotter = PlotterBase.PlotterBase(None, manager=dummy_manager(), quickplot=True) |
---|
[b2a5042] | 38 | self.isWindows = platform.system=="Windows" |
---|
[3b7b218] | 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 ''' |
---|
[c4e5400] | 67 | with self.assertRaises(NotImplementedError): |
---|
[aadf0af1] | 68 | self.plotter.createContextMenu() |
---|
[3b7b218] | 69 | |
---|
| 70 | def testClean(self): |
---|
| 71 | ''' test the graph cleanup ''' |
---|
[c4e5400] | 72 | self.plotter.figure.delaxes = MagicMock() |
---|
| 73 | self.plotter.clean() |
---|
| 74 | self.assertTrue(self.plotter.figure.delaxes.called) |
---|
[3b7b218] | 75 | |
---|
| 76 | def testPlot(self): |
---|
| 77 | ''' test the pure virtual method ''' |
---|
| 78 | with self.assertRaises(NotImplementedError): |
---|
| 79 | self.plotter.plot() |
---|
| 80 | |
---|
[c4e5400] | 81 | def notestOnCloseEvent(self): |
---|
[3b7b218] | 82 | ''' test the plotter close behaviour ''' |
---|
[c4e5400] | 83 | PlotHelper.deletePlot = MagicMock() |
---|
| 84 | self.plotter.closeEvent(None) |
---|
| 85 | self.assertTrue(PlotHelper.deletePlot.called) |
---|
[3b7b218] | 86 | |
---|
| 87 | def testOnImageSave(self): |
---|
| 88 | ''' test the workspace save ''' |
---|
[c4e5400] | 89 | self.plotter.toolbar.save_figure = MagicMock() |
---|
| 90 | self.plotter.onImageSave() |
---|
| 91 | self.assertTrue(self.plotter.toolbar.save_figure.called) |
---|
[3b7b218] | 92 | |
---|
| 93 | def testOnImagePrint(self): |
---|
| 94 | ''' test the workspace print ''' |
---|
[c4e5400] | 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) |
---|
[3b7b218] | 115 | |
---|
| 116 | def testOnGridToggle(self): |
---|
| 117 | ''' test toggling the grid lines ''' |
---|
[c4e5400] | 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 |
---|
[b2a5042] | 151 | if not self.isWindows: |
---|
| 152 | return |
---|
[c4e5400] | 153 | self.clipboard_called = False |
---|
| 154 | def done(): |
---|
| 155 | self.clipboard_called = True |
---|
[464cd07] | 156 | QtCore.QObject.connect(QtGui.qApp.clipboard(), QtCore.SIGNAL("dataChanged()"), done) |
---|
[c4e5400] | 157 | actions[2].trigger() |
---|
| 158 | QtGui.qApp.processEvents() |
---|
| 159 | # Make sure clipboard got updated. |
---|
| 160 | self.assertTrue(self.clipboard_called) |
---|
[3b7b218] | 161 | |
---|
[27313b7] | 162 | def testOnWindowsTitle(self): |
---|
[9290b1a] | 163 | """ Test changing the plot title""" |
---|
[27313b7] | 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") |
---|
[3b7b218] | 176 | |
---|
[9290b1a] | 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 | |
---|
[3b7b218] | 197 | if __name__ == "__main__": |
---|
| 198 | unittest.main() |
---|