[telepathy-mission-control/master] Add regression tests for current functionality of EnsureChannel

Simon McVittie simon.mcvittie at collabora.co.uk
Tue Jun 30 08:24:23 PDT 2009


These are quite important to have before refactoring the way in which
channels close, and they already point out various bugs.
---
 test/twisted/Makefile.am                         |    3 +
 test/twisted/dispatcher/ensure-and-redispatch.py |  262 +++++++++++++++++++++
 test/twisted/dispatcher/ensure-is-approval.py    |  263 ++++++++++++++++++++++
 test/twisted/dispatcher/ensure-rapidly.py        |  215 ++++++++++++++++++
 4 files changed, 743 insertions(+), 0 deletions(-)
 create mode 100644 test/twisted/dispatcher/ensure-and-redispatch.py
 create mode 100644 test/twisted/dispatcher/ensure-is-approval.py
 create mode 100644 test/twisted/dispatcher/ensure-rapidly.py

diff --git a/test/twisted/Makefile.am b/test/twisted/Makefile.am
index ef867bc..f14de28 100644
--- a/test/twisted/Makefile.am
+++ b/test/twisted/Makefile.am
@@ -32,6 +32,9 @@ TWISTED_BASIC_TESTS = \
 	dispatcher/dispatch-activatable.py \
 	dispatcher/dispatch-obsolete.py \
 	dispatcher/dispatch-text.py \
+	dispatcher/ensure-and-redispatch.py \
+	dispatcher/ensure-is-approval.py \
+	dispatcher/ensure-rapidly.py \
 	dispatcher/exploding-bundles.py \
 	dispatcher/fdo-21034.py \
 	dispatcher/handle-channels-fails.py \
