2008-08-06 22:17:00 +02:00
|
|
|
import unittest
|
|
|
|
|
2008-08-09 02:24:08 +02:00
|
|
|
import lib
|
|
|
|
lib.setup_env()
|
2008-08-06 22:17:00 +02:00
|
|
|
|
|
|
|
from data import *
|
|
|
|
|
|
|
|
from mock import Mock, expectParams
|
2009-01-11 14:49:03 +01:00
|
|
|
from gajim_mocks import *
|
2008-08-06 22:17:00 +02:00
|
|
|
|
2017-08-13 13:18:56 +02:00
|
|
|
from gajim.common import app
|
2017-08-12 02:39:55 +02:00
|
|
|
from gajim.common import contacts as contacts_module
|
|
|
|
from gajim import roster_window
|
2008-08-06 22:17:00 +02:00
|
|
|
|
2017-08-13 13:18:56 +02:00
|
|
|
app.get_jid_from_account = lambda acc: 'myjid@' + acc
|
2008-08-06 22:17:00 +02:00
|
|
|
|
2009-11-09 23:03:16 +01:00
|
|
|
|
2008-08-06 22:17:00 +02:00
|
|
|
class TestRosterWindow(unittest.TestCase):
|
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
def setUp(self):
|
2017-08-13 13:18:56 +02:00
|
|
|
app.interface = MockInterface()
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2017-08-09 15:34:58 +02:00
|
|
|
self.C_NAME = roster_window.Column.NAME
|
|
|
|
self.C_TYPE = roster_window.Column.TYPE
|
|
|
|
self.C_JID = roster_window.Column.JID
|
|
|
|
self.C_ACCOUNT = roster_window.Column.ACCOUNT
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
# Add after creating RosterWindow
|
|
|
|
# We want to test the filling explicitly
|
2017-08-13 13:18:56 +02:00
|
|
|
app.contacts = contacts_module.LegacyContactsAPI()
|
|
|
|
app.connections = {}
|
|
|
|
self.roster = roster_window.RosterWindow(app.app)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
for acc in contacts:
|
2017-08-13 13:18:56 +02:00
|
|
|
app.connections[acc] = MockConnection(acc)
|
|
|
|
app.contacts.add_account(acc)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2017-09-15 15:11:55 +02:00
|
|
|
def tearDown(self):
|
|
|
|
self.roster.window.destroy()
|
|
|
|
# Clean main loop
|
|
|
|
from gi.repository import GLib
|
|
|
|
mc = GLib.main_context_default()
|
|
|
|
while mc.pending():
|
|
|
|
mc.iteration()
|
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
### Custom assertions
|
|
|
|
def assert_all_contacts_are_in_roster(self, acc):
|
|
|
|
for jid in contacts[acc]:
|
|
|
|
self.assert_contact_is_in_roster(jid, acc)
|
|
|
|
|
|
|
|
def assert_contact_is_in_roster(self, jid, account):
|
2017-08-13 13:18:56 +02:00
|
|
|
contacts = app.contacts.get_contacts(account, jid)
|
2010-02-08 15:08:40 +01:00
|
|
|
# check for all resources
|
|
|
|
for contact in contacts:
|
|
|
|
iters = self.roster._get_contact_iter(jid, account,
|
|
|
|
model=self.roster.model)
|
|
|
|
|
2017-08-13 13:18:56 +02:00
|
|
|
if jid != app.get_jid_from_account(account):
|
2010-02-08 15:08:40 +01:00
|
|
|
# We don't care for groups of SelfContact
|
|
|
|
self.assertTrue(len(iters) == len(contact.get_shown_groups()),
|
|
|
|
msg='Contact is not in all his groups')
|
|
|
|
|
|
|
|
# Are we big brother?
|
|
|
|
bb_jid = None
|
|
|
|
bb_account = None
|
2017-08-13 13:18:56 +02:00
|
|
|
family = app.contacts.get_metacontacts_family(account, jid)
|
2010-02-08 15:08:40 +01:00
|
|
|
if family:
|
|
|
|
nearby_family, bb_jid, bb_account = \
|
|
|
|
self.roster._get_nearby_family_and_big_brother(family, account)
|
|
|
|
|
|
|
|
is_in_nearby_family = (jid, account) in (
|
|
|
|
(data['jid'], data['account']) for data in nearby_family)
|
|
|
|
self.assertTrue(is_in_nearby_family,
|
|
|
|
msg='Contact not in his own nearby family')
|
|
|
|
|
|
|
|
is_big_brother = (bb_jid, bb_account) == (jid, account)
|
|
|
|
|
|
|
|
# check for each group tag
|
|
|
|
for titerC in iters:
|
|
|
|
self.assertTrue(self.roster.model.iter_is_valid(titerC),
|
|
|
|
msg='Contact iter invalid')
|
|
|
|
|
|
|
|
c_model = self.roster.model[titerC]
|
2010-08-27 11:47:38 +02:00
|
|
|
# name can be stricked if contact or group is blocked
|
2017-09-10 12:21:10 +02:00
|
|
|
# self.assertEqual(contact.get_shown_name(), c_model[self.C_NAME],
|
2010-08-27 11:47:38 +02:00
|
|
|
# msg='Contact name missmatch')
|
2017-09-10 12:21:10 +02:00
|
|
|
self.assertEqual(contact.jid, c_model[self.C_JID],
|
2010-02-08 15:08:40 +01:00
|
|
|
msg='Jid missmatch')
|
|
|
|
|
|
|
|
if not self.roster.regroup:
|
2017-09-10 12:21:10 +02:00
|
|
|
self.assertEqual(account, c_model[self.C_ACCOUNT],
|
2010-02-08 15:08:40 +01:00
|
|
|
msg='Account missmatch')
|
|
|
|
|
|
|
|
# Check for correct nesting
|
|
|
|
parent_iter = self.roster.model.iter_parent(titerC)
|
|
|
|
p_model = self.roster.model[parent_iter]
|
|
|
|
if family:
|
|
|
|
if is_big_brother:
|
|
|
|
self.assertTrue(p_model[self.C_TYPE] == 'group',
|
|
|
|
msg='Big Brother is not on top')
|
|
|
|
else:
|
|
|
|
self.assertTrue(p_model[self.C_TYPE] == 'contact',
|
|
|
|
msg='Little Brother brother has no BigB')
|
|
|
|
else:
|
2017-08-13 13:18:56 +02:00
|
|
|
if jid == app.get_jid_from_account(account):
|
2010-02-08 15:08:40 +01:00
|
|
|
self.assertTrue(p_model[self.C_TYPE] == 'account',
|
|
|
|
msg='SelfContact is not on top')
|
|
|
|
else:
|
|
|
|
self.assertTrue(p_model[self.C_TYPE] == 'group',
|
|
|
|
msg='Contact not found in a group')
|
|
|
|
|
|
|
|
def assert_group_is_in_roster(self, group, account):
|
|
|
|
#TODO
|
|
|
|
pass
|
|
|
|
|
|
|
|
def assert_account_is_in_roster(self, acc):
|
|
|
|
titerA = self.roster._get_account_iter(acc, model=self.roster.model)
|
|
|
|
self.assertTrue(self.roster.model.iter_is_valid(titerA),
|
|
|
|
msg='Account iter is invalid')
|
|
|
|
|
|
|
|
acc_model = self.roster.model[titerA]
|
2017-09-10 12:21:10 +02:00
|
|
|
self.assertEqual(acc_model[self.C_TYPE], 'account',
|
2010-02-08 15:08:40 +01:00
|
|
|
msg='No account found')
|
|
|
|
|
|
|
|
if not self.roster.regroup:
|
2017-09-10 12:21:10 +02:00
|
|
|
self.assertEqual(acc_model[self.C_ACCOUNT], acc,
|
2010-02-08 15:08:40 +01:00
|
|
|
msg='Account not found')
|
|
|
|
|
2017-08-13 13:18:56 +02:00
|
|
|
self_jid = app.get_jid_from_account(acc)
|
2017-09-10 12:21:10 +02:00
|
|
|
self.assertEqual(acc_model[self.C_JID], self_jid,
|
2010-02-08 15:08:40 +01:00
|
|
|
msg='Account JID not found in account row')
|
|
|
|
|
|
|
|
def assert_model_is_in_sync(self):
|
|
|
|
#TODO: check that iter_n_children returns the correct numbers
|
|
|
|
pass
|
|
|
|
|
|
|
|
# tests
|
|
|
|
def test_fill_contacts_and_groups_dicts(self):
|
|
|
|
for acc in contacts:
|
|
|
|
self.roster.fill_contacts_and_groups_dicts(contacts[acc], acc)
|
|
|
|
|
|
|
|
for jid in contacts[acc]:
|
2017-08-13 13:18:56 +02:00
|
|
|
instances = app.contacts.get_contacts(acc, jid)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
# Created a contact for each single jid?
|
|
|
|
self.assertTrue(len(instances) == 1)
|
|
|
|
|
|
|
|
# Contacts kept their info
|
|
|
|
contact = instances[0]
|
2017-09-10 12:21:10 +02:00
|
|
|
self.assertEqual(sorted(contact.groups), sorted(contacts[acc][jid]['groups']),
|
2010-02-08 15:08:40 +01:00
|
|
|
msg='Group Missmatch')
|
|
|
|
|
|
|
|
groups = contacts[acc][jid]['groups'] or ['General',]
|
|
|
|
|
|
|
|
def test_fill_roster_model(self):
|
|
|
|
for acc in contacts:
|
|
|
|
self.roster.fill_contacts_and_groups_dicts(contacts[acc], acc)
|
|
|
|
|
|
|
|
self.roster.add_account(acc)
|
|
|
|
self.assert_account_is_in_roster(acc)
|
|
|
|
|
|
|
|
self.roster.add_account_contacts(acc)
|
|
|
|
self.assert_all_contacts_are_in_roster(acc)
|
|
|
|
|
|
|
|
self.assert_model_is_in_sync()
|
2008-08-06 22:17:00 +02:00
|
|
|
|
|
|
|
|
|
|
|
class TestRosterWindowRegrouped(TestRosterWindow):
|
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
def setUp(self):
|
2017-08-13 13:18:56 +02:00
|
|
|
app.config.set('mergeaccounts', True)
|
2010-02-08 15:08:40 +01:00
|
|
|
TestRosterWindow.setUp(self)
|
2008-08-06 22:17:00 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
def test_toggle_regroup(self):
|
|
|
|
self.roster.regroup = not self.roster.regroup
|
|
|
|
self.roster.setup_and_draw_roster()
|
|
|
|
self.roster.regroup = not self.roster.regroup
|
|
|
|
self.roster.setup_and_draw_roster()
|
2008-08-06 22:17:00 +02:00
|
|
|
|
|
|
|
|
|
|
|
class TestRosterWindowMetaContacts(TestRosterWindowRegrouped):
|
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
def test_receive_metacontact_data(self):
|
|
|
|
for complete_data in metacontact_data:
|
|
|
|
t_acc = complete_data[0]['account']
|
|
|
|
t_jid = complete_data[0]['jid']
|
|
|
|
data = complete_data[1:]
|
|
|
|
for brother in data:
|
|
|
|
acc = brother['account']
|
|
|
|
jid = brother['jid']
|
2017-08-13 13:18:56 +02:00
|
|
|
app.contacts.add_metacontact(t_acc, t_jid, acc, jid)
|
2010-02-08 15:08:40 +01:00
|
|
|
self.roster.setup_and_draw_roster()
|
2008-08-06 22:17:00 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
def test_connect_new_metacontact(self):
|
|
|
|
self.test_fill_roster_model()
|
2008-08-06 22:17:00 +02:00
|
|
|
|
2013-01-01 19:44:25 +01:00
|
|
|
jid = 'coolstuff@gajim.org'
|
2017-08-13 13:18:56 +02:00
|
|
|
contact = app.contacts.create_contact(jid, account1)
|
|
|
|
app.contacts.add_contact(account1, contact)
|
2010-02-08 15:08:40 +01:00
|
|
|
self.roster.add_contact(jid, account1)
|
|
|
|
self.roster.chg_contact_status(contact, 'offline', '', account1)
|
2008-08-06 22:17:00 +02:00
|
|
|
|
2017-08-13 13:18:56 +02:00
|
|
|
app.contacts.add_metacontact(account1, 'samejid@gajim.org',
|
2010-02-08 15:08:40 +01:00
|
|
|
account1, jid)
|
|
|
|
self.roster.chg_contact_status(contact, 'online', '', account1)
|
2008-08-06 22:17:00 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
self.assert_model_is_in_sync()
|
2008-08-06 22:17:00 +02:00
|
|
|
|
|
|
|
|
2008-08-19 00:15:45 +02:00
|
|
|
|
2008-08-06 22:17:00 +02:00
|
|
|
if __name__ == '__main__':
|
2010-02-08 15:08:40 +01:00
|
|
|
unittest.main()
|