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