diff --git a/test/twisted/dispatcher/ensure-and-redispatch.py b/test/twisted/dispatcher/ensure-and-redispatch.py
new file mode 100644
index 0000000..e9072f5
--- /dev/null
+++ b/test/twisted/dispatcher/ensure-and-redispatch.py
@@ -0,0 +1,262 @@
+# Copyright (C) 2009 Nokia Corporation
+# Copyright (C) 2009 Collabora Ltd.
+#
+# This library is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 2.1 of the License, or (at your option) any later version.
+#
+# This library is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+# 02110-1301 USA
+
+"""Feature test ensuring that MC deals correctly with EnsureChannel returning
+a channel that has already been dispatched to a handler.
+"""
+
+import dbus
+import dbus.service
+
+from servicetest import EventPattern, tp_name_prefix, tp_path_prefix, \
+        call_async
+from mctest import exec_test, SimulatedConnection, SimulatedClient, \
+        create_fakecm_account, enable_fakecm_account, SimulatedChannel, \
+        expect_client_setup
+import constants as cs
+
+def test(q, bus, mc):
+    params = dbus.Dictionary({"account": "someguy at example.com",
+        "password": "secrecy"}, signature='sv')
+    cm_name_ref, account = create_fakecm_account(q, bus, mc, params)
+    conn = enable_fakecm_account(q, bus, mc, account, params)
+
+    text_fixed_properties = dbus.Dictionary({
+        cs.CHANNEL + '.TargetHandleType': cs.HT_CONTACT,
+        cs.CHANNEL + '.ChannelType': cs.CHANNEL_TYPE_TEXT,
+        }, signature='sv')
+
+    client = SimulatedClient(q, bus, 'Empathy',
+            observe=[text_fixed_properties], approve=[text_fixed_properties],
+            handle=[text_fixed_properties], bypass_approval=False)
+
+    # wait for MC to download the properties
+    expect_client_setup(q, [client])
+
+    channel = test_channel_creation(q, bus, account, client, conn)
+    test_channel_redispatch(q, bus, account, client, conn, channel)
+    channel.close()
+
+def test_channel_creation(q, bus, account, client, conn):
+    user_action_time = dbus.Int64(1238582606)
+
+    cd = bus.get_object(cs.CD, cs.CD_PATH)
+    cd_props = dbus.Interface(cd, cs.PROPERTIES_IFACE)
+
+    # chat UI calls ChannelDispatcher.EnsureChannel
+    request = dbus.Dictionary({
+            cs.CHANNEL + '.ChannelType': cs.CHANNEL_TYPE_TEXT,
+            cs.CHANNEL + '.TargetHandleType': cs.HT_CONTACT,
+            cs.CHANNEL + '.TargetID': 'juliet',
+            }, signature='sv')
+    account_requests = dbus.Interface(account,
+            cs.ACCOUNT_IFACE_NOKIA_REQUESTS)
+    call_async(q, cd, 'EnsureChannel',
+            account.object_path, request, user_action_time, client.bus_name,
+            dbus_interface=cs.CD)
+    ret = q.expect('dbus-return', method='EnsureChannel')
+    request_path = ret.value[0]
+
+    # chat UI connects to signals and calls ChannelRequest.Proceed()
+
+    cr = bus.get_object(cs.AM, request_path)
+    request_props = cr.GetAll(cs.CR, dbus_interface=cs.PROPERTIES_IFACE)
+    assert request_props['Account'] == account.object_path
+    assert request_props['Requests'] == [request]
+    assert request_props['UserActionTime'] == user_action_time
+    assert request_props['PreferredHandler'] == client.bus_name
+    assert request_props['Interfaces'] == []
+
+    cr.Proceed(dbus_interface=cs.CR)
+
+    cm_request_call, add_request_call = q.expect_many(
+            EventPattern('dbus-method-call',
+                interface=cs.CONN_IFACE_REQUESTS,
+                method='EnsureChannel',
+                path=conn.object_path, args=[request], handled=False),
+            EventPattern('dbus-method-call', handled=False,
+                interface=cs.CLIENT_IFACE_REQUESTS,
+                method='AddRequest', path=client.object_path),
+            )
+
+    assert add_request_call.args[0] == request_path
+    request_props = add_request_call.args[1]
+    assert request_props[cs.CR + '.Account'] == account.object_path
+    assert request_props[cs.CR + '.Requests'] == [request]
+    assert request_props[cs.CR + '.UserActionTime'] == user_action_time
+    assert request_props[cs.CR + '.PreferredHandler'] == client.bus_name
+    assert request_props[cs.CR + '.Interfaces'] == []
+
+    q.dbus_return(add_request_call.message, signature='')
+
+    # Time passes. A channel is returned.
+
+    channel_immutable = dbus.Dictionary(request)
+    channel_immutable[cs.CHANNEL + '.InitiatorID'] = conn.self_ident
+    channel_immutable[cs.CHANNEL + '.InitiatorHandle'] = conn.self_handle
+    channel_immutable[cs.CHANNEL + '.Requested'] = True
+    channel_immutable[cs.CHANNEL + '.Interfaces'] = \
+        dbus.Array([], signature='s')
+    channel_immutable[cs.CHANNEL + '.TargetHandle'] = \
+        conn.ensure_handle(cs.HT_CONTACT, 'juliet')
+    channel = SimulatedChannel(conn, channel_immutable)
+
+    # this order of events is guaranteed by telepathy-spec (since 0.17.14)
+    q.dbus_return(cm_request_call.message, True, # <- Yours
+            channel.object_path, channel.immutable, signature='boa{sv}')
+    channel.announce()
+
+    # Observer should get told, processing waits for it
+    e = q.expect('dbus-method-call',
+            path=client.object_path,
+            interface=cs.OBSERVER, method='ObserveChannels',
+            handled=False)
+    assert e.args[0] == account.object_path, e.args
+    assert e.args[1] == conn.object_path, e.args
+    assert e.args[3] == '/', e.args         # no dispatch operation
+    assert e.args[4] == [request_path], e.args
+    channels = e.args[2]
+    assert len(channels) == 1, channels
+    assert channels[0][0] == channel.object_path, channels
+    assert channels[0][1] == channel_immutable, channels
+
+    # Observer says "OK, go"
+    q.dbus_return(e.message, signature='')
+
+    # Handler is next
+    e = q.expect('dbus-method-call',
+            path=client.object_path,
+            interface=cs.HANDLER, method='HandleChannels',
+            handled=False)
+    assert e.args[0] == account.object_path, e.args
+    assert e.args[1] == conn.object_path, e.args
+    channels = e.args[2]
+    assert len(channels) == 1, channels
+    assert channels[0][0] == channel.object_path, channels
+    assert channels[0][1] == channel_immutable, channels
+    assert e.args[3] == [request_path], e.args
+    assert e.args[4] == user_action_time
+    assert isinstance(e.args[5], dict)
+    assert len(e.args) == 6
+
+    # Handler accepts the Channels
+    q.dbus_return(e.message, signature='')
+
+    # CR emits Succeeded (or in Mardy's version, Account emits Succeeded)
+    q.expect_many(
+            EventPattern('dbus-signal', path=account.object_path,
+                interface=cs.ACCOUNT_IFACE_NOKIA_REQUESTS, signal='Succeeded',
+                args=[request_path]),
+            EventPattern('dbus-signal', path=request_path,
+                interface=cs.CR, signal='Succeeded'),
+            )
+
+    return channel
+
+def test_channel_redispatch(q, bus, account, client, conn, channel):
+    user_action_time = dbus.Int64(1244444444)
+
+    # Because we create no new channels, nothing should be observed.
+    forbidden = [
+            EventPattern('dbus-method-call', method='ObserveChannels'),
+            ]
+    q.forbid_events(forbidden)
+
+    cd = bus.get_object(cs.CD, cs.CD_PATH)
+    cd_props = dbus.Interface(cd, cs.PROPERTIES_IFACE)
+
+    # UI calls ChannelDispatcher.EnsureChannel again
+    request = dbus.Dictionary({
+            cs.CHANNEL + '.ChannelType': cs.CHANNEL_TYPE_TEXT,
+            cs.CHANNEL + '.TargetHandleType': cs.HT_CONTACT,
+            cs.CHANNEL + '.TargetID': 'juliet',
+            }, signature='sv')
+    call_async(q, cd, 'EnsureChannel',
+            account.object_path, request, user_action_time, client.bus_name,
+            dbus_interface=cs.CD)
+    ret = q.expect('dbus-return', method='EnsureChannel')
+    request_path = ret.value[0]
+
+    # UI connects to signals and calls ChannelRequest.Proceed()
+
+    cr = bus.get_object(cs.AM, request_path)
+    request_props = cr.GetAll(cs.CR, dbus_interface=cs.PROPERTIES_IFACE)
+    assert request_props['Account'] == account.object_path
+    assert request_props['Requests'] == [request]
+    assert request_props['UserActionTime'] == user_action_time
+    assert request_props['PreferredHandler'] == client.bus_name
+    assert request_props['Interfaces'] == []
+
+    cr.Proceed(dbus_interface=cs.CR)
+
+    cm_request_call, add_request_call = q.expect_many(
+            EventPattern('dbus-method-call',
+                interface=cs.CONN_IFACE_REQUESTS,
+                method='EnsureChannel',
+                path=conn.object_path, args=[request], handled=False),
+            EventPattern('dbus-method-call', handled=False,
+                interface=cs.CLIENT_IFACE_REQUESTS,
+                method='AddRequest', path=client.object_path),
+            )
+
+    assert add_request_call.args[0] == request_path
+    request_props = add_request_call.args[1]
+    assert request_props[cs.CR + '.Account'] == account.object_path
+    assert request_props[cs.CR + '.Requests'] == [request]
+    assert request_props[cs.CR + '.UserActionTime'] == user_action_time
+    assert request_props[cs.CR + '.PreferredHandler'] == client.bus_name
+    assert request_props[cs.CR + '.Interfaces'] == []
+
+    q.dbus_return(add_request_call.message, signature='')
+
+    # Time passes. The same channel is returned.
+    q.dbus_return(cm_request_call.message, False, # <- Yours
+            channel.object_path, channel.immutable, signature='boa{sv}')
+
+    # Handler is re-invoked
+    e = q.expect('dbus-method-call',
+            path=client.object_path,
+            interface=cs.HANDLER, method='HandleChannels',
+            handled=False)
+    assert e.args[0] == account.object_path, e.args
+    assert e.args[1] == conn.object_path, e.args
+    channels = e.args[2]
+    assert len(channels) == 1, channels
+    assert channels[0][0] == channel.object_path, channels
+    assert channels[0][1] == channel.immutable, channels
+    assert e.args[3] == [request_path], e.args
+    assert e.args[4] == user_action_time
+    assert isinstance(e.args[5], dict)
+    assert len(e.args) == 6
+
+    # Handler accepts the Channels
+    q.dbus_return(e.message, signature='')
+
+    # CR emits Succeeded (or in Mardy's version, Account emits Succeeded)
+    q.expect_many(
+            EventPattern('dbus-signal', path=account.object_path,
+                interface=cs.ACCOUNT_IFACE_NOKIA_REQUESTS, signal='Succeeded',
+                args=[request_path]),
+            EventPattern('dbus-signal', path=request_path,
+                interface=cs.CR, signal='Succeeded'),
+            )
+
+    q.unforbid_events(forbidden)
+
+if __name__ == '__main__':
+    exec_test(test, {})
diff --git a/test/twisted/dispatcher/ensure-is-approval.py b/test/twisted/dispatcher/ensure-is-approval.py
new file mode 100644
index 0000000..de21ea6
--- /dev/null
+++ b/test/twisted/dispatcher/ensure-is-approval.py
@@ -0,0 +1,263 @@
+# Copyright (C) 2009 Nokia Corporation
+# Copyright (C) 2009 Collabora Ltd.
+#
+# This library is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 2.1 of the License, or (at your option) any later version.
+#
+# This library is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+# 02110-1301 USA
+
+import dbus
+"""Regression test for EnsureChannel counting as approval of an incoming text
+channel.
+"""
+
+import dbus
+import dbus.bus
+import dbus.service
+
+from servicetest import EventPattern, tp_name_prefix, tp_path_prefix, \
+        call_async, sync_dbus
+from mctest import exec_test, SimulatedConnection, SimulatedClient, \
+        create_fakecm_account, enable_fakecm_account, SimulatedChannel, \
+        expect_client_setup
+import constants as cs
+
+def test(q, bus, mc):
+    params = dbus.Dictionary({"account": "someguy at example.com",
+        "password": "secrecy"}, signature='sv')
+    cm_name_ref, account = create_fakecm_account(q, bus, mc, params)
+    conn = enable_fakecm_account(q, bus, mc, account, params)
+
+    text_fixed_properties = dbus.Dictionary({
+        cs.CHANNEL + '.TargetHandleType': cs.HT_CONTACT,
+        cs.CHANNEL + '.ChannelType': cs.CHANNEL_TYPE_TEXT,
+        }, signature='sv')
+
+    empathy_bus = dbus.bus.BusConnection()
+    kopete_bus = dbus.bus.BusConnection()
+    q.attach_to_bus(empathy_bus)
+    q.attach_to_bus(kopete_bus)
+    # Two clients want to observe, approve and handle channels
+    empathy = SimulatedClient(q, empathy_bus, 'Empathy',
+            observe=[text_fixed_properties], approve=[text_fixed_properties],
+            handle=[text_fixed_properties], bypass_approval=False)
+    kopete = SimulatedClient(q, kopete_bus, 'Kopete',
+            observe=[text_fixed_properties], approve=[text_fixed_properties],
+            handle=[text_fixed_properties], bypass_approval=False)
+
+    # wait for MC to download the properties
+    expect_client_setup(q, [empathy, kopete])
+
+    # subscribe to the OperationList interface (MC assumes that until this
+    # property has been retrieved once, nobody cares)
+
+    cd = bus.get_object(cs.CD, cs.CD_PATH)
+    cd_props = dbus.Interface(cd, cs.PROPERTIES_IFACE)
+    assert cd_props.Get(cs.CD_IFACE_OP_LIST, 'DispatchOperations') == []
+
+    channel_properties = dbus.Dictionary(text_fixed_properties,
+            signature='sv')
+    channel_properties[cs.CHANNEL + '.TargetID'] = 'juliet'
+    channel_properties[cs.CHANNEL + '.TargetHandle'] = \
+            conn.ensure_handle(cs.HT_CONTACT, 'juliet')
+    channel_properties[cs.CHANNEL + '.InitiatorID'] = 'juliet'
+    channel_properties[cs.CHANNEL + '.InitiatorHandle'] = \
+            conn.ensure_handle(cs.HT_CONTACT, 'juliet')
+    channel_properties[cs.CHANNEL + '.Requested'] = False
+    channel_properties[cs.CHANNEL + '.Interfaces'] = dbus.Array(signature='s')
+
+    chan = SimulatedChannel(conn, channel_properties)
+    chan.announce()
+
+    # A channel dispatch operation is created
+
+    e = q.expect('dbus-signal',
+            path=cs.CD_PATH,
+            interface=cs.CD_IFACE_OP_LIST,
+            signal='NewDispatchOperation')
+
+    cdo_path = e.args[0]
+    cdo_properties = e.args[1]
+
+    assert cdo_properties[cs.CDO + '.Account'] == account.object_path
+    assert cdo_properties[cs.CDO + '.Connection'] == conn.object_path
+    assert cs.CDO + '.Interfaces' in cdo_properties
+
+    handlers = cdo_properties[cs.CDO + '.PossibleHandlers'][:]
+    handlers.sort()
+    assert handlers == [cs.tp_name_prefix + '.Client.Empathy',
+            cs.tp_name_prefix + '.Client.Kopete'], handlers
+
+    assert cs.CD_IFACE_OP_LIST in cd_props.Get(cs.CD, 'Interfaces')
+    assert cd_props.Get(cs.CD_IFACE_OP_LIST, 'DispatchOperations') ==\
+            [(cdo_path, cdo_properties)]
+
+    cdo = bus.get_object(cs.CD, cdo_path)
+    cdo_iface = dbus.Interface(cdo, cs.CDO)
+    cdo_props_iface = dbus.Interface(cdo, cs.PROPERTIES_IFACE)
+
+    assert cdo_props_iface.Get(cs.CDO, 'Interfaces') == \
+            cdo_properties[cs.CDO + '.Interfaces']
+    assert cdo_props_iface.Get(cs.CDO, 'Connection') == conn.object_path
+    assert cdo_props_iface.Get(cs.CDO, 'Account') == account.object_path
+    assert cdo_props_iface.Get(cs.CDO, 'Channels') == [(chan.object_path,
+        channel_properties)]
+    assert cdo_props_iface.Get(cs.CDO, 'PossibleHandlers') == \
+            cdo_properties[cs.CDO + '.PossibleHandlers']
+
+    # Both Observers are told about the new channel
+
+    e, k = q.expect_many(
+            EventPattern('dbus-method-call',
+                path=empathy.object_path,
+                interface=cs.OBSERVER, method='ObserveChannels',
+                handled=False),
+            EventPattern('dbus-method-call',
+                path=kopete.object_path,
+                interface=cs.OBSERVER, method='ObserveChannels',
+                handled=False),
+            )
+    assert e.args[0] == account.object_path, e.args
+    assert e.args[1] == conn.object_path, e.args
+    assert e.args[3] == cdo_path, e.args
+    assert e.args[4] == [], e.args      # no requests satisfied
+    channels = e.args[2]
+    assert len(channels) == 1, channels
+    assert channels[0][0] == chan.object_path, channels
+    assert channels[0][1] == channel_properties, channels
+
+    assert k.args == e.args
+
+    # Both Observers indicate that they are ready to proceed
+    q.dbus_return(k.message, bus=empathy_bus, signature='')
+    q.dbus_return(e.message, bus=kopete_bus, signature='')
+
+    # The Approvers are next
+
+    e, k = q.expect_many(
+            EventPattern('dbus-method-call',
+                path=empathy.object_path,
+                interface=cs.APPROVER, method='AddDispatchOperation',
+                handled=False),
+            EventPattern('dbus-method-call',
+                path=kopete.object_path,
+                interface=cs.APPROVER, method='AddDispatchOperation',
+                handled=False),
+            )
+
+    assert e.args == [[(chan.object_path, channel_properties)],
+            cdo_path, cdo_properties]
+    assert k.args == e.args
+
+    q.dbus_return(e.message, bus=empathy_bus, signature='')
+    q.dbus_return(k.message, bus=kopete_bus, signature='')
+
+    # Both Approvers now have a flashing icon or something, trying to get the
+    # user's attention. However, the user is busy looking through the address
+    # book, and independently decides to talk to Juliet. The address book
+    # is from KDE so wants Kopete to be used.
+
+    user_action_time = dbus.Int64(1238582606)
+
+    request = dbus.Dictionary({
+            cs.CHANNEL + '.ChannelType': cs.CHANNEL_TYPE_TEXT,
+            cs.CHANNEL + '.TargetHandleType': cs.HT_CONTACT,
+            cs.CHANNEL + '.TargetID': 'juliet',
+            }, signature='sv')
+    call_async(q, cd, 'EnsureChannel',
+            account.object_path, request, user_action_time, kopete.bus_name,
+            dbus_interface=cs.CD)
+    ret, add_request_call = q.expect_many(
+            EventPattern('dbus-return', method='EnsureChannel'),
+            EventPattern('dbus-method-call', handled=False,
+                interface=cs.CLIENT_IFACE_REQUESTS,
+                method='AddRequest'), # FIXME: path=kopete.object_path),
+            )
+    request_path = ret.value[0]
+
+    assert add_request_call.args[0] == request_path
+    request_props = add_request_call.args[1]
+    assert request_props[cs.CR + '.Account'] == account.object_path
+    assert request_props[cs.CR + '.Requests'] == [request]
+    assert request_props[cs.CR + '.UserActionTime'] == user_action_time
+    # FIXME: untrue
+    # assert request_props[cs.CR + '.PreferredHandler'] == kopete.bus_name
+    assert request_props[cs.CR + '.Interfaces'] == []
+
+    # UI connects to signals and calls ChannelRequest.Proceed()
+
+    cr = bus.get_object(cs.AM, request_path)
+    request_props = cr.GetAll(cs.CR, dbus_interface=cs.PROPERTIES_IFACE)
+    assert request_props['Account'] == account.object_path
+    assert request_props['Requests'] == [request]
+    assert request_props['UserActionTime'] == user_action_time
+    assert request_props['PreferredHandler'] == kopete.bus_name
+    assert request_props['Interfaces'] == []
+
+    cr.Proceed(dbus_interface=cs.CR)
+
+    cm_request_call = q.expect('dbus-method-call',
+            interface=cs.CONN_IFACE_REQUESTS,
+            method='EnsureChannel',
+            path=conn.object_path, args=[request], handled=False)
+
+    # FIXME: we should be able to assert that this goes to Kopete, above
+    if add_request_call.path == kopete.object_path:
+        q.dbus_return(add_request_call.message, bus=kopete_bus, signature='')
+    elif add_request_call.path == empathy.object_path:
+        q.dbus_return(add_request_call.message, bus=empathy_bus, signature='')
+    else:
+        assert False, "%s is neither Empathy nor Kopete" % add_request_call.path
+
+    # Time passes. The CM returns the existing channel
+
+    q.dbus_return(cm_request_call.message, False,
+            chan.object_path, chan.immutable, signature='boa{sv}')
+
+    # EnsureChannel constitutes approval, so Kopete is told to handle the
+    # channel
+
+    e = q.expect('dbus-method-call',
+            # FIXME: untrue: path=kopete.object_path,
+            interface=cs.HANDLER, method='HandleChannels',
+            handled=False)
+    assert e.args[0] == account.object_path, e.args
+    assert e.args[1] == conn.object_path, e.args
+    channels = e.args[2]
+    assert len(channels) == 1, channels
+    assert channels[0][0] == chan.object_path, channels
+    assert channels[0][1] == chan.immutable, channels
+    assert e.args[3] == [request_path], e.args
+    # FIXME: untrue: assert e.args[4] == user_action_time
+    assert isinstance(e.args[5], dict)
+    assert len(e.args) == 6
+
+    # Handler accepts the Channels
+    if e.path == kopete.object_path:
+        q.dbus_return(e.message, bus=kopete_bus, signature='')
+    elif e.path == empathy.object_path:
+        q.dbus_return(e.message, bus=empathy_bus, signature='')
+    else:
+        assert False, "%s is neither Empathy nor Kopete" % e.path
+
+    # FIXME: this shouldn't happen until after HandleChannels has succeeded,
+    # but MC currently does this as soon as HandleWith is called (fd.o #21003)
+    #q.expect('dbus-signal', path=cdo_path, signal='Finished')
+    #q.expect('dbus-signal', path=cs.CD_PATH,
+    #    signal='DispatchOperationFinished', args=[cdo_path])
+
+    # Now there are no more active channel dispatch operations
+    assert cd_props.Get(cs.CD_IFACE_OP_LIST, 'DispatchOperations') == []
+
+if __name__ == '__main__':
+    exec_test(test, {})
diff --git a/test/twisted/dispatcher/ensure-rapidly.py b/test/twisted/dispatcher/ensure-rapidly.py
new file mode 100644
index 0000000..9fcb35f
--- /dev/null
+++ b/test/twisted/dispatcher/ensure-rapidly.py
@@ -0,0 +1,215 @@
+# Copyright (C) 2009 Nokia Corporation
+# Copyright (C) 2009 Collabora Ltd.
+#
+# This library is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 2.1 of the License, or (at your option) any later version.
+#
+# This library is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+# 02110-1301 USA
+
+"""Feature test ensuring that MC deals correctly with EnsureChannel returning
+a channel that has already been dispatched to a handler.
+"""
+
+import dbus
+import dbus.service
+
+from servicetest import EventPattern, tp_name_prefix, tp_path_prefix, \
+        call_async
+from mctest import exec_test, SimulatedConnection, SimulatedClient, \
+        create_fakecm_account, enable_fakecm_account, SimulatedChannel, \
+        expect_client_setup
+import constants as cs
+
+def test(q, bus, mc):
+    params = dbus.Dictionary({"account": "someguy at example.com",
+        "password": "secrecy"}, signature='sv')
+    cm_name_ref, account = create_fakecm_account(q, bus, mc, params)
+    conn = enable_fakecm_account(q, bus, mc, account, params)
+
+    text_fixed_properties = dbus.Dictionary({
+        cs.CHANNEL + '.TargetHandleType': cs.HT_CONTACT,
+        cs.CHANNEL + '.ChannelType': cs.CHANNEL_TYPE_TEXT,
+        }, signature='sv')
+
+    client = SimulatedClient(q, bus, 'Empathy',
+            observe=[text_fixed_properties], approve=[text_fixed_properties],
+            handle=[text_fixed_properties], bypass_approval=False)
+
+    # wait for MC to download the properties
+    expect_client_setup(q, [client])
+
+    channel = test_channel_creation(q, bus, account, client, conn)
+    channel.close()
+
+def test_channel_creation(q, bus, account, client, conn):
+    user_action_time1 = dbus.Int64(1238582606)
+    user_action_time2 = dbus.Int64(1244444444)
+
+    cd = bus.get_object(cs.CD, cs.CD_PATH)
+    cd_props = dbus.Interface(cd, cs.PROPERTIES_IFACE)
+
+    # chat UI calls ChannelDispatcher.EnsureChannel
+    request = dbus.Dictionary({
+            cs.CHANNEL + '.ChannelType': cs.CHANNEL_TYPE_TEXT,
+            cs.CHANNEL + '.TargetHandleType': cs.HT_CONTACT,
+            cs.CHANNEL + '.TargetID': 'juliet',
+            }, signature='sv')
+    account_requests = dbus.Interface(account,
+            cs.ACCOUNT_IFACE_NOKIA_REQUESTS)
+    call_async(q, cd, 'EnsureChannel',
+            account.object_path, request, user_action_time1, client.bus_name,
+            dbus_interface=cs.CD)
+    ret = q.expect('dbus-return', method='EnsureChannel')
+    request_path = ret.value[0]
+
+    # chat UI connects to signals and calls ChannelRequest.Proceed()
+
+    cr1 = bus.get_object(cs.AM, request_path)
+    request_props = cr1.GetAll(cs.CR, dbus_interface=cs.PROPERTIES_IFACE)
+    assert request_props['Account'] == account.object_path
+    assert request_props['Requests'] == [request]
+    assert request_props['UserActionTime'] == user_action_time1
+    assert request_props['PreferredHandler'] == client.bus_name
+    assert request_props['Interfaces'] == []
+
+    cr1.Proceed(dbus_interface=cs.CR)
+
+    cm_request_call1, add_request_call1 = q.expect_many(
+            EventPattern('dbus-method-call',
+                interface=cs.CONN_IFACE_REQUESTS,
+                method='EnsureChannel',
+                path=conn.object_path, args=[request], handled=False),
+            EventPattern('dbus-method-call', handled=False,
+                interface=cs.CLIENT_IFACE_REQUESTS,
+                method='AddRequest', path=client.object_path),
+            )
+
+    # Before the first request has succeeded, the user gets impatient and
+    # the UI re-requests.
+    call_async(q, cd, 'EnsureChannel',
+            account.object_path, request, user_action_time2, client.bus_name,
+            dbus_interface=cs.CD)
+    ret = q.expect('dbus-return', method='EnsureChannel')
+    request_path = ret.value[0]
+    cr2 = bus.get_object(cs.AM, request_path)
+
+    request_props = cr2.GetAll(cs.CR, dbus_interface=cs.PROPERTIES_IFACE)
+    assert request_props['Account'] == account.object_path
+    assert request_props['Requests'] == [request]
+    assert request_props['UserActionTime'] == user_action_time2
+    assert request_props['PreferredHandler'] == client.bus_name
+    assert request_props['Interfaces'] == []
+
+    cr2.Proceed(dbus_interface=cs.CR)
+
+    cm_request_call2, add_request_call2 = q.expect_many(
+            EventPattern('dbus-method-call',
+                interface=cs.CONN_IFACE_REQUESTS,
+                method='EnsureChannel',
+                path=conn.object_path, args=[request], handled=False),
+            EventPattern('dbus-method-call', handled=False,
+                interface=cs.CLIENT_IFACE_REQUESTS,
+                method='AddRequest', path=client.object_path),
+            )
+
+    assert add_request_call1.args[0] == cr1.object_path
+    request_props = add_request_call1.args[1]
+    assert request_props[cs.CR + '.Account'] == account.object_path
+    assert request_props[cs.CR + '.Requests'] == [request]
+    assert request_props[cs.CR + '.UserActionTime'] == user_action_time1
+    assert request_props[cs.CR + '.PreferredHandler'] == client.bus_name
+    assert request_props[cs.CR + '.Interfaces'] == []
+
+    assert add_request_call2.args[0] == cr2.object_path
+    request_props = add_request_call2.args[1]
+    assert request_props[cs.CR + '.Account'] == account.object_path
+    assert request_props[cs.CR + '.Requests'] == [request]
+    assert request_props[cs.CR + '.UserActionTime'] == user_action_time2
+    assert request_props[cs.CR + '.PreferredHandler'] == client.bus_name
+    assert request_props[cs.CR + '.Interfaces'] == []
+
+    q.dbus_return(add_request_call1.message, signature='')
+    q.dbus_return(add_request_call2.message, signature='')
+
+    # Time passes. A channel is returned.
+
+    channel_immutable = dbus.Dictionary(request)
+    channel_immutable[cs.CHANNEL + '.InitiatorID'] = conn.self_ident
+    channel_immutable[cs.CHANNEL + '.InitiatorHandle'] = conn.self_handle
+    channel_immutable[cs.CHANNEL + '.Requested'] = True
+    channel_immutable[cs.CHANNEL + '.Interfaces'] = \
+        dbus.Array([], signature='s')
+    channel_immutable[cs.CHANNEL + '.TargetHandle'] = \
+        conn.ensure_handle(cs.HT_CONTACT, 'juliet')
+    channel = SimulatedChannel(conn, channel_immutable)
+
+    # this order of events is guaranteed by telepathy-spec (since 0.17.14)
+    q.dbus_return(cm_request_call1.message, True, # <- Yours
+            channel.object_path, channel.immutable, signature='boa{sv}')
+    q.dbus_return(cm_request_call2.message, False, # <- Yours
+            channel.object_path, channel.immutable, signature='boa{sv}')
+    channel.announce()
+
+    # Observer should get told, processing waits for it
+    e = q.expect('dbus-method-call',
+            path=client.object_path,
+            interface=cs.OBSERVER, method='ObserveChannels',
+            handled=False)
+    assert e.args[0] == account.object_path, e.args
+    assert e.args[1] == conn.object_path, e.args
+    assert e.args[3] == '/', e.args         # no dispatch operation
+    assert sorted(e.args[4]) == sorted([cr1.object_path,
+        cr2.object_path]), e.args
+    channels = e.args[2]
+    assert len(channels) == 1, channels
+    assert channels[0][0] == channel.object_path, channels
+    assert channels[0][1] == channel.immutable, channels
+
+    # Observer says "OK, go"
+    q.dbus_return(e.message, signature='')
+
+    # Handler is next
+    e = q.expect('dbus-method-call',
+            path=client.object_path,
+            interface=cs.HANDLER, method='HandleChannels',
+            handled=False)
+    assert e.args[0] == account.object_path, e.args
+    assert e.args[1] == conn.object_path, e.args
+    channels = e.args[2]
+    assert len(channels) == 1, channels
+    assert channels[0][0] == channel.object_path, channels
+    assert channels[0][1] == channel_immutable, channels
+    assert sorted(e.args[3]) == sorted([cr1.object_path,
+        cr2.object_path]), e.args
+    # FIXME: we ought to define which of these it should be, probably the
+    # larger (because that was the last relevant user input)
+    assert e.args[4] in [user_action_time1, user_action_time2]
+    assert isinstance(e.args[5], dict)
+    assert len(e.args) == 6
+
+    # Handler accepts the Channels
+    q.dbus_return(e.message, signature='')
+
+    # CR emits Succeeded (or in Mardy's version, Account emits Succeeded)
+    q.expect_many(
+            EventPattern('dbus-signal', path=account.object_path,
+                interface=cs.ACCOUNT_IFACE_NOKIA_REQUESTS, signal='Succeeded',
+                args=[request_path]),
+            EventPattern('dbus-signal', path=request_path,
+                interface=cs.CR, signal='Succeeded'),
+            )
+
+    return channel
+
+if __name__ == '__main__':
+    exec_test(test, {})
-- 
1.5.6.5




More information about the telepathy-commits mailing list