source: sasview/src/sas/qtgui/UnitTesting/GuiManagerTest.py @ 2d647cf

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 2d647cf was 0fc37fea, checked in by Piotr Rozyczko <piotr.rozyczko@…>, 8 years ago

Added python wrapper for unit testing

  • Property mode set to 100755
File size: 10.7 KB
RevLine 
[488c49d]1import sys
[9e426c1]2import subprocess
[488c49d]3import unittest
[9e426c1]4import webbrowser
5import logging
[488c49d]6
7from PyQt4.QtGui import *
8from PyQt4.QtTest import QTest
9from PyQt4.QtCore import *
[9e426c1]10from PyQt4.QtWebKit import *
[488c49d]11from mock import MagicMock
12
13# Local
[e540cd2]14from DataExplorer import DataExplorerWindow
15from AboutBox import AboutBox
16from WelcomePanel import WelcomePanel
[ff49d4d]17from IPythonWidget import IPythonWidget
[e540cd2]18
[0fc37fea]19from GuiManager import Acknowledgements, GuiManager
20from MainWindow import MainSasViewWindow
[9e426c1]21from UnitTesting.TestUtils import QtSignalSpy
[488c49d]22
[5032ea68]23app = QApplication(sys.argv)
[488c49d]24
25class GuiManagerTest(unittest.TestCase):
[5032ea68]26    '''Test the Main Window functionality'''
[488c49d]27    def setUp(self):
28        '''Create the tested object'''
[0fc37fea]29        class MainWindow(MainSasViewWindow):
[5032ea68]30            # Main window of the application
31            def __init__(self, reactor, parent=None):
[0fc37fea]32                super(MainWindow, self).__init__(parent)
[5032ea68]33       
34                # define workspace for dialogs.
35                self.workspace = QWorkspace(self)
36                self.setCentralWidget(self.workspace)
[488c49d]37
[0fc37fea]38        self.manager = GuiManager(MainWindow(None), None)
[488c49d]39
40    def tearDown(self):
41        '''Destroy the GUI'''
42        self.manager = None
43
44    def testDefaults(self):
[e540cd2]45        """
46        Test the object in its default state
47        """
48        self.assertIsInstance(self.manager.filesWidget, DataExplorerWindow)
49        self.assertIsInstance(self.manager.dockedFilesWidget, QDockWidget)
[0cd8612]50        self.assertIsInstance(self.manager.dockedFilesWidget.widget(), DataExplorerWindow)
[e540cd2]51        self.assertEqual(self.manager.dockedFilesWidget.features(), QDockWidget.NoDockWidgetFeatures)
52        self.assertEqual(self.manager._workspace.dockWidgetArea(self.manager.dockedFilesWidget), Qt.LeftDockWidgetArea)
[0cd8612]53
54        self.assertIsInstance(self.manager.logDockWidget, QDockWidget)
55        self.assertIsInstance(self.manager.logDockWidget.widget(), QTextBrowser)
56        self.assertEqual(self.manager._workspace.dockWidgetArea(self.manager.logDockWidget), Qt.BottomDockWidgetArea)
57
[e540cd2]58        self.assertIsInstance(self.manager.ackWidget, Acknowledgements)
59        self.assertIsInstance(self.manager.aboutWidget, AboutBox)
60        self.assertIsInstance(self.manager.welcomePanel, WelcomePanel)
61
[0cd8612]62    def testLogging(self):
63        """
64        Test logging of stdout, stderr and log messages
65        """
66        # See if the log window is empty
67        self.assertEqual(self.manager.logDockWidget.widget().toPlainText(), "")
68
69        # Now, send some message to stdout.
70        # We are in the MainWindow scope, so simple 'print' will work
71        message = "from stdout"
72        print message
73        self.assertIn(message, self.manager.logDockWidget.widget().toPlainText())
74
75        # Send some message to stderr
76        message = "from stderr"
77        sys.stderr.write(message)
78        self.assertIn(message, self.manager.logDockWidget.widget().toPlainText())
79
80        # And finally, send a log message
81        import logging
82        message = "from logging"
83        message_logged = "ERROR: " + message
84        logging.error(message)
85        self.assertIn(message_logged, self.manager.logDockWidget.widget().toPlainText())
86
[ff49d4d]87    def testConsole(self):
88        """
89        Test the docked QtConsole
90        """
91        # Invoke the console action
92        self.manager.actionPython_Shell_Editor()
93
94        # Test the widegt properties
95        self.assertIsInstance(self.manager.ipDockWidget, QDockWidget)
96        self.assertIsInstance(self.manager.ipDockWidget.widget(), IPythonWidget)
97        self.assertEqual(self.manager._workspace.dockWidgetArea(self.manager.ipDockWidget), Qt.RightDockWidgetArea)
98
[488c49d]99    def testUpdatePerspective(self):
100        """
101        """
102        pass
103
104    def testUpdateStatusBar(self):
105        """
106        """
107        pass
108
109    def testSetData(self):
110        """
111        """
112        pass
113
114    def testSetData(self):
115        """
116        """
117        pass
118
[9e426c1]119    def testQuitApplication(self):
120        """
121        Test that the custom exit method is called on shutdown
122        """
123        # Must mask sys.exit, otherwise the whole testing process stops.
124        sys.exit = MagicMock()
125
126        # Say No to the close dialog
127        QMessageBox.question = MagicMock(return_value=QMessageBox.No)
128
129        # Open, then close the manager
130        self.manager.quitApplication()
131
132        # See that the MessageBox method got called
133        self.assertTrue(QMessageBox.question.called)
134
135        # Say Yes to the close dialog
136        QMessageBox.question = MagicMock(return_value=QMessageBox.Yes)
137
138        # Open, then close the manager
139        self.manager.quitApplication()
140
141        # See that the MessageBox method got called
142        self.assertTrue(QMessageBox.question.called)
143
144    def testCheckUpdate(self):
145        """
146        Tests the SasView website version polling
147        """
148        self.manager.processVersion = MagicMock()
149        version = {'update_url'  : 'http://www.sasview.org/sasview.latestversion', 
150                   'version'     : '3.1.2',
151                   'download_url': 'https://github.com/SasView/sasview/releases'}
152        self.manager.checkUpdate()
153
154        self.manager.processVersion.assert_called_with(version)
155
156        pass
157
158    def testProcessVersion(self):
159        """
160        Tests the version checker logic
161        """
162        # 1. version = 0.0.0
163        version_info = {u'version' : u'0.0.0'}
164        spy_status_update = QtSignalSpy(self.manager, self.manager.communicate.statusBarUpdateSignal)
165
166        self.manager.processVersion(version_info)
167
168        self.assertEqual(spy_status_update.count(), 1)
169        message = 'Could not connect to the application server. Please try again later.'
170        self.assertIn(message, str(spy_status_update.signal(index=0)))
171
172        # 2. version < LocalConfig.__version__
173        version_info = {u'version' : u'0.0.1'}
174        spy_status_update = QtSignalSpy(self.manager, self.manager.communicate.statusBarUpdateSignal)
175
176        self.manager.processVersion(version_info)
177
178        self.assertEqual(spy_status_update.count(), 1)
179        message = 'You have the latest version of SasView'
180        self.assertIn(message, str(spy_status_update.signal(index=0)))
181
182        # 3. version > LocalConfig.__version__
183        version_info = {u'version' : u'999.0.0'}
184        spy_status_update = QtSignalSpy(self.manager, self.manager.communicate.statusBarUpdateSignal)
185        webbrowser.open = MagicMock()
186
187        self.manager.processVersion(version_info)
188
189        self.assertEqual(spy_status_update.count(), 1)
190        message = 'Version 999.0.0 is available!'
191        self.assertIn(message, str(spy_status_update.signal(index=0)))
192
193        webbrowser.open.assert_called_with("https://github.com/SasView/sasview/releases")
194
[e540cd2]195        # 4. couldn't load version
[9e426c1]196        version_info = {}
197        logging.error = MagicMock()
198        spy_status_update = QtSignalSpy(self.manager, self.manager.communicate.statusBarUpdateSignal)
199
200        self.manager.processVersion(version_info)
201
202        # Retrieve and compare arguments of the mocked call
203        message = "guiframe: could not get latest application version number"
204        args, _ = logging.error.call_args
205        self.assertIn(message, args[0])
206
207        # Check the signal message
208        message = 'Could not connect to the application server.'
209        self.assertIn(message, str(spy_status_update.signal(index=0)))
210
[488c49d]211    def testActions(self):
212        """
213        """
214        pass
215
[e540cd2]216    #### FILE ####
[5032ea68]217    def testActionLoadData(self):
218        """
219        Menu File/Load Data File(s)
220        """
221        # Mock the system file open method
[9e426c1]222        QFileDialog.getOpenFileNames = MagicMock(return_value=None)
[5032ea68]223
224        # invoke the action
[9e426c1]225        self.manager.actionLoadData()
[5032ea68]226
227        # Test the getOpenFileName() dialog called once
[9e426c1]228        self.assertTrue(QFileDialog.getOpenFileNames.called)
[e540cd2]229
230    def testActionLoadDataFolder(self):
231        """
232        Menu File/Load Data Folder
233        """
234        # Mock the system file open method
235        QFileDialog.getExistingDirectory = MagicMock(return_value=None)
236
237        # invoke the action
238        self.manager.actionLoad_Data_Folder()
239
240        # Test the getOpenFileName() dialog called once
241        self.assertTrue(QFileDialog.getExistingDirectory.called)
242
243    #### VIEW ####
244    def testActionHideToolbar(self):
245        """
246        Menu View/Hide Toolbar
247        """
248        # Need to display the main window to initialize the toolbar.
249        self.manager._workspace.show()
250
251        # Check the initial state
252        self.assertTrue(self.manager._workspace.toolBar.isVisible())
253        self.assertEqual('Hide Toolbar', self.manager._workspace.actionHide_Toolbar.text())
254
255        # Invoke action
256        self.manager.actionHide_Toolbar()
257
258        # Assure changes propagated correctly
259        self.assertFalse(self.manager._workspace.toolBar.isVisible())
260        self.assertEqual('Show Toolbar', self.manager._workspace.actionHide_Toolbar.text())
261
262        # Revert
263        self.manager.actionHide_Toolbar()
264
265        # Assure the original values are back
266        self.assertTrue(self.manager._workspace.toolBar.isVisible())
267        self.assertEqual('Hide Toolbar', self.manager._workspace.actionHide_Toolbar.text())
268
269
270    #### HELP ####
[9e426c1]271    def testActionDocumentation(self):
272        """
273        Menu Help/Documentation
274        """
275        #Mock the QWebView method
276        QWebView.show = MagicMock()
277
278        # Assure the filename is correct
279        self.assertIn("index.html", self.manager._helpLocation)
280
281        # Invoke the action
282        self.manager.actionDocumentation()
283
284        # Check if show() got called
285        self.assertTrue(QWebView.show.called)
286
287    def testActionTutorial(self):
288        """
289        Menu Help/Tutorial
290        """
291        # Mock subprocess.Popen
292        subprocess.Popen = MagicMock()
293
294        tested_location = self.manager._tutorialLocation
295
296        # Assure the filename is correct
297        self.assertIn("Tutorial.pdf", tested_location)
298
299        # Invoke the action
300        self.manager.actionTutorial()
301
302        # Check if popen() got called
303        self.assertTrue(subprocess.Popen.called)
304
305        #Check the popen() call arguments
306        subprocess.Popen.assert_called_with([tested_location], shell=True)
307
308    def testActionAcknowledge(self):
309        """
310        Menu Help/Acknowledge
311        """
[f82ab8c]312        self.manager.actionAcknowledge()
313
314        # Check if the window is actually opened.
315        self.assertTrue(self.manager.ackWidget.isVisible())
316        self.assertIn("developers@sasview.org", self.manager.ackWidget.label.text())
[9e426c1]317
318    def testActionCheck_for_update(self):
319        """
320        Menu Help/Check for update
321        """
322        # Just make sure checkUpdate is called.
323        self.manager.checkUpdate = MagicMock()
324
325        self.manager.actionCheck_for_update()
[5032ea68]326
[9e426c1]327        self.assertTrue(self.manager.checkUpdate.called)
328             
[488c49d]329       
330if __name__ == "__main__":
331    unittest.main()
332
Note: See TracBrowser for help on using the repository browser.