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

Last change on this file since 7be7136 was 170e95d, checked in by celinedurniak <celine.durniak@…>, 7 years ago

Corrected layout of Resolution panel

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