2008-06-19 01:58:19 +02:00
|
|
|
'''
|
2008-12-24 15:50:50 +01:00
|
|
|
Module with dummy classes for unit testing of XMPP and related code.
|
2008-06-19 01:58:19 +02:00
|
|
|
'''
|
|
|
|
|
|
|
|
import threading, time, os.path, sys
|
|
|
|
|
2008-12-24 15:50:50 +01:00
|
|
|
gajim_root = os.path.join(os.path.abspath(os.path.dirname(__file__)), '..')
|
|
|
|
sys.path.append(gajim_root + '/src/common/xmpp')
|
|
|
|
import idlequeue
|
|
|
|
from client import PlugIn
|
|
|
|
|
2008-12-15 22:06:08 +01:00
|
|
|
import lib
|
|
|
|
lib.setup_env()
|
|
|
|
|
2008-06-19 01:58:19 +02:00
|
|
|
|
2008-12-24 15:50:50 +01:00
|
|
|
from mock import Mock
|
2008-06-19 01:58:19 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
idlequeue_interval = 0.2
|
|
|
|
'''
|
|
|
|
IdleQueue polling interval. 200ms is used in Gajim as default
|
|
|
|
'''
|
|
|
|
|
|
|
|
class IdleQueueThread(threading.Thread):
|
|
|
|
'''
|
|
|
|
Thread for regular processing of idlequeue.
|
|
|
|
'''
|
|
|
|
def __init__(self):
|
|
|
|
self.iq = idlequeue.IdleQueue()
|
|
|
|
self.stop = threading.Event()
|
|
|
|
'''
|
|
|
|
Event used to stopping the thread main loop.
|
|
|
|
'''
|
|
|
|
|
|
|
|
self.stop.clear()
|
|
|
|
threading.Thread.__init__(self)
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
while not self.stop.isSet():
|
|
|
|
self.iq.process()
|
|
|
|
time.sleep(idlequeue_interval)
|
|
|
|
|
|
|
|
def stop_thread(self):
|
|
|
|
self.stop.set()
|
|
|
|
|
|
|
|
|
|
|
|
class IdleMock:
|
|
|
|
'''
|
|
|
|
Serves as template for testing objects that are normally controlled by GUI.
|
|
|
|
Allows to wait for asynchronous callbacks with wait() method.
|
|
|
|
'''
|
|
|
|
def __init__(self):
|
|
|
|
self.event = threading.Event()
|
|
|
|
'''
|
|
|
|
Event is used for waiting on callbacks.
|
|
|
|
'''
|
|
|
|
self.event.clear()
|
|
|
|
|
|
|
|
def wait(self):
|
|
|
|
'''
|
2008-12-24 15:50:50 +01:00
|
|
|
Waiting until some callback sets the event and clearing the event
|
|
|
|
subsequently.
|
2008-06-19 01:58:19 +02:00
|
|
|
'''
|
|
|
|
self.event.wait()
|
|
|
|
self.event.clear()
|
|
|
|
|
|
|
|
def set_event(self):
|
|
|
|
self.event.set()
|
|
|
|
|
|
|
|
|
2008-12-24 15:50:50 +01:00
|
|
|
class MockConnectionClass(IdleMock, Mock):
|
2008-06-19 01:58:19 +02:00
|
|
|
'''
|
|
|
|
Class simulating Connection class from src/common/connection.py
|
|
|
|
|
|
|
|
It is derived from Mock in order to avoid defining all methods
|
|
|
|
from real Connection that are called from NBClient or Dispatcher
|
|
|
|
( _event_dispatcher for example)
|
|
|
|
'''
|
|
|
|
|
|
|
|
def __init__(self, *args):
|
|
|
|
self.connect_succeeded = True
|
|
|
|
IdleMock.__init__(self)
|
|
|
|
Mock.__init__(self, *args)
|
|
|
|
|
|
|
|
def on_connect(self, success, *args):
|
|
|
|
'''
|
|
|
|
Method called after connecting - after receiving <stream:features>
|
|
|
|
from server (NOT after TLS stream restart) or connect failure
|
|
|
|
'''
|
2008-12-24 15:50:50 +01:00
|
|
|
#print 'on_connect - args:'
|
|
|
|
#print ' success - %s' % success
|
|
|
|
#for i in args:
|
|
|
|
# print ' %s' % i
|
2008-06-19 01:58:19 +02:00
|
|
|
self.connect_succeeded = success
|
|
|
|
self.set_event()
|
|
|
|
|
|
|
|
def on_auth(self, con, auth):
|
|
|
|
'''
|
2008-12-24 15:50:50 +01:00
|
|
|
Method called after authentication, regardless of the result.
|
2008-06-19 01:58:19 +02:00
|
|
|
|
|
|
|
:Parameters:
|
|
|
|
con : NonBlockingClient
|
|
|
|
reference to authenticated object
|
|
|
|
auth : string
|
|
|
|
type of authetication in case of success ('old_auth', 'sasl') or
|
|
|
|
None in case of auth failure
|
|
|
|
'''
|
|
|
|
#print 'on_auth - args:'
|
|
|
|
#print ' con: %s' % con
|
|
|
|
#print ' auth: %s' % auth
|
|
|
|
self.auth_connection = con
|
|
|
|
self.auth = auth
|
|
|
|
self.set_event()
|
|
|
|
|
2008-12-24 15:50:50 +01:00
|
|
|
# vim: se ts=3:
|