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

ESS_GUIESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalc
Last change on this file since 863ebca was 863ebca, checked in by Piotr Rozyczko <piotrrozyczko@…>, 6 years ago

Introduced navigation bar toggle in context menu for all types of
charts. SASVIEW-890

  • Property mode set to 100644
File size: 6.8 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
[863ebca]86    def notestOnImagePrint(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()
[863ebca]126        self.assertEqual(len(actions), 6)
[c4e5400]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
[863ebca]149        # Trigger toggle navigation bar and make sure the method is called
150        self.assertEqual(actions[4].text(), "Toggle Navigation Menu")
151        isShown = self.plotter.toolbar.isVisible()
152        self.assertTrue(isShow)
153        actions[4].trigger()
154        isShown = self.plotter.toolbar.isVisible()
155        self.assertFalse(isShow)
156        actions[4].trigger()
157        isShown = self.plotter.toolbar.isVisible()
158        self.assertTrue(isShow)
159
160
[27313b7]161    def testOnWindowsTitle(self):
[9290b1a]162        """ Test changing the plot title"""
[27313b7]163        # Mock the modal dialog's response
[53c771e]164        QtWidgets.QDialog.exec_ = MagicMock(return_value=QtWidgets.QDialog.Accepted)
[27313b7]165        self.plotter.show()
166        # Assure the original title is none
167        self.assertEqual(self.plotter.windowTitle(), "")
168        self.plotter.manager.communicator = MagicMock()
169
170        WindowTitle.title = MagicMock(return_value="I am a new title")
171        # Change the title
172        self.plotter.onWindowsTitle()
173
174        self.assertEqual(self.plotter.windowTitle(), "I am a new title")
[3b7b218]175
[9290b1a]176    def testOnMplMouseDown(self):
177        """ Test what happens on mouse click down in chart """
178        pass
179
180    def testOnMplMouseUp(self):
181        """ Test what happens on mouse release in chart """
182        pass
183
184    def testOnMplMouseMotion(self):
185        """ Test what happens on mouse move in chart """
186        pass
187
188    def testOnMplPick(self):
189        """ Test what happens on mouse pick in chart """
190        pass
191
192    def testOnMplWheel(self):
193        """ Test what happens on mouse pick in chart """
194        pass
195
[3b7b218]196if __name__ == "__main__":
197    unittest.main()
Note: See TracBrowser for help on using the repository browser.