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

« back to all changes in this revision

Viewing changes to tests/twisted/dispatcher/ensure-rapidly.py

  • Committer: Bazaar Package Importer
  • Author(s): Jonny Lamb
  • Date: 2011-01-27 17:54:12 UTC
  • mto: (0.12.1 upstream) (7.1.4 maverick)
  • mto: This revision was merged to the branch mainline in revision 8.
  • Revision ID: james.westby@ubuntu.com-20110127175412-cijhp5z0763s11cy
Tags: upstream-5.7.2
ImportĀ upstreamĀ versionĀ 5.7.2

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
"""Feature test ensuring that MC deals correctly with EnsureChannel returning
 
20
a channel that has already been dispatched to a handler.
 
21
"""
 
22
 
 
23
import dbus
 
24
import dbus.service
 
25
 
 
26
from servicetest import (EventPattern, tp_name_prefix, tp_path_prefix,
 
27
        call_async, assertContains, assertLength, assertEquals)
 
28
from mctest import exec_test, SimulatedConnection, SimulatedClient, \
 
29
        create_fakecm_account, enable_fakecm_account, SimulatedChannel, \
 
30
        expect_client_setup
 
31
import constants as cs
 
32
 
 
33
def test(q, bus, mc):
 
34
    params = dbus.Dictionary({"account": "someguy@example.com",
 
35
        "password": "secrecy"}, signature='sv')
 
36
    cm_name_ref, account = create_fakecm_account(q, bus, mc, params)
 
37
    conn = enable_fakecm_account(q, bus, mc, account, params)
 
38
 
 
39
    text_fixed_properties = dbus.Dictionary({
 
40
        cs.CHANNEL + '.TargetHandleType': cs.HT_CONTACT,
 
41
        cs.CHANNEL + '.ChannelType': cs.CHANNEL_TYPE_TEXT,
 
42
        }, signature='sv')
 
43
 
 
44
    client = SimulatedClient(q, bus, 'Empathy',
 
45
            observe=[text_fixed_properties], approve=[text_fixed_properties],
 
46
            handle=[text_fixed_properties], bypass_approval=False)
 
47
 
 
48
    # wait for MC to download the properties
 
49
    expect_client_setup(q, [client])
 
50
 
 
51
    channel = test_channel_creation(q, bus, account, client, conn,
 
52
            yours_first=True, swap_requests=False)
 
53
    channel.close()
 
54
 
 
55
    channel = test_channel_creation(q, bus, account, client, conn,
 
56
            yours_first=True, swap_requests=True)
 
57
    channel.close()
 
58
 
 
59
    channel = test_channel_creation(q, bus, account, client, conn,
 
60
            yours_first=False, swap_requests=False)
 
61
    channel.close()
 
62
 
 
63
    channel = test_channel_creation(q, bus, account, client, conn,
 
64
            yours_first=False, swap_requests=True)
 
65
    channel.close()
 
66
 
 
67
def test_channel_creation(q, bus, account, client, conn,
 
68
        yours_first=True, swap_requests=False):
 
69
    user_action_time1 = dbus.Int64(1238582606)
 
70
    user_action_time2 = dbus.Int64(1244444444)
 
71
 
 
72
    cd = bus.get_object(cs.CD, cs.CD_PATH)
 
73
    cd_props = dbus.Interface(cd, cs.PROPERTIES_IFACE)
 
74
 
 
75
    # chat UI calls ChannelDispatcher.EnsureChannel
 
76
    request = dbus.Dictionary({
 
77
            cs.CHANNEL + '.ChannelType': cs.CHANNEL_TYPE_TEXT,
 
78
            cs.CHANNEL + '.TargetHandleType': cs.HT_CONTACT,
 
79
            cs.CHANNEL + '.TargetID': 'juliet',
 
80
            }, signature='sv')
 
81
    account_requests = dbus.Interface(account,
 
82
            cs.ACCOUNT_IFACE_NOKIA_REQUESTS)
 
83
    call_async(q, cd, 'EnsureChannel',
 
84
            account.object_path, request, user_action_time1, client.bus_name,
 
85
            dbus_interface=cs.CD)
 
86
    ret = q.expect('dbus-return', method='EnsureChannel')
 
87
    request_path = ret.value[0]
 
