[488c49d] | 1 | import sys |
---|
[9e426c1] | 2 | import subprocess |
---|
[488c49d] | 3 | import unittest |
---|
[9e426c1] | 4 | import webbrowser |
---|
| 5 | import logging |
---|
[488c49d] | 6 | |
---|
| 7 | from PyQt4.QtGui import * |
---|
| 8 | from PyQt4.QtTest import QTest |
---|
| 9 | from PyQt4.QtCore import * |
---|
[9e426c1] | 10 | from PyQt4.QtWebKit import * |
---|
[488c49d] | 11 | from mock import MagicMock |
---|
| 12 | |
---|
| 13 | # Local |
---|
[e540cd2] | 14 | from DataExplorer import DataExplorerWindow |
---|
| 15 | from UI.AcknowledgementsUI import Acknowledgements |
---|
| 16 | from AboutBox import AboutBox |
---|
| 17 | from WelcomePanel import WelcomePanel |
---|
| 18 | |
---|
[488c49d] | 19 | from GuiManager import GuiManager |
---|
[5032ea68] | 20 | from UI.MainWindowUI import MainWindow |
---|
[9e426c1] | 21 | from UnitTesting.TestUtils import QtSignalSpy |
---|
[488c49d] | 22 | |
---|
[5032ea68] | 23 | app = QApplication(sys.argv) |
---|
[488c49d] | 24 | |
---|
| 25 | class 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) |
---|
| 50 | self.assertEqual(self.manager.dockedFilesWidget.features(), QDockWidget.NoDockWidgetFeatures) |
---|
| 51 | self.assertEqual(self.manager._workspace.dockWidgetArea(self.manager.dockedFilesWidget), Qt.LeftDockWidgetArea) |
---|
| 52 | self.assertIsInstance(self.manager.ackWidget, Acknowledgements) |
---|
| 53 | self.assertIsInstance(self.manager.aboutWidget, AboutBox) |
---|
| 54 | self.assertIsInstance(self.manager.welcomePanel, WelcomePanel) |
---|
| 55 | |
---|
[488c49d] | 56 | def testUpdatePerspective(self): |
---|
| 57 | """ |
---|
| 58 | """ |
---|
| 59 | pass |
---|
| 60 | |
---|
| 61 | def testUpdateStatusBar(self): |
---|
| 62 | """ |
---|
| 63 | """ |
---|
| 64 | pass |
---|
| 65 | |
---|
| 66 | def testSetData(self): |
---|
| 67 | """ |
---|
| 68 | """ |
---|
| 69 | pass |
---|
| 70 | |
---|
| 71 | def testSetData(self): |
---|
| 72 | """ |
---|
| 73 | """ |
---|
| 74 | pass |
---|
| 75 | |
---|
[9e426c1] | 76 | def testQuitApplication(self): |
---|
| 77 | """ |
---|
| 78 | Test that the custom exit method is called on shutdown |
---|
| 79 | """ |
---|
| 80 | # Must mask sys.exit, otherwise the whole testing process stops. |
---|
| 81 | sys.exit = MagicMock() |
---|
| 82 | |
---|
| 83 | # Say No to the close dialog |
---|
| 84 | QMessageBox.question = MagicMock(return_value=QMessageBox.No) |
---|
| 85 | |
---|
| 86 | # Open, then close the manager |
---|
| 87 | self.manager.quitApplication() |
---|
| 88 | |
---|
| 89 | # See that the MessageBox method got called |
---|
| 90 | self.assertTrue(QMessageBox.question.called) |
---|
| 91 | |
---|
| 92 | # Say Yes to the close dialog |
---|
| 93 | QMessageBox.question = MagicMock(return_value=QMessageBox.Yes) |
---|
| 94 | |
---|
| 95 | # Open, then close the manager |
---|
| 96 | self.manager.quitApplication() |
---|
| 97 | |
---|
| 98 | # See that the MessageBox method got called |
---|
| 99 | self.assertTrue(QMessageBox.question.called) |
---|
| 100 | |
---|
| 101 | def testCheckUpdate(self): |
---|
| 102 | """ |
---|
| 103 | Tests the SasView website version polling |
---|
| 104 | """ |
---|
| 105 | self.manager.processVersion = MagicMock() |
---|
| 106 | version = {'update_url' : 'http://www.sasview.org/sasview.latestversion', |
---|
| 107 | 'version' : '3.1.2', |
---|
| 108 | 'download_url': 'https://github.com/SasView/sasview/releases'} |
---|
| 109 | self.manager.checkUpdate() |
---|
| 110 | |
---|
| 111 | self.manager.processVersion.assert_called_with(version) |
---|
| 112 | |
---|
| 113 | pass |
---|
| 114 | |
---|
| 115 | def testProcessVersion(self): |
---|
| 116 | """ |
---|
| 117 | Tests the version checker logic |
---|
| 118 | """ |
---|
| 119 | # 1. version = 0.0.0 |
---|
| 120 | version_info = {u'version' : u'0.0.0'} |
---|
| 121 | spy_status_update = QtSignalSpy(self.manager, self.manager.communicate.statusBarUpdateSignal) |
---|
| 122 | |
---|
| 123 | self.manager.processVersion(version_info) |
---|
| 124 | |
---|
| 125 | self.assertEqual(spy_status_update.count(), 1) |
---|
| 126 | message = 'Could not connect to the application server. Please try again later.' |
---|
| 127 | self.assertIn(message, str(spy_status_update.signal(index=0))) |
---|
| 128 | |
---|
| 129 | # 2. version < LocalConfig.__version__ |
---|
| 130 | version_info = {u'version' : u'0.0.1'} |
---|
| 131 | spy_status_update = QtSignalSpy(self.manager, self.manager.communicate.statusBarUpdateSignal) |
---|
| 132 | |
---|
| 133 | self.manager.processVersion(version_info) |
---|
| 134 | |
---|
| 135 | self.assertEqual(spy_status_update.count(), 1) |
---|
| 136 | message = 'You have the latest version of SasView' |
---|
| 137 | self.assertIn(message, str(spy_status_update.signal(index=0))) |
---|
| 138 | |
---|
| 139 | # 3. version > LocalConfig.__version__ |
---|
| 140 | version_info = {u'version' : u'999.0.0'} |
---|
| 141 | spy_status_update = QtSignalSpy(self.manager, self.manager.communicate.statusBarUpdateSignal) |
---|
| 142 | webbrowser.open = MagicMock() |
---|
| 143 | |
---|
| 144 | self.manager.processVersion(version_info) |
---|
| 145 | |
---|
| 146 | self.assertEqual(spy_status_update.count(), 1) |
---|
| 147 | message = 'Version 999.0.0 is available!' |
---|
| 148 | self.assertIn(message, str(spy_status_update.signal(index=0))) |
---|
| 149 | |
---|
| 150 | webbrowser.open.assert_called_with("https://github.com/SasView/sasview/releases") |
---|
| 151 | |
---|
[e540cd2] | 152 | # 4. couldn't load version |
---|
[9e426c1] | 153 | version_info = {} |
---|
| 154 | logging.error = MagicMock() |
---|
| 155 | spy_status_update = QtSignalSpy(self.manager, self.manager.communicate.statusBarUpdateSignal) |
---|
| 156 | |
---|
| 157 | self.manager.processVersion(version_info) |
---|
| 158 | |
---|
| 159 | # Retrieve and compare arguments of the mocked call |
---|
| 160 | message = "guiframe: could not get latest application version number" |
---|
| 161 | args, _ = logging.error.call_args |
---|
| 162 | self.assertIn(message, args[0]) |
---|
| 163 | |
---|
| 164 | # Check the signal message |
---|
| 165 | message = 'Could not connect to the application server.' |
---|
| 166 | self.assertIn(message, str(spy_status_update.signal(index=0))) |
---|
| 167 | |
---|
[488c49d] | 168 | def testActions(self): |
---|
| 169 | """ |
---|
| 170 | """ |
---|
| 171 | pass |
---|
| 172 | |
---|
[e540cd2] | 173 | #### FILE #### |
---|
[5032ea68] | 174 | def testActionLoadData(self): |
---|
| 175 | """ |
---|
| 176 | Menu File/Load Data File(s) |
---|
| 177 | """ |
---|
| 178 | # Mock the system file open method |
---|
[9e426c1] | 179 | QFileDialog.getOpenFileNames = MagicMock(return_value=None) |
---|
[5032ea68] | 180 | |
---|
| 181 | # invoke the action |
---|
[9e426c1] | 182 | self.manager.actionLoadData() |
---|
[5032ea68] | 183 | |
---|
| 184 | # Test the getOpenFileName() dialog called once |
---|
[9e426c1] | 185 | self.assertTrue(QFileDialog.getOpenFileNames.called) |
---|
[e540cd2] | 186 | |
---|
| 187 | def testActionLoadDataFolder(self): |
---|
| 188 | """ |
---|
| 189 | Menu File/Load Data Folder |
---|
| 190 | """ |
---|
| 191 | # Mock the system file open method |
---|
| 192 | QFileDialog.getExistingDirectory = MagicMock(return_value=None) |
---|
| 193 | |
---|
| 194 | # invoke the action |
---|
| 195 | self.manager.actionLoad_Data_Folder() |
---|
| 196 | |
---|
| 197 | # Test the getOpenFileName() dialog called once |
---|
| 198 | self.assertTrue(QFileDialog.getExistingDirectory.called) |
---|
| 199 | |
---|
| 200 | #### VIEW #### |
---|
| 201 | def testActionHideToolbar(self): |
---|
| 202 | """ |
---|
| 203 | Menu View/Hide Toolbar |
---|
| 204 | """ |
---|
| 205 | # Need to display the main window to initialize the toolbar. |
---|
| 206 | self.manager._workspace.show() |
---|
| 207 | |
---|
| 208 | # Check the initial state |
---|
| 209 | self.assertTrue(self.manager._workspace.toolBar.isVisible()) |
---|
| 210 | self.assertEqual('Hide Toolbar', self.manager._workspace.actionHide_Toolbar.text()) |
---|
| 211 | |
---|
| 212 | # Invoke action |
---|
| 213 | self.manager.actionHide_Toolbar() |
---|
| 214 | |
---|
| 215 | # Assure changes propagated correctly |
---|
| 216 | self.assertFalse(self.manager._workspace.toolBar.isVisible()) |
---|
| 217 | self.assertEqual('Show Toolbar', self.manager._workspace.actionHide_Toolbar.text()) |
---|
| 218 | |
---|
| 219 | # Revert |
---|
| 220 | self.manager.actionHide_Toolbar() |
---|
| 221 | |
---|
| 222 | # Assure the original values are back |
---|
| 223 | self.assertTrue(self.manager._workspace.toolBar.isVisible()) |
---|
| 224 | self.assertEqual('Hide Toolbar', self.manager._workspace.actionHide_Toolbar.text()) |
---|
| 225 | |
---|
| 226 | |
---|
| 227 | #### HELP #### |
---|
[9e426c1] | 228 | def testActionDocumentation(self): |
---|
| 229 | """ |
---|
| 230 | Menu Help/Documentation |
---|
| 231 | """ |
---|
| 232 | #Mock the QWebView method |
---|
| 233 | QWebView.show = MagicMock() |
---|
| 234 | |
---|
| 235 | # Assure the filename is correct |
---|
| 236 | self.assertIn("index.html", self.manager._helpLocation) |
---|
| 237 | |
---|
| 238 | # Invoke the action |
---|
| 239 | self.manager.actionDocumentation() |
---|
| 240 | |
---|
| 241 | # Check if show() got called |
---|
| 242 | self.assertTrue(QWebView.show.called) |
---|
| 243 | |
---|
| 244 | def testActionTutorial(self): |
---|
| 245 | """ |
---|
| 246 | Menu Help/Tutorial |
---|
| 247 | """ |
---|
| 248 | # Mock subprocess.Popen |
---|
| 249 | subprocess.Popen = MagicMock() |
---|
| 250 | |
---|
| 251 | tested_location = self.manager._tutorialLocation |
---|
| 252 | |
---|
| 253 | # Assure the filename is correct |
---|
| 254 | self.assertIn("Tutorial.pdf", tested_location) |
---|
| 255 | |
---|
| 256 | # Invoke the action |
---|
| 257 | self.manager.actionTutorial() |
---|
| 258 | |
---|
| 259 | # Check if popen() got called |
---|
| 260 | self.assertTrue(subprocess.Popen.called) |
---|
| 261 | |
---|
| 262 | #Check the popen() call arguments |
---|
| 263 | subprocess.Popen.assert_called_with([tested_location], shell=True) |
---|
| 264 | |
---|
| 265 | def testActionAcknowledge(self): |
---|
| 266 | """ |
---|
| 267 | Menu Help/Acknowledge |
---|
| 268 | """ |
---|
[f82ab8c] | 269 | self.manager.actionAcknowledge() |
---|
| 270 | |
---|
| 271 | # Check if the window is actually opened. |
---|
| 272 | self.assertTrue(self.manager.ackWidget.isVisible()) |
---|
| 273 | self.assertIn("developers@sasview.org", self.manager.ackWidget.label.text()) |
---|
[9e426c1] | 274 | |
---|
| 275 | def testActionCheck_for_update(self): |
---|
| 276 | """ |
---|
| 277 | Menu Help/Check for update |
---|
| 278 | """ |
---|
| 279 | # Just make sure checkUpdate is called. |
---|
| 280 | self.manager.checkUpdate = MagicMock() |
---|
| 281 | |
---|
| 282 | self.manager.actionCheck_for_update() |
---|
[5032ea68] | 283 | |
---|
[9e426c1] | 284 | self.assertTrue(self.manager.checkUpdate.called) |
---|
| 285 | |
---|
[488c49d] | 286 | |
---|
| 287 | if __name__ == "__main__": |
---|
| 288 | unittest.main() |
---|
| 289 | |
---|