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

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

Fixed printer and clipboard devices

  • Property mode set to 100644
File size: 6.9 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.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 '''
[c4e5400]67        with self.assertRaises(NotImplementedError):
[aadf0af1]68            self.plotter.createContextMenu()
[3b7b218]69
70    def testClean(self):
71        ''' test the graph cleanup '''
[c4e5400]72        self.plotter.figure.delaxes = MagicMock()
73        self.plotter.clean()
74        self.assertTrue(self.plotter.figure.delaxes.called)
[3b7b218]75
76    def testPlot(self):
77        ''' test the pure virtual method '''
78        with self.assertRaises(NotImplementedError):
79            self.plotter.plot()
80
[c4e5400]81    def notestOnCloseEvent(self):
[3b7b218]82        ''' test the plotter close behaviour '''
[c4e5400]83        PlotHelper.deletePlot = MagicMock()
84        self.plotter.closeEvent(None)
85        self.assertTrue(PlotHelper.deletePlot.called)
[3b7b218]86
87    def testOnImageSave(self):
88        ''' test the workspace save '''
[c4e5400]89        self.plotter.toolbar.save_figure = MagicMock()
90        self.plotter.onImageSave()
91        self.assertTrue(self.plotter.toolbar.save_figure.called)
[3b7b218]92
[dd150ef]93    def testOnImagePrint(self):
[3b7b218]94        ''' test the workspace print '''
[c4e5400]95        QtGui.QPainter.end = MagicMock()
[53c771e]96        QtWidgets.QLabel.render = MagicMock()
[c4e5400]97
98        # First, let's cancel printing
[53c771e]99        QtPrintSupport.QPrintDialog.exec_ = MagicMock(return_value=QtWidgets.QDialog.Rejected)
[c4e5400]100        self.plotter.onImagePrint()
101        self.assertFalse(QtGui.QPainter.end.called)
[53c771e]102        self.assertFalse(QtWidgets.QLabel.render.called)
[c4e5400]103
104        # Let's print now
[53c771e]105        QtPrintSupport.QPrintDialog.exec_ = MagicMock(return_value=QtWidgets.QDialog.Accepted)
[c4e5400]106        self.plotter.onImagePrint()
107        self.assertTrue(QtGui.QPainter.end.called)
[53c771e]108        self.assertTrue(QtWidgets.QLabel.render.called)
[c4e5400]109
[dd150ef]110    def testOnClipboardCopy(self):
[c4e5400]111        ''' test the workspace screen copy '''
112        QtGui.QClipboard.setPixmap = MagicMock()
113        self.plotter.onClipboardCopy()
114        self.assertTrue(QtGui.QClipboard.setPixmap.called)
[3b7b218]115
116    def testOnGridToggle(self):
117        ''' test toggling the grid lines '''
[c4e5400]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")
[53c771e]143        QtPrintSupport.QPrintDialog.exec_ = MagicMock(return_value=QtWidgets.QDialog.Rejected)
[c4e5400]144        actions[1].trigger()
[53c771e]145        self.assertTrue(QtPrintSupport.QPrintDialog.exec_.called)
[c4e5400]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
[b2a5042]151        if not self.isWindows:
152            return
[c4e5400]153        self.clipboard_called = False
154        def done():
155            self.clipboard_called = True
[53c771e]156        QtCore.QObject.connect(QtWidgets.qApp.clipboard(), QtCore.SIGNAL("dataChanged()"), done)
[c4e5400]157        actions[2].trigger()
[53c771e]158        QtWidgets.qApp.processEvents()
[c4e5400]159        # Make sure clipboard got updated.
160        self.assertTrue(self.clipboard_called)
[3b7b218]161
[27313b7]162    def testOnWindowsTitle(self):
[9290b1a]163        """ Test changing the plot title"""
[27313b7]164        # Mock the modal dialog's response
[53c771e]165        QtWidgets.QDialog.exec_ = MagicMock(return_value=QtWidgets.QDialog.Accepted)
[27313b7]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")
[3b7b218]176
[9290b1a]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
[3b7b218]197if __name__ == "__main__":
198    unittest.main()
Note: See TracBrowser for help on using the repository browser.