[6d05e1d] | 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 |
---|
[257bd57] | 8 | from mock import patch |
---|
[6d05e1d] | 9 | |
---|
| 10 | ####### TEMP |
---|
[416fa8f] | 11 | import path_prepare |
---|
[6d05e1d] | 12 | ####### |
---|
| 13 | from sas.sasgui.guiframe.dataFitting import Data1D |
---|
| 14 | from sas.sasgui.guiframe.dataFitting import Data2D |
---|
[db5cd8d] | 15 | from UnitTesting.TestUtils import WarningTestNotImplemented |
---|
[6d05e1d] | 16 | |
---|
| 17 | # Tested module |
---|
| 18 | import sas.qtgui.Plotter as Plotter |
---|
| 19 | |
---|
| 20 | app = QtGui.QApplication(sys.argv) |
---|
| 21 | |
---|
[87cc73a] | 22 | |
---|
[6d05e1d] | 23 | class PlotterTest(unittest.TestCase): |
---|
| 24 | '''Test the Plotter 1D class''' |
---|
| 25 | def setUp(self): |
---|
| 26 | '''create''' |
---|
[257bd57] | 27 | |
---|
[6d05e1d] | 28 | self.plotter = Plotter.Plotter(None, quickplot=True) |
---|
| 29 | self.data = Data1D(x=[1.0, 2.0, 3.0], |
---|
| 30 | y=[10.0, 11.0, 12.0], |
---|
| 31 | dx=[0.1, 0.2, 0.3], |
---|
| 32 | dy=[0.1, 0.2, 0.3]) |
---|
| 33 | self.data.title="Test data" |
---|
[aadf0af1] | 34 | self.data.name="Test name" |
---|
[6d05e1d] | 35 | self.data.id = 1 |
---|
| 36 | |
---|
| 37 | def tearDown(self): |
---|
| 38 | '''destroy''' |
---|
| 39 | self.plotter = None |
---|
| 40 | |
---|
| 41 | def testDataProperty(self): |
---|
| 42 | """ Adding data """ |
---|
| 43 | self.plotter.data = self.data |
---|
| 44 | |
---|
| 45 | self.assertEqual(self.plotter.data, self.data) |
---|
[aadf0af1] | 46 | self.assertEqual(self.plotter._title, self.data.name) |
---|
[6d05e1d] | 47 | self.assertEqual(self.plotter.xLabel, "$()$") |
---|
| 48 | self.assertEqual(self.plotter.yLabel, "$()$") |
---|
| 49 | |
---|
[fecfe28] | 50 | def testPlotWithErrors(self): |
---|
| 51 | """ Look at the plotting with error bars""" |
---|
[6d05e1d] | 52 | self.plotter.data = self.data |
---|
| 53 | self.plotter.show() |
---|
| 54 | FigureCanvas.draw = MagicMock() |
---|
| 55 | |
---|
[fecfe28] | 56 | self.plotter.plot(hide_error=False) |
---|
[6d05e1d] | 57 | |
---|
[fecfe28] | 58 | self.assertEqual(self.plotter.ax.get_xscale(), 'log') |
---|
| 59 | self.assertTrue(FigureCanvas.draw.called) |
---|
| 60 | |
---|
| 61 | def testPlotWithoutErrors(self): |
---|
| 62 | """ Look at the plotting without error bars""" |
---|
| 63 | self.plotter.data = self.data |
---|
| 64 | self.plotter.show() |
---|
| 65 | FigureCanvas.draw = MagicMock() |
---|
| 66 | |
---|
| 67 | self.plotter.plot(hide_error=True) |
---|
| 68 | |
---|
| 69 | self.assertEqual(self.plotter.ax.get_yscale(), 'log') |
---|
[6d05e1d] | 70 | self.assertTrue(FigureCanvas.draw.called) |
---|
| 71 | |
---|
[b46f285] | 72 | def testCreateContextMenuQuick(self): |
---|
[6d05e1d] | 73 | """ Test the right click menu """ |
---|
[aadf0af1] | 74 | self.plotter.createContextMenuQuick() |
---|
[6d05e1d] | 75 | actions = self.plotter.contextMenu.actions() |
---|
| 76 | self.assertEqual(len(actions), 7) |
---|
| 77 | |
---|
| 78 | # Trigger Save Image and make sure the method is called |
---|
| 79 | self.assertEqual(actions[0].text(), "Save Image") |
---|
| 80 | self.plotter.toolbar.save_figure = MagicMock() |
---|
| 81 | actions[0].trigger() |
---|
| 82 | self.assertTrue(self.plotter.toolbar.save_figure.called) |
---|
| 83 | |
---|
| 84 | # Trigger Print Image and make sure the method is called |
---|
| 85 | self.assertEqual(actions[1].text(), "Print Image") |
---|
| 86 | QtGui.QPrintDialog.exec_ = MagicMock(return_value=QtGui.QDialog.Rejected) |
---|
| 87 | actions[1].trigger() |
---|
| 88 | self.assertTrue(QtGui.QPrintDialog.exec_.called) |
---|
| 89 | |
---|
| 90 | # Trigger Copy to Clipboard and make sure the method is called |
---|
| 91 | self.assertEqual(actions[2].text(), "Copy to Clipboard") |
---|
| 92 | |
---|
| 93 | # Spy on cliboard's dataChanged() signal |
---|
| 94 | self.clipboard_called = False |
---|
| 95 | def done(): |
---|
| 96 | self.clipboard_called = True |
---|
| 97 | QtCore.QObject.connect(app.clipboard(), QtCore.SIGNAL("dataChanged()"), done) |
---|
| 98 | actions[2].trigger() |
---|
| 99 | QtGui.qApp.processEvents() |
---|
| 100 | # Make sure clipboard got updated. |
---|
[27313b7] | 101 | #self.assertTrue(self.clipboard_called) |
---|
[6d05e1d] | 102 | |
---|
| 103 | # Trigger Toggle Grid and make sure the method is called |
---|
| 104 | self.assertEqual(actions[4].text(), "Toggle Grid On/Off") |
---|
| 105 | self.plotter.ax.grid = MagicMock() |
---|
| 106 | actions[4].trigger() |
---|
| 107 | self.assertTrue(self.plotter.ax.grid.called) |
---|
| 108 | |
---|
| 109 | # Trigger Change Scale and make sure the method is called |
---|
| 110 | self.assertEqual(actions[6].text(), "Change Scale") |
---|
| 111 | self.plotter.properties.exec_ = MagicMock(return_value=QtGui.QDialog.Rejected) |
---|
| 112 | actions[6].trigger() |
---|
| 113 | self.assertTrue(self.plotter.properties.exec_.called) |
---|
| 114 | |
---|
[b46f285] | 115 | def testXyTransform(self): |
---|
| 116 | """ Tests the XY transformation and new chart update """ |
---|
[257bd57] | 117 | self.plotter.plot(self.data) |
---|
[6d05e1d] | 118 | |
---|
[b46f285] | 119 | # Transform the points |
---|
| 120 | self.plotter.xyTransform(xLabel="x", yLabel="log10(y)") |
---|
[6d05e1d] | 121 | |
---|
[b46f285] | 122 | # Assure new plot has correct labels |
---|
[64f1e93] | 123 | self.assertEqual(self.plotter.ax.get_xlabel(), "$()$") |
---|
| 124 | self.assertEqual(self.plotter.ax.get_ylabel(), "$()$") |
---|
[b46f285] | 125 | # ... and scale |
---|
| 126 | self.assertEqual(self.plotter.xscale, "linear") |
---|
| 127 | self.assertEqual(self.plotter.yscale, "log") |
---|
| 128 | # See that just one plot is present |
---|
| 129 | self.assertEqual(len(self.plotter.plot_dict), 1) |
---|
| 130 | self.assertEqual(len(self.plotter.ax.collections), 1) |
---|
[64f1e93] | 131 | |
---|
[9290b1a] | 132 | def testAddText(self): |
---|
| 133 | """ Checks the functionality of adding text to graph """ |
---|
[257bd57] | 134 | |
---|
| 135 | self.plotter.plot(self.data) |
---|
| 136 | self.plotter.x_click = 100.0 |
---|
| 137 | self.plotter.y_click = 100.0 |
---|
| 138 | # modify the text edit control |
---|
| 139 | test_text = "Smoke in cabin" |
---|
| 140 | test_font = QtGui.QFont("Arial", 16, QtGui.QFont.Bold) |
---|
| 141 | test_color = "#00FF00" |
---|
| 142 | self.plotter.addText.textEdit.setText(test_text) |
---|
| 143 | |
---|
| 144 | # Return the requested font parameters |
---|
| 145 | self.plotter.addText.font = MagicMock(return_value = test_font) |
---|
| 146 | self.plotter.addText.color = MagicMock(return_value = test_color) |
---|
| 147 | # Return OK from the dialog |
---|
| 148 | self.plotter.addText.exec_ = MagicMock(return_value = QtGui.QDialog.Accepted) |
---|
| 149 | # Add text to graph |
---|
| 150 | self.plotter.onAddText() |
---|
| 151 | self.plotter.show() |
---|
| 152 | # Check if the text was added properly |
---|
| 153 | self.assertEqual(len(self.plotter.textList), 1) |
---|
| 154 | self.assertEqual(self.plotter.textList[0].get_text(), test_text) |
---|
| 155 | self.assertEqual(self.plotter.textList[0].get_color(), test_color) |
---|
| 156 | self.assertEqual(self.plotter.textList[0].get_fontproperties().get_family()[0], 'Arial') |
---|
| 157 | self.assertEqual(self.plotter.textList[0].get_fontproperties().get_size(), 16) |
---|
[9290b1a] | 158 | |
---|
| 159 | def testOnRemoveText(self): |
---|
| 160 | """ Cheks if annotations can be removed from the graph """ |
---|
[257bd57] | 161 | |
---|
| 162 | # Add some text |
---|
| 163 | self.plotter.plot(self.data) |
---|
| 164 | test_text = "Safety instructions" |
---|
| 165 | self.plotter.addText.textEdit.setText(test_text) |
---|
| 166 | # Return OK from the dialog |
---|
| 167 | self.plotter.addText.exec_ = MagicMock(return_value = QtGui.QDialog.Accepted) |
---|
| 168 | # Add text to graph |
---|
| 169 | self.plotter.onAddText() |
---|
| 170 | self.plotter.show() |
---|
| 171 | # Check if the text was added properly |
---|
| 172 | self.assertEqual(len(self.plotter.textList), 1) |
---|
| 173 | |
---|
| 174 | # Now, remove the text |
---|
| 175 | self.plotter.onRemoveText() |
---|
| 176 | |
---|
| 177 | # And assure no text is displayed |
---|
| 178 | self.assertEqual(self.plotter.textList, []) |
---|
| 179 | |
---|
| 180 | # Attempt removal on empty and check |
---|
| 181 | self.plotter.onRemoveText() |
---|
| 182 | self.assertEqual(self.plotter.textList, []) |
---|
[9290b1a] | 183 | |
---|
[d3ca363] | 184 | def testOnSetGraphRange(self): |
---|
| 185 | """ Cheks if the graph can be resized for range """ |
---|
[257bd57] | 186 | new_x = (1,2) |
---|
| 187 | new_y = (10,11) |
---|
| 188 | self.plotter.plot(self.data) |
---|
| 189 | self.plotter.show() |
---|
| 190 | self.plotter.setRange.exec_ = MagicMock(return_value = QtGui.QDialog.Accepted) |
---|
| 191 | self.plotter.setRange.xrange = MagicMock(return_value = new_x) |
---|
| 192 | self.plotter.setRange.yrange = MagicMock(return_value = new_y) |
---|
| 193 | |
---|
| 194 | # Call the tested method |
---|
| 195 | self.plotter.onSetGraphRange() |
---|
| 196 | # See that ranges changed accordingly |
---|
| 197 | self.assertEqual(self.plotter.ax.get_xlim(), new_x) |
---|
| 198 | self.assertEqual(self.plotter.ax.get_ylim(), new_y) |
---|
[d3ca363] | 199 | |
---|
| 200 | def testOnResetGraphRange(self): |
---|
| 201 | """ Cheks if the graph can be reset after resizing for range """ |
---|
[257bd57] | 202 | # New values |
---|
| 203 | new_x = (1,2) |
---|
| 204 | new_y = (10,11) |
---|
| 205 | # define the plot |
---|
| 206 | self.plotter.plot(self.data) |
---|
| 207 | self.plotter.show() |
---|
| 208 | |
---|
| 209 | # mock setRange methods |
---|
| 210 | self.plotter.setRange.exec_ = MagicMock(return_value = QtGui.QDialog.Accepted) |
---|
| 211 | self.plotter.setRange.xrange = MagicMock(return_value = new_x) |
---|
| 212 | self.plotter.setRange.yrange = MagicMock(return_value = new_y) |
---|
| 213 | |
---|
| 214 | # Change the axes range |
---|
| 215 | self.plotter.onSetGraphRange() |
---|
| 216 | |
---|
| 217 | # Now, reset the range back |
---|
| 218 | self.plotter.onResetGraphRange() |
---|
| 219 | |
---|
| 220 | # See that ranges are changed |
---|
| 221 | self.assertNotEqual(self.plotter.ax.get_xlim(), new_x) |
---|
| 222 | self.assertNotEqual(self.plotter.ax.get_ylim(), new_y) |
---|
[9290b1a] | 223 | |
---|
[b46f285] | 224 | def testOnLinearFit(self): |
---|
| 225 | """ Checks the response to LinearFit call """ |
---|
[db5cd8d] | 226 | WarningTestNotImplemented() |
---|
[b46f285] | 227 | |
---|
| 228 | def testOnRemovePlot(self): |
---|
| 229 | """ Assure plots get removed when requested """ |
---|
[db5cd8d] | 230 | WarningTestNotImplemented() |
---|
[b46f285] | 231 | |
---|
| 232 | def testRemovePlot(self): |
---|
| 233 | """ Test plot removal """ |
---|
[db5cd8d] | 234 | WarningTestNotImplemented() |
---|
[b46f285] | 235 | |
---|
| 236 | def testOnToggleHideError(self): |
---|
| 237 | """ Test the error bar toggle on plots """ |
---|
[db5cd8d] | 238 | WarningTestNotImplemented() |
---|
[b46f285] | 239 | |
---|
| 240 | def testOnFitDisplay(self): |
---|
| 241 | """ Test the fit line display on the chart """ |
---|
[db5cd8d] | 242 | WarningTestNotImplemented() |
---|
[87cc73a] | 243 | |
---|
| 244 | def testReplacePlot(self): |
---|
| 245 | """ Test the plot refresh functionality """ |
---|
[db5cd8d] | 246 | WarningTestNotImplemented() |
---|
[87cc73a] | 247 | |
---|
| 248 | def testReplacePlot(self): |
---|
| 249 | """ Test the plot refresh functionality """ |
---|
[db5cd8d] | 250 | WarningTestNotImplemented(sys._getframe().f_code.co_name) |
---|
[87cc73a] | 251 | |
---|
| 252 | def testOnModifyPlot(self): |
---|
| 253 | """ Test the functionality for changing plot properties """ |
---|
[db5cd8d] | 254 | WarningTestNotImplemented() |
---|
[b46f285] | 255 | |
---|
[6d05e1d] | 256 | if __name__ == "__main__": |
---|
| 257 | unittest.main() |
---|