1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
|
# coding=utf-8
"""
Test that chat state notifications are correctly sent and received on text
channels.
"""
from twisted.words.xish import domish
from servicetest import (assertEquals, assertNotEquals,
assertLength, wrap_channel, EventPattern, call_async,
sync_dbus)
from gabbletest import exec_test, make_result_iq, sync_stream, make_presence
import constants as cs
import ns
def check_state_notification(elem, name, allow_body=False):
assertEquals('message', elem.name)
assertEquals('chat', elem['type'])
children = list(elem.elements())
notification = [x for x in children if x.uri == ns.CHAT_STATES][0]
assert notification.name == name, notification.toXml()
if not allow_body:
assert len(children) == 1, elem.toXml()
def make_message(jid, body=None, state=None):
m = domish.Element((None, 'message'))
m['from'] = jid
m['type'] = 'chat'
if state is not None:
m.addElement((ns.CHAT_STATES, state))
if body is not None:
m.addElement('body', content=body)
return m
def test(q, bus, conn, stream):
self_handle = conn.GetSelfHandle()
jid = 'foo@bar.com'
full_jid = 'foo@bar.com/Foo'
foo_handle = conn.RequestHandles(cs.HT_CONTACT, [jid])[0]
path = conn.Requests.CreateChannel(
{ cs.CHANNEL_TYPE: cs.CHANNEL_TYPE_TEXT,
cs.TARGET_HANDLE_TYPE: cs.HT_CONTACT,
cs.TARGET_HANDLE: foo_handle,
})[0]
chan = wrap_channel(bus.get_object(conn.bus_name, path), 'Text',
['ChatState', 'Destroyable'])
presence = make_presence(full_jid, status='hello',
caps={
'node': 'http://telepathy.freedesktop.org/homeopathy',
'ver' : '0.1',
})
stream.send(presence)
version_event = q.expect('stream-iq', to=full_jid,
query_ns=ns.DISCO_INFO,
query_node='http://telepathy.freedesktop.org/homeopathy#0.1')
result = make_result_iq(stream, version_event.stanza)
query = result.firstChildElement()
feature = query.addElement('feature')
feature['var'] = ns.CHAT_STATES
stream.send(result)
sync_stream(q, stream)
states = chan.Properties.Get(cs.CHANNEL_IFACE_CHAT_STATE, 'ChatStates')
assertEquals(cs.CHAT_STATE_INACTIVE,
states.get(self_handle, cs.CHAT_STATE_INACTIVE))
assertEquals(cs.CHAT_STATE_INACTIVE,
states.get(foo_handle, cs.CHAT_STATE_INACTIVE))
# Receiving chat states:
# Composing...
stream.send(make_message(full_jid, state='composing'))
changed = q.expect('dbus-signal', signal='ChatStateChanged',
path=chan.object_path)
handle, state = changed.args
assertEquals(foo_handle, handle)
assertEquals(cs.CHAT_STATE_COMPOSING, state)
states = chan.Properties.Get(cs.CHANNEL_IFACE_CHAT_STATE, 'ChatStates')
assertEquals(cs.CHAT_STATE_INACTIVE,
states.get(self_handle, cs.CHAT_STATE_INACTIVE))
assertEquals(cs.CHAT_STATE_COMPOSING,
states.get(foo_handle, cs.CHAT_STATE_INACTIVE))
# Message!
stream.send(make_message(full_jid, body='hello', state='active'))
changed = q.expect('dbus-signal', signal='ChatStateChanged',
path=chan.object_path)
handle, state = changed.args
assertEquals(foo_handle, handle)
assertEquals(cs.CHAT_STATE_ACTIVE, state)
states = chan.Properties.Get(cs.CHANNEL_IFACE_CHAT_STATE, 'ChatStates')
assertEquals(cs.CHAT_STATE_INACTIVE,
states.get(self_handle, cs.CHAT_STATE_INACTIVE))
assertEquals(cs.CHAT_STATE_ACTIVE,
states.get(foo_handle, cs.CHAT_STATE_INACTIVE))
# Assert that a redundant chat-state change doesn't emit a signal
forbidden = [EventPattern('dbus-signal', signal='ChatStateChanged',
args=[foo_handle, cs.CHAT_STATE_ACTIVE])]
q.forbid_events(forbidden)
m = domish.Element((None, 'message'))
m['from'] = 'foo@bar.com/Foo'
m['type'] = 'chat'
m.addElement((ns.CHAT_STATES, 'active'))
m.addElement('body', content='hello')
stream.send(m)
sync_dbus(bus, q, conn)
sync_stream(q, stream)
q.unforbid_events(forbidden)
# Sending chat states:
# Composing...
chan.ChatState.SetChatState(cs.CHAT_STATE_COMPOSING)
stream_message = q.expect('stream-message')
check_state_notification(stream_message.stanza, 'composing')
states = chan.Properties.Get(cs.CHANNEL_IFACE_CHAT_STATE, 'ChatStates')
assertEquals(cs.CHAT_STATE_COMPOSING,
states.get(self_handle, cs.CHAT_STATE_INACTIVE))
assertEquals(cs.CHAT_STATE_ACTIVE,
states.get(foo_handle, cs.CHAT_STATE_INACTIVE))
# XEP 0085:
# every content message SHOULD contain an <active/> notification.
chan.Text.Send(0, 'hi.')
stream_message = q.expect('stream-message')
elem = stream_message.stanza
assertEquals('chat', elem['type'])
check_state_notification(elem, 'active', allow_body=True)
states = chan.Properties.Get(cs.CHANNEL_IFACE_CHAT_STATE, 'ChatStates')
assertEquals(cs.CHAT_STATE_ACTIVE,
states.get(self_handle, cs.CHAT_STATE_INACTIVE))
assertEquals(cs.CHAT_STATE_ACTIVE,
states.get(foo_handle, cs.CHAT_STATE_INACTIVE))
def is_body(e):
if e.name == 'body':
assert e.children[0] == u'hi.', e.toXml()
return True
return False
assert len([x for x in elem.elements() if is_body(x)]) == 1, elem.toXml()
# Close the channel without acking the received message. The peer should
# get a <gone/> notification, and the channel should respawn.
chan.Close()
gone, _, _ = q.expect_many(
EventPattern('stream-message'),
EventPattern('dbus-signal', signal='Closed'),
EventPattern('dbus-signal', signal='NewChannel'),
)
check_state_notification(gone.stanza, 'gone')
# Reusing the proxy object because we happen to know it'll be at the same
# path...
# Destroy the channel. The peer shouldn't get a <gone/> notification, since
# we already said we were gone and haven't sent them any messages to the
# contrary.
es = [EventPattern('stream-message')]
q.forbid_events(es)
chan.Destroyable.Destroy()
sync_stream(q, stream)
# Make the channel anew.
path = conn.Requests.CreateChannel(
{ cs.CHANNEL_TYPE: cs.CHANNEL_TYPE_TEXT,
cs.TARGET_HANDLE_TYPE: cs.HT_CONTACT,
cs.TARGET_HANDLE: foo_handle,
})[0]
chan = wrap_channel(bus.get_object(conn.bus_name, path), 'Text',
['ChatState', 'Destroyable'])
# Close it immediately; the peer should again not get a <gone/>
# notification, since we haven't sent any notifications on that channel.
chan.Close()
sync_stream(q, stream)
q.unforbid_events(es)
# XEP-0085 §5.1 defines how to negotiate support for chat states with a
# contact in the absence of capabilities. This is useful when talking to
# invisible contacts, for example.
# First, if we receive a message from a contact, containing an <active/>
# notification, they support chat states, so we should send them.
jid = 'i@example.com'
full_jid = jid + '/GTalk'
path = conn.Requests.CreateChannel(
{ cs.CHANNEL_TYPE: cs.CHANNEL_TYPE_TEXT,
cs.TARGET_HANDLE_TYPE: cs.HT_CONTACT,
cs.TARGET_ID: jid,
})[0]
chan = wrap_channel(bus.get_object(conn.bus_name, path), 'Text',
['ChatState'])
stream.send(make_message(full_jid, body='i am invisible', state='active'))
changed = q.expect('dbus-signal', signal='ChatStateChanged',
path=chan.object_path)
assertEquals(cs.CHAT_STATE_ACTIVE, changed.args[1])
# We've seen them send a chat state notification, so we should send them
# notifications when the UI tells us to.
chan.ChatState.SetChatState(cs.CHAT_STATE_COMPOSING)
stream_message = q.expect('stream-message', to=full_jid)
check_state_notification(stream_message.stanza, 'composing')
changed = q.expect('dbus-signal', signal='ChatStateChanged',
path=chan.object_path)
handle, state = changed.args
assertEquals(cs.CHAT_STATE_COMPOSING, state)
assertEquals(self_handle, handle)
chan.Text.Send(0, 'very convincing')
stream_message = q.expect('stream-message', to=full_jid)
check_state_notification(stream_message.stanza, 'active', allow_body=True)
# Now, test the case where we start the negotiation, and the contact
# turns out to support chat state notifications.
jid = 'c@example.com'
full_jid = jid + '/GTalk'
path = conn.Requests.CreateChannel(
{ cs.CHANNEL_TYPE: cs.CHANNEL_TYPE_TEXT,
cs.TARGET_HANDLE_TYPE: cs.HT_CONTACT,
cs.TARGET_ID: jid,
})[0]
chan = wrap_channel(bus.get_object(conn.bus_name, path), 'Text',
['ChatState'])
# We shouldn't send any notifications until we actually send a message.
# But ChatStateChanged is still emitted locally
e = EventPattern('stream-message', to=jid)
q.forbid_events([e])
for i in [cs.CHAT_STATE_ACTIVE, cs.CHAT_STATE_COMPOSING,
cs.CHAT_STATE_PAUSED, cs.CHAT_STATE_INACTIVE ]:
chan.ChatState.SetChatState(i)
changed = q.expect('dbus-signal', signal='ChatStateChanged',
path=chan.object_path)
handle, state = changed.args
assertEquals(i, state)
assertEquals(self_handle, handle)
sync_stream(q, stream)
q.unforbid_events([e])
# When we send a message, say we're active.
chan.Text.Send(0, 'is anyone there?')
stream_message = q.expect('stream-message', to=jid)
check_state_notification(stream_message.stanza, 'active', allow_body=True)
# The D-Bus property changes, too
changed = q.expect('dbus-signal', signal='ChatStateChanged',
path=chan.object_path)
handle, state = changed.args
assertEquals(cs.CHAT_STATE_ACTIVE, state)
assertEquals(self_handle, handle)
# We get a notification back from our contact.
stream.send(make_message(full_jid, state='composing'))
# Wait until gabble tells us the chat-state of the remote party has
# changed so we know gabble knows chat state notification are supported
changed = q.expect('dbus-signal', signal='ChatStateChanged',
path=chan.object_path)
handle, state = changed.args
assertEquals(cs.CHAT_STATE_COMPOSING, state)
assertNotEquals(foo_handle, handle)
# So now we know they support notification, so should send notifications.
chan.ChatState.SetChatState(cs.CHAT_STATE_COMPOSING)
# This doesn't check whether we're sending to the bare jid, or the
# jid+resource. In fact, the notification is sent to the bare jid, because
# we only update which jid we send to when we actually receive a message,
# not when we receive a notification. wjt thinks this is less surprising
# than the alternative:
#
# • I'm talking to you on my N900, and signed in on my laptop;
# • I enter one character in a tab to you on my laptop, and then delete
# it;
# • Now your messages to me appear on my laptop (until I send you another
# one from my N900)!
stream_message = q.expect('stream-message')
check_state_notification(stream_message.stanza, 'composing')
# The D-Bus property changes, too
changed = q.expect('dbus-signal', signal='ChatStateChanged',
path=chan.object_path)
handle, state = changed.args
assertEquals(cs.CHAT_STATE_COMPOSING, state)
assertEquals(self_handle, handle)
# But! Now they start messaging us from a different client, which *doesn't*
# support notifications.
other_jid = jid + '/Library'
stream.send(make_message(other_jid, body='grr, library computers'))
q.expect('dbus-signal', signal='Received')
# Okay, we should stop sending typing notifications.
e = EventPattern('stream-message', to=other_jid)
q.forbid_events([e])
for i in [cs.CHAT_STATE_COMPOSING, cs.CHAT_STATE_INACTIVE,
cs.CHAT_STATE_PAUSED, cs.CHAT_STATE_ACTIVE]:
chan.ChatState.SetChatState(i)
sync_stream(q, stream)
q.unforbid_events([e])
# Now, test the case where we start the negotiation, and the contact
# does not support chat state notifications
jid = 'twitterbot@example.com'
full_jid = jid + '/Nonsense'
path = conn.Requests.CreateChannel(
{ cs.CHANNEL_TYPE: cs.CHANNEL_TYPE_TEXT,
cs.TARGET_HANDLE_TYPE: cs.HT_CONTACT,
cs.TARGET_ID: jid,
})[0]
chan = wrap_channel(bus.get_object(conn.bus_name, path), 'Text',
['ChatState'])
# We shouldn't send any notifications until we actually send a message.
e = EventPattern('stream-message', to=jid)
q.forbid_events([e])
for i in [cs.CHAT_STATE_COMPOSING, cs.CHAT_STATE_INACTIVE,
cs.CHAT_STATE_PAUSED, cs.CHAT_STATE_ACTIVE]:
chan.ChatState.SetChatState(i)
sync_stream(q, stream)
q.unforbid_events([e])
# When we send a message, say we're active.
chan.Text.Send(0, '#n900 #maemo #zomg #woo #yay http://bit.ly/n900')
stream_message = q.expect('stream-message', to=jid)
check_state_notification(stream_message.stanza, 'active', allow_body=True)
# They reply without a chat state.
stream.send(make_message(full_jid, body="posted."))
q.expect('dbus-signal', signal='Received')
# Okay, we shouldn't send any more.
e = EventPattern('stream-message', to=other_jid)
q.forbid_events([e])
for i in [cs.CHAT_STATE_COMPOSING, cs.CHAT_STATE_INACTIVE,
cs.CHAT_STATE_PAUSED, cs.CHAT_STATE_ACTIVE]:
chan.ChatState.SetChatState(i)
sync_stream(q, stream)
q.unforbid_events([e])
chan.Text.Send(0, '@stephenfry simmer down')
message = q.expect('stream-message')
states = [x for x in message.stanza.elements() if x.uri == ns.CHAT_STATES]
assertLength(0, states)
if __name__ == '__main__':
exec_test(test)
|