[55d89f8] | 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 |
---|
[64f1e93] | 8 | from mpl_toolkits.mplot3d import Axes3D |
---|
[55d89f8] | 9 | |
---|
| 10 | ####### TEMP |
---|
[416fa8f] | 11 | import path_prepare |
---|
[55d89f8] | 12 | ####### |
---|
| 13 | from sas.sasgui.guiframe.dataFitting import Data1D |
---|
| 14 | from sas.sasgui.guiframe.dataFitting import Data2D |
---|
| 15 | |
---|
| 16 | # Tested module |
---|
| 17 | import sas.qtgui.Plotter2D as Plotter2D |
---|
| 18 | |
---|
| 19 | app = QtGui.QApplication(sys.argv) |
---|
| 20 | |
---|
| 21 | class Plotter2DTest(unittest.TestCase): |
---|
| 22 | '''Test the Plotter 2D class''' |
---|
| 23 | def setUp(self): |
---|
| 24 | '''create''' |
---|
| 25 | self.plotter = Plotter2D.Plotter2D(None, quickplot=True) |
---|
| 26 | |
---|
| 27 | self.data = Data2D(image=[0.1]*4, |
---|
| 28 | qx_data=[1.0, 2.0, 3.0, 4.0], |
---|
| 29 | qy_data=[10.0, 11.0, 12.0, 13.0], |
---|
| 30 | dqx_data=[0.1, 0.2, 0.3, 0.4], |
---|
| 31 | dqy_data=[0.1, 0.2, 0.3, 0.4], |
---|
| 32 | q_data=[1,2,3,4], |
---|
| 33 | xmin=-1.0, xmax=5.0, |
---|
| 34 | ymin=-1.0, ymax=15.0, |
---|
| 35 | zmin=-1.0, zmax=20.0) |
---|
| 36 | |
---|
| 37 | self.data.title="Test data" |
---|
| 38 | self.data.id = 1 |
---|
| 39 | |
---|
| 40 | def tearDown(self): |
---|
| 41 | '''destroy''' |
---|
| 42 | self.plotter = None |
---|
| 43 | |
---|
| 44 | def testDataProperty(self): |
---|
| 45 | """ Adding data """ |
---|
| 46 | self.plotter.data = self.data |
---|
| 47 | |
---|
| 48 | self.assertEqual(self.plotter.data, self.data) |
---|
| 49 | self.assertEqual(self.plotter._title, self.data.title) |
---|
| 50 | self.assertEqual(self.plotter.xLabel, "$\\rm{Q_{x}}(A^{-1})$") |
---|
| 51 | self.assertEqual(self.plotter.yLabel, "$\\rm{Q_{y}}(A^{-1})$") |
---|
| 52 | |
---|
| 53 | def testPlot(self): |
---|
| 54 | """ Look at the plotting """ |
---|
| 55 | self.plotter.data = self.data |
---|
| 56 | self.plotter.show() |
---|
[64f1e93] | 57 | FigureCanvas.draw_idle = MagicMock() |
---|
[55d89f8] | 58 | |
---|
| 59 | self.plotter.plot() |
---|
| 60 | |
---|
[64f1e93] | 61 | self.assertTrue(FigureCanvas.draw_idle.called) |
---|
| 62 | |
---|
| 63 | def testOnToggleScale(self): |
---|
| 64 | """ Respond to the event by replotting """ |
---|
| 65 | self.plotter.data = self.data |
---|
| 66 | self.plotter.show() |
---|
| 67 | FigureCanvas.draw_idle = MagicMock() |
---|
| 68 | |
---|
| 69 | self.plotter.onToggleScale(None) |
---|
| 70 | |
---|
| 71 | self.assertTrue(FigureCanvas.draw_idle.called) |
---|
[55d89f8] | 72 | |
---|
| 73 | def testContextMenuQuickPlot(self): |
---|
| 74 | """ Test the right click menu """ |
---|
| 75 | self.plotter.data = self.data |
---|
[b46f285] | 76 | self.plotter.createContextMenuQuick() |
---|
[55d89f8] | 77 | actions = self.plotter.contextMenu.actions() |
---|
| 78 | self.assertEqual(len(actions), 7) |
---|
| 79 | |
---|
| 80 | # Trigger Save Image and make sure the method is called |
---|
| 81 | self.assertEqual(actions[0].text(), "Save Image") |
---|
| 82 | self.plotter.toolbar.save_figure = MagicMock() |
---|
| 83 | actions[0].trigger() |
---|
| 84 | self.assertTrue(self.plotter.toolbar.save_figure.called) |
---|
| 85 | |
---|
| 86 | # Trigger Print Image and make sure the method is called |
---|
| 87 | self.assertEqual(actions[1].text(), "Print Image") |
---|
| 88 | QtGui.QPrintDialog.exec_ = MagicMock(return_value=QtGui.QDialog.Rejected) |
---|
| 89 | actions[1].trigger() |
---|
| 90 | self.assertTrue(QtGui.QPrintDialog.exec_.called) |
---|
| 91 | |
---|
| 92 | # Trigger Copy to Clipboard and make sure the method is called |
---|
| 93 | self.assertEqual(actions[2].text(), "Copy to Clipboard") |
---|
| 94 | |
---|
| 95 | # Spy on cliboard's dataChanged() signal |
---|
| 96 | self.clipboard_called = False |
---|
| 97 | def done(): |
---|
| 98 | self.clipboard_called = True |
---|
| 99 | QtCore.QObject.connect(app.clipboard(), QtCore.SIGNAL("dataChanged()"), done) |
---|
| 100 | actions[2].trigger() |
---|
| 101 | QtGui.qApp.processEvents() |
---|
| 102 | # Make sure clipboard got updated. |
---|
[27313b7] | 103 | #self.assertTrue(self.clipboard_called) |
---|
[55d89f8] | 104 | |
---|
| 105 | # Trigger Toggle Grid and make sure the method is called |
---|
| 106 | self.assertEqual(actions[4].text(), "Toggle Grid On/Off") |
---|
| 107 | self.plotter.ax.grid = MagicMock() |
---|
| 108 | actions[4].trigger() |
---|
| 109 | self.assertTrue(self.plotter.ax.grid.called) |
---|
| 110 | |
---|
| 111 | # Trigger Change Scale and make sure the method is called |
---|
| 112 | self.assertEqual(actions[6].text(), "Toggle Linear/Log Scale") |
---|
[64f1e93] | 113 | FigureCanvas.draw_idle = MagicMock() |
---|
[55d89f8] | 114 | actions[6].trigger() |
---|
[64f1e93] | 115 | self.assertTrue(FigureCanvas.draw_idle.called) |
---|
| 116 | |
---|
| 117 | def testShowNoPlot(self): |
---|
| 118 | """ Test the plot rendering and generation """ |
---|
| 119 | |
---|
| 120 | FigureCanvas.draw_idle = MagicMock() |
---|
| 121 | FigureCanvas.draw = MagicMock() |
---|
| 122 | |
---|
| 123 | # Test early return on no data |
---|
| 124 | self.plotter.showPlot(data=None, |
---|
| 125 | qx_data=self.data.qx_data, |
---|
| 126 | qy_data=self.data.qy_data, |
---|
| 127 | xmin=self.data.xmin, |
---|
| 128 | xmax=self.data.xmax, |
---|
| 129 | ymin=self.data.ymin, ymax=self.data.ymax, |
---|
| 130 | cmap=None, zmin=None, |
---|
| 131 | zmax=None) |
---|
| 132 | |
---|
| 133 | self.assertFalse(FigureCanvas.draw_idle.called) |
---|
| 134 | self.assertFalse(FigureCanvas.draw.called) |
---|
| 135 | |
---|
| 136 | def testShow3DPlot(self): |
---|
| 137 | """ Test the 3Dplot rendering and generation """ |
---|
| 138 | # Test 3D printout |
---|
| 139 | FigureCanvas.draw = MagicMock() |
---|
| 140 | Axes3D.plot_surface = MagicMock() |
---|
| 141 | self.plotter.figure.colorbar = MagicMock() |
---|
| 142 | |
---|
| 143 | self.plotter.dimension = 3 |
---|
| 144 | self.plotter.data = self.data |
---|
| 145 | self.plotter.showPlot(data=self.plotter.data.data, |
---|
| 146 | qx_data=self.data.qx_data, |
---|
| 147 | qy_data=self.data.qy_data, |
---|
| 148 | xmin=self.data.xmin, |
---|
| 149 | xmax=self.data.xmax, |
---|
| 150 | ymin=self.data.ymin, ymax=self.data.ymax, |
---|
| 151 | cmap=None, zmin=None, |
---|
| 152 | zmax=None) |
---|
| 153 | self.assertTrue(Axes3D.plot_surface.called) |
---|
| 154 | self.assertTrue(FigureCanvas.draw.called) |
---|
| 155 | |
---|
| 156 | def testShow2DPlot(self): |
---|
| 157 | """ Test the 2Dplot rendering and generation """ |
---|
| 158 | # Test 2D printout |
---|
| 159 | FigureCanvas.draw_idle = MagicMock() |
---|
| 160 | self.plotter.figure.colorbar = MagicMock() |
---|
| 161 | |
---|
| 162 | self.plotter.dimension = 2 |
---|
| 163 | self.plotter.data = self.data |
---|
| 164 | self.plotter.showPlot(data=self.plotter.data.data, |
---|
| 165 | qx_data=self.data.qx_data, |
---|
| 166 | qy_data=self.data.qy_data, |
---|
| 167 | xmin=self.data.xmin, |
---|
| 168 | xmax=self.data.xmax, |
---|
| 169 | ymin=self.data.ymin, ymax=self.data.ymax, |
---|
| 170 | cmap=None, zmin=None, |
---|
| 171 | zmax=None) |
---|
| 172 | self.assertTrue(FigureCanvas.draw_idle.called) |
---|
[55d89f8] | 173 | |
---|
| 174 | |
---|
| 175 | if __name__ == "__main__": |
---|
| 176 | unittest.main() |
---|