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