示例#1
0
    def setUp(self):
        self.jid = '[email protected]/Gajim'
        self.conn = MockConnection(account_name, {'send_stanza': None})
        self.sess = ChatControlSession(self.conn, self.jid, None)
        gajim.logger = MockLogger()

        # initially there are no events
        self.assertEqual(0, len(gajim.events.get_events(account_name)))

        # no notifications have been sent
        self.assertEqual(0, len(notify.notifications))
示例#2
0
	def setUp(self):
		self.jid = '[email protected]/Gajim'
		self.conn = MockConnection(account_name, {'send_stanza': None})
		self.sess = ChatControlSession(self.conn, self.jid, None)
		gajim.logger = MockLogger()

		# initially there are no events
		self.assertEqual(0, len(gajim.events.get_events(account_name)))

		# no notifications have been sent
		self.assertEqual(0, len(notify.notifications))
示例#3
0
class TestChatControlSession(unittest.TestCase):
    def setUp(self):
        self.jid = '[email protected]/Gajim'
        self.conn = MockConnection(account_name, {'send_stanza': None})
        self.sess = ChatControlSession(self.conn, self.jid, None)
        gajim.logger = MockLogger()

        # initially there are no events
        self.assertEqual(0, len(gajim.events.get_events(account_name)))

        # no notifications have been sent
        self.assertEqual(0, len(notify.notifications))

    def tearDown(self):
        # remove all events and notifications that were added
        gajim.events._events = {}
        notify.notifications = []

    def receive_chat_msg(self, jid, msgtxt):
        '''simulate receiving a chat message from jid'''
        msg = xmpp.Message()
        msg.setBody(msgtxt)
        msg.setType('chat')

        tim = time.localtime()
        encrypted = False
        self.sess.received(jid, msgtxt, tim, encrypted, msg)

    # ----- custom assertions -----
    def assert_new_message_notification(self):
        '''a new_message notification has been sent'''
        self.assertEqual(1, len(notify.notifications))
        notif = notify.notifications[0]
        self.assertEqual('new_message', notif[0])

    def assert_first_message_notification(self):
        '''this message was treated as a first message'''
        self.assert_new_message_notification()
        notif = notify.notifications[0]
        params = notif[3]
        first = params[1]
        self.assert_(first,
                     'message should have been treated as a first message')

    def assert_not_first_message_notification(self):
        '''this message was not treated as a first message'''
        self.assert_new_message_notification()
        notif = notify.notifications[0]
        params = notif[3]
        first = params[1]
        self.assert_(not first,
                     'message was unexpectedly treated as a first message')

    # ----- tests -----
    def test_receive_nocontrol(self):
        '''test receiving a message in a blank state'''
        jid = '[email protected]/Gajim'
        msgtxt = 'testing one two three'

        self.receive_chat_msg(jid, msgtxt)

        # message was logged
        calls = gajim.logger.mockGetNamedCalls('write')
        self.assertEqual(1, len(calls))

        # no ChatControl was open and autopopup was off
        # so the message goes into the event queue
        self.assertEqual(1, len(gajim.events.get_events(account_name)))

        self.assert_first_message_notification()

        # no control is attached to the session
        self.assertEqual(None, self.sess.control)

    def test_receive_already_has_control(self):
        '''test receiving a message with a session already attached to a
		control'''

        jid = '[email protected]/Gajim'
        msgtxt = 'testing one two three'

        self.sess.control = MockChatControl(jid, account_name)

        self.receive_chat_msg(jid, msgtxt)

        # message was logged
        calls = gajim.logger.mockGetNamedCalls('write')
        self.assertEqual(1, len(calls))

        # the message does not go into the event queue
        self.assertEqual(0, len(gajim.events.get_events(account_name)))

        self.assert_not_first_message_notification()

        # message was printed to the control
        calls = self.sess.control.mockGetNamedCalls('print_conversation')
        self.assertEqual(1, len(calls))

    def test_received_orphaned_control(self):
        '''test receiving a message when a control that doesn't have a session
		attached exists'''

        jid = '*****@*****.**'
        fjid = jid + '/Gajim'
        msgtxt = 'testing one two three'

        ctrl = MockChatControl(jid, account_name)
        gajim.interface.msg_win_mgr = Mock({'get_control': ctrl})
        gajim.interface.msg_win_mgr.mockSetExpectation(
            'get_control', expectParams(jid, account_name))

        self.receive_chat_msg(fjid, msgtxt)

        # message was logged
        calls = gajim.logger.mockGetNamedCalls('write')
        self.assertEqual(1, len(calls))

        # the message does not go into the event queue
        self.assertEqual(0, len(gajim.events.get_events(account_name)))

        self.assert_not_first_message_notification()

        # this session is now attached to that control
        self.assertEqual(self.sess, ctrl.session)
        self.assertEqual(ctrl, self.sess.control, 'foo')

        # message was printed to the control
        calls = ctrl.mockGetNamedCalls('print_conversation')
        self.assertEqual(1, len(calls))
