From 52220d7dc1d5950d1a1f9fd4b1ff8ae581381fd7 Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Mon, 23 Sep 2013 13:58:27 +0100 Subject: [PATCH 06/16] tests: use more constants, reduce hard-coding of o.fd.T --- tests/twisted/text/destroy.py | 40 ++++++++++--------------- tests/twisted/text/ensure.py | 45 ++++++++++------------------ tests/twisted/text/initiate-requestotron.py | 44 ++++++++++----------------- tests/twisted/text/respawn.py | 46 +++++++++++------------------ tests/twisted/text/test-text.py | 28 +++++++----------- 5 files changed, 75 insertions(+), 128 deletions(-) diff --git a/tests/twisted/text/destroy.py b/tests/twisted/text/destroy.py index 4c495bb..33b1de1 100644 --- a/tests/twisted/text/destroy.py +++ b/tests/twisted/text/destroy.py @@ -17,8 +17,8 @@ def test(q, bus, conn, stream): jid = 'foo@bar.com' foo_handle = conn.get_contact_handle_sync(jid) - call_async(q, conn, 'RequestChannel', - 'org.freedesktop.Telepathy.Channel.Type.Text', 1, foo_handle, True) + call_async(q, conn, 'RequestChannel', cs.CHANNEL_TYPE_TEXT, + cs.HT_CONTACT, foo_handle, True) ret, sig = q.expect_many( EventPattern('dbus-return', method='RequestChannel'), @@ -26,34 +26,24 @@ def test(q, bus, conn, stream): ) text_chan = bus.get_object(conn.bus_name, ret.value[0]) - chan_iface = dbus.Interface(text_chan, - 'org.freedesktop.Telepathy.Channel') - text_iface = dbus.Interface(text_chan, - 'org.freedesktop.Telepathy.Channel.Type.Text') - destroyable_iface = dbus.Interface(text_chan, - 'org.freedesktop.Telepathy.Channel.Interface.Destroyable') + chan_iface = dbus.Interface(text_chan, cs.CHANNEL) + text_iface = dbus.Interface(text_chan, cs.CHANNEL_TYPE_TEXT) + destroyable_iface = dbus.Interface(text_chan, cs.CHANNEL_IFACE_DESTROYABLE) assertLength(1, sig.args) assertLength(1, sig.args[0]) # one channel assertLength(2, sig.args[0][0]) # two struct members assertEquals(ret.value[0], sig.args[0][0][0]) emitted_props = sig.args[0][0][1] - assert emitted_props['org.freedesktop.Telepathy.Channel.ChannelType'] ==\ - 'org.freedesktop.Telepathy.Channel.Type.Text' - assert emitted_props['org.freedesktop.Telepathy.Channel.' - 'TargetHandleType'] == 1 - assert emitted_props['org.freedesktop.Telepathy.Channel.TargetHandle'] ==\ - foo_handle - assert emitted_props['org.freedesktop.Telepathy.Channel.TargetID'] == jid - assert emitted_props['org.freedesktop.Telepathy.Channel.' - 'Requested'] == True - assert emitted_props['org.freedesktop.Telepathy.Channel.' - 'InitiatorHandle'] == self_handle - assert emitted_props['org.freedesktop.Telepathy.Channel.' - 'InitiatorID'] == 'test@localhost' - - channel_props = text_chan.GetAll( - 'org.freedesktop.Telepathy.Channel', + assertEquals(cs.CHANNEL_TYPE_TEXT, emitted_props[cs.CHANNEL_TYPE]) + assertEquals(cs.HT_CONTACT, emitted_props[cs.TARGET_HANDLE_TYPE]) + assertEquals(foo_handle, emitted_props[cs.TARGET_HANDLE]) + assertEquals(jid, emitted_props[cs.TARGET_ID]) + assertEquals(True, emitted_props[cs.REQUESTED]) + assertEquals(self_handle, emitted_props[cs.INITIATOR_HANDLE]) + assertEquals('test@localhost', emitted_props[cs.INITIATOR_ID]) + + channel_props = text_chan.GetAll(cs.CHANNEL, dbus_interface=dbus.PROPERTIES_IFACE) assert channel_props['TargetID'] == jid,\ (channel_props['TargetID'], jid) @@ -99,7 +89,7 @@ def test(q, bus, conn, stream): assert event.args[5] == 'hello' messages = text_chan.ListPendingMessages(False, - dbus_interface='org.freedesktop.Telepathy.Channel.Type.Text') + dbus_interface=cs.CHANNEL_TYPE_TEXT) assert messages == \ [(hello_message_id, hello_message_time, foo_handle, 0, 0, 'hello')], messages diff --git a/tests/twisted/text/ensure.py b/tests/twisted/text/ensure.py index 0c4e182..6896408 100644 --- a/tests/twisted/text/ensure.py +++ b/tests/twisted/text/ensure.py @@ -20,8 +20,7 @@ def test(q, bus, conn, stream): jids = ['foo@bar.com', 'truc@cafe.fr'] handles = conn.get_contact_handles_sync(jids) - properties = conn.GetAll( - 'org.freedesktop.Telepathy.Connection.Interface.Requests', + properties = conn.GetAll(cs.CONN_IFACE_REQUESTS, dbus_interface=dbus.PROPERTIES_IFACE) # Difference from Gabble: Haze's roster channels spring to life even if you # haven't received the XMPP roster. @@ -29,13 +28,10 @@ def test(q, bus, conn, stream): if c[1][cs.CHANNEL_TYPE] == cs.CHANNEL_TYPE_TEXT ] assert text_channels == [], text_channels - assert ({'org.freedesktop.Telepathy.Channel.ChannelType': - 'org.freedesktop.Telepathy.Channel.Type.Text', - 'org.freedesktop.Telepathy.Channel.TargetHandleType': 1, + assert ({cs.CHANNEL_TYPE: cs.CHANNEL_TYPE_TEXT, + cs.TARGET_HANDLE_TYPE: cs.HT_CONTACT, }, - ['org.freedesktop.Telepathy.Channel.TargetHandle', - 'org.freedesktop.Telepathy.Channel.TargetID' - ], + [cs.TARGET_HANDLE, cs.TARGET_ID], ) in properties.get('RequestableChannelClasses'),\ properties['RequestableChannelClasses'] @@ -75,8 +71,7 @@ def test_ensure_ensure(q, conn, self_handle, jid, handle): assertEquals(path, sig.args[0][0][0]) assertEquals(emitted_props, sig.args[0][0][1]) - properties = conn.GetAll( - 'org.freedesktop.Telepathy.Connection.Interface.Requests', + properties = conn.GetAll(cs.CONN_IFACE_REQUESTS, dbus_interface=dbus.PROPERTIES_IFACE) assertContains(sig.args[0][0], properties['Channels']) @@ -119,8 +114,7 @@ def test_request_ensure(q, conn, self_handle, jid, handle): assertEquals(path, sig.args[0][0][0]) assertEquals(emitted_props, sig.args[0][0][1]) - properties = conn.GetAll( - 'org.freedesktop.Telepathy.Connection.Interface.Requests', + properties = conn.GetAll(cs.CONN_IFACE_REQUESTS, dbus_interface=dbus.PROPERTIES_IFACE) assertContains(sig.args[0][0], properties['Channels']) @@ -140,26 +134,19 @@ def test_request_ensure(q, conn, self_handle, jid, handle): def check_props(props, self_handle, handle, jid): - assert props['org.freedesktop.Telepathy.Channel.ChannelType'] ==\ - 'org.freedesktop.Telepathy.Channel.Type.Text' - assert props['org.freedesktop.Telepathy.Channel.' - 'TargetHandleType'] == 1 - assert props['org.freedesktop.Telepathy.Channel.TargetHandle'] ==\ - handle - assert props['org.freedesktop.Telepathy.Channel.TargetID'] == jid - assert props['org.freedesktop.Telepathy.Channel.' - 'Requested'] == True - assert props['org.freedesktop.Telepathy.Channel.' - 'InitiatorHandle'] == self_handle - assert props['org.freedesktop.Telepathy.Channel.' - 'InitiatorID'] == 'test@localhost' + assertEquals(cs.CHANNEL_TYPE_TEXT, props[cs.CHANNEL_TYPE]) + assertEquals(cs.HT_CONTACT, props[cs.TARGET_HANDLE_TYPE]) + assertEquals(handle, props[cs.TARGET_HANDLE]) + assertEquals(jid, props[cs.TARGET_ID]) + assertEquals(True, props[cs.REQUESTED]) + assertEquals(self_handle, props[cs.INITIATOR_HANDLE]) + assertEquals('test@localhost', props[cs.INITIATOR_ID]) def request_props(handle): - return { 'org.freedesktop.Telepathy.Channel.ChannelType': - 'org.freedesktop.Telepathy.Channel.Type.Text', - 'org.freedesktop.Telepathy.Channel.TargetHandleType': 1, - 'org.freedesktop.Telepathy.Channel.TargetHandle': handle, + return { cs.CHANNEL_TYPE: cs.CHANNEL_TYPE_TEXT, + cs.TARGET_HANDLE_TYPE: cs.HT_CONTACT, + cs.TARGET_HANDLE: handle, } diff --git a/tests/twisted/text/initiate-requestotron.py b/tests/twisted/text/initiate-requestotron.py index 41b9407..c76dd36 100644 --- a/tests/twisted/text/initiate-requestotron.py +++ b/tests/twisted/text/initiate-requestotron.py @@ -20,8 +20,7 @@ def test(q, bus, conn, stream): jid = 'foo@bar.com' foo_handle = conn.get_contact_handle_sync(jid) - properties = conn.GetAll( - 'org.freedesktop.Telepathy.Connection.Interface.Requests', + properties = conn.GetAll(cs.CONN_IFACE_REQUESTS, dbus_interface=dbus.PROPERTIES_IFACE) # Difference from Gabble: Haze's roster channels spring to life even if you # haven't received the XMPP roster. @@ -29,22 +28,18 @@ def test(q, bus, conn, stream): if c[1][cs.CHANNEL_TYPE] == cs.CHANNEL_TYPE_TEXT ] assert text_channels == [], text_channels - assert ({'org.freedesktop.Telepathy.Channel.ChannelType': - 'org.freedesktop.Telepathy.Channel.Type.Text', - 'org.freedesktop.Telepathy.Channel.TargetHandleType': 1, + assert ({cs.CHANNEL_TYPE: cs.CHANNEL_TYPE_TEXT, + cs.TARGET_HANDLE_TYPE: cs.HT_CONTACT, }, - ['org.freedesktop.Telepathy.Channel.TargetHandle', - 'org.freedesktop.Telepathy.Channel.TargetID' - ], + [cs.TARGET_HANDLE, cs.TARGET_ID], ) in properties.get('RequestableChannelClasses'),\ properties['RequestableChannelClasses'] call_async(q, conn.Requests, 'CreateChannel', - { 'org.freedesktop.Telepathy.Channel.ChannelType': - 'org.freedesktop.Telepathy.Channel.Type.Text', - 'org.freedesktop.Telepathy.Channel.TargetHandleType': 1, - 'org.freedesktop.Telepathy.Channel.TargetHandle': foo_handle, - }) + { cs.CHANNEL_TYPE: cs.CHANNEL_TYPE_TEXT, + cs.TARGET_HANDLE_TYPE: cs.HT_CONTACT, + cs.TARGET_HANDLE: foo_handle, + }) ret, sig = q.expect_many( EventPattern('dbus-return', method='CreateChannel'), @@ -53,19 +48,13 @@ def test(q, bus, conn, stream): assert len(ret.value) == 2 emitted_props = ret.value[1] - assert emitted_props['org.freedesktop.Telepathy.Channel.ChannelType'] ==\ - 'org.freedesktop.Telepathy.Channel.Type.Text' - assert emitted_props['org.freedesktop.Telepathy.Channel.' - 'TargetHandleType'] == 1 - assert emitted_props['org.freedesktop.Telepathy.Channel.TargetHandle'] ==\ - foo_handle - assert emitted_props['org.freedesktop.Telepathy.Channel.TargetID'] == jid - assert emitted_props['org.freedesktop.Telepathy.Channel.' - 'Requested'] == True - assert emitted_props['org.freedesktop.Telepathy.Channel.' - 'InitiatorHandle'] == self_handle - assert emitted_props['org.freedesktop.Telepathy.Channel.' - 'InitiatorID'] == 'test@localhost' + assertEquals(cs.CHANNEL_TYPE_TEXT, emitted_props[cs.CHANNEL_TYPE]) + assertEquals(cs.HT_CONTACT, emitted_props[cs.TARGET_HANDLE_TYPE]) + assertEquals(foo_handle, emitted_props[cs.TARGET_HANDLE]) + assertEquals(jid, emitted_props[cs.TARGET_ID]) + assertEquals(True, emitted_props[cs.REQUESTED]) + assertEquals(self_handle, emitted_props[cs.INITIATOR_HANDLE]) + assertEquals('test@localhost', emitted_props[cs.INITIATOR_ID]) assertLength(1, sig.args) assertLength(1, sig.args[0]) # one channel @@ -73,8 +62,7 @@ def test(q, bus, conn, stream): assertEquals(ret.value[0], sig.args[0][0][0]) assertEquals(ret.value[1], sig.args[0][0][1]) - properties = conn.GetAll( - 'org.freedesktop.Telepathy.Connection.Interface.Requests', + properties = conn.GetAll(cs.CONN_IFACE_REQUESTS, dbus_interface=dbus.PROPERTIES_IFACE) assertContains(sig.args[0][0], properties['Channels']) diff --git a/tests/twisted/text/respawn.py b/tests/twisted/text/respawn.py index 5510a92..6bcc059 100644 --- a/tests/twisted/text/respawn.py +++ b/tests/twisted/text/respawn.py @@ -16,8 +16,8 @@ def test(q, bus, conn, stream): jid = 'foo@bar.com' foo_handle = conn.get_contact_handle_sync(jid) - call_async(q, conn, 'RequestChannel', - 'org.freedesktop.Telepathy.Channel.Type.Text', 1, foo_handle, True) + call_async(q, conn, 'RequestChannel', cs.CHANNEL_TYPE_TEXT, + cs.HT_CONTACT, foo_handle, True) ret, sig = q.expect_many( EventPattern('dbus-return', method='RequestChannel'), @@ -25,32 +25,23 @@ def test(q, bus, conn, stream): ) text_chan = bus.get_object(conn.bus_name, ret.value[0]) - chan_iface = dbus.Interface(text_chan, - 'org.freedesktop.Telepathy.Channel') - text_iface = dbus.Interface(text_chan, - 'org.freedesktop.Telepathy.Channel.Type.Text') + chan_iface = dbus.Interface(text_chan, cs.CHANNEL) + text_iface = dbus.Interface(text_chan, cs.CHANNEL_TYPE_TEXT) assertLength(1, sig.args) assertLength(1, sig.args[0]) # one channel assertLength(2, sig.args[0][0]) # two struct members assertEquals(ret.value[0], sig.args[0][0][0]) emitted_props = sig.args[0][0][1] - assert emitted_props['org.freedesktop.Telepathy.Channel.ChannelType'] ==\ - 'org.freedesktop.Telepathy.Channel.Type.Text' - assert emitted_props['org.freedesktop.Telepathy.Channel.' - 'TargetHandleType'] == 1 - assert emitted_props['org.freedesktop.Telepathy.Channel.TargetHandle'] ==\ - foo_handle - assert emitted_props['org.freedesktop.Telepathy.Channel.TargetID'] == jid - assert emitted_props['org.freedesktop.Telepathy.Channel.' - 'Requested'] == True - assert emitted_props['org.freedesktop.Telepathy.Channel.' - 'InitiatorHandle'] == self_handle - assert emitted_props['org.freedesktop.Telepathy.Channel.' - 'InitiatorID'] == 'test@localhost' - - channel_props = text_chan.GetAll( - 'org.freedesktop.Telepathy.Channel', + assertEquals(cs.CHANNEL_TYPE_TEXT, emitted_props[cs.CHANNEL_TYPE]) + assertEquals(cs.HT_CONTACT, emitted_props[cs.TARGET_HANDLE_TYPE]) + assertEquals(foo_handle, emitted_props[cs.TARGET_HANDLE]) + assertEquals(jid, emitted_props[cs.TARGET_ID]) + assertEquals(True, emitted_props[cs.REQUESTED]) + assertEquals(self_handle, emitted_props[cs.INITIATOR_HANDLE]) + assertEquals('test@localhost', emitted_props[cs.INITIATOR_ID]) + + channel_props = text_chan.GetAll(cs.CHANNEL, dbus_interface=dbus.PROPERTIES_IFACE) assert channel_props['TargetID'] == jid,\ (channel_props['TargetID'], jid) @@ -96,7 +87,7 @@ def test(q, bus, conn, stream): assert event.args[5] == 'hello' messages = text_chan.ListPendingMessages(False, - dbus_interface='org.freedesktop.Telepathy.Channel.Type.Text') + dbus_interface=cs.CHANNEL_TYPE_TEXT) assert messages == \ [(hello_message_id, hello_message_time, foo_handle, 0, 0, 'hello')], messages @@ -126,8 +117,7 @@ def test(q, bus, conn, stream): event = q.expect('dbus-return', method='Close') - channel_props = text_chan.GetAll( - 'org.freedesktop.Telepathy.Channel', + channel_props = text_chan.GetAll(cs.CHANNEL, dbus_interface=dbus.PROPERTIES_IFACE) assert channel_props['TargetID'] == jid,\ (channel_props['TargetID'], jid) @@ -140,7 +130,7 @@ def test(q, bus, conn, stream): # the message is still there messages = text_chan.ListPendingMessages(False, - dbus_interface='org.freedesktop.Telepathy.Channel.Type.Text') + dbus_interface=cs.CHANNEL_TYPE_TEXT) assert messages == \ [(hello_message_id, hello_message_time, foo_handle, 0, 8, 'hello')], messages @@ -148,10 +138,10 @@ def test(q, bus, conn, stream): # acknowledge it text_chan.AcknowledgePendingMessages([hello_message_id], - dbus_interface='org.freedesktop.Telepathy.Channel.Type.Text') + dbus_interface=cs.CHANNEL_TYPE_TEXT) messages = text_chan.ListPendingMessages(False, - dbus_interface='org.freedesktop.Telepathy.Channel.Type.Text') + dbus_interface=cs.CHANNEL_TYPE_TEXT) assert messages == [] # close the channel again diff --git a/tests/twisted/text/test-text.py b/tests/twisted/text/test-text.py index aef4abf..91ebb6a 100644 --- a/tests/twisted/text/test-text.py +++ b/tests/twisted/text/test-text.py @@ -8,7 +8,7 @@ import dbus from twisted.words.xish import domish from hazetest import exec_test -from servicetest import EventPattern, assertEquals +from servicetest import EventPattern, assertEquals, assertContains import constants as cs def test(q, bus, conn, stream): @@ -30,21 +30,16 @@ def test(q, bus, conn, stream): text_chan = bus.get_object(conn.bus_name, event.args[0][0][0]) # Exercise basic Channel Properties from spec 0.17.7 - channel_props = text_chan.GetAll( - 'org.freedesktop.Telepathy.Channel', + channel_props = text_chan.GetAll(cs.CHANNEL, dbus_interface=dbus.PROPERTIES_IFACE) assertEquals(foo_at_bar_dot_com_handle, channel_props.get('TargetHandle')) assert channel_props.get('TargetHandleType') == 1,\ channel_props.get('TargetHandleType') - assert channel_props.get('ChannelType') == \ - 'org.freedesktop.Telepathy.Channel.Type.Text',\ - channel_props.get('ChannelType') - assert 'org.freedesktop.Telepathy.Channel.Interface.ChatState' in \ - channel_props.get('Interfaces', ()), \ - channel_props.get('Interfaces') - assert 'org.freedesktop.Telepathy.Channel.Interface.Messages' in \ - channel_props.get('Interfaces', ()), \ - channel_props.get('Interfaces') + assertEquals(cs.CHANNEL_TYPE_TEXT, channel_props.get('ChannelType')) + assertContains(cs.CHANNEL_IFACE_CHAT_STATE, + channel_props.get('Interfaces', ())) + assertContains(cs.CHANNEL_IFACE_MESSAGES, + channel_props.get('Interfaces', ())) assert channel_props['TargetID'] == jid,\ (channel_props['TargetID'], jid) assert channel_props['Requested'] == False @@ -85,8 +80,7 @@ def test(q, bus, conn, stream): # PendingMessagesRemoved fires. message_id = header['pending-message-id'] - dbus.Interface(text_chan, - u'org.freedesktop.Telepathy.Channel.Type.Text' + dbus.Interface(text_chan, cs.CHANNEL_TYPE_TEXT ).AcknowledgePendingMessages([message_id]) removed = q.expect('dbus-signal', signal='PendingMessagesRemoved') @@ -105,8 +99,7 @@ def test(q, bus, conn, stream): } ] - dbus.Interface(text_chan, - u'org.freedesktop.Telepathy.Channel.Interface.Messages' + dbus.Interface(text_chan, cs.CHANNEL_IFACE_MESSAGES ).SendMessage(greeting, dbus.UInt32(0)) stream_message, sent, message_sent = q.expect_many( @@ -140,8 +133,7 @@ def test(q, bus, conn, stream): # Send a message using Channel.Type.Text API - dbus.Interface(text_chan, - u'org.freedesktop.Telepathy.Channel.Type.Text').Send(0, 'goodbye') + dbus.Interface(text_chan, cs.CHANNEL_TYPE_TEXT).Send(0, 'goodbye') stream_message, sent, message_sent = q.expect_many( EventPattern('stream-message'), -- 1.8.4.rc3