1 | import sys |
---|
2 | import unittest |
---|
3 | |
---|
4 | from PyQt4 import QtCore |
---|
5 | from PyQt4 import QtGui |
---|
6 | |
---|
7 | # SV imports |
---|
8 | from sas.sascalc.dataloader.loader import Loader |
---|
9 | from sas.sasgui.guiframe.data_manager import DataManager |
---|
10 | |
---|
11 | # Tested module |
---|
12 | from GuiUtils import * |
---|
13 | |
---|
14 | class GuiUtilsTest(unittest.TestCase): |
---|
15 | '''Test the GUI Utilities methods''' |
---|
16 | def setUp(self): |
---|
17 | '''Empty''' |
---|
18 | pass |
---|
19 | |
---|
20 | def tearDown(self): |
---|
21 | '''Empty''' |
---|
22 | pass |
---|
23 | |
---|
24 | def testDefaults(self): |
---|
25 | """ |
---|
26 | Test all the global constants defined in the file. |
---|
27 | """ |
---|
28 | # Should probably test the constants in the file, |
---|
29 | # but this will done after trimming down GuiUtils |
---|
30 | # and retaining only necessary variables. |
---|
31 | pass |
---|
32 | |
---|
33 | def testGetAppDir(self): |
---|
34 | """ |
---|
35 | """ |
---|
36 | pass |
---|
37 | |
---|
38 | def testGetUserDirectory(self): |
---|
39 | """ |
---|
40 | Simple test of user directory getter |
---|
41 | """ |
---|
42 | home_dir = os.path.expanduser("~") |
---|
43 | self.assertIn(home_dir, get_user_directory()) |
---|
44 | |
---|
45 | def testCommunicate(self): |
---|
46 | """ |
---|
47 | Test the container class with signal definitions |
---|
48 | """ |
---|
49 | com = Communicate() |
---|
50 | |
---|
51 | # All defined signals |
---|
52 | list_of_signals = [ |
---|
53 | 'fileReadSignal', |
---|
54 | 'fileDataReceivedSignal', |
---|
55 | 'statusBarUpdateSignal', |
---|
56 | 'updatePerspectiveWithDataSignal', |
---|
57 | 'updateModelFromPerspectiveSignal' |
---|
58 | ] |
---|
59 | |
---|
60 | # Assure all signals are defined. |
---|
61 | for signal in list_of_signals: |
---|
62 | self.assertIn(signal, dir(com)) |
---|
63 | |
---|
64 | |
---|
65 | def testUpdateModelItem(self): |
---|
66 | """ |
---|
67 | Test the QModelItem update method |
---|
68 | """ |
---|
69 | test_item = QtGui.QStandardItem() |
---|
70 | test_list = ['aa','11'] |
---|
71 | update_data = QtCore.QVariant(test_list) |
---|
72 | name = "Black Sabbath" |
---|
73 | |
---|
74 | # update the item |
---|
75 | updateModelItem(test_item, update_data, name) |
---|
76 | |
---|
77 | # Make sure test_item got all data added |
---|
78 | self.assertEqual(test_item.child(0).text(), name) |
---|
79 | self.assertTrue(test_item.child(0).isCheckable()) |
---|
80 | list_from_item = test_item.child(0).child(0).data().toPyObject() |
---|
81 | self.assertIsInstance(list_from_item, list) |
---|
82 | self.assertEqual(str(list_from_item[0]), test_list[0]) |
---|
83 | self.assertEqual(str(list_from_item[1]), test_list[1]) |
---|
84 | |
---|
85 | |
---|
86 | def testPlotsFromCheckedItems(self): |
---|
87 | """ |
---|
88 | Test addition of a plottable to the model |
---|
89 | """ |
---|
90 | |
---|
91 | # Mockup data |
---|
92 | test_list0 = "FRIDAY" |
---|
93 | test_list1 = "SATURDAY" |
---|
94 | test_list2 = "MONDAY" |
---|
95 | |
---|
96 | # Main item ("file") |
---|
97 | checkbox_model = QtGui.QStandardItemModel() |
---|
98 | checkbox_item = QtGui.QStandardItem(True) |
---|
99 | checkbox_item.setCheckable(True) |
---|
100 | checkbox_item.setCheckState(QtCore.Qt.Checked) |
---|
101 | test_item0 = QtGui.QStandardItem() |
---|
102 | test_item0.setData(QtCore.QVariant(test_list0)) |
---|
103 | |
---|
104 | # Checked item 1 |
---|
105 | test_item1 = QtGui.QStandardItem(True) |
---|
106 | test_item1.setCheckable(True) |
---|
107 | test_item1.setCheckState(QtCore.Qt.Checked) |
---|
108 | object_item = QtGui.QStandardItem() |
---|
109 | object_item.setData(QtCore.QVariant(test_list1)) |
---|
110 | test_item1.setChild(0, object_item) |
---|
111 | |
---|
112 | checkbox_item.setChild(0, test_item0) |
---|
113 | checkbox_item.appendRow(test_item1) |
---|
114 | |
---|
115 | # Unchecked item 2 |
---|
116 | test_item2 = QtGui.QStandardItem(True) |
---|
117 | test_item2.setCheckable(True) |
---|
118 | test_item2.setCheckState(QtCore.Qt.Unchecked) |
---|
119 | object_item = QtGui.QStandardItem() |
---|
120 | object_item.setData(QtCore.QVariant(test_list2)) |
---|
121 | test_item2.setChild(0, object_item) |
---|
122 | checkbox_item.appendRow(test_item2) |
---|
123 | |
---|
124 | checkbox_model.appendRow(checkbox_item) |
---|
125 | |
---|
126 | # Pull out the "plottable" documents |
---|
127 | plot_list = plotsFromCheckedItems(checkbox_model) |
---|
128 | |
---|
129 | # Make sure only the checked data is present |
---|
130 | # FRIDAY IN |
---|
131 | self.assertIn(test_list0, plot_list) |
---|
132 | # SATURDAY IN |
---|
133 | self.assertIn(test_list1, plot_list) |
---|
134 | # MONDAY NOT IN |
---|
135 | self.assertNotIn(test_list2, plot_list) |
---|
136 | |
---|
137 | def testInfoFromData(self): |
---|
138 | """ |
---|
139 | Test Info element extraction from a plottable object |
---|
140 | """ |
---|
141 | loader = Loader() |
---|
142 | manager = DataManager() |
---|
143 | |
---|
144 | # get Data1D |
---|
145 | p_file="cyl_400_20.txt" |
---|
146 | output_object = loader.load(p_file) |
---|
147 | new_data = manager.create_gui_data(output_object, p_file) |
---|
148 | |
---|
149 | # Extract Info elements into a model item |
---|
150 | item = infoFromData(new_data) |
---|
151 | |
---|
152 | # Test the item and its children |
---|
153 | self.assertIsInstance(item, QtGui.QStandardItem) |
---|
154 | self.assertEqual(item.rowCount(), 5) |
---|
155 | self.assertEqual(item.text(), "Info") |
---|
156 | self.assertIn(p_file, item.child(0).text()) |
---|
157 | self.assertIn("Run", item.child(1).text()) |
---|
158 | self.assertIn("Data1D", item.child(2).text()) |
---|
159 | self.assertIn(p_file, item.child(3).text()) |
---|
160 | self.assertIn("Process",item.child(4).text()) |
---|
161 | |
---|
162 | |
---|
163 | if __name__ == "__main__": |
---|
164 | unittest.main() |
---|
165 | |
---|