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

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

Refactored to allow running with run.py.
Minor fixes to plotting.

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