def test_manage_listeners(self): dispatcher = EventDispatcher() listeners = list(dispatcher.get_listeners(TestEvent)) self.assertEqual(len(listeners), 0) test_callback_prio_0_cb_0 = Mock() test_callback_prio_0_cb_1 = Mock() test_callback_prio_1_cb_0 = Mock() other_callback_prio_2_cb_0 = Mock() # Register callbacks, first priority 1 ... dispatcher.add_listener(TestEvent, 1, test_callback_prio_1_cb_0) # ... aftwerwards priority 0 ... key_prio_0_cb_0 = dispatcher.add_listener(TestEvent, 0, test_callback_prio_0_cb_0) dispatcher.add_listener(TestEvent, 0, test_callback_prio_0_cb_1) # ... and finally priority 2 for another event. dispatcher.add_listener(OtherEvent, 2, other_callback_prio_2_cb_0) # Collect callbacks from listeners list. actual_handlers = [(key.priority, handler.callback) for key, handler in dispatcher.get_listeners(TestEvent)] expected_handlers = [ (0, test_callback_prio_0_cb_0), (0, test_callback_prio_0_cb_1), (1, test_callback_prio_1_cb_0), ] self.assertEqual(expected_handlers, actual_handlers) # Remove one listener. dispatcher.remove_listener(TestEvent, key_prio_0_cb_0) # Collect callbacks from listeners list. actual_handlers = [(key.priority, handler.callback) for key, handler in dispatcher.get_listeners(TestEvent)] expected_handlers = [ (0, test_callback_prio_0_cb_1), (1, test_callback_prio_1_cb_0), ] self.assertEqual(expected_handlers, actual_handlers)