Ignore:
Timestamp:
Nov 1, 2008, 7:18:50 PM (13 years ago)
Author:
Ralph Meijer <ralphm@…>
Branch:
default
Convert:
svn:b33ecbfc-034c-dc11-8662-000475d9059e/trunk@134
Message:

Bring subprotocols code in line with Twisted trunk.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • wokkel/test/test_subprotocols.py

    r4 r45  
    55Tests for L{wokkel.subprotocols}
    66"""
     7
     8from zope.interface.verify import verifyObject
    79
    810from twisted.trial import unittest
     
    1214from twisted.words.protocols.jabber import error, xmlstream
    1315
    14 from wokkel import subprotocols
     16from wokkel import iwokkel, subprotocols
    1517
    1618class DummyFactory(object):
     19    """
     20    Dummy XmlStream factory that only registers bootstrap observers.
     21    """
    1722    def __init__(self):
    1823        self.callbacks = {}
    1924
     25
    2026    def addBootstrap(self, event, callback):
    2127        self.callbacks[event] = callback
    2228
    2329
     30
    2431class DummyXMPPHandler(subprotocols.XMPPHandler):
     32    """
     33    Dummy XMPP subprotocol handler to count the methods are called on it.
     34    """
    2535    def __init__(self):
    2636        self.doneMade = 0
     
    2838        self.doneLost = 0
    2939
     40
    3041    def makeConnection(self, xs):
    3142        self.connectionMade()
    3243
     44
    3345    def connectionMade(self):
    3446        self.doneMade += 1
    3547
     48
    3649    def connectionInitialized(self):
    3750        self.doneInitialized += 1
    3851
     52
    3953    def connectionLost(self, reason):
    4054        self.doneLost += 1
    4155
    4256
     57
    4358class XMPPHandlerTest(unittest.TestCase):
     59    """
     60    Tests for L{subprotocols.XMPPHandler}.
     61    """
     62
     63    def test_interface(self):
     64        """
     65        L{xmlstream.XMPPHandler} implements L{iwokkel.IXMPPHandler}.
     66        """
     67        verifyObject(iwokkel.IXMPPHandler, subprotocols.XMPPHandler())
     68
    4469
    4570    def test_send(self):
     
    4772        Test that data is passed on for sending by the stream manager.
    4873        """
    49 
    5074        class DummyStreamManager(object):
    5175            def __init__(self):
     
    6185
    6286
     87    def test_makeConnection(self):
     88        """
     89        Test that makeConnection saves the XML stream and calls connectionMade.
     90        """
     91        class TestXMPPHandler(subprotocols.XMPPHandler):
     92            def connectionMade(self):
     93                self.doneMade = True
     94
     95        handler = TestXMPPHandler()
     96        xs = xmlstream.XmlStream(xmlstream.Authenticator())
     97        handler.makeConnection(xs)
     98        self.assertTrue(handler.doneMade)
     99        self.assertIdentical(xs, handler.xmlstream)
     100
     101
     102    def test_connectionLost(self):
     103        """
     104        Test that connectionLost forgets the XML stream.
     105        """
     106        handler = subprotocols.XMPPHandler()
     107        xs = xmlstream.XmlStream(xmlstream.Authenticator())
     108        handler.makeConnection(xs)
     109        handler.connectionLost(Exception())
     110        self.assertIdentical(None, handler.xmlstream)
     111
     112
     113
     114class XMPPHandlerCollectionTest(unittest.TestCase):
     115    """
     116    Tests for L{subprotocols.XMPPHandlerCollection}.
     117    """
     118
     119    def setUp(self):
     120        self.collection = subprotocols.XMPPHandlerCollection()
     121
     122
     123    def test_interface(self):
     124        """
     125        L{subprotocols.StreamManager} implements L{iwokkel.IXMPPHandlerCollection}.
     126        """
     127        verifyObject(iwokkel.IXMPPHandlerCollection, self.collection)
     128
     129
     130    def test_addHandler(self):
     131        """
     132        Test the addition of a protocol handler.
     133        """
     134        handler = DummyXMPPHandler()
     135        handler.setHandlerParent(self.collection)
     136        self.assertIn(handler, self.collection)
     137        self.assertIdentical(self.collection, handler.parent)
     138
     139
     140    def test_removeHandler(self):
     141        """
     142        Test removal of a protocol handler.
     143        """
     144        handler = DummyXMPPHandler()
     145        handler.setHandlerParent(self.collection)
     146        handler.disownHandlerParent(self.collection)
     147        self.assertNotIn(handler, self.collection)
     148        self.assertIdentical(None, handler.parent)
     149
     150
     151
    63152class StreamManagerTest(unittest.TestCase):
     153    """
     154    Tests for L{subprotocols.StreamManager}.
     155    """
    64156
    65157    def setUp(self):
     
    83175                          sm.factory.callbacks['//event/xmpp/initfailed'])
    84176
    85     def test__connected(self):
     177
     178    def test_connected(self):
    86179        """
    87180        Test that protocol handlers have their connectionMade method called
     
    97190        self.assertEquals(0, handler.doneLost)
    98191
    99     def test__authd(self):
     192
     193    def test_connectedLogTrafficFalse(self):
     194        """
     195        Test raw data functions unset when logTraffic is set to False.
     196        """
     197        sm = self.streamManager
     198        handler = DummyXMPPHandler()
     199        handler.setHandlerParent(sm)
     200        xs = xmlstream.XmlStream(xmlstream.Authenticator())
     201        sm._connected(xs)
     202        self.assertIdentical(None, xs.rawDataInFn)
     203        self.assertIdentical(None, xs.rawDataOutFn)
     204
     205
     206    def test_connectedLogTrafficTrue(self):
     207        """
     208        Test raw data functions set when logTraffic is set to True.
     209        """
     210        sm = self.streamManager
     211        sm.logTraffic = True
     212        handler = DummyXMPPHandler()
     213        handler.setHandlerParent(sm)
     214        xs = xmlstream.XmlStream(xmlstream.Authenticator())
     215        sm._connected(xs)
     216        self.assertNotIdentical(None, xs.rawDataInFn)
     217        self.assertNotIdentical(None, xs.rawDataOutFn)
     218
     219
     220    def test_authd(self):
    100221        """
    101222        Test that protocol handlers have their connectionInitialized method
     
    111232        self.assertEquals(0, handler.doneLost)
    112233
    113     def test__disconnected(self):
     234
     235    def test_disconnected(self):
    114236        """
    115237        Test that protocol handlers have their connectionLost method
     
    125247        self.assertEquals(1, handler.doneLost)
    126248
     249
    127250    def test_addHandler(self):
    128251        """
     
    132255        handler = DummyXMPPHandler()
    133256        handler.setHandlerParent(sm)
    134         self.assertIn(handler, sm)
    135         self.assertIdentical(sm, handler.parent)
    136257
    137258        self.assertEquals(0, handler.doneMade)
    138259        self.assertEquals(0, handler.doneInitialized)
    139260        self.assertEquals(0, handler.doneLost)
     261
    140262
    141263    def test_addHandlerInitialized(self):
     
    188310        self.assertEquals("<presence/>", xs.transport.value())
    189311
     312
    190313    def test_sendNotConnected(self):
    191314        """
     
    216339
    217340        self.assertEquals("<presence/>", xs.transport.value())
    218         self.failIf(sm._packetQueue)
     341        self.assertFalse(sm._packetQueue)
     342
    219343
    220344    def test_sendNotInitialized(self):
     
    236360        self.assertEquals("<presence/>", sm._packetQueue[0])
    237361
     362
    238363    def test_sendDisconnected(self):
    239364        """
     
    258383
    259384
     385
    260386class DummyIQHandler(subprotocols.IQHandlerMixin):
    261387    iqHandlers = {'/iq[@type="get"]': 'onGet'}
Note: See TracChangeset for help on using the changeset viewer.