test_sessions.py 5.58 KB
Newer Older
1 2
import unittest

3 4
import lib
lib.setup_env()
5

6
import notify
André's avatar
André committed
7
import nbxmpp
8

9
from gajim.common import app
André's avatar
André committed
10 11 12
from gajim.common import nec
from gajim.common import ged
from gajim.common.nec import NetworkEvent
13
from gajim.common.modules.message import MessageReceivedEvent
André's avatar
André committed
14
from gajim.common.modules.message import DecryptedMessageReceivedEvent
15

André's avatar
André committed
16 17
from gajim.session import ChatControlSession
from gajim.roster_window import RosterWindow
18

19
from gajim_mocks import *
Weblate's avatar
Weblate committed
20
from data import account1
21

22
app.interface = MockInterface()
23

24 25

# name to use for the test account
Weblate's avatar
Weblate committed
26
account_name = account1
27 28 29


class TestChatControlSession(unittest.TestCase):
30
    ''' Testclass for session.py '''
31

Yann Leboulanger's avatar
Yann Leboulanger committed
32 33
    @classmethod
    def setUpClass(cls):
34
        app.nec = nec.NetworkEventsController()
Yann Leboulanger's avatar
Yann Leboulanger committed
35
        cls.conn = MockConnection(account_name, {'send_stanza': None})
36 37
        app.logger = MockLogger()
        app.default_session_type = ChatControlSession
38

Yann Leboulanger's avatar
Yann Leboulanger committed
39
    def setUp(self):
40
        app.notification = notify.Notification()
41

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

45
    def tearDown(self):
46
        app.notification.clean()
47

48 49
    def receive_chat_msg(self, jid, msgtxt):
        '''simulate receiving a chat message from jid'''
Yann Leboulanger's avatar
Yann Leboulanger committed
50
        msg = nbxmpp.Message()
51 52
        msg.setBody(msgtxt)
        msg.setType('chat')
53

Yann Leboulanger's avatar
Yann Leboulanger committed
54 55 56 57
        xml = """<message from='%s' id='1' type='chat'><body>%s</body>
            <thread>123</thread></message>""" % (jid, msgtxt)
        stanza = nbxmpp.protocol.Message(node=nbxmpp.simplexml.XML2Node(xml))
        self.conn._messageCB(None, stanza)
58

59 60 61 62
    # ----- custom assertions -----
    def assert_new_message_notification(self):
        '''a new_message notification has been sent'''
        self.assertEqual(1, len(notify.notifications))
Yann Leboulanger's avatar
Yann Leboulanger committed
63 64
        notif = notify.notifications[-1]
        self.assertEqual('New Message', notif.popup_event_type)
65

66 67 68
    def assert_first_message_notification(self):
        '''this message was treated as a first message'''
        self.assert_new_message_notification()
Yann Leboulanger's avatar
Yann Leboulanger committed
69 70
        notif = notify.notifications[-1]
        first = notif.first_unread
71
        self.assertTrue(first,
Yann Leboulanger's avatar
Yann Leboulanger committed
72
            'message should have been treated as a first message')
73

74 75 76
    def assert_not_first_message_notification(self):
        '''this message was not treated as a first message'''
        self.assert_new_message_notification()
Yann Leboulanger's avatar
Yann Leboulanger committed
77 78
        notif = notify.notifications[-1]
        first = notif.first_unread
79
        self.assertTrue(not first,
Yann Leboulanger's avatar
Yann Leboulanger committed
80
            'message was unexpectedly treated as a first message')
81

82
    # ----- tests -----
Yann Leboulanger's avatar
Yann Leboulanger committed
83
    def test_receive_1nocontrol(self):
84
        '''test receiving a message in a blank state'''
Yann Leboulanger's avatar
Yann Leboulanger committed
85 86 87 88 89
        jid = 'bct@necronomicorp.com'
        fjid = 'bct@necronomicorp.com/Gajim'
        msgtxt = 'testing one'

        self.receive_chat_msg(fjid, msgtxt)
90

