| 1 | import unittest |
|---|
| 2 | |
|---|
| 3 | import time |
|---|
| 4 | |
|---|
| 5 | import lib |
|---|
| 6 | lib.setup_env() |
|---|
| 7 | |
|---|
| 8 | from data import * |
|---|
| 9 | |
|---|
| 10 | from mock import Mock, expectParams |
|---|
| 11 | from mocks import * |
|---|
| 12 | |
|---|
| 13 | from common import gajim |
|---|
| 14 | from common import zeroconf |
|---|
| 15 | import roster_window |
|---|
| 16 | |
|---|
| 17 | gajim.get_jid_from_account = lambda acc: 'myjid@' + acc |
|---|
| 18 | |
|---|
| 19 | class TestRosterWindow(unittest.TestCase): |
|---|
| 20 | |
|---|
| 21 | def setUp(self): |
|---|
| 22 | gajim.interface = MockInterface() |
|---|
| 23 | self.roster = roster_window.RosterWindow() |
|---|
| 24 | |
|---|
| 25 | self.C_NAME = roster_window.C_NAME |
|---|
| 26 | self.C_TYPE = roster_window.C_TYPE |
|---|
| 27 | self.C_JID = roster_window.C_JID |
|---|
| 28 | self.C_ACCOUNT = roster_window.C_ACCOUNT |
|---|
| 29 | |
|---|
| 30 | # Add after creating RosterWindow |
|---|
| 31 | # We want to test the filling explicitly |
|---|
| 32 | for acc in contacts: |
|---|
| 33 | gajim.connections[acc] = MockConnection(acc) |
|---|
| 34 | |
|---|
| 35 | def tearDown(self): |
|---|
| 36 | self.roster.model.clear() |
|---|
| 37 | for acc in gajim.contacts.get_accounts(): |
|---|
| 38 | gajim.contacts.clear_contacts(acc) |
|---|
| 39 | |
|---|
| 40 | ### Custom assertions |
|---|
| 41 | def assert_all_contacts_are_in_roster(self, acc): |
|---|
| 42 | for jid in contacts[acc]: |
|---|
| 43 | self.assert_contact_is_in_roster(jid, acc) |
|---|
| 44 | |
|---|
| 45 | def assert_contact_is_in_roster(self, jid, account): |
|---|
| 46 | contacts = gajim.contacts.get_contacts(account, jid) |
|---|
| 47 | # check for all resources |
|---|
| 48 | for contact in contacts: |
|---|
| 49 | iters = self.roster._get_contact_iter(jid, account, |
|---|
| 50 | model=self.roster.model) |
|---|
| 51 | |
|---|
| 52 | if jid != gajim.get_jid_from_account(account): |
|---|
| 53 | # We don't care for groups of SelfContact |
|---|
| 54 | self.assertTrue(len(iters) == len(contact.get_shown_groups()), |
|---|
| 55 | msg='Contact is not in all his groups') |
|---|
| 56 | |
|---|
| 57 | # Are we big brother? |
|---|
| 58 | bb_jid = None |
|---|
| 59 | bb_account = None |
|---|
| 60 | family = gajim.contacts.get_metacontacts_family(account, jid) |
|---|
| 61 | if family: |
|---|
| 62 | nearby_family, bb_jid, bb_account = \ |
|---|
| 63 | self.roster._get_nearby_family_and_big_brother(family, account) |
|---|
| 64 | |
|---|
| 65 | is_in_nearby_family = (jid, account) in ( |
|---|
| 66 | (data['jid'], data['account']) for data in nearby_family) |
|---|
| 67 | self.assertTrue(is_in_nearby_family, |
|---|
| 68 | msg='Contact not in his own nearby family') |
|---|
| 69 | |
|---|
| 70 | is_big_brother = (bb_jid, bb_account) == (jid, account) |
|---|
| 71 | |
|---|
| 72 | # check for each group tag |
|---|
| 73 | for titerC in iters: |
|---|
| 74 | self.assertTrue(self.roster.model.iter_is_valid(titerC), |
|---|
| 75 | msg='Contact iter invalid') |
|---|
| 76 | |
|---|
| 77 | c_model = self.roster.model[titerC] |
|---|
| 78 | self.assertEquals(contact.get_shown_name(), c_model[self.C_NAME], |
|---|
| 79 | msg='Contact name missmatch') |
|---|
| 80 | self.assertEquals(contact.jid, c_model[self.C_JID], |
|---|
| 81 | msg='Jid missmatch') |
|---|
| 82 | |
|---|
| 83 | if not self.roster.regroup: |
|---|
| 84 | self.assertEquals(account, c_model[self.C_ACCOUNT], |
|---|
| 85 | msg='Account missmatch') |
|---|
| 86 | |
|---|
| 87 | # Check for correct nesting |
|---|
| 88 | parent_iter = self.roster.model.iter_parent(titerC) |
|---|
| 89 | p_model = self.roster.model[parent_iter] |
|---|
| 90 | if family: |
|---|
| 91 | if is_big_brother: |
|---|
| 92 | self.assertTrue(p_model[self.C_TYPE] == 'group', |
|---|
| 93 | msg='Big Brother is not on top') |
|---|
| 94 | else: |
|---|
| 95 | self.assertTrue(p_model[self.C_TYPE] == 'contact', |
|---|
| 96 | msg='Little Brother brother has no BigB') |
|---|
| 97 | else: |
|---|
| 98 | if jid == gajim.get_jid_from_account(account): |
|---|
| 99 | self.assertTrue(p_model[self.C_TYPE] == 'account', |
|---|
| 100 | msg='SelfContact is not on top') |
|---|
| 101 | else: |
|---|
| 102 | self.assertTrue(p_model[self.C_TYPE] == 'group', |
|---|
| 103 | msg='Contact not found in a group') |
|---|
| 104 | |
|---|
| 105 | def assert_group_is_in_roster(self, group, account): |
|---|
| 106 | #TODO |
|---|
| 107 | pass |
|---|
| 108 | |
|---|
| 109 | def assert_account_is_in_roster(self, acc): |
|---|
| 110 | titerA = self.roster._get_account_iter(acc, model=self.roster.model) |
|---|
| 111 | self.assertTrue(self.roster.model.iter_is_valid(titerA), |
|---|
| 112 | msg='Account iter is invalid') |
|---|
| 113 | |
|---|
| 114 | acc_model = self.roster.model[titerA] |
|---|
| 115 | self.assertEquals(acc_model[self.C_TYPE], 'account', |
|---|
| 116 | msg='No account found') |
|---|
| 117 | |
|---|
| 118 | if not self.roster.regroup: |
|---|
| 119 | self.assertEquals(acc_model[self.C_ACCOUNT], acc, |
|---|
| 120 | msg='Account not found') |
|---|
| 121 | |
|---|
| 122 | self_jid = gajim.get_jid_from_account(acc) |
|---|
| 123 | self.assertEquals(acc_model[self.C_JID], self_jid, |
|---|
| 124 | msg='Account JID not found in account row') |
|---|
| 125 | |
|---|
| 126 | def assert_model_is_in_sync(self): |
|---|
| 127 | #TODO: check that iter_n_children returns the correct numbers |
|---|
| 128 | pass |
|---|
| 129 | |
|---|
| 130 | # tests |
|---|
| 131 | def test_fill_contacts_and_groups_dicts(self): |
|---|
| 132 | for acc in contacts: |
|---|
| 133 | self.roster.fill_contacts_and_groups_dicts(contacts[acc], acc) |
|---|
| 134 | |
|---|
| 135 | for jid in contacts[acc]: |
|---|
| 136 | instances = gajim.contacts.get_contacts(acc, jid) |
|---|
| 137 | |
|---|
| 138 | # Created a contact for each single jid? |
|---|
| 139 | self.assertTrue(len(instances) == 1) |
|---|
| 140 | |
|---|
| 141 | # Contacts kept their info |
|---|
| 142 | contact = instances[0] |
|---|
| 143 | self.assertEquals(contact.groups, contacts[acc][jid]['groups'], |
|---|
| 144 | msg='Group Missmatch') |
|---|
| 145 | |
|---|
| 146 | groups = contacts[acc][jid]['groups'] or ['General',] |
|---|
| 147 | |
|---|
| 148 | # cleanup |
|---|
| 149 | self.roster.model.clear() |
|---|
| 150 | for acc in contacts: |
|---|
| 151 | gajim.contacts.clear_contacts(acc) |
|---|
| 152 | |
|---|
| 153 | def test_fill_roster_model(self): |
|---|
| 154 | for acc in contacts: |
|---|
| 155 | self.roster.fill_contacts_and_groups_dicts(contacts[acc], acc) |
|---|
| 156 | |
|---|
| 157 | self.roster.add_account(acc) |
|---|
| 158 | self.assert_account_is_in_roster(acc) |
|---|
| 159 | |
|---|
| 160 | self.roster.add_account_contacts(acc) |
|---|
| 161 | self.assert_all_contacts_are_in_roster(acc) |
|---|
| 162 | |
|---|
| 163 | self.assert_model_is_in_sync() |
|---|
| 164 | |
|---|
| 165 | |
|---|
| 166 | class TestRosterWindowRegrouped(TestRosterWindow): |
|---|
| 167 | |
|---|
| 168 | def setUp(self): |
|---|
| 169 | gajim.config.set('mergeaccounts', True) |
|---|
| 170 | TestRosterWindow.setUp(self) |
|---|
| 171 | |
|---|
| 172 | def test_toggle_regroup(self): |
|---|
| 173 | self.roster.regroup = not self.roster.regroup |
|---|
| 174 | self.roster.setup_and_draw_roster() |
|---|
| 175 | self.roster.regroup = not self.roster.regroup |
|---|
| 176 | self.roster.setup_and_draw_roster() |
|---|
| 177 | |
|---|
| 178 | |
|---|
| 179 | class TestRosterWindowMetaContacts(TestRosterWindowRegrouped): |
|---|
| 180 | |
|---|
| 181 | def test_receive_metacontact_data(self): |
|---|
| 182 | for complete_data in metacontact_data: |
|---|
| 183 | t_acc = complete_data[0]['account'] |
|---|
| 184 | t_jid = complete_data[0]['jid'] |
|---|
| 185 | data = complete_data[1:] |
|---|
| 186 | for brother in data: |
|---|
| 187 | acc = brother['account'] |
|---|
| 188 | jid = brother['jid'] |
|---|
| 189 | gajim.contacts.add_metacontact(t_acc, t_jid, acc, jid) |
|---|
| 190 | self.roster.setup_and_draw_roster() |
|---|
| 191 | |
|---|
| 192 | def test_connect_new_metacontact(self): |
|---|
| 193 | self.test_fill_roster_model() |
|---|
| 194 | |
|---|
| 195 | jid = u'coolstuff@gajim.org' |
|---|
| 196 | contact = gajim.contacts.create_contact(jid) |
|---|
| 197 | gajim.contacts.add_contact(account1, contact) |
|---|
| 198 | self.roster.add_contact(jid, account1) |
|---|
| 199 | self.roster.chg_contact_status(contact, 'offline', '', account1) |
|---|
| 200 | |
|---|
| 201 | gajim.contacts.add_metacontact(account1, u'samejid@gajim.org', |
|---|
| 202 | account1, jid) |
|---|
| 203 | self.roster.chg_contact_status(contact, 'online', '', account1) |
|---|
| 204 | |
|---|
| 205 | self.assert_model_is_in_sync() |
|---|
| 206 | |
|---|
| 207 | |
|---|
| 208 | |
|---|
| 209 | if __name__ == '__main__': |
|---|
| 210 | unittest.main() |
|---|
| 211 | |
|---|
| 212 | # vim: se ts=3: |
|---|