1 | import sys |
---|
2 | import time |
---|
3 | import numpy |
---|
4 | import logging |
---|
5 | import unittest |
---|
6 | from PyQt5 import QtGui, QtWidgets |
---|
7 | from PyQt5 import QtCore |
---|
8 | from PyQt5.QtTest import QTest |
---|
9 | from PyQt5.QtCore import Qt |
---|
10 | from unittest.mock import MagicMock |
---|
11 | |
---|
12 | from twisted.internet import threads |
---|
13 | |
---|
14 | # from UnitTesting.TestUtils import QtSignalSpy |
---|
15 | # from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas |
---|
16 | from 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 | |
---|
24 | BG_COLOR_ERR = 'background-color: rgb(244, 170, 164);' |
---|
25 | |
---|
26 | BG_COLOR_WARNING = 'background-color: rgb(244, 217, 164);' |
---|
27 | |
---|
28 | |
---|
29 | if not QtWidgets.QApplication.instance(): |
---|
30 | app = QtWidgets.QApplication(sys.argv) |
---|
31 | |
---|
32 | |
---|
33 | class 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.onHelp() |
---|
243 | |
---|
244 | def testOnReset(self): |
---|
245 | """ Test onReset function""" |
---|
246 | # modify gui |
---|
247 | self.widget.txtQx.setText('33.0') |
---|
248 | self.widget.cbSource.setCurrentIndex(0) |
---|
249 | |
---|
250 | # check that GUI has been modified |
---|
251 | self.assertNotEqual(self.widget.cbSource.currentText(), 'Neutron') |
---|
252 | # apply reset |
---|
253 | QTest.mouseClick(self.widget.cmdReset, Qt.LeftButton) |
---|
254 | # check that we get back to the initial state |
---|
255 | self.assertEqual(self.widget.txtQx.text(), '0.0') |
---|
256 | self.assertEqual(self.widget.cbSource.currentText(), 'Neutron') |
---|
257 | |
---|
258 | def testOnClose(self): |
---|
259 | """ test Closing window """ |
---|
260 | closeButton = self.widget.cmdClose |
---|
261 | QTest.mouseClick(closeButton, Qt.LeftButton) |
---|
262 | |
---|
263 | def testOnCompute(self): |
---|
264 | """ """ |
---|
265 | threads.deferToThread = MagicMock() |
---|
266 | self.widget.onCompute() |
---|
267 | |
---|
268 | # thread called |
---|
269 | self.assertTrue(threads.deferToThread.called) |
---|
270 | self.assertEqual(threads.deferToThread.call_args_list[0][0][0].__name__, 'map_wrapper') |
---|
271 | |
---|
272 | # the Compute button changed caption and got disabled |
---|
273 | self.assertEqual(self.widget.cmdCompute.text(), 'Wait...') |
---|
274 | self.assertFalse(self.widget.cmdCompute.isEnabled()) |
---|
275 | |
---|
276 | |
---|
277 | if __name__ == "__main__": |
---|
278 | unittest.main() |
---|