source: sasview/src/sas/qtgui/UnitTesting/PlotterTest.py @ 257bd57

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 257bd57 was 257bd57, checked in by Piotr Rozyczko <rozyczko@…>, 6 years ago

unit tests for graph range and add text

  • Property mode set to 100644
File size: 9.6 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.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
48    def testPlotWithErrors(self):
49        """ Look at the plotting with error bars"""
50        self.plotter.data = self.data
51        self.plotter.show()
52        FigureCanvas.draw = MagicMock()
53
54        self.plotter.plot(hide_error=False)
55
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')
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.
98        #self.assertTrue(self.clipboard_called)
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"""
114        self.plotter.plot(self.data)
115
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})$")
126        self.assertEqual(self.plotter.ax.get_ylabel(), "$\\ln{()}()$")
127
128        self.plotter.xyTransform(xLabel="ln(x)", yLabel="y^(2)")
129        self.assertEqual(self.plotter.ax.get_xlabel(), "$\\ln{()}()$")
130        self.assertEqual(self.plotter.ax.get_ylabel(), "$^{2}(()^{2})$")
131
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)")
141        self.assertEqual(self.plotter.ax.get_ylabel(), "$1/\\sqrt{}(()^{-0.5})$")
142
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
158    def testAddText(self):
159        """ Checks the functionality of adding text to graph """
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)
184
185    def testOnRemoveText(self):
186        """ Cheks if annotations can be removed from the graph """
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, [])
209
210    def testOnSetGraphRange(self):
211        """ Cheks if the graph can be resized for range """
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)
225
226    def testOnResetGraphRange(self):
227        """ Cheks if the graph can be reset after resizing for range """
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)
249
250if __name__ == "__main__":
251    unittest.main()
Note: See TracBrowser for help on using the repository browser.