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

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 14ec91c5 was 14ec91c5, checked in by Piotr Rozyczko <rozyczko@…>, 6 years ago

More code review related fixes

  • Property mode set to 100644
File size: 3.8 KB
Line 
1import sys
2import unittest
3import webbrowser
4from unittest.mock import MagicMock
5
6from PyQt5 import QtGui, QtWidgets
7from PyQt5 import QtCore
8from PyQt5 import QtTest
9from PyQt5 import QtWebKit
10
11# set up import paths
12import sas.qtgui.path_prepare
13
14from sas.qtgui.UnitTesting.TestUtils import QtSignalSpy
15
16# Local
17from sas.qtgui.Plotting.SlicerParameters import SlicerParameters
18
19if not QtWidgets.QApplication.instance():
20    app = QtWidgets.QApplication(sys.argv)
21
22class 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, QtCore.QIdentityProxyModel)
41        self.assertIsInstance(self.widget.lstParams.itemDelegate(), QtWidgets.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(QtWidgets.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 notestOnHelp(self):
72        ''' Assure clicking on help returns QtWeb view on requested page'''
73        self.widget.show()
74
75        #Mock the webbrowser.open method
76        webbrowser.open = MagicMock()
77
78        # Invoke the action
79        self.widget.onHelp()
80
81        # Check if show() got called
82        self.assertTrue(webbrowser.open.called)
83
84        # Assure the filename is correct
85        self.assertIn("graph_help.html", webbrowser.open.call_args[0][0])
86       
87    def testSetModel(self):
88        ''' Test if resetting the model works'''
89       
90        item1 = QtGui.QStandardItem("s1")
91        item2 = QtGui.QStandardItem("5.0")
92        new_model = QtGui.QStandardItemModel()
93        new_model.appendRow([item1, item2])
94        item1 = QtGui.QStandardItem("s2")
95        item2 = QtGui.QStandardItem("20.0")
96        new_model.appendRow([item1, item2])
97        # Force the new model onto the widget
98        self.widget.setModel(model=new_model)
99
100        # Test if the widget got it
101        self.assertEqual(self.widget.model.columnCount(), 2)
102        self.assertEqual(self.widget.model.rowCount(), 2)
103        self.assertEqual(self.widget.model.item(0,0).text(), 's1')
104        self.assertEqual(self.widget.model.item(1,0).text(), 's2')
105
106
107       
108if __name__ == "__main__":
109    unittest.main()
Note: See TracBrowser for help on using the repository browser.