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

ESS_GUI
Last change on this file was 144fe21, checked in by Piotr Rozyczko <rozyczko@…>, 6 years ago

new unit test runner script + test fixes. SASVIEW-970

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