no need of the jid parameter when we look for self contact iter
[gajim.git] / test / test_sessions.py
bloba28b9259b9bbbdc1ee2e2821f4cebf4c0e8368ef
1 import unittest
3 import time
5 import lib
6 lib.setup_env()
8 from common import gajim
9 from common import xmpp
11 from mock import Mock, expectParams
12 from gajim_mocks import *
14 from common.stanza_session import StanzaSession
16 # name to use for the test account
17 account_name = 'test'
19 class TestStanzaSession(unittest.TestCase):
20 ''' Testclass for common/stanzasession.py '''
21 def setUp(self):
22 self.jid = 'test@example.org/Gajim'
23 self.conn = MockConnection(account_name, {'send_stanza': None})
24 self.sess = StanzaSession(self.conn, self.jid, None, 'chat')
26 def test_generate_thread_id(self):
27 # thread_id is a string
28 self.assert_(isinstance(self.sess.thread_id, str))
30 # it should be somewhat long, to avoid clashes
31 self.assert_(len(self.sess.thread_id) >= 32)
33 def test_is_loggable(self):
34 # by default a session should be loggable
35 # (unless the no_log_for setting says otherwise)
36 self.assert_(self.sess.is_loggable())
38 def test_terminate(self):
39 # termination is sent by default
40 self.sess.last_send = time.time()
41 self.sess.terminate()
43 self.assertEqual(None, self.sess.status)
45 calls = self.conn.mockGetNamedCalls('send_stanza')
46 msg = calls[0].getParam(0)
48 self.assertEqual(msg.getThread(), self.sess.thread_id)
50 def test_terminate_without_sending(self):
51 # no termination is sent if no messages have been sent in the session
52 self.sess.terminate()
54 self.assertEqual(None, self.sess.status)
56 calls = self.conn.mockGetNamedCalls('send_stanza')
57 self.assertEqual(0, len(calls))
59 def test_terminate_no_remote_xep_201(self):
60 # no termination is sent if only messages without thread ids have been
61 # received
62 self.sess.last_send = time.time()
63 self.sess.last_receive = time.time()
64 self.sess.terminate()
66 self.assertEqual(None, self.sess.status)
68 calls = self.conn.mockGetNamedCalls('send_stanza')
69 self.assertEqual(0, len(calls))
71 from session import ChatControlSession
73 gajim.interface = MockInterface()
75 import notify
77 class TestChatControlSession(unittest.TestCase):
78 ''' Testclass for session.py '''
79 def setUp(self):
80 self.jid = 'test@example.org/Gajim'
81 self.conn = MockConnection(account_name, {'send_stanza': None})
82 self.sess = ChatControlSession(self.conn, self.jid, None)
83 gajim.logger = MockLogger()
85 # initially there are no events
86 self.assertEqual(0, len(gajim.events.get_events(account_name)))
88 # no notifications have been sent
89 self.assertEqual(0, len(notify.notifications))
91 def tearDown(self):
92 # remove all events and notifications that were added
93 gajim.events._events = {}
94 notify.notifications = []
96 def receive_chat_msg(self, jid, msgtxt):
97 '''simulate receiving a chat message from jid'''
98 msg = xmpp.Message()
99 msg.setBody(msgtxt)
100 msg.setType('chat')
102 tim = time.localtime()
103 encrypted = False
104 self.sess.received(jid, msgtxt, tim, encrypted, msg)
106 # ----- custom assertions -----
107 def assert_new_message_notification(self):
108 '''a new_message notification has been sent'''
109 self.assertEqual(1, len(notify.notifications))
110 notif = notify.notifications[0]
111 self.assertEqual('new_message', notif[0])
113 def assert_first_message_notification(self):
114 '''this message was treated as a first message'''
115 self.assert_new_message_notification()
116 notif = notify.notifications[0]
117 params = notif[3]
118 first = params[1]
119 self.assert_(first, 'message should have been treated as a first message')
121 def assert_not_first_message_notification(self):
122 '''this message was not treated as a first message'''
123 self.assert_new_message_notification()
124 notif = notify.notifications[0]
125 params = notif[3]
126 first = params[1]
127 self.assert_(not first,
128 'message was unexpectedly treated as a first message')
130 # ----- tests -----
131 def test_receive_nocontrol(self):
132 '''test receiving a message in a blank state'''
133 jid = 'bct@necronomicorp.com/Gajim'
134 msgtxt = 'testing one two three'
136 self.receive_chat_msg(jid, msgtxt)
138 # message was logged
139 calls = gajim.logger.mockGetNamedCalls('write')
140 self.assertEqual(1, len(calls))
142 # no ChatControl was open and autopopup was off
143 # so the message goes into the event queue
144 self.assertEqual(1, len(gajim.events.get_events(account_name)))
146 self.assert_first_message_notification()
148 # no control is attached to the session
149 self.assertEqual(None, self.sess.control)
151 def test_receive_already_has_control(self):
152 '''test receiving a message with a session already attached to a
153 control'''
155 jid = 'bct@necronomicorp.com/Gajim'
156 msgtxt = 'testing one two three'
158 self.sess.control = MockChatControl(jid, account_name)
160 self.receive_chat_msg(jid, msgtxt)
162 # message was logged
163 calls = gajim.logger.mockGetNamedCalls('write')
164 self.assertEqual(1, len(calls))
166 # the message does not go into the event queue
167 self.assertEqual(0, len(gajim.events.get_events(account_name)))
169 self.assert_not_first_message_notification()
171 # message was printed to the control
172 calls = self.sess.control.mockGetNamedCalls('print_conversation')
173 self.assertEqual(1, len(calls))
175 def test_received_orphaned_control(self):
176 '''test receiving a message when a control that doesn't have a session
177 attached exists'''
179 jid = 'bct@necronomicorp.com'
180 fjid = jid + '/Gajim'
181 msgtxt = 'testing one two three'
183 ctrl = MockChatControl(jid, account_name)
184 gajim.interface.msg_win_mgr = Mock({'get_control': ctrl})
185 gajim.interface.msg_win_mgr.mockSetExpectation('get_control',
186 expectParams(jid, account_name))
188 self.receive_chat_msg(fjid, msgtxt)
190 # message was logged
191 calls = gajim.logger.mockGetNamedCalls('write')
192 self.assertEqual(1, len(calls))
194 # the message does not go into the event queue
195 self.assertEqual(0, len(gajim.events.get_events(account_name)))
197 self.assert_not_first_message_notification()
199 # this session is now attached to that control
200 self.assertEqual(self.sess, ctrl.session)
201 self.assertEqual(ctrl, self.sess.control, 'foo')
203 # message was printed to the control
204 calls = ctrl.mockGetNamedCalls('print_conversation')
205 self.assertEqual(1, len(calls))
207 if __name__ == '__main__':
208 unittest.main()
210 # vim: se ts=3: