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

Last change on this file since a7067ef2 was 8222f171, checked in by Piotr Rozyczko <rozyczko@…>, 7 years ago

SASVIEW-625: code review fixes. Corrected handling for cancelling the file open dialog, updated test cases.

  • Property mode set to 100644
File size: 10.9 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# set up import paths
14import path_prepare
15
16# Local
17from sas.qtgui.MainWindow.DataExplorer import DataExplorerWindow
18from sas.qtgui.MainWindow.AboutBox import AboutBox
19from sas.qtgui.MainWindow.WelcomePanel import WelcomePanel
20from sas.qtgui.Utilities.IPythonWidget import IPythonWidget
21
22from sas.qtgui.MainWindow.GuiManager import Acknowledgements, GuiManager
23from sas.qtgui.MainWindow.MainWindow import MainSasViewWindow
24from sas.qtgui.UnitTesting.TestUtils import QtSignalSpy
25
26if not QApplication.instance():
27    app = QApplication(sys.argv)
28
29class GuiManagerTest(unittest.TestCase):
30    '''Test the Main Window functionality'''
31    def setUp(self):
32        '''Create the tested object'''
33        class MainWindow(MainSasViewWindow):
34            # Main window of the application
35            def __init__(self, reactor, parent=None):
36                super(MainWindow, self).__init__(parent)
37       
38                # define workspace for dialogs.
39                self.workspace = QWorkspace(self)
40                self.setCentralWidget(self.workspace)
41
42        self.manager = GuiManager(MainWindow(None))
43
44    def tearDown(self):
45        '''Destroy the GUI'''
46        self.manager = None
47
48    def testDefaults(self):
49        """
50        Test the object in its default state
51        """
52        self.assertIsInstance(self.manager.filesWidget, DataExplorerWindow)
53        self.assertIsInstance(self.manager.dockedFilesWidget, QDockWidget)
54        self.assertIsInstance(self.manager.dockedFilesWidget.widget(), DataExplorerWindow)
55        self.assertEqual(self.manager.dockedFilesWidget.features(), QDockWidget.NoDockWidgetFeatures)
56        self.assertEqual(self.manager._workspace.dockWidgetArea(self.manager.dockedFilesWidget), Qt.LeftDockWidgetArea)
57
58        self.assertIsInstance(self.manager.logDockWidget, QDockWidget)
59        self.assertIsInstance(self.manager.logDockWidget.widget(), QTextBrowser)
60        self.assertEqual(self.manager._workspace.dockWidgetArea(self.manager.logDockWidget), Qt.BottomDockWidgetArea)
61
62        self.assertIsInstance(self.manager.ackWidget, Acknowledgements)
63        self.assertIsInstance(self.manager.aboutWidget, AboutBox)
64        self.assertIsInstance(self.manager.welcomePanel, WelcomePanel)
65
66    def skip_testLogging(self):
67        """
68        Test logging of stdout, stderr and log messages
69        """
70        # See if the log window is empty
71        self.assertEqual(self.manager.logDockWidget.widget().toPlainText(), "")
72
73        # Now, send some message to stdout.
74        # We are in the MainWindow scope, so simple 'print' will work
75        message = "from stdout"
76        print message
77        self.assertIn(message, self.manager.logDockWidget.widget().toPlainText())
78
79        # Send some message to stderr
80        message = "from stderr"
81        sys.stderr.write(message)
82        self.assertIn(message, self.manager.logDockWidget.widget().toPlainText())
83
84        # And finally, send a log message
85        import logging
86        message = "from logging"
87        message_logged = "ERROR: " + message
88        logging.error(message)
89        self.assertIn(message_logged, self.manager.logDockWidget.widget().toPlainText())
90
91    def testConsole(self):
92        """
93        Test the docked QtConsole
94        """
95        # Invoke the console action
96        self.manager.actionPython_Shell_Editor()
97
98        # Test the widegt properties
99        self.assertIsInstance(self.manager.ipDockWidget, QDockWidget)
100        self.assertIsInstance(self.manager.ipDockWidget.widget(), IPythonWidget)
101        self.assertEqual(self.manager._workspace.dockWidgetArea(self.manager.ipDockWidget), Qt.RightDockWidgetArea)
102
103    def testUpdatePerspective(self):
104        """
105        """
106        pass
107
108    def testUpdateStatusBar(self):
109        """
110        """
111        pass
112
113    def testSetData(self):
114        """
115        """
116        pass
117
118    def testQuitApplication(self):
119        """
120        Test that the custom exit method is called on shutdown
121        """
122        self.manager._workspace.show()
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'     : '4.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 skip_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.