source: sasview/src/sas/qtgui/UnitTesting/PlotterBaseTest.py @ 9f25bce

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

Plot specific part of the context menu SASVIEW-427

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