[f82ab8c] | 1 | import sys |
---|
| 2 | import unittest |
---|
| 3 | import webbrowser |
---|
| 4 | |
---|
[53c771e] | 5 | from PyQt5 import QtGui, QtWidgets |
---|
| 6 | from PyQt5.QtTest import QTest |
---|
| 7 | from PyQt5 import QtCore |
---|
[7fb471d] | 8 | from unittest.mock import MagicMock |
---|
[f82ab8c] | 9 | |
---|
[31c5b58] | 10 | # set up import paths |
---|
| 11 | import path_prepare |
---|
| 12 | |
---|
[f82ab8c] | 13 | # Local |
---|
[83eb5208] | 14 | from sas.qtgui.MainWindow.AboutBox import AboutBox |
---|
| 15 | import sas.qtgui.Utilities.LocalConfig as LocalConfig |
---|
[f82ab8c] | 16 | |
---|
[53c771e] | 17 | if not QtWidgets.QApplication.instance(): |
---|
| 18 | app = QtWidgets.QApplication(sys.argv) |
---|
[f82ab8c] | 19 | |
---|
| 20 | class AboutBoxTest(unittest.TestCase): |
---|
| 21 | '''Test the AboutBox''' |
---|
| 22 | def setUp(self): |
---|
| 23 | '''Create the AboutBox''' |
---|
| 24 | self.widget = AboutBox(None) |
---|
| 25 | |
---|
| 26 | def tearDown(self): |
---|
| 27 | '''Destroy the AboutBox''' |
---|
| 28 | self.widget.close() |
---|
| 29 | self.widget = None |
---|
| 30 | |
---|
| 31 | def testDefaults(self): |
---|
| 32 | '''Test the GUI in its default state''' |
---|
[53c771e] | 33 | self.assertIsInstance(self.widget, QtWidgets.QWidget) |
---|
[f82ab8c] | 34 | self.assertEqual(self.widget.windowTitle(), "About") |
---|
| 35 | self.assertEqual(self.widget.cmdOK.text(), "OK") |
---|
| 36 | |
---|
| 37 | self.assertIn("SasView", self.widget.label_2.text()) |
---|
| 38 | # Link buttons pixmaps don't contain image filenames, so can't check this. |
---|
| 39 | # self.assertEqual(self.widget.cmdLinkUT.icon().name(), "utlogo.gif") |
---|
| 40 | |
---|
| 41 | |
---|
| 42 | def testVersion(self): |
---|
| 43 | """ |
---|
| 44 | Assure the version number is as expected |
---|
| 45 | """ |
---|
| 46 | version = self.widget.lblVersion |
---|
[53c771e] | 47 | self.assertIsInstance(version, QtWidgets.QLabel) |
---|
[f82ab8c] | 48 | self.assertEqual(str(version.text()), str(LocalConfig.__version__)) |
---|
| 49 | |
---|
| 50 | def testAbout(self): |
---|
| 51 | """ |
---|
| 52 | Assure the about label is filled properly |
---|
| 53 | """ |
---|
| 54 | about = self.widget.lblAbout |
---|
[53c771e] | 55 | self.assertIsInstance(about, QtWidgets.QLabel) |
---|
[f82ab8c] | 56 | # build version |
---|
| 57 | self.assertIn(str(LocalConfig.__build__), about.text()) |
---|
| 58 | # License |
---|
| 59 | self.assertIn(str(LocalConfig._copyright), about.text()) |
---|
| 60 | # URLs |
---|
| 61 | self.assertIn(str(LocalConfig._homepage), about.text()) |
---|
| 62 | self.assertIn(str(LocalConfig.__download_page__), about.text()) |
---|
| 63 | self.assertIn(str(LocalConfig._license), about.text()) |
---|
| 64 | |
---|
| 65 | # Are links enabled? |
---|
| 66 | self.assertTrue(about.openExternalLinks()) |
---|
| 67 | |
---|
| 68 | def testAddActions(self): |
---|
| 69 | """ |
---|
| 70 | Assure link buttons are set up correctly |
---|
| 71 | """ |
---|
| 72 | webbrowser.open = MagicMock() |
---|
| 73 | all_hosts = [ |
---|
| 74 | LocalConfig._nist_url, |
---|
| 75 | LocalConfig._umd_url, |
---|
| 76 | LocalConfig._sns_url, |
---|
| 77 | LocalConfig._nsf_url, |
---|
| 78 | LocalConfig._isis_url, |
---|
| 79 | LocalConfig._ess_url, |
---|
| 80 | LocalConfig._ill_url, |
---|
[e207c3f] | 81 | LocalConfig._ansto_url, |
---|
[7385fec] | 82 | LocalConfig._inst_url, |
---|
| 83 | LocalConfig._delft_url, |
---|
| 84 | LocalConfig._bam_url, |
---|
| 85 | LocalConfig._diamond_url] |
---|
[e540cd2] | 86 | |
---|
[f82ab8c] | 87 | # Press the buttons |
---|
[53c771e] | 88 | buttonList = self.widget.findChildren(QtWidgets.QPushButton) |
---|
[f82ab8c] | 89 | for button in buttonList: |
---|
[464cd07] | 90 | QTest.mouseClick(button, QtCore.Qt.LeftButton) |
---|
[f82ab8c] | 91 | #open_link = webbrowser.open.call_args |
---|
| 92 | args, _ = webbrowser.open.call_args |
---|
| 93 | # args[0] contains the actual argument sent to open() |
---|
| 94 | self.assertIn(args[0], all_hosts) |
---|
| 95 | |
---|
| 96 | # The above test also greedily catches the OK button, |
---|
| 97 | # so let's test it separately. |
---|
| 98 | # Show the widget |
---|
| 99 | self.widget.show() |
---|
| 100 | self.assertTrue(self.widget.isVisible()) |
---|
| 101 | # Click on the OK button |
---|
[464cd07] | 102 | QTest.mouseClick(self.widget.cmdOK, QtCore.Qt.LeftButton) |
---|
[f82ab8c] | 103 | # assure the widget is no longer seen |
---|
| 104 | self.assertFalse(self.widget.isVisible()) |
---|
| 105 | |
---|
| 106 | if __name__ == "__main__": |
---|
| 107 | unittest.main() |
---|