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

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 8353d90 was 53c771e, checked in by Piotr Rozyczko <rozyczko@…>, 7 years ago

Converted unit tests

  • Property mode set to 100644
File size: 3.9 KB
RevLine 
[cf89cce]1import sys
2import unittest
[7fb471d]3from unittest.mock import MagicMock
[cf89cce]4
[53c771e]5from PyQt5 import QtGui, QtWidgets
6from PyQt5 import QtCore
7from PyQt5 import QtTest
8from PyQt5 import QtWebKit
[cf89cce]9
10# set up import paths
[83eb5208]11import sas.qtgui.path_prepare
[cf89cce]12
[83eb5208]13from sas.qtgui.UnitTesting.TestUtils import QtSignalSpy
[cf89cce]14
15# Local
[83eb5208]16from sas.qtgui.Plotting.SlicerParameters import SlicerParameters
[cf89cce]17
[53c771e]18if not QtWidgets.QApplication.instance():
19    app = QtWidgets.QApplication(sys.argv)
[cf89cce]20
21class 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
[4992ff2]39        self.assertIsInstance(self.widget.proxy, QtCore.QIdentityProxyModel)
[53c771e]40        self.assertIsInstance(self.widget.lstParams.itemDelegate(), QtWidgets.QStyledItemDelegate)
[cf89cce]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.close_signal)
63        # Click on the "Close" button
[53c771e]64        QtTest.QTest.mouseClick(self.widget.buttonBox.button(QtWidgets.QDialogButtonBox.Close), QtCore.Qt.LeftButton)
[cf89cce]65        # Check the signal
66        self.assertEqual(spy_close.count(), 1)
67        # Assure the window got closed
68        self.assertFalse(self.widget.isVisible())
69
[53c771e]70    def notestOnHelp(self):
[cf89cce]71        ''' Assure clicking on help returns QtWeb view on requested page'''
72        self.widget.show()
73
74        #Mock the QWebView method
75        QtWebKit.QWebView.show = MagicMock()
76        QtWebKit.QWebView.load = MagicMock()
77
78        # Invoke the action
79        self.widget.onHelp()
80
81        # Check if show() got called
82        self.assertTrue(QtWebKit.QWebView.show.called)
83
84        # Assure the filename is correct
85        self.assertIn("graph_help.html", QtWebKit.QWebView.load.call_args[0][0].toString())
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.