1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
|
'''
Tests for the miscellaneous functions scattered throughout src/gajim.py
'''
import unittest
import lib
lib.setup_env()
from common import gajim
from gajim import Interface
from gajim_mocks import *
gajim.logger = MockLogger()
Interface()
class TestMiscInterface(unittest.TestCase):
def test_links_regexp_entire(self):
def assert_matches_all(str_):
m = gajim.interface.basic_pattern_re.match(str_)
# the match should equal the string
str_span = (0, len(str_))
self.assertEqual(m.span(), str_span)
# these entire strings should be parsed as links
assert_matches_all('http://google.com/')
assert_matches_all('http://google.com')
assert_matches_all('http://www.google.ca/search?q=xmpp')
assert_matches_all('http://tools.ietf.org/html/draft-saintandre-rfc3920bis-05#section-12.3')
assert_matches_all('http://en.wikipedia.org/wiki/Protocol_(computing)')
assert_matches_all(
'http://en.wikipedia.org/wiki/Protocol_%28computing%29')
assert_matches_all('mailto:test@example.org')
assert_matches_all('xmpp:example-node@example.com')
assert_matches_all('xmpp:example-node@example.com/some-resource')
assert_matches_all('xmpp:example-node@example.com?message')
assert_matches_all('xmpp://guest@example.com/support@example.com?message')
import time
from data import *
import roster_window
import notify
class TestStatusChange(unittest.TestCase):
'''tests gajim.py's incredibly complex handle_event_notify'''
def setUp(self):
gajim.interface.roster = roster_window.RosterWindow()
for acc in contacts:
gajim.connections[acc] = MockConnection(acc)
gajim.interface.roster.fill_contacts_and_groups_dicts(contacts[acc],
acc)
gajim.interface.roster.add_account(acc)
gajim.interface.roster.add_account_contacts(acc)
self.assertEqual(0, len(notify.notifications))
def tearDown(self):
gajim.interface.roster.model.clear()
for acc in contacts:
gajim.contacts.clear_contacts(acc)
del gajim.interface.roster
notify.notifications = []
def contact_comes_online(self, account, jid, resource, prio):
'''a remote contact comes online'''
gajim.interface.handle_event_notify(account, (jid, 'online', "I'm back!",
resource, prio, None, time.time(), None))
contact = None
for c in gajim.contacts.get_contacts(account, jid):
if c.resource == resource:
contact = c
break
self.assertEqual('online', contact.show)
self.assertEqual("I'm back!", contact.status)
self.assertEqual(prio, contact.priority)
# the most recent notification is that the contact connected
self.assertEqual('contact_connected', notify.notifications[-1][0])
def contact_goes_offline(self, account, jid, resource, prio,
still_exists = True):
'''a remote contact goes offline.'''
gajim.interface.handle_event_notify(account, (jid, 'offline', 'Goodbye!',
resource, prio, None, time.time(), None))
contact = None
for c in gajim.contacts.get_contacts(account, jid):
if c.resource == resource:
contact = c
break
if not still_exists:
self.assert_(contact is None)
return
self.assertEqual('offline', contact.show)
self.assertEqual('Goodbye!', contact.status)
self.assertEqual(prio, contact.priority)
self.assertEqual('contact_disconnected', notify.notifications[-1][0])
def user_starts_chatting(self, jid, account, resource=None):
'''the user opens a chat window and starts talking'''
ctrl = MockChatControl(jid, account)
win = MockWindow()
win.new_tab(ctrl)
gajim.interface.msg_win_mgr._windows['test'] = win
if resource:
jid = jid + '/' + resource
# a basic session is started
session = gajim.connections[account1].make_new_session(jid,
'01234567890abcdef', cls=MockSession)
ctrl.set_session(session)
return ctrl
def user_starts_esession(self, jid, resource, account):
'''the user opens a chat window and starts an encrypted session'''
ctrl = self.user_starts_chatting(jid, account, resource)
ctrl.session.status = 'active'
ctrl.session.enable_encryption = True
return ctrl
def test_contact_comes_online(self):
jid = 'default1@gajim.org'
# contact is offline initially
contacts = gajim.contacts.get_contacts(account1, jid)
self.assertEqual(1, len(contacts))
self.assertEqual('offline', contacts[0].show)
self.assertEqual('', contacts[0].status)
self.contact_comes_online(account1, jid, 'lowprio', 1)
def test_contact_goes_offline(self):
jid = 'default1@gajim.org'
self.contact_comes_online(account1, jid, 'lowprio', 1)
ctrl = self.user_starts_chatting(jid, account1)
orig_sess = ctrl.session
self.contact_goes_offline(account1, jid, 'lowprio', 1)
# session hasn't changed since we were talking to the bare jid
self.assertEqual(orig_sess, ctrl.session)
def test_two_resources_higher_comes_online(self):
jid = 'default1@gajim.org'
self.contact_comes_online(account1, jid, 'lowprio', 1)
ctrl = self.user_starts_chatting(jid, account1)
self.contact_comes_online(account1, jid, 'highprio', 50)
# old session was dropped
self.assertEqual(None, ctrl.session)
def test_two_resources_higher_goes_offline(self):
jid = 'default1@gajim.org'
self.contact_comes_online(account1, jid, 'lowprio', 1)
self.contact_comes_online(account1, jid, 'highprio', 50)
ctrl = self.user_starts_chatting(jid, account1)
self.contact_goes_offline(account1, jid, 'highprio', 50,
still_exists=False)
# old session was dropped
self.assertEqual(None, ctrl.session)
def test_two_resources_higher_comes_online_with_esession(self):
jid = 'default1@gajim.org'
self.contact_comes_online(account1, jid, 'lowprio', 1)
ctrl = self.user_starts_esession(jid, 'lowprio', account1)
self.contact_comes_online(account1, jid, 'highprio', 50)
# session was associated with the low priority full jid, so it should
# have been removed from the control
self.assertEqual(None, ctrl.session)
def test_two_resources_higher_goes_offline_with_esession(self):
jid = 'default1@gajim.org'
self.contact_comes_online(account1, jid, 'lowprio', 1)
self.contact_comes_online(account1, jid, 'highprio', 50)
ctrl = self.user_starts_esession(jid, 'highprio', account1)
self.contact_goes_offline(account1, jid, 'highprio', 50,
still_exists=False)
# session was associated with the high priority full jid, so it should
# have been removed from the control
self.assertEqual(None, ctrl.session)
if __name__ == '__main__':
unittest.main()
# vim: se ts=3:
|