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

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

Added unit test for the new docked widget

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