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