source: sasview/src/sas/qtgui/Plotting/UnitTesting/SlicerParametersTest.py @ cd4f421

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalc
Last change on this file since cd4f421 was 464cd07, checked in by Piotr Rozyczko <rozyczko@…>, 7 years ago

Use singleton QApplication in unit tests to avoid issues on Ubuntu. SASVIEW-485

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