Esempio n. 1
0
    def setUp(self):
        self.reg = Registry()
        self.called = []

        self.op_queue = []
        def remote_cleanup():
            self.called.append('remote_cleanup')
        self.account = TestAccount(self.reg, (self.op_queue.append,
                                              remote_cleanup))

        self.server = TestServer(self.called.append)

        self.mainthread_queue = []
        mock_main_thread(self.mainthread_queue.append)
Esempio n. 2
0
 def setUp(self):
     self.reg = Registry()
     self.events = []
Esempio n. 3
0
class RegistryTest(unittest.TestCase):
    def setUp(self):
        self.reg = Registry()
        self.events = []

    def subscribe(self, selector, name):
        callback = lambda **kwargs: self.events.append( (name, kwargs) )
        self.reg.subscribe(selector, callback)
        return callback

    def unsubscribe(self, selector, callback):
        self.reg.unsubscribe(selector, callback)

    def assertEvents(self, *reference):
        self.assertEqual(len(reference), len(self.events))
        for item in reference:
            self.assertTrue(item in self.events)
        self.events[:] = []

    def test_basic(self):
        self.subscribe('sel1', 'first subscriber')
        self.reg.notify('sel1', msg="hello")
        self.assertEvents(('first subscriber', {'msg': 'hello'}))

    def test_two_subscribers(self):
        self.subscribe('sel1', 'first')
        self.subscribe('sel1', 'second')
        self.reg.notify('sel1', a="b")
        self.assertEvents(('first', {'a': 'b'}), ('second', {'a': 'b'}))

    def test_two_events(self):
        self.subscribe('sel', 'sub')
        self.reg.notify('sel', a="b")
        self.reg.notify('sel', c="d")
        self.assertEvents(('sub', {'a': 'b'}), ('sub', {'c': 'd'}))

    def test_discriminate(self):
        self.subscribe('sel1', 'first')
        self.subscribe('sel2', 'second')
        self.reg.notify('sel1')
        self.assertEvents(('first', {}))

    def test_unsubscribe(self):
        c1 = self.subscribe('sel', 'first')
        c2 = self.subscribe('sel', 'second')
        self.reg.notify('sel')
        self.assertEvents(('first', {}), ('second', {}))

        self.unsubscribe('sel', c1)
        self.reg.notify('sel')
        self.assertEvents(('second', {}))

        self.unsubscribe('sel', c2)
        self.reg.notify('sel')
        self.assertEvents()

        self.assertRaises(KeyError, self.unsubscribe, 'sel', c1)

    def test_objc_selector(self):
        events = self.events

        from Foundation import NSObject
        class ObjcClass(NSObject):
            @classmethod
            def new_with_name(cls, name):
                i = cls.new()
                i.name = name
                return i
            def dosomething(self, msg):
                events.append( (self.name, {'msg': msg}) )

        i1 = ObjcClass.new_with_name('x')
        i2 = ObjcClass.new_with_name('y')
        self.reg.subscribe('sel', i1.dosomething)
        self.reg.subscribe('sel', i2.dosomething)
        self.reg.notify('sel', msg=3)
        self.assertEvents(('x', {'msg': 3}), ('y', {'msg': 3}))

        self.assertRaises(KeyError, self.reg.unsubscribe, 'z', i1.dosomething)

        self.reg.unsubscribe('sel', i1.dosomething)
        self.reg.notify('sel', msg=3)
        self.assertEvents(('y', {'msg': 3}))

        self.reg.unsubscribe('sel', i2.dosomething)
        self.reg.notify('sel', msg=3)
        self.assertEvents()
Esempio n. 4
0
class AccountTest(unittest.TestCase):
    def setUp(self):
        self.reg = Registry()
        self.called = []

        self.op_queue = []
        def remote_cleanup():
            self.called.append('remote_cleanup')
        self.account = TestAccount(self.reg, (self.op_queue.append,
                                              remote_cleanup))

        self.server = TestServer(self.called.append)

        self.mainthread_queue = []
        mock_main_thread(self.mainthread_queue.append)

    def tearDown(self):
        restore_main_thread()

    def _run_loop(self):
        while True:
            if self.op_queue:
                op = self.op_queue.pop()
                op.is_queued(self.reg)
                op(self.server)
            elif self.mainthread_queue:
                callback = self.mainthread_queue.pop()
                callback()
            else:
                break

    def test_list_folders(self):
        self.assertEqual(self.account.folders, [])
        self.account.update_if_needed()
        self._run_loop()

        self.assertEqual(self.called, ['get_mailboxes'])
        self.assertEqual(len(self.account.folders), 2)
        folder_one = self.account.folders[0]
        self.assertTrue(isinstance(folder_one, Folder))
        self.assertEqual(folder_one.imap_name, 'folder one')

    def test_messages_in_folder(self):
        self.account.update_if_needed()
        self._run_loop()
        folder_one = self.account.folders[0]
        self.assertEqual(folder_one.messages, {})
        self.called[:] = []

        folder_one.update_if_needed()
        self._run_loop()
        self.assertEqual(self.called, ['__enter__', '__exit__',
                '__enter__', 'message_headers [6]', '__exit__'])
        self.assertEqual(len(folder_one.messages), 1)

    def test_message(self):
        self.account.update_if_needed()
        self._run_loop()
        folder_one = self.account.folders[0]
        folder_one.update_if_needed()
        self._run_loop()
        message = folder_one.messages[6]
        self.assertEqual(dict(message.headers), {'From': '*****@*****.**'})
        self.called[:] = []

        message.request_fullmsg()
        self._run_loop()
        self.assertEqual(self.called, ['__enter__', 'full_message',
                                       '__exit__'])

    def test_events(self):
        events = []
        def event_logger(name):
            return lambda **kwargs: events.append( (name, kwargs) )

        self.reg.subscribe((self.account, 'folders_updated'),
                           event_logger('folders updated'))
        self.account.update_if_needed()
        self._run_loop()
        self.assertEqual(events,
                         [('folders updated', {'account': self.account})])
        events[:] = []

        folder_one = self.account.folders[0]
        self.reg.subscribe((folder_one, 'messages_updated'),
                           event_logger('messages updated'))
        folder_one.update_if_needed()
        self._run_loop()
        self.assertEqual(events,
                         [('messages updated', {'folder': folder_one}),
                          ('messages updated', {'folder': folder_one})])
        events[:] = []

        message = folder_one.messages[6]
        self.reg.subscribe((message, 'full_message'),
                           event_logger('full message'))
        message.request_fullmsg()
        self._run_loop()
        self.assertEqual(len(events), 1)
        self.assertEqual(events[0][0], 'full message')
        self.assertTrue(events[0][1]['message'] is message)
        mime = events[0][1]['mime']
        self.assertEqual(mime['From'], '*****@*****.**')
        self.assertEqual(mime.get_payload(), 'hello world!')
        events[:] = []