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