source: sasview/src/sas/qtgui/UnitTesting/PlotterTest.py @ 570a58f9

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalc
Last change on this file since 570a58f9 was aadf0af1, checked in by Piotr Rozyczko <rozyczko@…>, 7 years ago

Plot specific part of the context menu SASVIEW-427

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