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

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

Converted unit tests

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