[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 |
---|
| 14 | from GuiManager import GuiManager |
---|
[5032ea68] | 15 | from UI.MainWindowUI import MainWindow |
---|
[9e426c1] | 16 | from UnitTesting.TestUtils import QtSignalSpy |
---|
[488c49d] | 17 | |
---|
[5032ea68] | 18 | app = QApplication(sys.argv) |
---|
[488c49d] | 19 | |
---|
| 20 | class GuiManagerTest(unittest.TestCase): |
---|
[5032ea68] | 21 | '''Test the Main Window functionality''' |
---|
[488c49d] | 22 | def setUp(self): |
---|
| 23 | '''Create the tested object''' |
---|
[5032ea68] | 24 | class MainSasViewWindow(MainWindow): |
---|
| 25 | # Main window of the application |
---|
| 26 | def __init__(self, reactor, parent=None): |
---|
| 27 | super(MainSasViewWindow, self).__init__(parent) |
---|
| 28 | |
---|
| 29 | # define workspace for dialogs. |
---|
| 30 | self.workspace = QWorkspace(self) |
---|
| 31 | self.setCentralWidget(self.workspace) |
---|
[488c49d] | 32 | |
---|
[5032ea68] | 33 | self.manager = GuiManager(MainSasViewWindow(None), None) |
---|
[488c49d] | 34 | |
---|
| 35 | def tearDown(self): |
---|
| 36 | '''Destroy the GUI''' |
---|
| 37 | self.manager = None |
---|
| 38 | |
---|
| 39 | def testDefaults(self): |
---|
| 40 | '''Test the object in its default state''' |
---|
| 41 | pass |
---|
| 42 | |
---|
| 43 | def testUpdatePerspective(self): |
---|
| 44 | """ |
---|
| 45 | """ |
---|
| 46 | pass |
---|
| 47 | |
---|
| 48 | def testUpdateStatusBar(self): |
---|
| 49 | """ |
---|
| 50 | """ |
---|
| 51 | pass |
---|
| 52 | |
---|
| 53 | def testSetData(self): |
---|
| 54 | """ |
---|
| 55 | """ |
---|
| 56 | pass |
---|
| 57 | |
---|
| 58 | def testSetData(self): |
---|
| 59 | """ |
---|
| 60 | """ |
---|
| 61 | pass |
---|
| 62 | |
---|
[9e426c1] | 63 | def testQuitApplication(self): |
---|
| 64 | """ |
---|
| 65 | Test that the custom exit method is called on shutdown |
---|
| 66 | """ |
---|
| 67 | # Must mask sys.exit, otherwise the whole testing process stops. |
---|
| 68 | sys.exit = MagicMock() |
---|
| 69 | |
---|
| 70 | # Say No to the close dialog |
---|
| 71 | QMessageBox.question = MagicMock(return_value=QMessageBox.No) |
---|
| 72 | |
---|
| 73 | # Open, then close the manager |
---|
| 74 | self.manager.quitApplication() |
---|
| 75 | |
---|
| 76 | # See that the MessageBox method got called |
---|
| 77 | self.assertTrue(QMessageBox.question.called) |
---|
| 78 | # sys.exit() not called this time |
---|
| 79 | self.assertFalse(sys.exit.called) |
---|
| 80 | |
---|
| 81 | # Say Yes to the close dialog |
---|
| 82 | QMessageBox.question = MagicMock(return_value=QMessageBox.Yes) |
---|
| 83 | |
---|
| 84 | # Open, then close the manager |
---|
| 85 | self.manager.quitApplication() |
---|
| 86 | |
---|
| 87 | # See that the MessageBox method got called |
---|
| 88 | self.assertTrue(QMessageBox.question.called) |
---|
| 89 | |
---|
| 90 | def testCheckUpdate(self): |
---|
| 91 | """ |
---|
| 92 | Tests the SasView website version polling |
---|
| 93 | """ |
---|
| 94 | self.manager.processVersion = MagicMock() |
---|
| 95 | version = {'update_url' : 'http://www.sasview.org/sasview.latestversion', |
---|
| 96 | 'version' : '3.1.2', |
---|
| 97 | 'download_url': 'https://github.com/SasView/sasview/releases'} |
---|
| 98 | self.manager.checkUpdate() |
---|
| 99 | |
---|
| 100 | self.manager.processVersion.assert_called_with(version) |
---|
| 101 | |
---|
| 102 | pass |
---|
| 103 | |
---|
| 104 | def testProcessVersion(self): |
---|
| 105 | """ |
---|
| 106 | Tests the version checker logic |
---|
| 107 | """ |
---|
| 108 | # 1. version = 0.0.0 |
---|
| 109 | version_info = {u'version' : u'0.0.0'} |
---|
| 110 | spy_status_update = QtSignalSpy(self.manager, self.manager.communicate.statusBarUpdateSignal) |
---|
| 111 | |
---|
| 112 | self.manager.processVersion(version_info) |
---|
| 113 | |
---|
| 114 | self.assertEqual(spy_status_update.count(), 1) |
---|
| 115 | message = 'Could not connect to the application server. Please try again later.' |
---|
| 116 | self.assertIn(message, str(spy_status_update.signal(index=0))) |
---|
| 117 | |
---|
| 118 | # 2. version < LocalConfig.__version__ |
---|
| 119 | version_info = {u'version' : u'0.0.1'} |
---|
| 120 | spy_status_update = QtSignalSpy(self.manager, self.manager.communicate.statusBarUpdateSignal) |
---|
| 121 | |
---|
| 122 | self.manager.processVersion(version_info) |
---|
| 123 | |
---|
| 124 | self.assertEqual(spy_status_update.count(), 1) |
---|
| 125 | message = 'You have the latest version of SasView' |
---|
| 126 | self.assertIn(message, str(spy_status_update.signal(index=0))) |
---|
| 127 | |
---|
| 128 | # 3. version > LocalConfig.__version__ |
---|
| 129 | version_info = {u'version' : u'999.0.0'} |
---|
| 130 | spy_status_update = QtSignalSpy(self.manager, self.manager.communicate.statusBarUpdateSignal) |
---|
| 131 | webbrowser.open = MagicMock() |
---|
| 132 | |
---|
| 133 | self.manager.processVersion(version_info) |
---|
| 134 | |
---|
| 135 | self.assertEqual(spy_status_update.count(), 1) |
---|
| 136 | message = 'Version 999.0.0 is available!' |
---|
| 137 | self.assertIn(message, str(spy_status_update.signal(index=0))) |
---|
| 138 | |
---|
| 139 | webbrowser.open.assert_called_with("https://github.com/SasView/sasview/releases") |
---|
| 140 | |
---|
[f82ab8c] | 141 | ## 4. version > LocalConfig.__version__ and standalone |
---|
| 142 | #version_info = {u'version' : u'999.0.0'} |
---|
| 143 | #spy_status_update = QtSignalSpy(self.manager, self.manager.communicate.statusBarUpdateSignal) |
---|
| 144 | #webbrowser.open = MagicMock() |
---|
[9e426c1] | 145 | |
---|
[f82ab8c] | 146 | #self.manager.processVersion(version_info, standalone=True) |
---|
[9e426c1] | 147 | |
---|
[f82ab8c] | 148 | #self.assertEqual(spy_status_update.count(), 1) |
---|
| 149 | #message = 'See the help menu to download it' |
---|
| 150 | #self.assertIn(message, str(spy_status_update.signal(index=0))) |
---|
[9e426c1] | 151 | |
---|
[f82ab8c] | 152 | #self.assertFalse(webbrowser.open.called) |
---|
[9e426c1] | 153 | |
---|
| 154 | # 5. couldn't load version |
---|
| 155 | version_info = {} |
---|
| 156 | logging.error = MagicMock() |
---|
| 157 | spy_status_update = QtSignalSpy(self.manager, self.manager.communicate.statusBarUpdateSignal) |
---|
| 158 | |
---|
| 159 | self.manager.processVersion(version_info) |
---|
| 160 | |
---|
| 161 | # Retrieve and compare arguments of the mocked call |
---|
| 162 | message = "guiframe: could not get latest application version number" |
---|
| 163 | args, _ = logging.error.call_args |
---|
| 164 | self.assertIn(message, args[0]) |
---|
| 165 | |
---|
| 166 | # Check the signal message |
---|
| 167 | message = 'Could not connect to the application server.' |
---|
| 168 | self.assertIn(message, str(spy_status_update.signal(index=0))) |
---|
| 169 | |
---|
[488c49d] | 170 | def testActions(self): |
---|
| 171 | """ |
---|
| 172 | """ |
---|
| 173 | pass |
---|
| 174 | |
---|
[5032ea68] | 175 | def testActionLoadData(self): |
---|
| 176 | """ |
---|
| 177 | Menu File/Load Data File(s) |
---|
| 178 | """ |
---|
| 179 | # Mock the system file open method |
---|
[9e426c1] | 180 | QFileDialog.getOpenFileNames = MagicMock(return_value=None) |
---|
[5032ea68] | 181 | |
---|
| 182 | # invoke the action |
---|
[9e426c1] | 183 | self.manager.actionLoadData() |
---|
[5032ea68] | 184 | |
---|
| 185 | # Test the getOpenFileName() dialog called once |
---|
[9e426c1] | 186 | self.assertTrue(QFileDialog.getOpenFileNames.called) |
---|
[5032ea68] | 187 | |
---|
[9e426c1] | 188 | def testActionDocumentation(self): |
---|
| 189 | """ |
---|
| 190 | Menu Help/Documentation |
---|
| 191 | """ |
---|
| 192 | #Mock the QWebView method |
---|
| 193 | QWebView.show = MagicMock() |
---|
| 194 | |
---|
| 195 | # Assure the filename is correct |
---|
| 196 | self.assertIn("index.html", self.manager._helpLocation) |
---|
| 197 | |
---|
| 198 | # Invoke the action |
---|
| 199 | self.manager.actionDocumentation() |
---|
| 200 | |
---|
| 201 | # Check if show() got called |
---|
| 202 | self.assertTrue(QWebView.show.called) |
---|
| 203 | |
---|
| 204 | def testActionTutorial(self): |
---|
| 205 | """ |
---|
| 206 | Menu Help/Tutorial |
---|
| 207 | """ |
---|
| 208 | # Mock subprocess.Popen |
---|
| 209 | subprocess.Popen = MagicMock() |
---|
| 210 | |
---|
| 211 | tested_location = self.manager._tutorialLocation |
---|
| 212 | |
---|
| 213 | # Assure the filename is correct |
---|
| 214 | self.assertIn("Tutorial.pdf", tested_location) |
---|
| 215 | |
---|
| 216 | # Invoke the action |
---|
| 217 | self.manager.actionTutorial() |
---|
| 218 | |
---|
| 219 | # Check if popen() got called |
---|
| 220 | self.assertTrue(subprocess.Popen.called) |
---|
| 221 | |
---|
| 222 | #Check the popen() call arguments |
---|
| 223 | subprocess.Popen.assert_called_with([tested_location], shell=True) |
---|
| 224 | |
---|
| 225 | def testActionAcknowledge(self): |
---|
| 226 | """ |
---|
| 227 | Menu Help/Acknowledge |
---|
| 228 | """ |
---|
[f82ab8c] | 229 | self.manager.actionAcknowledge() |
---|
| 230 | |
---|
| 231 | # Check if the window is actually opened. |
---|
| 232 | self.assertTrue(self.manager.ackWidget.isVisible()) |
---|
| 233 | self.assertIn("developers@sasview.org", self.manager.ackWidget.label.text()) |
---|
[9e426c1] | 234 | |
---|
| 235 | def testActionCheck_for_update(self): |
---|
| 236 | """ |
---|
| 237 | Menu Help/Check for update |
---|
| 238 | """ |
---|
| 239 | # Just make sure checkUpdate is called. |
---|
| 240 | self.manager.checkUpdate = MagicMock() |
---|
| 241 | |
---|
| 242 | self.manager.actionCheck_for_update() |
---|
[5032ea68] | 243 | |
---|
[9e426c1] | 244 | self.assertTrue(self.manager.checkUpdate.called) |
---|
| 245 | |
---|
[488c49d] | 246 | |
---|
| 247 | if __name__ == "__main__": |
---|
| 248 | unittest.main() |
---|
| 249 | |
---|