1 | import sys |
---|
2 | import unittest |
---|
3 | import webbrowser |
---|
4 | from unittest.mock import MagicMock |
---|
5 | |
---|
6 | from PyQt5 import QtGui, QtWidgets |
---|
7 | from PyQt5 import QtCore |
---|
8 | from PyQt5 import QtTest |
---|
9 | |
---|
10 | # set up import paths |
---|
11 | import sas.qtgui.path_prepare |
---|
12 | |
---|
13 | from sas.qtgui.UnitTesting.TestUtils import QtSignalSpy |
---|
14 | |
---|
15 | # Local |
---|
16 | from sas.qtgui.Plotting.SlicerParameters import SlicerParameters |
---|
17 | |
---|
18 | if not QtWidgets.QApplication.instance(): |
---|
19 | app = QtWidgets.QApplication(sys.argv) |
---|
20 | |
---|
21 | class SlicerParametersTest(unittest.TestCase): |
---|
22 | '''Test the SlicerParameters dialog''' |
---|
23 | def setUp(self): |
---|
24 | '''Create the SlicerParameters dialog''' |
---|
25 | |
---|
26 | item1 = QtGui.QStandardItem("dromekage") |
---|
27 | item2 = QtGui.QStandardItem("999.0") |
---|
28 | self.model = QtGui.QStandardItemModel() |
---|
29 | self.model.appendRow([item1, item2]) |
---|
30 | self.widget = SlicerParameters(model=self.model) |
---|
31 | |
---|
32 | def tearDown(self): |
---|
33 | '''Destroy the model''' |
---|
34 | self.widget = None |
---|
35 | |
---|
36 | def testDefaults(self): |
---|
37 | '''Test the GUI in its default state''' |
---|
38 | #self.widget.mapper |
---|
39 | self.assertIsInstance(self.widget.proxy, QtCore.QIdentityProxyModel) |
---|
40 | self.assertIsInstance(self.widget.lstParams.itemDelegate(), QtWidgets.QStyledItemDelegate) |
---|
41 | self.assertTrue(self.widget.lstParams.model().columnReadOnly(0)) |
---|
42 | self.assertFalse(self.widget.lstParams.model().columnReadOnly(1)) |
---|
43 | |
---|
44 | # Test the proxy model |
---|
45 | self.assertEqual(self.widget.lstParams.model(), self.widget.proxy) |
---|
46 | self.assertEqual(self.widget.proxy.columnCount(), 2) |
---|
47 | self.assertEqual(self.widget.proxy.rowCount(), 1) |
---|
48 | self.assertEqual(self.widget.model.item(0,0).text(), 'dromekage') |
---|
49 | self.assertEqual(self.widget.model.item(0,1).text(), '999.0') |
---|
50 | |
---|
51 | # Check the flags in the proxy model |
---|
52 | flags = self.widget.proxy.flags(self.widget.proxy.index(0,0)) |
---|
53 | self.assertFalse(flags & QtCore.Qt.ItemIsEditable) |
---|
54 | self.assertTrue(flags & QtCore.Qt.ItemIsSelectable) |
---|
55 | self.assertTrue(flags & QtCore.Qt.ItemIsEnabled) |
---|
56 | |
---|
57 | def testClose(self): |
---|
58 | ''' Assure that clicking on Close triggers right behaviour''' |
---|
59 | self.widget.show() |
---|
60 | |
---|
61 | # Set up the spy |
---|
62 | spy_close = QtSignalSpy(self.widget, self.widget.closeWidgetSignal) |
---|
63 | # Click on the "Close" button |
---|
64 | QtTest.QTest.mouseClick(self.widget.buttonBox.button(QtWidgets.QDialogButtonBox.Close), QtCore.Qt.LeftButton) |
---|
65 | # Check the signal |
---|
66 | self.assertEqual(spy_close.count(), 1) |
---|
67 | # Assure the window got closed |
---|
68 | self.assertFalse(self.widget.isVisible()) |
---|
69 | |
---|
70 | def notestOnHelp(self): |
---|
71 | ''' Assure clicking on help returns QtWeb view on requested page''' |
---|
72 | self.widget.show() |
---|
73 | |
---|
74 | #Mock the webbrowser.open method |
---|
75 | webbrowser.open = MagicMock() |
---|
76 | |
---|
77 | # Invoke the action |
---|
78 | self.widget.onHelp() |
---|
79 | |
---|
80 | # Check if show() got called |
---|
81 | self.assertTrue(webbrowser.open.called) |
---|
82 | |
---|
83 | # Assure the filename is correct |
---|
84 | self.assertIn("graph_help.html", webbrowser.open.call_args[0][0]) |
---|
85 | |
---|
86 | def testSetModel(self): |
---|
87 | ''' Test if resetting the model works''' |
---|
88 | |
---|
89 | item1 = QtGui.QStandardItem("s1") |
---|
90 | item2 = QtGui.QStandardItem("5.0") |
---|
91 | new_model = QtGui.QStandardItemModel() |
---|
92 | new_model.appendRow([item1, item2]) |
---|
93 | item1 = QtGui.QStandardItem("s2") |
---|
94 | item2 = QtGui.QStandardItem("20.0") |
---|
95 | new_model.appendRow([item1, item2]) |
---|
96 | # Force the new model onto the widget |
---|
97 | self.widget.setModel(model=new_model) |
---|
98 | |
---|
99 | # Test if the widget got it |
---|
100 | self.assertEqual(self.widget.model.columnCount(), 2) |
---|
101 | self.assertEqual(self.widget.model.rowCount(), 2) |
---|
102 | self.assertEqual(self.widget.model.item(0,0).text(), 's1') |
---|
103 | self.assertEqual(self.widget.model.item(1,0).text(), 's2') |
---|
104 | |
---|
105 | |
---|
106 | |
---|
107 | if __name__ == "__main__": |
---|
108 | unittest.main() |
---|