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