1 | import sys |
---|
2 | import unittest |
---|
3 | #from twisted.trial import unittest |
---|
4 | #from twisted.internet import reactor, defer, interfaces, threads, protocol, error |
---|
5 | |
---|
6 | from PyQt4.QtGui import * |
---|
7 | from PyQt4.QtTest import QTest |
---|
8 | from PyQt4.QtCore import * |
---|
9 | from mock import MagicMock |
---|
10 | |
---|
11 | # Local |
---|
12 | from sas.sasgui.guiframe.dataFitting import Data1D |
---|
13 | from DataExplorer import DataExplorerWindow |
---|
14 | from GuiManager import GuiManager |
---|
15 | from GuiUtils import * |
---|
16 | from UnitTesting.TestUtils import QtSignalSpy |
---|
17 | |
---|
18 | app = QApplication(sys.argv) |
---|
19 | |
---|
20 | class DataExplorerTest(unittest.TestCase): |
---|
21 | '''Test the Data Explorer GUI''' |
---|
22 | def setUp(self): |
---|
23 | '''Create the GUI''' |
---|
24 | class MyPerspective(object): |
---|
25 | def communicator(self): |
---|
26 | return Communicate() |
---|
27 | def allowBatch(self): |
---|
28 | return False |
---|
29 | def setData(self, data_list=None): |
---|
30 | return None |
---|
31 | def title(self): |
---|
32 | return "Dummy Perspective" |
---|
33 | |
---|
34 | class dummy_manager(object): |
---|
35 | def communicator(self): |
---|
36 | return Communicate() |
---|
37 | def perspective(self): |
---|
38 | return MyPerspective() |
---|
39 | |
---|
40 | self.form = DataExplorerWindow(None, dummy_manager()) |
---|
41 | |
---|
42 | def tearDown(self): |
---|
43 | '''Destroy the GUI''' |
---|
44 | self.form.close() |
---|
45 | self.form = None |
---|
46 | |
---|
47 | def testDefaults(self): |
---|
48 | '''Test the GUI in its default state''' |
---|
49 | self.assertIsInstance(self.form, QTabWidget) |
---|
50 | self.assertIsInstance(self.form.treeView, QTreeView) |
---|
51 | self.assertIsInstance(self.form.listView, QListView) |
---|
52 | self.assertEqual(self.form.count(), 2) |
---|
53 | |
---|
54 | self.assertEqual(self.form.cmdLoad.text(), "Load") |
---|
55 | self.assertEqual(self.form.cmdDelete.text(), "Delete") |
---|
56 | self.assertEqual(self.form.cmdSendTo.text(), "Send to") |
---|
57 | self.assertEqual(self.form.chkBatch.text(), "Batch mode") |
---|
58 | self.assertFalse(self.form.chkBatch.isChecked()) |
---|
59 | |
---|
60 | self.assertEqual(self.form.cbSelect.count(), 6) |
---|
61 | self.assertEqual(self.form.cbSelect.currentIndex(), 0) |
---|
62 | |
---|
63 | # Class is in the default state even without pressing OK |
---|
64 | self.assertEqual(self.form.treeView.model().rowCount(), 0) |
---|
65 | self.assertEqual(self.form.treeView.model().columnCount(), 0) |
---|
66 | self.assertEqual(self.form.model.rowCount(), 0) |
---|
67 | self.assertEqual(self.form.model.columnCount(), 0) |
---|
68 | |
---|
69 | def testLoadButton(self): |
---|
70 | loadButton = self.form.cmdLoad |
---|
71 | |
---|
72 | # Mock the system file open method |
---|
73 | QtGui.QFileDialog.getOpenFileName = MagicMock(return_value=None) |
---|
74 | |
---|
75 | # Click on the Load button |
---|
76 | QTest.mouseClick(loadButton, Qt.LeftButton) |
---|
77 | |
---|
78 | # Test the getOpenFileName() dialog called once |
---|
79 | self.assertTrue(QtGui.QFileDialog.getOpenFileName.called) |
---|
80 | QtGui.QFileDialog.getOpenFileName.assert_called_once() |
---|
81 | |
---|
82 | def testDeleteButton(self): |
---|
83 | """ |
---|
84 | Functionality of the delete button |
---|
85 | """ |
---|
86 | |
---|
87 | deleteButton = self.form.cmdDelete |
---|
88 | |
---|
89 | # Mock the confirmation dialog with return=Yes |
---|
90 | QtGui.QMessageBox.question = MagicMock(return_value=QtGui.QMessageBox.No) |
---|
91 | |
---|
92 | # Populate the model |
---|
93 | filename = ["cyl_400_20.txt", "Dec07031.ASC", "cyl_400_20.txt"] |
---|
94 | self.form.readData(filename) |
---|
95 | |
---|
96 | # Assure the model contains three items |
---|
97 | self.assertEqual(self.form.model.rowCount(), 3) |
---|
98 | |
---|
99 | # Assure the checkboxes are on |
---|
100 | item1 = self.form.model.item(0) |
---|
101 | item2 = self.form.model.item(1) |
---|
102 | item3 = self.form.model.item(2) |
---|
103 | self.assertTrue(item1.checkState() == QtCore.Qt.Checked) |
---|
104 | self.assertTrue(item2.checkState() == QtCore.Qt.Checked) |
---|
105 | self.assertTrue(item3.checkState() == QtCore.Qt.Checked) |
---|
106 | |
---|
107 | # Click on the delete button |
---|
108 | QTest.mouseClick(deleteButton, Qt.LeftButton) |
---|
109 | |
---|
110 | # Test the warning dialog called once |
---|
111 | self.assertTrue(QtGui.QMessageBox.question.called) |
---|
112 | |
---|
113 | # Assure the model still contains the items |
---|
114 | self.assertEqual(self.form.model.rowCount(), 3) |
---|
115 | |
---|
116 | # Now, mock the confirmation dialog with return=Yes |
---|
117 | QtGui.QMessageBox.question = MagicMock(return_value=QtGui.QMessageBox.Yes) |
---|
118 | |
---|
119 | # Click on the delete button |
---|
120 | QTest.mouseClick(deleteButton, Qt.LeftButton) |
---|
121 | |
---|
122 | # Test the warning dialog called once |
---|
123 | self.assertTrue(QtGui.QMessageBox.question.called) |
---|
124 | |
---|
125 | # Assure the model contains no items |
---|
126 | self.assertEqual(self.form.model.rowCount(), 0) |
---|
127 | |
---|
128 | # Click delete once again to assure no nasty behaviour on empty model |
---|
129 | QTest.mouseClick(deleteButton, Qt.LeftButton) |
---|
130 | |
---|
131 | |
---|
132 | def testSendToButton(self): |
---|
133 | """ |
---|
134 | Test that clicking the Send To button sends checked data to a perspective |
---|
135 | """ |
---|
136 | |
---|
137 | # Populate the model |
---|
138 | filename = ["cyl_400_20.txt"] |
---|
139 | self.form.readData(filename) |
---|
140 | |
---|
141 | # setData is the method we want to see called |
---|
142 | mocked = self.form.parent.perspective().setData |
---|
143 | mocked = MagicMock(filename) |
---|
144 | |
---|
145 | # Assure the checkbox is on |
---|
146 | self.form.cbSelect.setCurrentIndex(0) |
---|
147 | |
---|
148 | # Click on the Send To button |
---|
149 | QTest.mouseClick(self.form.cmdSendTo, Qt.LeftButton) |
---|
150 | |
---|
151 | # Test the set_data method called once |
---|
152 | # self.assertTrue(mocked.called) |
---|
153 | |
---|
154 | # open another file |
---|
155 | filename = ["cyl_400_20.txt"] |
---|
156 | self.form.readData(filename) |
---|
157 | |
---|
158 | # Mock the warning message |
---|
159 | QtGui.QMessageBox = MagicMock() |
---|
160 | |
---|
161 | # Click on the button |
---|
162 | QTest.mouseClick(self.form.cmdSendTo, Qt.LeftButton) |
---|
163 | |
---|
164 | # Assure the message box popped up |
---|
165 | QtGui.QMessageBox.assert_called_once() |
---|
166 | |
---|
167 | |
---|
168 | def testDataSelection(self): |
---|
169 | """ |
---|
170 | Tests the functionality of the Selection Option combobox |
---|
171 | """ |
---|
172 | # Populate the model with 1d and 2d data |
---|
173 | filename = ["cyl_400_20.txt", "Dec07031.ASC"] |
---|
174 | self.form.readData(filename) |
---|
175 | |
---|
176 | # Unselect all data |
---|
177 | self.form.cbSelect.setCurrentIndex(1) |
---|
178 | |
---|
179 | # Test the current selection |
---|
180 | item1D = self.form.model.item(0) |
---|
181 | item2D = self.form.model.item(1) |
---|
182 | self.assertTrue(item1D.checkState() == QtCore.Qt.Unchecked) |
---|
183 | self.assertTrue(item2D.checkState() == QtCore.Qt.Unchecked) |
---|
184 | |
---|
185 | # Select all data |
---|
186 | self.form.cbSelect.setCurrentIndex(0) |
---|
187 | |
---|
188 | # Test the current selection |
---|
189 | self.assertTrue(item1D.checkState() == QtCore.Qt.Checked) |
---|
190 | self.assertTrue(item2D.checkState() == QtCore.Qt.Checked) |
---|
191 | |
---|
192 | # select 1d data |
---|
193 | self.form.cbSelect.setCurrentIndex(2) |
---|
194 | |
---|
195 | # Test the current selection |
---|
196 | self.assertTrue(item1D.checkState() == QtCore.Qt.Checked) |
---|
197 | self.assertTrue(item2D.checkState() == QtCore.Qt.Unchecked) |
---|
198 | |
---|
199 | # unselect 1d data |
---|
200 | self.form.cbSelect.setCurrentIndex(3) |
---|
201 | |
---|
202 | # Test the current selection |
---|
203 | self.assertTrue(item1D.checkState() == QtCore.Qt.Unchecked) |
---|
204 | self.assertTrue(item2D.checkState() == QtCore.Qt.Unchecked) |
---|
205 | |
---|
206 | # select 2d data |
---|
207 | self.form.cbSelect.setCurrentIndex(4) |
---|
208 | |
---|
209 | # Test the current selection |
---|
210 | self.assertTrue(item1D.checkState() == QtCore.Qt.Unchecked) |
---|
211 | self.assertTrue(item2D.checkState() == QtCore.Qt.Checked) |
---|
212 | |
---|
213 | # unselect 2d data |
---|
214 | self.form.cbSelect.setCurrentIndex(5) |
---|
215 | |
---|
216 | # Test the current selection |
---|
217 | self.assertTrue(item1D.checkState() == QtCore.Qt.Unchecked) |
---|
218 | self.assertTrue(item2D.checkState() == QtCore.Qt.Unchecked) |
---|
219 | |
---|
220 | # choose impossible index and assure the code raises |
---|
221 | #with self.assertRaises(Exception): |
---|
222 | # self.form.cbSelect.setCurrentIndex(6) |
---|
223 | |
---|
224 | def testReadData(self): |
---|
225 | """ |
---|
226 | Test the low level readData() method |
---|
227 | """ |
---|
228 | filename = ["cyl_400_20.txt"] |
---|
229 | self.form.manager.add_data = MagicMock() |
---|
230 | |
---|
231 | # Initialize signal spy instances |
---|
232 | spy_status_update = QtSignalSpy(self.form, self.form.communicate.statusBarUpdateSignal) |
---|
233 | spy_data_received = QtSignalSpy(self.form, self.form.communicate.fileDataReceivedSignal) |
---|
234 | |
---|
235 | # Read in the file |
---|
236 | self.form.readData(filename) |
---|
237 | |
---|
238 | # Expected two status bar updates |
---|
239 | self.assertEqual(spy_status_update.count(), 1) |
---|
240 | self.assertIn(filename[0], str(spy_status_update.called()[0]['args'][0])) |
---|
241 | |
---|
242 | |
---|
243 | # Check that the model contains the item |
---|
244 | self.assertEqual(self.form.model.rowCount(), 1) |
---|
245 | self.assertEqual(self.form.model.columnCount(), 1) |
---|
246 | |
---|
247 | # The 0th item header should be the name of the file |
---|
248 | model_item = self.form.model.index(0,0) |
---|
249 | model_name = str(self.form.model.data(model_item).toString()) |
---|
250 | self.assertEqual(model_name, filename[0]) |
---|
251 | |
---|
252 | def testLoadFile(self): |
---|
253 | """ |
---|
254 | Test the threaded call to readData() |
---|
255 | """ |
---|
256 | pass |
---|
257 | |
---|
258 | def testGetWList(self): |
---|
259 | """ |
---|
260 | """ |
---|
261 | list = self.form.getWlist() |
---|
262 | defaults = 'All (*.*);;canSAS files (*.xml);;SESANS files' +\ |
---|
263 | ' (*.ses);;ASCII files (*.txt);;IGOR 2D files (*.asc);;' +\ |
---|
264 | 'IGOR/DAT 2D Q_map files (*.dat);;IGOR 1D files (*.abs);;'+\ |
---|
265 | 'HFIR 1D files (*.d1d);;DANSE files (*.sans);;NXS files (*.nxs)' |
---|
266 | self.assertEqual(defaults, list) |
---|
267 | |
---|
268 | def testLoadComplete(self): |
---|
269 | """ |
---|
270 | Test the callback method updating the data object |
---|
271 | """ |
---|
272 | |
---|
273 | data_dict = {"a1":Data1D()} |
---|
274 | |
---|
275 | self.form.manager.add_data = MagicMock() |
---|
276 | |
---|
277 | # Initialize signal spy instances |
---|
278 | spy_status_update = QtSignalSpy(self.form, self.form.communicate.statusBarUpdateSignal) |
---|
279 | spy_data_received = QtSignalSpy(self.form, self.form.communicate.fileDataReceivedSignal) |
---|
280 | |
---|
281 | # Read in the file |
---|
282 | self.form.loadComplete(data_dict, message="Loading Data Complete") |
---|
283 | |
---|
284 | # "Loading data complete" no longer sent in LoadFile but in callback |
---|
285 | self.assertIn("Loading Data Complete", str(spy_status_update.called()[0]['args'][0])) |
---|
286 | |
---|
287 | # Expect one Data Received signal |
---|
288 | self.assertEqual(spy_data_received.count(), 1) |
---|
289 | |
---|
290 | # Assure returned dictionary has correct data |
---|
291 | # We don't know the data ID, so need to iterate over dict |
---|
292 | data_dict = spy_data_received.called()[0]['args'][0] |
---|
293 | for data_key, data_value in data_dict.iteritems(): |
---|
294 | self.assertIsInstance(data_value, Data1D) |
---|
295 | |
---|
296 | # Assure add_data on data_manager was called (last call) |
---|
297 | self.assertTrue(self.form.manager.add_data.called) |
---|
298 | |
---|
299 | |
---|
300 | if __name__ == "__main__": |
---|
301 | unittest.main() |
---|