<commit_before>
"""
Test that redundant calls to SetPresence don't cause anything to happen.
"""

import dbus

from servicetest import match
from gabbletest import go

@match('dbus-signal', signal='StatusChanged', args=[0, 1])
def expect_connected(event, data):
    data['conn_presence'] = dbus.Interface(data['conn'],
        'org.freedesktop.Telepathy.Connection.Interface.Presence')

    # Set presence to away. This should cause PresenceUpdate to be emitted,
    # and a new <presence> stanza to be sent to the server.
    data['conn_presence'].SetStatus({'away': {'message': 'gone'}})
    return True

@match('dbus-signal', signal='PresenceUpdate')
def expect_presence_update1(event, data):
    assert event.args == [{1L: (0L, {u'away': {u'message': u'gone'}})}]
    return True

@match('stream-presence')
def expect_presence_stanza1(event, data):
    children = list(event.stanza.elements())
    assert children[0].name == 'show'
    assert str(children[0]) == 'away'
    assert children[1].name == 'status'
    assert str(children[1]) == 'gone'

    # Set presence a second time. Since this call is redundant, there should
    # be no PresenceUpdate or <presence> sent to the server.
    data['conn_presence'].SetStatus({'away':{'message': 'gone'}})

    # Set presence a third time. This call is not redundant, and should
    # generate a signal/message.
    data['conn_presence'].SetStatus({'available': {'message': 'yo'}})

    return True

@match('dbus-signal', signal='PresenceUpdate')
def expect_presence_update2(event, data):
    assert event.args == [{1L: (0L, {u'available': {u'message': u'yo'}})}]
    return True

@match('stream-presence')
def expect_presence_stanza2(event, data):
    children = list(event.stanza.elements())
    assert children[0].name == 'status'
    assert str(children[0]) == 'yo'

    # call SetPresence with no optional arguments, as this used to cause a
    # crash in tp-glib
    data['conn_presence'].SetStatus({'available': {}})

    return True

@match('dbus-signal', signal='PresenceUpdate')
def expect_presence_update3(event, data):
    assert event.args == [{1L: (0L, {u'available': {}})}]
    return True

@match('stream-presence')
def expect_presence_stanza3(event, data):
    data['conn_iface'].Disconnect()
    return True

@match('dbus-signal', signal='StatusChanged', args=[2, 1])
def expect_disconnected(event, data):
    return True

if __name__ == '__main__':
    go()
<commit_msg>port test-set-status-idempotence to the new test API<commit_after>
"""
Test that redundant calls to SetPresence don't cause anything to happen.
"""

import dbus

from servicetest import EventPattern
from gabbletest import exec_test

def test(q, bus, conn, stream):
    conn.Connect()

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

    presence_iface = dbus.Interface (conn,
        u'org.freedesktop.Telepathy.Connection.Interface.Presence')

    # Set presence to away. This should cause PresenceUpdate to be emitted,
    # and a new <presence> stanza to be sent to the server.
    presence_iface.SetStatus({'away': {'message': 'gone'}})

    signal, event = q.expect_many (
        EventPattern('dbus-signal', signal='PresenceUpdate'),
        EventPattern('stream-presence'))
    signal.args == [{1L: (0L, {u'away': {u'message': u'gone'}})}]

    children = list(event.stanza.elements())
    assert children[0].name == 'show'
    assert str(children[0]) == 'away'
    assert children[1].name == 'status'
    assert str(children[1]) == 'gone'

    # Set presence a second time. Since this call is redundant, there should
    # be no PresenceUpdate or <presence> sent to the server.
    presence_iface.SetStatus({'away':{'message': 'gone'}})

    # Set presence a third time. This call is not redundant, and should
    # generate a signal/message.
    presence_iface.SetStatus({'available': {'message': 'yo'}})

    signal, event = q.expect_many (
        EventPattern('dbus-signal', signal='PresenceUpdate'),
        EventPattern('stream-presence'))

    assert signal.args == [{1L: (0L, {u'available': {u'message': u'yo'}})}]
    children = list(event.stanza.elements())
    assert children[0].name == 'status'
    assert str(children[0]) == 'yo'

    # call SetPresence with no optional arguments, as this used to cause a
    # crash in tp-glib
    presence_iface.SetStatus({'available': {}})


    signal, _ = q.expect_many (
        EventPattern('dbus-signal', signal='PresenceUpdate'),
        EventPattern('stream-presence'))
    assert signal.args == [{1L: (0L, {u'available': {}})}]

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

if __name__ == '__main__':
    exec_test(test)
{'fraction_non_alphanumeric': 0.1514505119453925, 'fraction_numerical': 0.008105802047781569, 'mean_word_length': 4.186946902654867, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2264161', 'n_tokens_mistral': 1512, 'n_tokens_neox': 1472, 'n_words': 400}