示例#4
0
class TestChatControlSession(unittest.TestCase):
	def setUp(self):
		self.jid = '[email protected]/Gajim'
		self.conn = MockConnection(account_name, {'send_stanza': None})
		self.sess = ChatControlSession(self.conn, self.jid, None)
		gajim.logger = MockLogger()

		# initially there are no events
		self.assertEqual(0, len(gajim.events.get_events(account_name)))

		# no notifications have been sent
		self.assertEqual(0, len(notify.notifications))

	def tearDown(self):
		# remove all events and notifications that were added
		gajim.events._events = {}
		notify.notifications = []

	def receive_chat_msg(self, jid, msgtxt):
		'''simulate receiving a chat message from jid'''
		msg = xmpp.Message()
		msg.setBody(msgtxt)
		msg.setType('chat')

		tim = time.localtime()
		encrypted = False
		self.sess.received(jid, msgtxt, tim, encrypted, msg)

	# ----- custom assertions -----
	def assert_new_message_notification(self):
		'''a new_message notification has been sent'''
		self.assertEqual(1, len(notify.notifications))
		notif = notify.notifications[0]
		self.assertEqual('new_message', notif[0])

	def assert_first_message_notification(self):
		'''this message was treated as a first message'''
		self.assert_new_message_notification()
		notif = notify.notifications[0]
		params = notif[3]
		first = params[1]
		self.assert_(first, 'message should have been treated as a first message')

	def assert_not_first_message_notification(self):
		'''this message was not treated as a first message'''
		self.assert_new_message_notification()
		notif = notify.notifications[0]
		params = notif[3]
		first = params[1]
		self.assert_(not first,
			'message was unexpectedly treated as a first message')

	# ----- tests -----
	def test_receive_nocontrol(self):
		'''test receiving a message in a blank state'''
		jid = '[email protected]/Gajim'
		msgtxt = 'testing one two three'

		self.receive_chat_msg(jid, msgtxt)

		# message was logged
		calls = gajim.logger.mockGetNamedCalls('write')
		self.assertEqual(1, len(calls))

		# no ChatControl was open and autopopup was off
		# so the message goes into the event queue
		self.assertEqual(1, len(gajim.events.get_events(account_name)))

		self.assert_first_message_notification()

		# no control is attached to the session
		self.assertEqual(None, self.sess.control)

	def test_receive_already_has_control(self):
		'''test receiving a message with a session already attached to a
		control'''

		jid = '[email protected]/Gajim'
		msgtxt = 'testing one two three'

		self.sess.control = MockChatControl(jid, account_name)

		self.receive_chat_msg(jid, msgtxt)

		# message was logged
		calls = gajim.logger.mockGetNamedCalls('write')
		self.assertEqual(1, len(calls))

		# the message does not go into the event queue
		self.assertEqual(0, len(gajim.events.get_events(account_name)))

		self.assert_not_first_message_notification()

		# message was printed to the control
		calls = self.sess.control.mockGetNamedCalls('print_conversation')
		self.assertEqual(1, len(calls))

	def test_received_orphaned_control(self):
		'''test receiving a message when a control that doesn't have a session
		attached exists'''

		jid = '*****@*****.**'
		fjid = jid + '/Gajim'
		msgtxt = 'testing one two three'

		ctrl = MockChatControl(jid, account_name)
		gajim.interface.msg_win_mgr = Mock({'get_control': ctrl})
		gajim.interface.msg_win_mgr.mockSetExpectation('get_control',
			expectParams(jid, account_name))

		self.receive_chat_msg(fjid, msgtxt)

		# message was logged
		calls = gajim.logger.mockGetNamedCalls('write')
		self.assertEqual(1, len(calls))

		# the message does not go into the event queue
		self.assertEqual(0, len(gajim.events.get_events(account_name)))

		self.assert_not_first_message_notification()

		# this session is now attached to that control
		self.assertEqual(self.sess, ctrl.session)
		self.assertEqual(ctrl, self.sess.control, 'foo')

		# message was printed to the control
		calls = ctrl.mockGetNamedCalls('print_conversation')
		self.assertEqual(1, len(calls))