source: sasview/src/sas/qtgui/UnitTesting/Plotter2DTest.py @ 092a3d9

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

Color Map control for 2D charts. Initial commit - SASVIEW-391

  • Property mode set to 100755
File size: 6.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 mpl_toolkits.mplot3d import Axes3D
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 WarningTestNotImplemented
16
17# Tested module
18import sas.qtgui.Plotter2D as Plotter2D
19
20app = QtGui.QApplication(sys.argv)
21
22class 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()
58        FigureCanvas.draw_idle = MagicMock()
59
60        self.plotter.plot()
61
62        self.assertTrue(FigureCanvas.draw_idle.called)
63
64    def testCalculateDepth(self):
65        ''' Test the depth calculator '''
66        WarningTestNotImplemented()
67
68    def testOnColorMap(self):
69        ''' Respond to the color map event '''
70        WarningTestNotImplemented()
71
72    def testOnToggleScale(self):
73        """ Respond to the event by replotting """
74        self.plotter.data = self.data
75        self.plotter.show()
76        FigureCanvas.draw_idle = MagicMock()
77
78        self.plotter.onToggleScale(None)
79
80        self.assertTrue(FigureCanvas.draw_idle.called)
81
82    def testContextMenuQuickPlot(self):
83        """ Test the right click menu """
84        self.plotter.data = self.data
85        self.plotter.createContextMenuQuick()
86        actions = self.plotter.contextMenu.actions()
87        self.assertEqual(len(actions), 7)
88
89        # Trigger Save Image and make sure the method is called
90        self.assertEqual(actions[0].text(), "Save Image")
91        self.plotter.toolbar.save_figure = MagicMock()
92        actions[0].trigger()
93        self.assertTrue(self.plotter.toolbar.save_figure.called)
94
95        # Trigger Print Image and make sure the method is called
96        self.assertEqual(actions[1].text(), "Print Image")
97        QtGui.QPrintDialog.exec_ = MagicMock(return_value=QtGui.QDialog.Rejected)
98        actions[1].trigger()
99        self.assertTrue(QtGui.QPrintDialog.exec_.called)
100
101        # Trigger Copy to Clipboard and make sure the method is called
102        self.assertEqual(actions[2].text(), "Copy to Clipboard")
103
104        # Spy on cliboard's dataChanged() signal
105        self.clipboard_called = False
106        def done():
107            self.clipboard_called = True
108        QtCore.QObject.connect(app.clipboard(), QtCore.SIGNAL("dataChanged()"), done)
109        actions[2].trigger()
110        QtGui.qApp.processEvents()
111        # Make sure clipboard got updated.
112        #self.assertTrue(self.clipboard_called)
113
114        # Trigger Toggle Grid and make sure the method is called
115        self.assertEqual(actions[4].text(), "Toggle Grid On/Off")
116        self.plotter.ax.grid = MagicMock()
117        actions[4].trigger()
118        self.assertTrue(self.plotter.ax.grid.called)
119
120        # Trigger Change Scale and make sure the method is called
121        self.assertEqual(actions[6].text(), "Toggle Linear/Log Scale")
122        FigureCanvas.draw_idle = MagicMock()
123        actions[6].trigger()
124        self.assertTrue(FigureCanvas.draw_idle.called)
125
126    def testShowNoPlot(self):
127        """ Test the plot rendering and generation """
128
129        FigureCanvas.draw_idle = MagicMock()
130        FigureCanvas.draw = MagicMock()
131
132        # Test early return on no data
133        self.plotter.showPlot(data=None,
134                              qx_data=self.data.qx_data,
135                              qy_data=self.data.qy_data,
136                              xmin=self.data.xmin,
137                              xmax=self.data.xmax,
138                              ymin=self.data.ymin, ymax=self.data.ymax,
139                              cmap=None, zmin=None,
140                              zmax=None)
141
142        self.assertFalse(FigureCanvas.draw_idle.called)
143        self.assertFalse(FigureCanvas.draw.called)
144
145    def testShow3DPlot(self):
146        """ Test the 3Dplot rendering and generation """
147        # Test 3D printout
148        FigureCanvas.draw = MagicMock()
149        Axes3D.plot_surface = MagicMock()
150        self.plotter.figure.colorbar = MagicMock()
151
152        self.plotter.dimension = 3
153        self.plotter.data = self.data
154        self.plotter.showPlot(data=self.plotter.data.data,
155                              qx_data=self.data.qx_data,
156                              qy_data=self.data.qy_data,
157                              xmin=self.data.xmin,
158                              xmax=self.data.xmax,
159                              ymin=self.data.ymin, ymax=self.data.ymax,
160                              cmap=None, zmin=None,
161                              zmax=None)
162        self.assertTrue(Axes3D.plot_surface.called)
163        self.assertTrue(FigureCanvas.draw.called)
164
165    def testShow2DPlot(self):
166        """ Test the 2Dplot rendering and generation """
167        # Test 2D printout
168        FigureCanvas.draw_idle = MagicMock()
169        self.plotter.figure.colorbar = MagicMock()
170
171        self.plotter.dimension = 2
172        self.plotter.data = self.data
173        self.plotter.showPlot(data=self.plotter.data.data,
174                              qx_data=self.data.qx_data,
175                              qy_data=self.data.qy_data,
176                              xmin=self.data.xmin,
177                              xmax=self.data.xmax,
178                              ymin=self.data.ymin, ymax=self.data.ymax,
179                              cmap=None, zmin=None,
180                              zmax=None)
181        self.assertTrue(FigureCanvas.draw_idle.called)
182
183
184if __name__ == "__main__":
185    unittest.main()
Note: See TracBrowser for help on using the repository browser.