1 | import sys |
---|
2 | import unittest |
---|
3 | |
---|
4 | from PyQt4.QtGui import * |
---|
5 | from PyQt4.QtTest import QTest |
---|
6 | from PyQt4.QtCore import * |
---|
7 | from mock import MagicMock |
---|
8 | |
---|
9 | # Local |
---|
10 | from sas.sasgui.guiframe.dataFitting import Data1D |
---|
11 | from DataExplorer import DataExplorerWindow |
---|
12 | from GuiManager import GuiManager |
---|
13 | from GuiUtils import * |
---|
14 | from UnitTesting.TestUtils import QtSignalSpy |
---|
15 | |
---|
16 | app = QApplication(sys.argv) |
---|
17 | |
---|
18 | class DataExplorerTest(unittest.TestCase): |
---|
19 | '''Test the Data Explorer GUI''' |
---|
20 | def setUp(self): |
---|
21 | '''Create the GUI''' |
---|
22 | class dummy_manager(object): |
---|
23 | def communicator(self): |
---|
24 | return Communicate() |
---|
25 | |
---|
26 | self.form = DataExplorerWindow(None, dummy_manager()) |
---|
27 | |
---|
28 | def tearDown(self): |
---|
29 | '''Destroy the GUI''' |
---|
30 | self.form.close() |
---|
31 | self.form = None |
---|
32 | |
---|
33 | def testDefaults(self): |
---|
34 | '''Test the GUI in its default state''' |
---|
35 | self.assertIsInstance(self.form, QTabWidget) |
---|
36 | self.assertIsInstance(self.form.treeView, QTreeView) |
---|
37 | self.assertIsInstance(self.form.listView, QListView) |
---|
38 | self.assertEqual(self.form.count(), 2) |
---|
39 | |
---|
40 | self.assertEqual(self.form.cmdLoad.text(), "Load") |
---|
41 | self.assertEqual(self.form.cmdDelete.text(), "Delete") |
---|
42 | self.assertEqual(self.form.cmdSendTo.text(), "Send to") |
---|
43 | self.assertEqual(self.form.chkBatch.text(), "Batch mode") |
---|
44 | self.assertFalse(self.form.chkBatch.isChecked()) |
---|
45 | |
---|
46 | self.assertEqual(self.form.cbSelect.count(), 6) |
---|
47 | |
---|
48 | # Class is in the default state even without pressing OK |
---|
49 | self.assertEqual(self.form.treeView.model().rowCount(), 0) |
---|
50 | self.assertEqual(self.form.treeView.model().columnCount(), 0) |
---|
51 | self.assertEqual(self.form.model.rowCount(), 0) |
---|
52 | self.assertEqual(self.form.model.columnCount(), 0) |
---|
53 | |
---|
54 | def testLoadButton(self): |
---|
55 | loadButton = self.form.cmdLoad |
---|
56 | |
---|
57 | # Mock the system file open method |
---|
58 | QtGui.QFileDialog.getOpenFileName = MagicMock(return_value=None) |
---|
59 | |
---|
60 | # Click on the Load button |
---|
61 | QTest.mouseClick(loadButton, Qt.LeftButton) |
---|
62 | |
---|
63 | # Test the getOpenFileName() dialog called once |
---|
64 | self.assertTrue(QtGui.QFileDialog.getOpenFileName.called) |
---|
65 | QtGui.QFileDialog.getOpenFileName.assert_called_once() |
---|
66 | |
---|
67 | def testDeleteButton(self): |
---|
68 | |
---|
69 | deleteButton = self.form.cmdDelete |
---|
70 | |
---|
71 | # Mock the confirmation dialog with return=Yes |
---|
72 | |
---|
73 | # Populate the model |
---|
74 | |
---|
75 | # Assure the checkbox is on |
---|
76 | |
---|
77 | # Click on the delete button |
---|
78 | QTest.mouseClick(deleteButton, Qt.LeftButton) |
---|
79 | |
---|
80 | # Test the warning dialog called once |
---|
81 | # self.assertTrue(QtGui.QFileDialog.getOpenFileName.called) |
---|
82 | |
---|
83 | # Assure the model contains no items |
---|
84 | |
---|
85 | def testSendToButton(self): |
---|
86 | sendToButton = self.form.cmdSendTo |
---|
87 | |
---|
88 | # Mock the current perspective set_data method |
---|
89 | |
---|
90 | # Populate the model |
---|
91 | |
---|
92 | # Assure the checkbox is on |
---|
93 | |
---|
94 | # Click on the Send To button |
---|
95 | QTest.mouseClick(sendToButton, Qt.LeftButton) |
---|
96 | |
---|
97 | # Test the set_data method called once |
---|
98 | # self.assertTrue(QtGui.QFileDialog.getOpenFileName.called) |
---|
99 | |
---|
100 | # Assure the model contains no items |
---|
101 | |
---|
102 | def testReadData(self): |
---|
103 | """ |
---|
104 | Test the readData() method |
---|
105 | """ |
---|
106 | filename = ["cyl_400_20.txt"] |
---|
107 | self.form.manager.add_data = MagicMock() |
---|
108 | |
---|
109 | # Initialize signal spy instances |
---|
110 | spy_status_update = QtSignalSpy(self.form, self.form.communicate.statusBarUpdateSignal) |
---|
111 | spy_data_received = QtSignalSpy(self.form, self.form.communicate.fileDataReceivedSignal) |
---|
112 | |
---|
113 | # Read in the file |
---|
114 | self.form.readData(filename) |
---|
115 | |
---|
116 | # Expected two status bar updates |
---|
117 | self.assertEqual(spy_status_update.count(), 2) |
---|
118 | self.assertIn(filename[0], str(spy_status_update.called()[0]['args'][0])) |
---|
119 | self.assertIn("Loading Data Complete", str(spy_status_update.called()[1]['args'][0])) |
---|
120 | |
---|
121 | # Expect one Data Received signal |
---|
122 | self.assertEqual(spy_data_received.count(), 1) |
---|
123 | |
---|
124 | # Assure returned dictionary has correct data |
---|
125 | # We don't know the data ID, so need to iterate over dict |
---|
126 | data_dict = spy_data_received.called()[0]['args'][0] |
---|
127 | for data_key, data_value in data_dict.iteritems(): |
---|
128 | self.assertIsInstance(data_value, Data1D) |
---|
129 | |
---|
130 | # Check that the model contains the item |
---|
131 | self.assertEqual(self.form.model.rowCount(), 1) |
---|
132 | self.assertEqual(self.form.model.columnCount(), 1) |
---|
133 | |
---|
134 | # The 0th item header should be the name of the file |
---|
135 | model_item = self.form.model.index(0,0) |
---|
136 | model_name = str(self.form.model.data(model_item).toString()) |
---|
137 | self.assertEqual(model_name, filename[0]) |
---|
138 | |
---|
139 | # Assure add_data on data_manager was called (last call) |
---|
140 | self.assertTrue(self.form.manager.add_data.called) |
---|
141 | |
---|
142 | def testGetWList(self): |
---|
143 | """ |
---|
144 | """ |
---|
145 | list = self.form.getWlist() |
---|
146 | defaults = 'All (*.*);;canSAS files (*.xml);;SESANS files' +\ |
---|
147 | ' (*.ses);;ASCII files (*.txt);;IGOR 2D files (*.asc);;' +\ |
---|
148 | 'IGOR/DAT 2D Q_map files (*.dat);;IGOR 1D files (*.abs);;'+\ |
---|
149 | 'HFIR 1D files (*.d1d);;DANSE files (*.sans);;NXS files (*.nxs)' |
---|
150 | self.assertEqual(defaults, list) |
---|
151 | |
---|
152 | def testLoadComplete(self): |
---|
153 | """ |
---|
154 | """ |
---|
155 | # Initialize signal spy instances |
---|
156 | spy_status_update = QtSignalSpy(self.form, self.form.communicate.statusBarUpdateSignal) |
---|
157 | spy_data_received = QtSignalSpy(self.form, self.form.communicate.fileDataReceivedSignal) |
---|
158 | |
---|
159 | # Need an empty Data1D object |
---|
160 | mockData = Data1D() |
---|
161 | |
---|
162 | # Call the tested method |
---|
163 | self.form.loadComplete({"a":mockData}, message="test message") |
---|
164 | |
---|
165 | # test the signals |
---|
166 | self.assertEqual(spy_status_update.count(), 1) |
---|
167 | self.assertIn("message", str(spy_status_update.called()[0]['args'][0])) |
---|
168 | self.assertEqual(spy_data_received.count(), 1) |
---|
169 | |
---|
170 | # The data_manager update is going away, so don't bother testing |
---|
171 | |
---|
172 | if __name__ == "__main__": |
---|
173 | unittest.main() |
---|