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

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

output console + logging

  • Property mode set to 100755
File size: 10.3 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 UI.AcknowledgementsUI import Acknowledgements
16from AboutBox import AboutBox
17from WelcomePanel import WelcomePanel
18
[488c49d]19from GuiManager import GuiManager
[5032ea68]20from UI.MainWindowUI import MainWindow
[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'''
[5032ea68]29        class MainSasViewWindow(MainWindow):
30            # Main window of the application
31            def __init__(self, reactor, parent=None):
32                super(MainSasViewWindow, self).__init__(parent)
33       
34                # define workspace for dialogs.
35                self.workspace = QWorkspace(self)
36                self.setCentralWidget(self.workspace)
[488c49d]37
[5032ea68]38        self.manager = GuiManager(MainSasViewWindow(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
[488c49d]87    def testUpdatePerspective(self):
88        """
89        """
90        pass
91
92    def testUpdateStatusBar(self):
93        """
94        """
95        pass
96
97    def testSetData(self):
98        """
99        """
100        pass
101
102    def testSetData(self):
103        """
104        """
105        pass
106
[9e426c1]107    def testQuitApplication(self):
108        """
109        Test that the custom exit method is called on shutdown
110        """
111        # Must mask sys.exit, otherwise the whole testing process stops.
112        sys.exit = MagicMock()
113
114        # Say No to the close dialog
115        QMessageBox.question = MagicMock(return_value=QMessageBox.No)
116
117        # Open, then close the manager
118        self.manager.quitApplication()
119
120        # See that the MessageBox method got called
121        self.assertTrue(QMessageBox.question.called)
122
123        # Say Yes to the close dialog
124        QMessageBox.question = MagicMock(return_value=QMessageBox.Yes)
125
126        # Open, then close the manager
127        self.manager.quitApplication()
128
129        # See that the MessageBox method got called
130        self.assertTrue(QMessageBox.question.called)
131
132    def testCheckUpdate(self):
133        """
134        Tests the SasView website version polling
135        """
136        self.manager.processVersion = MagicMock()
137        version = {'update_url'  : 'http://www.sasview.org/sasview.latestversion', 
138                   'version'     : '3.1.2',
139                   'download_url': 'https://github.com/SasView/sasview/releases'}
140        self.manager.checkUpdate()
141
142        self.manager.processVersion.assert_called_with(version)
143
144        pass
145
146    def testProcessVersion(self):
147        """
148        Tests the version checker logic
149        """
150        # 1. version = 0.0.0
151        version_info = {u'version' : u'0.0.0'}
152        spy_status_update = QtSignalSpy(self.manager, self.manager.communicate.statusBarUpdateSignal)
153
154        self.manager.processVersion(version_info)
155
156        self.assertEqual(spy_status_update.count(), 1)
157        message = 'Could not connect to the application server. Please try again later.'
158        self.assertIn(message, str(spy_status_update.signal(index=0)))
159
160        # 2. version < LocalConfig.__version__
161        version_info = {u'version' : u'0.0.1'}
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 = 'You have the latest version of SasView'
168        self.assertIn(message, str(spy_status_update.signal(index=0)))
169
170        # 3. version > LocalConfig.__version__
171        version_info = {u'version' : u'999.0.0'}
172        spy_status_update = QtSignalSpy(self.manager, self.manager.communicate.statusBarUpdateSignal)
173        webbrowser.open = MagicMock()
174
175        self.manager.processVersion(version_info)
176
177        self.assertEqual(spy_status_update.count(), 1)
178        message = 'Version 999.0.0 is available!'
179        self.assertIn(message, str(spy_status_update.signal(index=0)))
180
181        webbrowser.open.assert_called_with("https://github.com/SasView/sasview/releases")
182
[e540cd2]183        # 4. couldn't load version
[9e426c1]184        version_info = {}
185        logging.error = MagicMock()
186        spy_status_update = QtSignalSpy(self.manager, self.manager.communicate.statusBarUpdateSignal)
187
188        self.manager.processVersion(version_info)
189
190        # Retrieve and compare arguments of the mocked call
191        message = "guiframe: could not get latest application version number"
192        args, _ = logging.error.call_args
193        self.assertIn(message, args[0])
194
195        # Check the signal message
196        message = 'Could not connect to the application server.'
197        self.assertIn(message, str(spy_status_update.signal(index=0)))
198
[488c49d]199    def testActions(self):
200        """
201        """
202        pass
203
[e540cd2]204    #### FILE ####
[5032ea68]205    def testActionLoadData(self):
206        """
207        Menu File/Load Data File(s)
208        """
209        # Mock the system file open method
[9e426c1]210        QFileDialog.getOpenFileNames = MagicMock(return_value=None)
[5032ea68]211
212        # invoke the action
[9e426c1]213        self.manager.actionLoadData()
[5032ea68]214
215        # Test the getOpenFileName() dialog called once
[9e426c1]216        self.assertTrue(QFileDialog.getOpenFileNames.called)
[e540cd2]217
218    def testActionLoadDataFolder(self):
219        """
220        Menu File/Load Data Folder
221        """
222        # Mock the system file open method
223        QFileDialog.getExistingDirectory = MagicMock(return_value=None)
224
225        # invoke the action
226        self.manager.actionLoad_Data_Folder()
227
228        # Test the getOpenFileName() dialog called once
229        self.assertTrue(QFileDialog.getExistingDirectory.called)
230
231    #### VIEW ####
232    def testActionHideToolbar(self):
233        """
234        Menu View/Hide Toolbar
235        """
236        # Need to display the main window to initialize the toolbar.
237        self.manager._workspace.show()
238
239        # Check the initial state
240        self.assertTrue(self.manager._workspace.toolBar.isVisible())
241        self.assertEqual('Hide Toolbar', self.manager._workspace.actionHide_Toolbar.text())
242
243        # Invoke action
244        self.manager.actionHide_Toolbar()
245
246        # Assure changes propagated correctly
247        self.assertFalse(self.manager._workspace.toolBar.isVisible())
248        self.assertEqual('Show Toolbar', self.manager._workspace.actionHide_Toolbar.text())
249
250        # Revert
251        self.manager.actionHide_Toolbar()
252
253        # Assure the original values are back
254        self.assertTrue(self.manager._workspace.toolBar.isVisible())
255        self.assertEqual('Hide Toolbar', self.manager._workspace.actionHide_Toolbar.text())
256
257
258    #### HELP ####
[9e426c1]259    def testActionDocumentation(self):
260        """
261        Menu Help/Documentation
262        """
263        #Mock the QWebView method
264        QWebView.show = MagicMock()
265
266        # Assure the filename is correct
267        self.assertIn("index.html", self.manager._helpLocation)
268
269        # Invoke the action
270        self.manager.actionDocumentation()
271
272        # Check if show() got called
273        self.assertTrue(QWebView.show.called)
274
275    def testActionTutorial(self):
276        """
277        Menu Help/Tutorial
278        """
279        # Mock subprocess.Popen
280        subprocess.Popen = MagicMock()
281
282        tested_location = self.manager._tutorialLocation
283
284        # Assure the filename is correct
285        self.assertIn("Tutorial.pdf", tested_location)
286
287        # Invoke the action
288        self.manager.actionTutorial()
289
290        # Check if popen() got called
291        self.assertTrue(subprocess.Popen.called)
292
293        #Check the popen() call arguments
294        subprocess.Popen.assert_called_with([tested_location], shell=True)
295
296    def testActionAcknowledge(self):
297        """
298        Menu Help/Acknowledge
299        """
[f82ab8c]300        self.manager.actionAcknowledge()
301
302        # Check if the window is actually opened.
303        self.assertTrue(self.manager.ackWidget.isVisible())
304        self.assertIn("developers@sasview.org", self.manager.ackWidget.label.text())
[9e426c1]305
306    def testActionCheck_for_update(self):
307        """
308        Menu Help/Check for update
309        """
310        # Just make sure checkUpdate is called.
311        self.manager.checkUpdate = MagicMock()
312
313        self.manager.actionCheck_for_update()
[5032ea68]314
[9e426c1]315        self.assertTrue(self.manager.checkUpdate.called)
316             
[488c49d]317       
318if __name__ == "__main__":
319    unittest.main()
320
Note: See TracBrowser for help on using the repository browser.