source: sasview/src/sas/qtgui/Plotting/UnitTesting/PlotterBaseTest.py @ dc5ef15

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

Removed qtgui dependency on sasgui and wx SASVIEW-590

  • Property mode set to 100644
File size: 6.7 KB
Line 
1import sys
2import unittest
3from mock import patch, MagicMock
4
5from PyQt4 import QtGui
6import matplotlib.pyplot as plt
7from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
8from matplotlib.backends.backend_qt4agg import NavigationToolbar2QT as NavigationToolbar
9
10####### TEMP
11import path_prepare
12#######
13
14from sas.qtgui.Plotting.ScaleProperties import ScaleProperties
15from sas.qtgui.Plotting.WindowTitle import WindowTitle
16from sas.qtgui.Utilities.GuiUtils import *
17import sas.qtgui.Plotting.PlotHelper as PlotHelper
18
19# Tested module
20import sas.qtgui.Plotting.PlotterBase as PlotterBase
21
22app = QtGui.QApplication(sys.argv)
23
24class PlotterBaseTest(unittest.TestCase):
25    '''Test the Plotter base class'''
26    def setUp(self):
27        '''create'''
28        class dummy_manager(object):
29            def communicator(self):
30                return Communicate()
31            def perspective(self):
32                return MyPerspective()
33
34        #PlotterBase.PlotterBase.contextMenuQuickPlot = MagicMock()
35        self.plotter = PlotterBase.PlotterBase(None, manager=dummy_manager(), quickplot=True)
36
37    def tearDown(self):
38        '''destroy'''
39        self.plotter = None
40        self.plotter_qp = None
41
42    def testDefaults(self):
43        """ default method variables values """
44        self.assertIsInstance(self.plotter, QtGui.QWidget)
45        self.assertIsInstance(self.plotter.canvas, FigureCanvas)
46        self.assertIsInstance(self.plotter.toolbar, NavigationToolbar)
47        self.assertIsInstance(self.plotter.properties, ScaleProperties)
48
49        self.assertEqual(self.plotter._data, [])
50        self.assertEqual(self.plotter._xscale, 'log')
51        self.assertEqual(self.plotter._yscale, 'log')
52        self.assertEqual(self.plotter.scale, 'linear')
53        self.assertFalse(self.plotter.grid_on)
54        self.assertEqual(self.plotter.x_label, 'log10(x)')
55        self.assertEqual(self.plotter.y_label, 'log10(y)')
56
57    def testData(self):
58        ''' Test the pure virtual method '''
59        with self.assertRaises(NotImplementedError):
60            self.plotter.data=[]
61
62    def testContextMenu(self):
63        ''' Test the default context menu '''
64        with self.assertRaises(NotImplementedError):
65            self.plotter.createContextMenu()
66
67    def testClean(self):
68        ''' test the graph cleanup '''
69        self.plotter.figure.delaxes = MagicMock()
70        self.plotter.clean()
71        self.assertTrue(self.plotter.figure.delaxes.called)
72
73    def testPlot(self):
74        ''' test the pure virtual method '''
75        with self.assertRaises(NotImplementedError):
76            self.plotter.plot()
77
78    def notestOnCloseEvent(self):
79        ''' test the plotter close behaviour '''
80        PlotHelper.deletePlot = MagicMock()
81        self.plotter.closeEvent(None)
82        self.assertTrue(PlotHelper.deletePlot.called)
83
84    def testOnImageSave(self):
85        ''' test the workspace save '''
86        self.plotter.toolbar.save_figure = MagicMock()
87        self.plotter.onImageSave()
88        self.assertTrue(self.plotter.toolbar.save_figure.called)
89
90    def testOnImagePrint(self):
91        ''' test the workspace print '''
92        QtGui.QPainter.end = MagicMock()
93        QtGui.QLabel.render = MagicMock()
94
95        # First, let's cancel printing
96        QtGui.QPrintDialog.exec_ = MagicMock(return_value=QtGui.QDialog.Rejected)
97        self.plotter.onImagePrint()
98        self.assertFalse(QtGui.QPainter.end.called)
99        self.assertFalse(QtGui.QLabel.render.called)
100
101        # Let's print now
102        QtGui.QPrintDialog.exec_ = MagicMock(return_value=QtGui.QDialog.Accepted)
103        self.plotter.onImagePrint()
104        self.assertTrue(QtGui.QPainter.end.called)
105        self.assertTrue(QtGui.QLabel.render.called)
106
107    def testOnClipboardCopy(self):
108        ''' test the workspace screen copy '''
109        QtGui.QClipboard.setPixmap = MagicMock()
110        self.plotter.onClipboardCopy()
111        self.assertTrue(QtGui.QClipboard.setPixmap.called)
112
113    def testOnGridToggle(self):
114        ''' test toggling the grid lines '''
115        # Check the toggle
116        orig_toggle = self.plotter.grid_on
117       
118        FigureCanvas.draw_idle = MagicMock()
119        self.plotter.onGridToggle()
120
121        self.assertTrue(FigureCanvas.draw_idle.called)
122        self.assertTrue(self.plotter.grid_on != orig_toggle)
123
124    def testDefaultContextMenu(self):
125        """ Test the right click default menu """
126
127        self.plotter.defaultContextMenu()
128
129        actions = self.plotter.contextMenu.actions()
130        self.assertEqual(len(actions), 4)
131
132        # Trigger Save Image and make sure the method is called
133        self.assertEqual(actions[0].text(), "Save Image")
134        self.plotter.toolbar.save_figure = MagicMock()
135        actions[0].trigger()
136        self.assertTrue(self.plotter.toolbar.save_figure.called)
137
138        # Trigger Print Image and make sure the method is called
139        self.assertEqual(actions[1].text(), "Print Image")
140        QtGui.QPrintDialog.exec_ = MagicMock(return_value=QtGui.QDialog.Rejected)
141        actions[1].trigger()
142        self.assertTrue(QtGui.QPrintDialog.exec_.called)
143
144        # Trigger Copy to Clipboard and make sure the method is called
145        self.assertEqual(actions[2].text(), "Copy to Clipboard")
146
147        # Spy on cliboard's dataChanged() signal
148        self.clipboard_called = False
149        def done():
150            self.clipboard_called = True
151        QtCore.QObject.connect(app.clipboard(), QtCore.SIGNAL("dataChanged()"), done)
152        actions[2].trigger()
153        QtGui.qApp.processEvents()
154        # Make sure clipboard got updated.
155        self.assertTrue(self.clipboard_called)
156
157    def testOnWindowsTitle(self):
158        """ Test changing the plot title"""
159        # Mock the modal dialog's response
160        QtGui.QDialog.exec_ = MagicMock(return_value=QtGui.QDialog.Accepted)
161        self.plotter.show()
162        # Assure the original title is none
163        self.assertEqual(self.plotter.windowTitle(), "")
164        self.plotter.manager.communicator = MagicMock()
165
166        WindowTitle.title = MagicMock(return_value="I am a new title")
167        # Change the title
168        self.plotter.onWindowsTitle()
169
170        self.assertEqual(self.plotter.windowTitle(), "I am a new title")
171
172    def testOnMplMouseDown(self):
173        """ Test what happens on mouse click down in chart """
174        pass
175
176    def testOnMplMouseUp(self):
177        """ Test what happens on mouse release in chart """
178        pass
179
180    def testOnMplMouseMotion(self):
181        """ Test what happens on mouse move in chart """
182        pass
183
184    def testOnMplPick(self):
185        """ Test what happens on mouse pick in chart """
186        pass
187
188    def testOnMplWheel(self):
189        """ Test what happens on mouse pick in chart """
190        pass
191
192if __name__ == "__main__":
193    unittest.main()
Note: See TracBrowser for help on using the repository browser.