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