88
 
 
89
    # chat UI connects to signals and calls ChannelRequest.Proceed()
 
90
 
 
91
    cr1 = bus.get_object(cs.AM, request_path)
 
92
    request_props = cr1.GetAll(cs.CR, dbus_interface=cs.PROPERTIES_IFACE)
 
93
    assert request_props['Account'] == account.object_path
 
94
    assert request_props['Requests'] == [request]
 
95
    assert request_props['UserActionTime'] == user_action_time1
 
96
    assert request_props['PreferredHandler'] == client.bus_name
 
97
    assert request_props['Interfaces'] == []
 
98
 
 
99
    cr1.Proceed(dbus_interface=cs.CR)
 
100
 
 
101
    cm_request_call1, add_request_call1 = q.expect_many(
 
102
            EventPattern('dbus-method-call',
 
103
                interface=cs.CONN_IFACE_REQUESTS,
 
104
                method='EnsureChannel',
 
105
                path=conn.object_path, args=[request], handled=False),
 
106
            EventPattern('dbus-method-call', handled=False,
 
107
                interface=cs.CLIENT_IFACE_REQUESTS,
 
108
                method='AddRequest', path=client.object_path),
 
109
            )
 
110
 
 
111
    # Before the first request has succeeded, the user gets impatient and
 
112
    # the UI re-requests.
 
113
    call_async(q, cd, 'EnsureChannel',
 
114
            account.object_path, request, user_action_time2, client.bus_name,
 
115
            dbus_interface=cs.CD)
 
116
    ret = q.expect('dbus-return', method='EnsureChannel')
 
117
    request_path = ret.value[0]
 
118
    cr2 = bus.get_object(cs.AM, request_path)
 
119
 
 
120
    request_props = cr2.GetAll(cs.CR, dbus_interface=cs.PROPERTIES_IFACE)
 
121
    assert request_props['Account'] == account.object_path
 
122
    assert request_props['Requests'] == [request]
 
123
    assert request_props['UserActionTime'] == user_action_time2
 
124
    assert request_props['PreferredHandler'] == client.bus_name
 
125
    assert request_props['Interfaces'] == []
 
126
 
 
127
    cr2.Proceed(dbus_interface=cs.CR)
 
128
 
 
129
    cm_request_call2, add_request_call2 = q.expect_many(
 
130
            EventPattern('dbus-method-call',
 
131
                interface=cs.CONN_IFACE_REQUESTS,
 
132
                method='EnsureChannel',
 
133
                path=conn.object_path, args=[request], handled=False),
 
134
            EventPattern('dbus-method-call', handled=False,
 
135
                interface=cs.CLIENT_IFACE_REQUESTS,
 
136
                method='AddRequest', path=client.object_path),
 
137
            )
 
138
 
 
139
    assert add_request_call1.args[0] == cr1.object_path
 
140
    request_props1 = add_request_call1.args[1]
 
141
    assert request_props1[cs.CR + '.Account'] == account.object_path
 
142
    assert request_props1[cs.CR + '.Requests'] == [request]
 
143
    assert request_props1[cs.CR + '.UserActionTime'] == user_action_time1
 
144
    assert request_props1[cs.CR + '.PreferredHandler'] == client.bus_name
 
145
    assert request_props1[cs.CR + '.Interfaces'] == []
 
146
 
 
147
    assert add_request_call2.args[0] == cr2.object_path
 
148
    request_props2 = add_request_call2.args[1]
 
149
    assert request_props2[cs.CR + '.Account'] == account.object_path
 
150
    assert request_props2[cs.CR + '.Requests'] == [request]
 
151
    assert request_props2[cs.CR + '.UserActionTime'] == user_action_time2
 
152
    assert request_props2[cs.CR + '.PreferredHandler'] == client.bus_name
 
153
    assert request_props2[cs.CR + '.Interfaces'] == []
 
154
 
 
155
    q.dbus_return(add_request_call1.message, signature='')
 
156
    q.dbus_return(add_request_call2.message, signature='')
 
157
 
 
158
    # Time passes. A channel is returned.
 
159
 
 
160
    channel_immutable = dbus.Dictionary(request)
 
161
    channel_immutable[cs.CHANNEL + '.InitiatorID'] = conn.self_ident
 
162
    channel_immutable[cs.CHANNEL + '.InitiatorHandle'] = conn.self_handle
 