Yann Leboulanger's avatar
Yann Leboulanger committed
91
        # session is created
92
        self.assertTrue((jid in self.conn.sessions) and (
Yann Leboulanger's avatar
Yann Leboulanger committed
93 94
            '123' in self.conn.sessions[jid]), 'session is not created')
        sess = self.conn.sessions[jid]['123']
95

96
        # message was logged
97
        calls = app.logger.mockGetNamedCalls('insert_into_logs')
98
        self.assertEqual(1, len(calls))
99

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

104
        self.assert_first_message_notification()
105

106
        # no control is attached to the session
Yann Leboulanger's avatar
Yann Leboulanger committed
107
        self.assertEqual(None, sess.control)
108

Yann Leboulanger's avatar
Yann Leboulanger committed
109
    def test_receive_2already_has_control(self):
110 111
        '''test receiving a message with a session already attached to a
        control'''
Yann Leboulanger's avatar
Yann Leboulanger committed
112 113 114
        jid = 'bct@necronomicorp.com'
        fjid = 'bct@necronomicorp.com/Gajim'
        msgtxt = 'testing two'
Weblate's avatar
Weblate committed
115
        app.interface.roster = RosterWindow(app.app)
116

Yann Leboulanger's avatar
Yann Leboulanger committed
117 118
        sess = self.conn.sessions[jid]['123']
        sess.control = MockChatControl(fjid, account_name)
119

Yann Leboulanger's avatar
Yann Leboulanger committed
120
        self.receive_chat_msg(fjid, msgtxt)
121

122
        # message was logged
123
        calls = app.logger.mockGetNamedCalls('insert_into_logs')
Yann Leboulanger's avatar
Yann Leboulanger committed
124
        self.assertEqual(2, len(calls))
125

126
        # the message does not go into the event queue
127
        self.assertEqual(1, len(app.events.get_events(account_name)))
128

129
        self.assert_not_first_message_notification()
130

131
        # message was printed to the control
Yann Leboulanger's avatar
Yann Leboulanger committed
132
        calls = sess.control.mockGetNamedCalls('print_conversation')
133
        self.assertEqual(1, len(calls))
Weblate's avatar
Weblate committed
134
        app.interface.roster.window.destroy()
135

Yann Leboulanger's avatar
Yann Leboulanger committed
136 137 138
    #def test_received_3orphaned_control(self):
        #'''test receiving a message when a control that doesn't have a session
        #attached exists'''
139

Yann Leboulanger's avatar
Yann Leboulanger committed
140 141 142
        #jid = 'bct@necronomicorp.com'
        #fjid = jid + '/Gajim'
        #msgtxt = 'testing three'
143

Yann Leboulanger's avatar
Yann Leboulanger committed
144 145 146 147
        #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))
148

Yann Leboulanger's avatar
Yann Leboulanger committed
149
        #self.receive_chat_msg(fjid, msgtxt)
150

Yann Leboulanger's avatar
Yann Leboulanger committed
151
        ## message was logged
Yann Leboulanger's avatar
Yann Leboulanger committed
152
        #calls = gajim.logger.mockGetNamedCalls('insert_into_logs')
Yann Leboulanger's avatar
Yann Leboulanger committed
153
        #self.assertEqual(1, len(calls))
154

Yann Leboulanger's avatar
Yann Leboulanger committed
155 156
        ## the message does not go into the event queue
        #self.assertEqual(0, len(gajim.events.get_events(account_name)))
157

Yann Leboulanger's avatar
Yann Leboulanger committed
158
        #self.assert_not_first_message_notification()
159

Yann Leboulanger's avatar
Yann Leboulanger committed
160 161 162
        ## this session is now attached to that control
        #self.assertEqual(self.sess, ctrl.session)
        #self.assertEqual(ctrl, self.sess.control, 'foo')
163

Yann Leboulanger's avatar
Yann Leboulanger committed
164 165 166
        ## message was printed to the control
        #calls = ctrl.mockGetNamedCalls('print_conversation')
        #self.assertEqual(1, len(calls))
167 168 169

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