source: sasview/src/sas/qtgui/Calculators/UnitTesting/ResolutionCalculatorPanelTest.py @ e90988c

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

Show help pages in default browser. Fixed some help links and modified unit tests. SASVIEW-800

  • Property mode set to 100644
File size: 12.4 KB
Line 
1import sys
2import time
3import numpy
4import logging
5import unittest
6from PyQt5 import QtGui, QtWidgets
7from PyQt5 import QtCore
8from PyQt5.QtTest import QTest
9from PyQt5.QtCore import Qt
10from unittest.mock import MagicMock
11
12from twisted.internet import threads
13
14# from UnitTesting.TestUtils import QtSignalSpy
15# from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
16from sas.qtgui.Calculators.ResolutionCalculatorPanel \
17    import ResolutionCalculatorPanel
18
19# from sas.qtgui.MainWindow.DataManager import DataManager
20# from sas.qtgui.MainWindow.GuiManager import GuiManager
21# from sas.qtgui.Utilities.GuiUtils import *
22
23
24BG_COLOR_ERR = 'background-color: rgb(244, 170, 164);'
25
26BG_COLOR_WARNING = 'background-color: rgb(244, 217, 164);'
27
28
29if not QtWidgets.QApplication.instance():
30    app = QtWidgets.QApplication(sys.argv)
31
32
33class ResolutionCalculatorPanelTest(unittest.TestCase):
34    """Test the ResolutionCalculator"""
35    def setUp(self):
36        """Create the ResolutionCalculator"""
37        self.widget = ResolutionCalculatorPanel(None)
38
39        # self.widget.onCompute = MagicMock()
40
41    def tearDown(self):
42        """Destroy the ResolutionCalculator"""
43        self.widget.close()
44        self.widget = None
45
46    def testDefaults(self):
47        """Test the GUI in its default state"""
48
49        self.assertIsInstance(self.widget, QtWidgets.QDialog)
50        self.assertEqual(self.widget.windowTitle(), "Q Resolution Estimator")
51        # size
52        self.assertEqual(self.widget.size().height(), 540)
53        self.assertEqual(self.widget.size().width(), 876)
54
55        # visibility
56        self.assertFalse(self.widget.lblSpectrum.isVisible())
57        self.assertFalse(self.widget.cbCustomSpectrum.isVisible())
58
59        # content of line edits
60        self.assertEqual(self.widget.txtDetectorPixSize.text(), '0.5, 0.5')
61        self.assertEqual(self.widget.txtDetectorSize.text(), '128, 128')
62        self.assertEqual(self.widget.txtSample2DetectorDistance.text(), '1000')
63        self.assertEqual(self.widget.txtSampleApertureSize.text(), '1.27')
64        self.assertEqual(self.widget.txtSampleOffset.text(), '0')
65        self.assertEqual(self.widget.txtSource2SampleDistance.text(), '1627')
66        self.assertEqual(self.widget.txtSourceApertureSize.text(), '3.81')
67        self.assertEqual(self.widget.txtWavelength.text(), '6.0')
68        self.assertEqual(self.widget.txtWavelengthSpread.text(), '0.125')
69
70        self.assertEqual(self.widget.txtQx.text(), '0.0')
71        self.assertEqual(self.widget.txtQy.text(), '0.0')
72
73        self.assertEqual(self.widget.txt1DSigma.text(), '0.0008289')
74        self.assertEqual(self.widget.txtSigma_lamd.text(), '3.168e-05')
75        self.assertEqual(self.widget.txtSigma_x.text(), '0.0008288')
76        self.assertEqual(self.widget.txtSigma_x.text(), '0.0008288')
77
78        # items of comboboxes
79        self.assertFalse(self.widget.cbCustomSpectrum.isEditable())
80        self.assertEqual(self.widget.cbCustomSpectrum.currentText(), 'Flat')
81        self.assertEqual(self.widget.cbCustomSpectrum.currentIndex(), 0)
82        self.assertListEqual([self.widget.cbCustomSpectrum.itemText(i) for i in
83                              range(self.widget.cbCustomSpectrum.count())],
84                             ['Flat', 'Add New'])
85
86        self.assertFalse(self.widget.cbSource.isEditable())
87        self.assertEqual(self.widget.cbSource.count(), 6)
88        self.assertEqual(self.widget.cbSource.currentText(), 'Neutron')
89        self.assertListEqual([self.widget.cbSource.itemText(i) for i in
90                              range(self.widget.cbSource.count())],
91                             ['Alpha', 'Deutron', 'Neutron', 'Photon',
92                              'Proton', 'Triton'])
93
94        self.assertFalse(self.widget.cbWaveColor.isEditable())
95        self.assertEqual(self.widget.cbWaveColor.count(), 2)
96        self.assertEqual(self.widget.cbWaveColor.currentText(), 'Monochromatic')
97        self.assertListEqual([self.widget.cbWaveColor.itemText(i) for i in
98                              range(self.widget.cbWaveColor.count())],
99                             ['Monochromatic', 'TOF'])
100
101        # read only text edits
102        self.assertTrue(self.widget.txtSigma_x.isReadOnly())
103        self.assertTrue(self.widget.txtSigma_y.isReadOnly())
104        self.assertTrue(self.widget.txtSigma_lamd.isReadOnly())
105        self.assertTrue(self.widget.txt1DSigma.isReadOnly())
106
107        # tooltips
108        self.assertEqual(self.widget.cbSource.toolTip(),
109                         "Source Selection: "
110                         "Affect on the gravitational contribution.")
111        self.widget.cbCustomSpectrum.toolTip(), \
112        "Wavelength Spectrum: Intensity vs. wavelength."
113        # print self.widget.txtDetectorPixSize.toolTip()
114        self.assertEqual(self.widget.txtDetectorPixSize.toolTip(),
115                         "Detector Pixel Size.")
116        self.assertEqual(self.widget.txtDetectorSize.toolTip(),
117                         "Number of Pixels on Detector.")
118        self.assertEqual(self.widget.txtSample2DetectorDistance.toolTip(),
119                         "Sample Aperture to Detector Distance.")
120        self.assertEqual(self.widget.txtSampleApertureSize.toolTip(),
121                         "Sample Aperture Size.")
122        self.assertEqual(self.widget.txtSampleOffset.toolTip(),
123                         "Sample Offset.")
124        self.assertEqual(self.widget.txtSource2SampleDistance.toolTip(),
125                         "Source to Sample Aperture Distance.")
126        self.assertEqual(self.widget.txtSourceApertureSize.toolTip(),
127                         "Source Aperture Size.")
128        self.assertEqual(self.widget.txtWavelength.toolTip(),
129                         "Wavelength of the Neutrons.")
130        self.assertEqual(self.widget.txtWavelengthSpread.toolTip(),
131                         "Wavelength Spread of Neutrons.")
132        self.assertEqual(self.widget.txtQx.toolTip(), "Type the Qx value.")
133        self.assertEqual(self.widget.txtQy.toolTip(), "Type the Qy value.")
134        self.assertEqual(self.widget.txt1DSigma.toolTip(),
135                         "Resolution in 1-dimension (for 1D data).")
136        self.assertEqual(self.widget.txtSigma_lamd.toolTip(),
137                         "The wavelength contribution in the radial direction."
138                         " Note: The phi component is always zero.")
139        self.assertEqual(self.widget.txtSigma_x.toolTip(),
140                         "The x component of the geometric resolution, "
141                         "excluding sigma_lamda.")
142        self.assertEqual(self.widget.txtSigma_y.toolTip(),
143                         "The y component of the geometric resolution, "
144                         "excluding sigma_lamda.")
145
146    def testFormatNumber(self):
147        self.assertEqual(self.widget.formatNumber('  7.123456  '), '7.123')
148
149    def testCheckWavelength(self):
150        """ Test validator for Wavelength text edit"""
151        self.widget.txtWavelength.clear()
152        # Enter invalid input for Monochromatic spectrum
153        # check that background becomes red and Compute button is disabled
154        QTest.keyClicks(self.widget.txtWavelength, 'vcv ')
155        QTest.keyClick(self.widget.txtWavelength, QtCore.Qt.Key_Return)
156        self.assertIn(BG_COLOR_ERR, self.widget.txtWavelength.styleSheet())
157        self.assertFalse(self.widget.cmdCompute.isEnabled())
158
159        # Enter invalid input for TOF spectrum
160        # check that background becomes red and Compute button is disabled
161        self.widget.cbWaveColor.setCurrentIndex(1)
162
163        self.widget.txtWavelength.clear()
164        QTest.keyClicks(self.widget.txtWavelength, '4')
165        QTest.keyClick(self.widget.txtWavelength, QtCore.Qt.Key_Return)
166        self.assertIn(BG_COLOR_ERR, self.widget.txtWavelength.styleSheet())
167        self.assertFalse(self.widget.cmdCompute.isEnabled())
168
169    def testCheckWavelengthSpread(self):
170        """ Test validator for WavelengthSpread """
171        self.widget.txtWavelengthSpread.clear()
172        QTest.keyClicks(self.widget.txtWavelengthSpread, '0.12; 1.3')
173        QTest.keyClick(self.widget.txtWavelengthSpread, QtCore.Qt.Key_Return)
174        self.assertIn(BG_COLOR_ERR,
175                      self.widget.txtWavelengthSpread.styleSheet())
176
177    def testCheckPixels(self):
178        """ Test validator for pixel size and number """
179        self.widget.txtDetectorPixSize.clear()
180        QTest.keyClicks(self.widget.txtDetectorPixSize, '0.12; 1.3')
181        QTest.keyClick(self.widget.txtDetectorPixSize, QtCore.Qt.Key_Return)
182        self.assertIn(BG_COLOR_ERR,
183                      self.widget.txtDetectorPixSize.styleSheet())
184
185        self.widget.txtDetectorSize.clear()
186        QTest.keyClicks(self.widget.txtDetectorSize, '0.12')
187        QTest.keyClick(self.widget.txtDetectorSize, QtCore.Qt.Key_Return)
188        self.assertIn(BG_COLOR_ERR,
189                      self.widget.txtDetectorSize.styleSheet())
190
191    def testCheckQx_y(self):
192        """ Test validator for qx and qy inputs """
193        self.widget.txtQx.clear()
194        QTest.keyClicks(self.widget.txtQx, '0.12; 1.3')
195        QTest.keyClick(self.widget.txtQx, QtCore.Qt.Key_Return)
196        self.assertIn(BG_COLOR_ERR,
197                      self.widget.txtQx.styleSheet())
198        # put back default value
199        self.widget.txtQx.setText('0.0')
200
201        self.widget.txtQy.clear()
202        QTest.keyClicks(self.widget.txtQy, '0.12, a')
203        QTest.keyClick(self.widget.txtQy, QtCore.Qt.Key_Return)
204        self.assertIn(BG_COLOR_ERR, self.widget.txtQy.styleSheet())
205        # put back default value
206        self.widget.txtQy.setText('0.0')
207
208    def testOnSelectWaveColor(self):
209        """ Test change of layout if type of source is TOF """
210        # choose TOF
211        AllItems = [self.widget.cbWaveColor.itemText(i)
212                    for i in range(self.widget.cbWaveColor.count())]
213        self.widget.cbWaveColor.setCurrentIndex(AllItems.index('TOF'))
214
215        # call function
216        self.widget.onSelectWaveColor()
217        self.widget.show()
218
219        # check that TOF is selected
220        self.assertTrue(self.widget.cbWaveColor.currentText(), 'TOF')
221
222        # check modifications of Wavelength text edit: min - max
223        self.assertEqual(self.widget.txtWavelength.text(), '6.0 - 12.0')
224        self.assertEqual(self.widget.txtWavelengthSpread.text(), '0.125 - 0.125')
225
226        # check that Spectrum label and cbCustomSpectrum are visible
227        self.assertTrue(self.widget.lblSpectrum.isVisible())
228        self.assertTrue(self.widget.cbCustomSpectrum.isVisible())
229
230    def testOnSelectCustomSpectrum(self):
231        """ Test Custom Spectrum: load file if 'Add New' """
232        QtWidgets.QFileDialog.getOpenFileName = MagicMock(return_value=("",""))
233        self.widget.cbCustomSpectrum.setCurrentIndex(1)
234
235        # Test the getOpenFileName() dialog called once
236        self.assertTrue(QtWidgets.QFileDialog.getOpenFileName.called)
237        QtWidgets.QFileDialog.getOpenFileName.assert_called_once()
238
239    def testHelp(self):
240        """ Assure help file is shown """
241        # this should not rise
242        self.widget.manager = QtWidgets.QWidget()
243        self.widget.manager.showHelp = MagicMock()
244        self.widget.onHelp()
245        self.assertTrue(self.widget.manager.showHelp.called_once())
246        args = self.widget.manager.showHelp.call_args
247        self.assertIn('resolution_calculator_help.html', args[0][0])
248
249    def testOnReset(self):
250        """ Test onReset function"""
251        # modify gui
252        self.widget.txtQx.setText('33.0')
253        self.widget.cbSource.setCurrentIndex(0)
254
255        # check that GUI has been modified
256        self.assertNotEqual(self.widget.cbSource.currentText(), 'Neutron')
257        # apply reset
258        QTest.mouseClick(self.widget.cmdReset, Qt.LeftButton)
259        # check that we get back to the initial state
260        self.assertEqual(self.widget.txtQx.text(), '0.0')
261        self.assertEqual(self.widget.cbSource.currentText(), 'Neutron')
262
263    def testOnClose(self):
264        """ test Closing window """
265        closeButton = self.widget.cmdClose
266        QTest.mouseClick(closeButton, Qt.LeftButton)
267
268    def testOnCompute(self):
269        """ """
270        threads.deferToThread = MagicMock()
271        self.widget.onCompute()
272
273        # thread called
274        self.assertTrue(threads.deferToThread.called)
275        self.assertEqual(threads.deferToThread.call_args_list[0][0][0].__name__, 'map_wrapper')
276
277        # the Compute button changed caption and got disabled
278        self.assertEqual(self.widget.cmdCompute.text(), 'Wait...')
279        self.assertFalse(self.widget.cmdCompute.isEnabled())
280
281
282if __name__ == "__main__":
283    unittest.main()
Note: See TracBrowser for help on using the repository browser.