163
    channel_immutable[cs.CHANNEL + '.Requested'] = True
 
164
    channel_immutable[cs.CHANNEL + '.Interfaces'] = \
 
165
        dbus.Array([], signature='s')
 
166
    channel_immutable[cs.CHANNEL + '.TargetHandle'] = \
 
167
        conn.ensure_handle(cs.HT_CONTACT, 'juliet')
 
168
    channel = SimulatedChannel(conn, channel_immutable)
 
169
 
 
170
    # Having announce() (i.e. NewChannels) come last is guaranteed by
 
171
    # telepathy-spec (since 0.17.14). There is no other ordering guarantee.
 
172
 
 
173
    if swap_requests:
 
174
        m2, m1 = cm_request_call1.message, cm_request_call2.message
 
175
    else:
 
176
        m1, m2 = cm_request_call1.message, cm_request_call2.message
 
177
 
 
178
    q.dbus_return(m1, yours_first,
 
179
            channel.object_path, channel.immutable, signature='boa{sv}')
 
180
    q.dbus_return(m2, not yours_first,
 
181
            channel.object_path, channel.immutable, signature='boa{sv}')
 
182
 
 
183
    channel.announce()
 
184
 
 
185
    # Observer should get told, processing waits for it
 
186
    e = q.expect('dbus-method-call',
 
187
            path=client.object_path,
 
188
            interface=cs.OBSERVER, method='ObserveChannels',
 
189
            handled=False)
 
190
    assert e.args[0] == account.object_path, e.args
 
191
    assert e.args[1] == conn.object_path, e.args
 
192
    assert e.args[3] == '/', e.args         # no dispatch operation
 
193
    assert sorted(e.args[4]) == sorted([cr1.object_path,
 
194
        cr2.object_path]), e.args
 
195
    channels = e.args[2]
 
196
    assert len(channels) == 1, channels
 
197
    assert channels[0][0] == channel.object_path, channels
 
198
    assert channels[0][1] == channel.immutable, channels
 
199
 
 
200
    # Observer says "OK, go"
 
201
    q.dbus_return(e.message, signature='')
 
202
 
 
203
    # Handler is next
 
204
    e = q.expect('dbus-method-call',
 
205
            path=client.object_path,
 
206
            interface=cs.HANDLER, method='HandleChannels',
 
207
            handled=False)
 
208
    assert e.args[0] == account.object_path, e.args
 
209
    assert e.args[1] == conn.object_path, e.args
 
210
    channels = e.args[2]
 
211
    assert len(channels) == 1, channels
 
212
    assert channels[0][0] == channel.object_path, channels
 
213
    assert channels[0][1] == channel_immutable, channels
 
214
    assert sorted(e.args[3]) == sorted([cr1.object_path,
 
215
        cr2.object_path]), e.args
 
216
    assert e.args[4] == user_action_time2, (e.args[4], user_action_time2)
 
217
    assert isinstance(e.args[5], dict)
 
218
    assertContains('request-properties', e.args[5])
 
219
    assertContains(cr1.object_path, e.args[5]['request-properties'])
 
220
    assertContains(cr2.object_path, e.args[5]['request-properties'])
 
221
    assertLength(2, e.args[5]['request-properties'])
 
222
    assertEquals(request_props1,
 
223
            e.args[5]['request-properties'][cr1.object_path])
 
224
    assertEquals(request_props2,
 
225
            e.args[5]['request-properties'][cr2.object_path])
 
226
    assert len(e.args) == 6
 
227
 
 
228
    # Handler accepts the Channels
 
229
    q.dbus_return(e.message, signature='')
 
230
 
 
231
    # CR emits Succeeded (or in Mardy's version, Account emits Succeeded)
 
232
    q.expect_many(
 
233
            EventPattern('dbus-signal', path=account.object_path,
 
234
                interface=cs.ACCOUNT_IFACE_NOKIA_REQUESTS, signal='Succeeded',
 
235
                args=[request_path]),
 
236
            EventPattern('dbus-signal', path=request_path,
 
237
                interface=cs.CR, signal='Succeeded'),
 
238
            )
 
239
 
 
240
    return channel
 
241
 
 
242
if __name__ == '__main__':
 
243
    exec_test(test, {})