8 from mock
import Mock
, expectParams
9 from gajim_mocks
import *
11 from common
import gajim
12 from common
import contacts
as contacts_module
15 gajim
.get_jid_from_account
= lambda acc
: 'myjid@' + acc
18 class TestRosterWindow(unittest
.TestCase
):
21 gajim
.interface
= MockInterface()
23 self
.C_NAME
= roster_window
.C_NAME
24 self
.C_TYPE
= roster_window
.C_TYPE
25 self
.C_JID
= roster_window
.C_JID
26 self
.C_ACCOUNT
= roster_window
.C_ACCOUNT
28 # Add after creating RosterWindow
29 # We want to test the filling explicitly
30 gajim
.contacts
= contacts_module
.LegacyContactsAPI()
31 gajim
.connections
= {}
32 self
.roster
= roster_window
.RosterWindow()
35 gajim
.connections
[acc
] = MockConnection(acc
)
36 gajim
.contacts
.add_account(acc
)
39 def assert_all_contacts_are_in_roster(self
, acc
):
40 for jid
in contacts
[acc
]:
41 self
.assert_contact_is_in_roster(jid
, acc
)
43 def assert_contact_is_in_roster(self
, jid
, account
):
44 contacts
= gajim
.contacts
.get_contacts(account
, jid
)
45 # check for all resources
46 for contact
in contacts
:
47 iters
= self
.roster
._get
_contact
_iter
(jid
, account
,
48 model
=self
.roster
.model
)
50 if jid
!= gajim
.get_jid_from_account(account
):
51 # We don't care for groups of SelfContact
52 self
.assertTrue(len(iters
) == len(contact
.get_shown_groups()),
53 msg
='Contact is not in all his groups')
58 family
= gajim
.contacts
.get_metacontacts_family(account
, jid
)
60 nearby_family
, bb_jid
, bb_account
= \
61 self
.roster
._get
_nearby
_family
_and
_big
_brother
(family
, account
)
63 is_in_nearby_family
= (jid
, account
) in (
64 (data
['jid'], data
['account']) for data
in nearby_family
)
65 self
.assertTrue(is_in_nearby_family
,
66 msg
='Contact not in his own nearby family')
68 is_big_brother
= (bb_jid
, bb_account
) == (jid
, account
)
70 # check for each group tag
72 self
.assertTrue(self
.roster
.model
.iter_is_valid(titerC
),
73 msg
='Contact iter invalid')
75 c_model
= self
.roster
.model
[titerC
]
76 # name can be stricked if contact or group is blocked
77 # self.assertEquals(contact.get_shown_name(), c_model[self.C_NAME],
78 # msg='Contact name missmatch')
79 self
.assertEquals(contact
.jid
, c_model
[self
.C_JID
],
82 if not self
.roster
.regroup
:
83 self
.assertEquals(account
, c_model
[self
.C_ACCOUNT
],
84 msg
='Account missmatch')
86 # Check for correct nesting
87 parent_iter
= self
.roster
.model
.iter_parent(titerC
)
88 p_model
= self
.roster
.model
[parent_iter
]
91 self
.assertTrue(p_model
[self
.C_TYPE
] == 'group',
92 msg
='Big Brother is not on top')
94 self
.assertTrue(p_model
[self
.C_TYPE
] == 'contact',
95 msg
='Little Brother brother has no BigB')
97 if jid
== gajim
.get_jid_from_account(account
):
98 self
.assertTrue(p_model
[self
.C_TYPE
] == 'account',
99 msg
='SelfContact is not on top')
101 self
.assertTrue(p_model
[self
.C_TYPE
] == 'group',
102 msg
='Contact not found in a group')
104 def assert_group_is_in_roster(self
, group
, account
):
108 def assert_account_is_in_roster(self
, acc
):
109 titerA
= self
.roster
._get
_account
_iter
(acc
, model
=self
.roster
.model
)
110 self
.assertTrue(self
.roster
.model
.iter_is_valid(titerA
),
111 msg
='Account iter is invalid')
113 acc_model
= self
.roster
.model
[titerA
]
114 self
.assertEquals(acc_model
[self
.C_TYPE
], 'account',
115 msg
='No account found')
117 if not self
.roster
.regroup
:
118 self
.assertEquals(acc_model
[self
.C_ACCOUNT
], acc
,
119 msg
='Account not found')
121 self_jid
= gajim
.get_jid_from_account(acc
)
122 self
.assertEquals(acc_model
[self
.C_JID
], self_jid
,
123 msg
='Account JID not found in account row')
125 def assert_model_is_in_sync(self
):
126 #TODO: check that iter_n_children returns the correct numbers
130 def test_fill_contacts_and_groups_dicts(self
):
132 self
.roster
.fill_contacts_and_groups_dicts(contacts
[acc
], acc
)
134 for jid
in contacts
[acc
]:
135 instances
= gajim
.contacts
.get_contacts(acc
, jid
)
137 # Created a contact for each single jid?
138 self
.assertTrue(len(instances
) == 1)
140 # Contacts kept their info
141 contact
= instances
[0]
142 self
.assertEquals(contact
.groups
, contacts
[acc
][jid
]['groups'],
143 msg
='Group Missmatch')
145 groups
= contacts
[acc
][jid
]['groups'] or ['General',]
147 def test_fill_roster_model(self
):
149 self
.roster
.fill_contacts_and_groups_dicts(contacts
[acc
], acc
)
151 self
.roster
.add_account(acc
)
152 self
.assert_account_is_in_roster(acc
)
154 self
.roster
.add_account_contacts(acc
)
155 self
.assert_all_contacts_are_in_roster(acc
)
157 self
.assert_model_is_in_sync()
160 class TestRosterWindowRegrouped(TestRosterWindow
):
163 gajim
.config
.set('mergeaccounts', True)
164 TestRosterWindow
.setUp(self
)
166 def test_toggle_regroup(self
):
167 self
.roster
.regroup
= not self
.roster
.regroup
168 self
.roster
.setup_and_draw_roster()
169 self
.roster
.regroup
= not self
.roster
.regroup
170 self
.roster
.setup_and_draw_roster()
173 class TestRosterWindowMetaContacts(TestRosterWindowRegrouped
):
175 def test_receive_metacontact_data(self
):
176 for complete_data
in metacontact_data
:
177 t_acc
= complete_data
[0]['account']
178 t_jid
= complete_data
[0]['jid']
179 data
= complete_data
[1:]
181 acc
= brother
['account']
183 gajim
.contacts
.add_metacontact(t_acc
, t_jid
, acc
, jid
)
184 self
.roster
.setup_and_draw_roster()
186 def test_connect_new_metacontact(self
):
187 self
.test_fill_roster_model()
189 jid
= u
'coolstuff@gajim.org'
190 contact
= gajim
.contacts
.create_contact(jid
, account1
)
191 gajim
.contacts
.add_contact(account1
, contact
)
192 self
.roster
.add_contact(jid
, account1
)
193 self
.roster
.chg_contact_status(contact
, 'offline', '', account1
)
195 gajim
.contacts
.add_metacontact(account1
, u
'samejid@gajim.org',
197 self
.roster
.chg_contact_status(contact
, 'online', '', account1
)
199 self
.assert_model_is_in_sync()
203 if __name__
== '__main__':