File: test_lib_pubsub_notify2_1.py

package info (click to toggle)
wxpython4.0 4.2.3%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 221,752 kB
  • sloc: cpp: 962,555; python: 230,573; ansic: 170,731; makefile: 51,756; sh: 9,342; perl: 1,564; javascript: 584; php: 326; xml: 200
file content (75 lines) | stat: -rw-r--r-- 2,682 bytes parent folder | download | duplicates (4)
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
"""

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


"""

import unittest

from unittests import wtc



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


class lib_pubsub_Notify2_1(wtc.PubsubTestCase):


    def test1_SubscribeNotify(self):
        from wx.lib.pubsub.utils.notification import useNotifyByPubsubMessage

        useNotifyByPubsubMessage()

        class MyListener:
            countSub = 0
            countUnsub = 0
            def listenerSub(self, msgTopic=self.pub.AUTO_TOPIC, listener=None,
                            topic=None, newSub=None):
                self.assertEqual(msgTopic.getName(), 'pubsub.subscribe' )
                assert topic.getName() in ('pubsub.unsubscribe', 'testSubscribeNotify')
                if newSub:
                    self.countSub += 1
            def listenerUnsub(self, msgTopic=self.pub.AUTO_TOPIC, topic=None,
                              listener=None, listenerRaw=None):
                assert topic.getName() in ('testSubscribeNotify', 'pubsub.subscribe' )
                self.assertEqual(msgTopic.getName(), 'pubsub.unsubscribe' )
                if listener is not None:
                    self.countUnsub += 1
            def listenerTest(self):
                raise NotImplementedError # should never get here

        self.pub.setNotificationFlags(subscribe=True, unsubscribe=True)
        self.pub.getDefaultTopicMgr().getOrCreateTopic('testSubscribeNotify')
        tmp = MyListener()
        tmp.assertEqual = self.assertEqual

        self.pub.subscribe(tmp.listenerSub, 'pubsub.subscribe')
        self.assertEqual(tmp.countSub, 0)   # don't notify of self subscription
        self.assertEqual(tmp.countUnsub, 0)
        sl, ok = self.pub.subscribe(tmp.listenerUnsub, 'pubsub.unsubscribe')
        assert ok
        self.assertEqual(tmp.countSub, 1)
        self.assertEqual(tmp.countUnsub, 0)

        self.pub.subscribe(tmp.listenerTest, 'testSubscribeNotify')
        self.assertEqual(tmp.countUnsub, 0)
        self.pub.unsubscribe(tmp.listenerTest, 'testSubscribeNotify')
        self.assertEqual(tmp.countUnsub, 1)

        self.pub.unsubscribe(tmp.listenerSub,   'pubsub.subscribe')
        self.assertEqual(tmp.countSub, 2)
        self.assertEqual(tmp.countUnsub, 2)
        self.pub.unsubscribe(tmp.listenerUnsub, 'pubsub.unsubscribe')
        self.assertEqual(tmp.countSub, 2)
        self.assertEqual(tmp.countUnsub, 2) # don't notify of self unsubscription



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


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