2009-01-11 14:49:03 +01:00
|
|
|
'''
|
|
|
|
Tests for the miscellaneous functions scattered throughout src/gajim.py
|
|
|
|
'''
|
2008-06-29 07:25:59 +02:00
|
|
|
import unittest
|
|
|
|
|
2008-08-09 02:24:08 +02:00
|
|
|
import lib
|
|
|
|
lib.setup_env()
|
2008-06-29 07:25:59 +02:00
|
|
|
|
2013-04-07 23:41:15 +02:00
|
|
|
import nbxmpp
|
|
|
|
|
2008-06-29 07:25:59 +02:00
|
|
|
from common import gajim
|
2009-11-09 23:03:16 +01:00
|
|
|
from common import contacts as contacts_module
|
2013-04-08 18:57:39 +02:00
|
|
|
from common import caps_cache
|
2008-06-29 07:25:59 +02:00
|
|
|
from gajim import Interface
|
|
|
|
|
2009-01-11 14:49:03 +01:00
|
|
|
from gajim_mocks import *
|
2008-08-09 01:53:44 +02:00
|
|
|
gajim.logger = MockLogger()
|
2008-06-29 07:25:59 +02:00
|
|
|
|
|
|
|
Interface()
|
|
|
|
|
2008-08-09 08:10:04 +02:00
|
|
|
import time
|
|
|
|
from data import *
|
|
|
|
|
|
|
|
import roster_window
|
2013-04-08 18:57:39 +02:00
|
|
|
import plugins
|
2008-08-09 08:10:04 +02:00
|
|
|
import notify
|
|
|
|
|
|
|
|
class TestStatusChange(unittest.TestCase):
|
2013-04-08 18:57:39 +02:00
|
|
|
'''tests gajim.py's incredibly complex presence handling'''
|
2008-08-09 08:10:04 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
def setUp(self):
|
2008-08-09 08:10:04 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
gajim.connections = {}
|
|
|
|
gajim.contacts = contacts_module.LegacyContactsAPI()
|
|
|
|
gajim.interface.roster = roster_window.RosterWindow()
|
2013-04-08 18:57:39 +02:00
|
|
|
gajim.plugin_manager = plugins.PluginManager()
|
|
|
|
gajim.logger = MockLogger()
|
|
|
|
caps_cache.initialize(gajim.logger)
|
2008-08-09 08:10:04 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
for acc in contacts:
|
|
|
|
gajim.connections[acc] = MockConnection(acc)
|
2008-08-09 08:10:04 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
gajim.interface.roster.fill_contacts_and_groups_dicts(contacts[acc],
|
|
|
|
acc)
|
|
|
|
gajim.interface.roster.add_account(acc)
|
|
|
|
gajim.interface.roster.add_account_contacts(acc)
|
2008-08-09 08:10:04 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
self.assertEqual(0, len(notify.notifications))
|
2008-08-09 08:10:04 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
def tearDown(self):
|
|
|
|
notify.notifications = []
|
2013-04-08 18:57:39 +02:00
|
|
|
for acc in contacts:
|
|
|
|
gajim.connections[acc].cleanup()
|
2008-08-09 08:10:04 +02:00
|
|
|
|
2013-04-08 18:57:39 +02:00
|
|
|
def contact_comes_online(self, account, jid, resource, prio,
|
|
|
|
should_popup=True):
|
2010-02-08 15:08:40 +01:00
|
|
|
'''a remote contact comes online'''
|
2013-04-08 18:57:39 +02:00
|
|
|
xml = """<presence from='%s/%s' id='123'><priority>%s</priority>
|
2013-04-07 23:41:15 +02:00
|
|
|
<c node='http://gajim.org' ver='pRCD6cgQ4SDqNMCjdhRV6TECx5o='
|
|
|
|
hash='sha-1' xmlns='http://jabber.org/protocol/caps'/>
|
|
|
|
<status>I'm back!</status>
|
|
|
|
</presence>
|
2013-04-08 18:57:39 +02:00
|
|
|
""" % (jid, resource, prio)
|
2013-04-07 23:41:15 +02:00
|
|
|
msg = nbxmpp.protocol.Presence(node=nbxmpp.simplexml.XML2Node(xml))
|
|
|
|
gajim.connections[account]._presenceCB(None, msg)
|
2008-08-09 08:10:04 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
contact = None
|
|
|
|
for c in gajim.contacts.get_contacts(account, jid):
|
|
|
|
if c.resource == resource:
|
|
|
|
contact = c
|
|
|
|
break
|
2008-08-09 08:10:04 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
self.assertEqual('online', contact.show)
|
|
|
|
self.assertEqual("I'm back!", contact.status)
|
|
|
|
self.assertEqual(prio, contact.priority)
|
2008-08-09 08:10:04 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
# the most recent notification is that the contact connected
|
2013-04-08 18:57:39 +02:00
|
|
|
if should_popup:
|
|
|
|
self.assertEqual('Contact Signed In',
|
|
|
|
notify.notifications[-1].popup_event_type)
|
|
|
|
else:
|
|
|
|
self.assertEqual('', notify.notifications[-1].popup_event_type)
|
2008-08-09 08:10:04 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
def contact_goes_offline(self, account, jid, resource, prio,
|
|
|
|
still_exists = True):
|
|
|
|
'''a remote contact goes offline.'''
|
2013-04-08 18:57:39 +02:00
|
|
|
xml = """<presence type='unavailable' from='%s/%s' id='123'>
|
|
|
|
<priority>%s</priority>
|
|
|
|
<c node='http://gajim.org' ver='pRCD6cgQ4SDqNMCjdhRV6TECx5o='
|
|
|
|
hash='sha-1' xmlns='http://jabber.org/protocol/caps'/>
|
|
|
|
<status>Goodbye!</status>
|
|
|
|
</presence>
|
|
|
|
""" % (jid, resource, prio)
|
|
|
|
msg = nbxmpp.protocol.Presence(node=nbxmpp.simplexml.XML2Node(xml))
|
|
|
|
gajim.connections[account]._presenceCB(None, msg)
|
2008-08-09 08:10:04 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
contact = None
|
|
|
|
for c in gajim.contacts.get_contacts(account, jid):
|
|
|
|
if c.resource == resource:
|
|
|
|
contact = c
|
|
|
|
break
|
2008-08-09 08:10:04 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
if not still_exists:
|
|
|
|
self.assert_(contact is None)
|
|
|
|
return
|
2008-08-09 08:10:04 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
self.assertEqual('offline', contact.show)
|
|
|
|
self.assertEqual('Goodbye!', contact.status)
|
|
|
|
self.assertEqual(prio, contact.priority)
|
2008-08-09 08:10:04 +02:00
|
|
|
|
2013-04-08 18:57:39 +02:00
|
|
|
self.assertEqual('Contact Signed Out',
|
|
|
|
notify.notifications[-1].popup_event_type)
|
2008-08-09 08:10:04 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
def user_starts_chatting(self, jid, account, resource=None):
|
|
|
|
'''the user opens a chat window and starts talking'''
|
|
|
|
ctrl = MockChatControl(jid, account)
|
|
|
|
win = MockWindow()
|
|
|
|
win.new_tab(ctrl)
|
|
|
|
gajim.interface.msg_win_mgr._windows['test'] = win
|
2008-08-09 08:10:04 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
if resource:
|
|
|
|
jid = jid + '/' + resource
|
2008-08-09 08:10:04 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
# a basic session is started
|
|
|
|
session = gajim.connections[account1].make_new_session(jid,
|
|
|
|
'01234567890abcdef', cls=MockSession)
|
|
|
|
ctrl.set_session(session)
|
2008-08-09 08:10:04 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
return ctrl
|
2008-08-09 08:10:04 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
def user_starts_esession(self, jid, resource, account):
|
|
|
|
'''the user opens a chat window and starts an encrypted session'''
|
|
|
|
ctrl = self.user_starts_chatting(jid, account, resource)
|
|
|
|
ctrl.session.status = 'active'
|
|
|
|
ctrl.session.enable_encryption = True
|
2008-08-09 08:10:04 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
return ctrl
|
2008-08-09 08:10:04 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
def test_contact_comes_online(self):
|
|
|
|
jid = 'default1@gajim.org'
|
2008-08-09 08:10:04 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
# contact is offline initially
|
|
|
|
contacts = gajim.contacts.get_contacts(account1, jid)
|
|
|
|
self.assertEqual(1, len(contacts))
|
|
|
|
self.assertEqual('offline', contacts[0].show)
|
|
|
|
self.assertEqual('', contacts[0].status)
|
2008-08-09 08:10:04 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
self.contact_comes_online(account1, jid, 'lowprio', 1)
|
2008-08-09 08:10:04 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
def test_contact_goes_offline(self):
|
|
|
|
jid = 'default1@gajim.org'
|
2008-08-09 08:10:04 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
self.contact_comes_online(account1, jid, 'lowprio', 1)
|
2008-08-09 08:10:04 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
ctrl = self.user_starts_chatting(jid, account1)
|
|
|
|
orig_sess = ctrl.session
|
2008-08-09 08:10:04 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
self.contact_goes_offline(account1, jid, 'lowprio', 1)
|
2008-08-09 08:10:04 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
# session hasn't changed since we were talking to the bare jid
|
|
|
|
self.assertEqual(orig_sess, ctrl.session)
|
2008-08-09 08:10:04 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
def test_two_resources_higher_comes_online(self):
|
|
|
|
jid = 'default1@gajim.org'
|
2008-08-09 08:10:04 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
self.contact_comes_online(account1, jid, 'lowprio', 1)
|
2008-08-09 08:10:04 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
ctrl = self.user_starts_chatting(jid, account1)
|
2008-08-09 08:10:04 +02:00
|
|
|
|
2013-04-08 18:57:39 +02:00
|
|
|
self.contact_comes_online(account1, jid, 'highprio', 50,
|
|
|
|
should_popup=False)
|
2008-08-09 08:10:04 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
# old session was dropped
|
|
|
|
self.assertEqual(None, ctrl.session)
|
2008-08-09 08:10:04 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
def test_two_resources_higher_goes_offline(self):
|
|
|
|
jid = 'default1@gajim.org'
|
2008-08-09 08:10:04 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
self.contact_comes_online(account1, jid, 'lowprio', 1)
|
2013-04-08 18:57:39 +02:00
|
|
|
self.contact_comes_online(account1, jid, 'highprio', 50,
|
|
|
|
should_popup=False)
|
2008-08-09 08:10:04 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
ctrl = self.user_starts_chatting(jid, account1)
|
2008-08-09 08:10:04 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
self.contact_goes_offline(account1, jid, 'highprio', 50,
|
2013-04-08 18:57:39 +02:00
|
|
|
still_exists=False)
|
2008-08-09 08:10:04 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
# old session was dropped
|
|
|
|
self.assertEqual(None, ctrl.session)
|
2008-08-09 08:10:04 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
def test_two_resources_higher_comes_online_with_esession(self):
|
|
|
|
jid = 'default1@gajim.org'
|
2008-08-09 08:10:04 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
self.contact_comes_online(account1, jid, 'lowprio', 1)
|
2008-08-09 08:10:04 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
ctrl = self.user_starts_esession(jid, 'lowprio', account1)
|
2008-08-09 08:10:04 +02:00
|
|
|
|
2013-04-08 18:57:39 +02:00
|
|
|
self.contact_comes_online(account1, jid, 'highprio', 50,
|
|
|
|
should_popup=False)
|
2008-08-09 08:10:04 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
# session was associated with the low priority full jid, so it should
|
|
|
|
# have been removed from the control
|
|
|
|
self.assertEqual(None, ctrl.session)
|
2008-08-09 08:10:04 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
def test_two_resources_higher_goes_offline_with_esession(self):
|
|
|
|
jid = 'default1@gajim.org'
|
2008-08-09 08:10:04 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
self.contact_comes_online(account1, jid, 'lowprio', 1)
|
|
|
|
self.contact_comes_online(account1, jid, 'highprio', 50)
|
2008-08-09 08:10:04 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
ctrl = self.user_starts_esession(jid, 'highprio', account1)
|
2008-08-09 08:10:04 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
self.contact_goes_offline(account1, jid, 'highprio', 50,
|
|
|
|
still_exists=False)
|
2008-08-09 08:10:04 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
# session was associated with the high priority full jid, so it should
|
|
|
|
# have been removed from the control
|
|
|
|
self.assertEqual(None, ctrl.session)
|
2008-07-29 21:49:31 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|