
"""
Test receiving delayed (offline) messages on a text channel.
"""

import datetime

from twisted.words.xish import domish

from gabbletest import exec_test

def test(q, bus, conn, stream):
    conn.Connect()
    q.expect('dbus-signal', signal='StatusChanged', args=[0, 1])

    m = domish.Element(('', 'message'))
    m['from'] = 'foo@bar.com'
    m['type'] = 'chat'
    m.addElement('body', content='hello')

    # add timestamp information
    x = m.addElement(('jabber:x:delay', 'x'))
    x['stamp'] = '20070517T16:15:01'

    stream.send(m)

    event = q.expect('dbus-signal', signal='NewChannel')
    assert event.args[1] == u'org.freedesktop.Telepathy.Channel.Type.Text'
    # check that handle type == contact handle
    assert event.args[2] == 1
    jid = conn.InspectHandles(1, [event.args[3]])[0]
    assert jid == 'foo@bar.com'

    event = q.expect('dbus-signal', signal='Received')
    assert (str(datetime.datetime.utcfromtimestamp(event.args[1]))
        == '2007-05-17 16:15:01')
    assert event.args[5] == 'hello'

    conn.Disconnect()
    q.expect('dbus-signal', signal='StatusChanged', args=[2, 1])

if __name__ == '__main__':
    exec_test(test)


@@ -152,16 +152,53 @@
 xec_test
+%0Afrom servicetest import EventPattern
 %0A%0Adef te
@@ -883,58 +883,181 @@
     
-event = q.expect('dbus-signal', signal='Received')
+received, message_received = q.expect_many(%0A        EventPattern('dbus-signal', signal='Received'),%0A        EventPattern('dbus-signal', signal='MessageReceived'),%0A        )%0A
 %0A   
@@ -1104,21 +1104,24 @@
 mestamp(
-event
+received
 .args%5B1%5D
@@ -1172,20 +1172,433 @@
 ert 
-event.args%5B5
+received.args%5B5%5D == 'hello'%0A%0A    message = message_received.args%5B0%5D%0A    header = message%5B0%5D%0A    message_sent_timestamp = header%5B'message-sent'%5D%0A    assert str(datetime.datetime.utcfromtimestamp(message_sent_timestamp)%0A        == '2007-05-17 16:15:01'), headers%0A    message_received_timestamp = header%5B'message-received'%5D%0A    assert message_received_timestamp %3E message_sent_timestamp, headers%0A%0A    assert message%5B1%5D%5B'content'
 %5D ==
@@ -1601,24 +1601,34 @@
 %5D == 'hello'
+, message%0A
 %0A%0A    conn.D

Test timestamps of delayed messages with Messages interface