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