From ed72914e1da0683a03bd4171528fc2d29e596dda Mon Sep 17 00:00:00 2001
From: Yann Leboulanger <asterix@lagaule.org>
Date: Mon, 8 Apr 2013 18:57:39 +0200
Subject: [PATCH] start fixing last test. still a problem with sessions

---
 .../integration/test_gui_event_integration.py | 50 +++++++++++++------
 test/lib/gajim_mocks.py                       |  8 ++-
 2 files changed, 42 insertions(+), 16 deletions(-)

diff --git a/test/integration/test_gui_event_integration.py b/test/integration/test_gui_event_integration.py
index 248562939e..06b8d638e0 100644
--- a/test/integration/test_gui_event_integration.py
+++ b/test/integration/test_gui_event_integration.py
@@ -10,6 +10,7 @@ import nbxmpp
 
 from common import gajim
 from common import contacts as contacts_module
+from common import caps_cache
 from gajim import Interface
 
 from gajim_mocks import *
@@ -21,17 +22,20 @@ import time
 from data import *
 
 import roster_window
-
+import plugins
 import notify
 
 class TestStatusChange(unittest.TestCase):
-    '''tests gajim.py's incredibly complex handle_event_notify'''
+    '''tests gajim.py's incredibly complex presence handling'''
 
     def setUp(self):
 
         gajim.connections = {}
         gajim.contacts = contacts_module.LegacyContactsAPI()
         gajim.interface.roster = roster_window.RosterWindow()
+        gajim.plugin_manager = plugins.PluginManager()
+        gajim.logger = MockLogger()
+        caps_cache.initialize(gajim.logger)
 
         for acc in contacts:
             gajim.connections[acc] = MockConnection(acc)
@@ -45,19 +49,20 @@ class TestStatusChange(unittest.TestCase):
 
     def tearDown(self):
         notify.notifications = []
+        for acc in contacts:
+            gajim.connections[acc].cleanup()
 
-    def contact_comes_online(self, account, jid, resource, prio):
+    def contact_comes_online(self, account, jid, resource, prio,
+    should_popup=True):
         '''a remote contact comes online'''
-        xml = """<presence from='%s/%s' id='123'>
+        xml = """<presence from='%s/%s' id='123'><priority>%s</priority>
             <c node='http://gajim.org' ver='pRCD6cgQ4SDqNMCjdhRV6TECx5o='
             hash='sha-1' xmlns='http://jabber.org/protocol/caps'/>
             <status>I'm back!</status>
             </presence>
-        """ % (jid, resource)
+        """ % (jid, resource, prio)
         msg = nbxmpp.protocol.Presence(node=nbxmpp.simplexml.XML2Node(xml))
         gajim.connections[account]._presenceCB(None, msg)
-#        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):
@@ -70,13 +75,24 @@ class TestStatusChange(unittest.TestCase):
         self.assertEqual(prio, contact.priority)
 
         # the most recent notification is that the contact connected
-        self.assertEqual('contact_connected', notify.notifications[-1][0])
+        if should_popup:
+            self.assertEqual('Contact Signed In',
+                notify.notifications[-1].popup_event_type)
+        else:
+            self.assertEqual('', notify.notifications[-1].popup_event_type)
 
     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))
+        xml = """<presence type='unavailable' from='%s/%s' id='123'>
+            <priority>%s</priority>
+            <c node='http://gajim.org' ver='pRCD6cgQ4SDqNMCjdhRV6TECx5o='
+            hash='sha-1' xmlns='http://jabber.org/protocol/caps'/>
+            <status>Goodbye!</status>
+            </presence>
+        """ % (jid, resource, prio)
+        msg = nbxmpp.protocol.Presence(node=nbxmpp.simplexml.XML2Node(xml))
+        gajim.connections[account]._presenceCB(None, msg)
 
         contact = None
         for c in gajim.contacts.get_contacts(account, jid):
@@ -92,7 +108,8 @@ class TestStatusChange(unittest.TestCase):
         self.assertEqual('Goodbye!', contact.status)
         self.assertEqual(prio, contact.priority)
 
-        self.assertEqual('contact_disconnected', notify.notifications[-1][0])
+        self.assertEqual('Contact Signed Out',
+            notify.notifications[-1].popup_event_type)
 
     def user_starts_chatting(self, jid, account, resource=None):
         '''the user opens a chat window and starts talking'''
@@ -150,7 +167,8 @@ class TestStatusChange(unittest.TestCase):
 
         ctrl = self.user_starts_chatting(jid, account1)
 
-        self.contact_comes_online(account1, jid, 'highprio', 50)
+        self.contact_comes_online(account1, jid, 'highprio', 50,
+            should_popup=False)
 
         # old session was dropped
         self.assertEqual(None, ctrl.session)
@@ -159,12 +177,13 @@ class TestStatusChange(unittest.TestCase):
         jid = 'default1@gajim.org'
 
         self.contact_comes_online(account1, jid, 'lowprio', 1)
-        self.contact_comes_online(account1, jid, 'highprio', 50)
+        self.contact_comes_online(account1, jid, 'highprio', 50,
+            should_popup=False)
 
         ctrl = self.user_starts_chatting(jid, account1)
 
         self.contact_goes_offline(account1, jid, 'highprio', 50,
-                still_exists=False)
+            still_exists=False)
 
         # old session was dropped
         self.assertEqual(None, ctrl.session)
@@ -176,7 +195,8 @@ class TestStatusChange(unittest.TestCase):
 
         ctrl = self.user_starts_esession(jid, 'lowprio', account1)
 
-        self.contact_comes_online(account1, jid, 'highprio', 50)
+        self.contact_comes_online(account1, jid, 'highprio', 50,
+            should_popup=False)
 
         # session was associated with the low priority full jid, so it should
         # have been removed from the control
diff --git a/test/lib/gajim_mocks.py b/test/lib/gajim_mocks.py
index 5995bc9565..30977538e6 100644
--- a/test/lib/gajim_mocks.py
+++ b/test/lib/gajim_mocks.py
@@ -11,9 +11,13 @@ from common.connection_handlers import ConnectionHandlers
 class MockConnection(Mock, ConnectionHandlers):
     def __init__(self, account, *args):
         Mock.__init__(self, *args)
-        ConnectionHandlers.__init__(self)
+
+        self.connection = Mock()
 
         self.name = account
+
+        ConnectionHandlers.__init__(self)
+
         self.connected = 2
         self.pep = {}
         self.blocked_contacts = {}
@@ -122,6 +126,7 @@ class MockInterface(Mock):
 class MockLogger(Mock):
     def __init__(self):
         Mock.__init__(self, {'write': None, 'get_transports_type': {}})
+        self.cur = Mock()
 
 
 class MockContact(Mock):
@@ -139,6 +144,7 @@ class MockSession(Mock):
         self.jid = jid
         self.type_ = type_
         self.thread_id = thread_id
+        self.resource = ''
 
         if not self.thread_id:
             self.thread_id = '%0x' % random.randint(0, 10000)
-- 
GitLab