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