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

ESS_GUIESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalc
Last change on this file since 0d72cac was 144fe21, checked in by Piotr Rozyczko <rozyczko@…>, 6 years ago

new unit test runner script + test fixes. SASVIEW-970

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