test_lib_pubsub_notify4.py 2.53 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
"""

:copyright: Copyright 2006-2009 by Oliver Schoenborn, all rights reserved.
:license: BSD, see LICENSE.txt for details.


"""

import unittest
from unittests import wtc

import six
from difflib import ndiff, unified_diff, context_diff


#---------------------------------------------------------------------------



class lib_pubsub_NotifyN(wtc.PubsubTestCase):

    def testNotifications(self):
        from wx.lib.pubsub.utils.notification import INotificationHandler

        class Handler(INotificationHandler):
            def __init__(self):
                self.resetCounts()
            def resetCounts(self):
                self.counts = dict(send=0, sub=0, unsub=0, delt=0, newt=0, dead=0, all=0)
            def notifySubscribe(self, pubListener, topicObj, newSub):
                self.counts['sub'] += 1
            def notifyUnsubscribe(self, pubListener, topicObj):
                self.counts['unsub'] += 1
            def notifyDeadListener(self, pubListener, topicObj):
                self.counts['dead'] += 1
            def notifySend(self, stage, topicObj, pubListener=None):
                if stage == 'pre': self.counts['send'] += 1
            def notifyNewTopic(self, topicObj, description, required, argsDocs):
                self.counts['newt'] += 1
            def notifyDelTopic(self, topicName):
                self.counts['delt'] += 1

        notifiee = Handler()
        self.pub.addNotificationHandler(notifiee)
        self.pub.setNotificationFlags(all=True)

        def verify(**ref):
            for key, val in six.iteritems(notifiee.counts):
                if key in ref:
                    self.assertEqual(val, ref[key], "\n%s\n%s" % (notifiee.counts, ref) )
                else:
                    self.assertEqual(val, 0, "%s = %s, expected 0" % (key, val))
            notifiee.resetCounts()

        verify()
        def testListener():
            pass
        def testListener2():
            pass

        self.pub.getDefaultTopicMgr().getOrCreateTopic('newTopic')
        verify(newt=1)

        self.pub.subscribe(testListener, 'newTopic')
        self.pub.subscribe(testListener2, 'newTopic')
        verify(sub=2)

        self.pub.sendMessage('newTopic')
        verify(send=1)

        del testListener
        verify(dead=1)

        self.pub.unsubscribe(testListener2,'newTopic')
        verify(unsub=1)

        self.pub.getDefaultTopicMgr().delTopic('newTopic')
        verify(delt=1)


#---------------------------------------------------------------------------


if __name__ == '__main__':
    unittest.main()