2010-10-01 17:49:58 +00:00
|
|
|
import time
|
2013-07-26 11:02:26 +00:00
|
|
|
import unittest
|
2014-07-17 12:19:04 +00:00
|
|
|
from slixmpp.test import SlixTest
|
2010-05-20 20:09:04 +00:00
|
|
|
|
|
|
|
|
2014-07-17 12:19:04 +00:00
|
|
|
class TestEvents(SlixTest):
|
2010-10-01 17:49:58 +00:00
|
|
|
|
|
|
|
def setUp(self):
|
2010-10-07 13:22:27 +00:00
|
|
|
self.stream_start()
|
2010-10-01 17:49:58 +00:00
|
|
|
|
|
|
|
def testEventHappening(self):
|
|
|
|
"""Test handler working"""
|
|
|
|
happened = []
|
|
|
|
|
|
|
|
def handletestevent(event):
|
|
|
|
happened.append(True)
|
|
|
|
|
|
|
|
self.xmpp.add_event_handler("test_event", handletestevent)
|
|
|
|
self.xmpp.event("test_event")
|
|
|
|
self.xmpp.event("test_event")
|
|
|
|
|
|
|
|
msg = "Event was not triggered the correct number of times: %s"
|
2018-10-08 21:25:23 +00:00
|
|
|
self.assertTrue(happened == [True, True], msg)
|
2010-10-01 17:49:58 +00:00
|
|
|
|
|
|
|
def testDelEvent(self):
|
|
|
|
"""Test handler working, then deleted and not triggered"""
|
|
|
|
happened = []
|
|
|
|
|
|
|
|
def handletestevent(event):
|
|
|
|
happened.append(True)
|
|
|
|
|
|
|
|
self.xmpp.add_event_handler("test_event", handletestevent)
|
|
|
|
self.xmpp.event("test_event", {})
|
|
|
|
|
|
|
|
self.xmpp.del_event_handler("test_event", handletestevent)
|
|
|
|
|
|
|
|
# Should not trigger because it was deleted
|
|
|
|
self.xmpp.event("test_event", {})
|
|
|
|
|
|
|
|
msg = "Event was not triggered the correct number of times: %s"
|
2018-10-08 21:25:23 +00:00
|
|
|
self.assertTrue(happened == [True], msg % happened)
|
2010-10-01 17:49:58 +00:00
|
|
|
|
2011-09-23 16:03:49 +00:00
|
|
|
def testAddDelAddEvent(self):
|
|
|
|
"""Test adding, then removing, then adding an event handler."""
|
|
|
|
happened = []
|
|
|
|
|
|
|
|
def handletestevent(event):
|
|
|
|
happened.append(True)
|
|
|
|
|
|
|
|
self.xmpp.add_event_handler("test_event", handletestevent)
|
|
|
|
self.xmpp.event("test_event", {})
|
|
|
|
|
|
|
|
self.xmpp.del_event_handler("test_event", handletestevent)
|
|
|
|
# Should not trigger because it was deleted
|
|
|
|
self.xmpp.event("test_event", {})
|
|
|
|
|
|
|
|
self.xmpp.add_event_handler("test_event", handletestevent)
|
|
|
|
self.xmpp.event("test_event", {})
|
|
|
|
|
|
|
|
msg = "Event was not triggered the correct number of times: %s"
|
2018-10-08 21:25:23 +00:00
|
|
|
self.assertTrue(happened == [True, True], msg % happened)
|
2011-09-23 16:03:49 +00:00
|
|
|
|
2010-10-01 17:49:58 +00:00
|
|
|
def testDisposableEvent(self):
|
|
|
|
"""Test disposable handler working, then not being triggered again."""
|
|
|
|
happened = []
|
|
|
|
|
|
|
|
def handletestevent(event):
|
|
|
|
happened.append(True)
|
|
|
|
|
|
|
|
self.xmpp.add_event_handler("test_event", handletestevent,
|
|
|
|
disposable=True)
|
|
|
|
self.xmpp.event("test_event", {})
|
|
|
|
|
|
|
|
# Should not trigger because it was deleted
|
|
|
|
self.xmpp.event("test_event", {})
|
|
|
|
|
|
|
|
msg = "Event was not triggered the correct number of times: %s"
|
2018-10-08 21:25:23 +00:00
|
|
|
self.assertTrue(happened == [True], msg % happened)
|
2010-10-01 17:49:58 +00:00
|
|
|
|
2010-05-20 20:09:04 +00:00
|
|
|
|
2010-07-20 16:22:25 +00:00
|
|
|
suite = unittest.TestLoader().loadTestsFromTestCase(TestEvents)
|