~ubuntu-branches/ubuntu/precise/telepathy-mission-control-5/precise

« back to all changes in this revision

Viewing changes to tests/twisted/dispatcher/create-delayed-by-plugin.py

  • Committer: Bazaar Package Importer
  • Author(s): Sjoerd Simons
  • Date: 2011-04-08 21:21:27 UTC
  • mto: (0.12.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 15.
  • Revision ID: james.westby@ubuntu.com-20110408212127-9efbdginfn4i11os
ImportĀ upstreamĀ versionĀ 5.7.9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2009 Nokia Corporation
 
2
# Copyright (C) 2009 Collabora Ltd.
 
3
#
 
4
# This library is free software; you can redistribute it and/or
 
5
# modify it under the terms of the GNU Lesser General Public
 
6
# License as published by the Free Software Foundation; either
 
7
# version 2.1 of the License, or (at your option) any later version.
 
8
#
 
9
# This library is distributed in the hope that it will be useful, but
 
10
# WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 
12
# Lesser General Public License for more details.
 
13
#
 
14
# You should have received a copy of the GNU Lesser General Public
 
15
# License along with this library; if not, write to the Free Software
 
16
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 
17
# 02110-1301 USA
 
18
 
 
19
import dbus
 
20
"""Regression test for dispatching a requested Text channel, with a plugin that
 
21
delays dispatching and asks for permission.
 
22
"""
 
23
 
 
24
import dbus
 
25
import dbus.service
 
26
 
 
27
from servicetest import EventPattern, tp_name_prefix, tp_path_prefix, \
 
28
        call_async, sync_dbus
 
29
from mctest import exec_test, SimulatedConnection, SimulatedClient, \
 
30
        create_fakecm_account, enable_fakecm_account, SimulatedChannel, \
 
31
        expect_client_setup
 
32
import constants as cs
 
33
 
 
34
text_fixed_properties = dbus.Dictionary({
 
35
    cs.CHANNEL + '.TargetHandleType': cs.HT_CONTACT,
 
36
    cs.CHANNEL + '.ChannelType': cs.CHANNEL_TYPE_TEXT,
 
37
    }, signature='sv')
 
38
 
 
39
def request_channel_expect_query(q, bus, account, conn, client):
 
40
    # This target is special-cased in test-plugin.c
 
41
    target = 'policy@example.com'
 
42
    request_properties = dbus.Dictionary(text_fixed_properties,
 
43
            signature='sv')
 
44
    request_properties[cs.CHANNEL + '.TargetID'] = target
 
45
 
 
46
    cd = bus.get_object(cs.CD, cs.CD_PATH)
 
47
 
 
48
    user_action_time = dbus.Int64(1238582606)
 
49
 
 
50
    call_async(q, cd, 'CreateChannel',
 
51
            account.object_path, request_properties, user_action_time,
 
52
            client.bus_name, dbus_interface=cs.CD)
 
53
    ret = q.expect('dbus-return', method='CreateChannel')
 
54
    request_path = ret.value[0]
 
55
 
 
56
    cr = bus.get_object(cs.AM, request_path)
 
57
    cr.Proceed(dbus_interface=cs.CR)
 
58
 
 
59
    cm_request_call = q.expect('dbus-method-call',
 
60
            interface=cs.CONN_IFACE_REQUESTS, method='CreateChannel',
 
61
            path=conn.object_path, args=[request_properties], handled=False)
 
62
 
 
63
    # A channel is returned eventually
 
64
 
 
65
    channel_properties = dbus.Dictionary(text_fixed_properties,
 
66
            signature='sv')
 
67
    channel_properties[cs.CHANNEL + '.TargetID'] = target
 
68
    channel_properties[cs.CHANNEL + '.TargetHandle'] = \
 
69
            conn.ensure_handle(cs.HT_CONTACT, target)
 
70
    channel_properties[cs.CHANNEL + '.InitiatorID'] = conn.self_ident
 
71
    channel_properties[cs.CHANNEL + '.InitiatorHandle'] = conn.self_handle
 
72
    channel_properties[cs.CHANNEL + '.Requested'] = True
 
73
    channel_properties[cs.CHANNEL + '.Interfaces'] = \
 
74
            dbus.Array([cs.CHANNEL_IFACE_GROUP,
 
75
                ],signature='s')
 
76
 
 
77
    chan = SimulatedChannel(conn, channel_properties, group=True)
 
78
    q.dbus_return(cm_request_call.message,
 
79
            chan.object_path, chan.immutable, signature='oa{sv}')
 
80
    chan.announce()
 
81
 
 
82
    # What does the policy service think?
 
83
    e = q.expect('dbus-method-call', path='/com/example/Policy',
 
84
            interface='com.example.Policy', method='RequestPermission')
 
85
 
 
86
    # Think about it for a bit
 
87
    sync_dbus(bus, q, account)
 
88
 
 
89
    # Let the test code decide how to reply
 
90
    return e, chan, cr
 
91
 
 
92
def test(q, bus, mc):
 
93
    params = dbus.Dictionary({"account": "someguy@example.com",
 
94
        "password": "secrecy"}, signature='sv')
 
95
    cm_name_ref, account = create_fakecm_account(q, bus, mc, params)
 
96
    conn = enable_fakecm_account(q, bus, mc, account, params)
 
97
 
 
98
    policy_bus_name_ref = dbus.service.BusName('com.example.Policy', bus)
 
99
 
 
100
    # Two clients want to observe, approve and handle channels
 
101
    empathy = SimulatedClient(q, bus, 'Empathy',
 
102
            observe=[text_fixed_properties], approve=[text_fixed_properties],
 
103
            handle=[text_fixed_properties], bypass_approval=False)
 
104
    kopete = SimulatedClient(q, bus, 'Kopete',
 
105
            observe=[text_fixed_properties], approve=[text_fixed_properties],
 
106
            handle=[text_fixed_properties], bypass_approval=False)
 
107
 
 
108
    # wait for MC to download the properties
 
109
    expect_client_setup(q, [empathy, kopete])
 
110
 
 
111
    # subscribe to the OperationList interface (MC assumes that until this
 
112
    # property has been retrieved once, nobody cares)
 
113
 
 
114
    cd = bus.get_object(cs.CD, cs.CD_PATH)
 
115
    cd_props = dbus.Interface(cd, cs.PROPERTIES_IFACE)
 
116
    assert cd_props.Get(cs.CD_IFACE_OP_LIST, 'DispatchOperations') == []
 
117
 
 
118
    e, chan, cr = request_channel_expect_query(q, bus, account, conn, empathy)
 
119
 
 
120
    # No.
 
121
    q.dbus_raise(e.message, 'com.example.Errors.No', 'Denied!')
 
122
 
 
123
    # The plugin responds
 
124
    e = q.expect('dbus-method-call',
 
125
            path=chan.object_path,
 
126
            interface=cs.CHANNEL_IFACE_GROUP,
 
127
            # this error message is from the plugin
 
128
            method='RemoveMembersWithReason', args=[[conn.self_handle],
 
129
                "Computer says no", cs.GROUP_REASON_PERMISSION_DENIED],
 
130
            handled=False)
 
131
    q.dbus_return(e.message, signature='')
 
132
    chan.close()
 
133
 
 
134
    # Try again
 
135
    e, chan, cr = request_channel_expect_query(q, bus, account, conn, kopete)
 
136
 
 
137
    # Yes.
 
138
    q.dbus_return(e.message, signature='')
 
139
 
 
140
    e, k = q.expect_many(
 
141
            EventPattern('dbus-method-call',
 
142
                path=empathy.object_path,
 
143
                interface=cs.OBSERVER, method='ObserveChannels',
 
144
                handled=False),
 
145
            EventPattern('dbus-method-call',
 
146
                path=kopete.object_path,
 
147
                interface=cs.OBSERVER, method='ObserveChannels',
 
148
                handled=False),
 
149
            )
 
150
    q.dbus_return(k.message, signature='')
 
151
    q.dbus_return(e.message, signature='')
 
152
 
 
153
    for _ in ('Kopete', 'Empathy'):
 
154
        e = q.expect('dbus-method-call',
 
155
                interface=cs.HANDLER, method='HandleChannels', handled=False)
 
156
        q.dbus_raise(e.message, cs.INVALID_ARGUMENT, 'Never mind')
 
157
 
 
158
    q.expect_many(
 
159
            EventPattern('dbus-signal', path=cr.object_path,
 
160
                interface=cs.CR, signal='Failed'),
 
161
            EventPattern('dbus-method-call', path=chan.object_path,
 
162
                interface=cs.CHANNEL, method='Close', handled=True),
 
163
            )
 
164
 
 
165
if __name__ == '__main__':
 
166
    exec_test(test, {})