From dbb383c7c604ce3f9dd7674d8a2e059767b37e12 Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Mon, 23 Sep 2013 13:56:23 +0100 Subject: [PATCH 05/16] Don't test NewChannel Also use assertEquals(), etc., on the lines I'm touching anyway. Just remove initiate.py: the whole point of that test is to exercise an obsolete API. The modern form of the initiation part is tested by initiate-requestotron.py and the text bits are tested by test-text.py. --- tests/twisted/Makefile.am | 1 - tests/twisted/sasl/saslutil.py | 13 ++-- tests/twisted/text/destroy.py | 22 +++---- tests/twisted/text/ensure.py | 57 +++++------------ tests/twisted/text/initiate-requestotron.py | 28 ++++----- tests/twisted/text/initiate.py | 98 ----------------------------- tests/twisted/text/respawn.py | 41 ++++++------ tests/twisted/text/test-text-delayed.py | 13 ++-- tests/twisted/text/test-text-no-body.py | 11 ++-- tests/twisted/text/test-text.py | 27 ++++---- 10 files changed, 82 insertions(+), 229 deletions(-) delete mode 100644 tests/twisted/text/initiate.py diff --git a/tests/twisted/Makefile.am b/tests/twisted/Makefile.am index da5d473..f932229 100644 --- a/tests/twisted/Makefile.am +++ b/tests/twisted/Makefile.am @@ -18,7 +18,6 @@ TWISTED_TESTS = \ text/destroy.py \ text/ensure.py \ text/initiate-requestotron.py \ - text/initiate.py \ text/respawn.py \ text/test-text-delayed.py \ text/test-text-no-body.py \ diff --git a/tests/twisted/sasl/saslutil.py b/tests/twisted/sasl/saslutil.py index c09ea9b..83afe2b 100644 --- a/tests/twisted/sasl/saslutil.py +++ b/tests/twisted/sasl/saslutil.py @@ -89,22 +89,17 @@ def connect_and_get_sasl_channel(q, bus, conn): return expect_sasl_channel(q, bus, conn) def expect_sasl_channel(q, bus, conn): - old_signal, new_signal = q.expect_many( - EventPattern('dbus-signal', signal='NewChannel', - predicate=lambda e: - e.args[1] == cs.CHANNEL_TYPE_SERVER_AUTHENTICATION), + signal, = q.expect_many( EventPattern('dbus-signal', signal='NewChannels', predicate=lambda e: e.args[0][0][1].get(cs.CHANNEL_TYPE) == cs.CHANNEL_TYPE_SERVER_AUTHENTICATION), ) - - path, type, handle_type, handle, suppress_handler = old_signal.args + path = signal.args[0][0][0] chan = SaslChannelWrapper(bus.get_object(conn.bus_name, path)) - assertLength(1, new_signal.args[0]) - assertEquals(path, new_signal.args[0][0][0]) - props = new_signal.args[0][0][1] + assertLength(1, signal.args[0]) + props = signal.args[0][0][1] assertEquals(cs.CHANNEL_IFACE_SASL_AUTH, props.get(cs.AUTH_METHOD)) return chan, props diff --git a/tests/twisted/text/destroy.py b/tests/twisted/text/destroy.py index aac3f94..4c495bb 100644 --- a/tests/twisted/text/destroy.py +++ b/tests/twisted/text/destroy.py @@ -8,7 +8,7 @@ import dbus from twisted.words.xish import domish from hazetest import exec_test -from servicetest import call_async, EventPattern, assertEquals +from servicetest import call_async, EventPattern, assertEquals, assertLength import constants as cs def test(q, bus, conn, stream): @@ -20,9 +20,8 @@ def test(q, bus, conn, stream): call_async(q, conn, 'RequestChannel', 'org.freedesktop.Telepathy.Channel.Type.Text', 1, foo_handle, True) - ret, old_sig, new_sig = q.expect_many( + ret, sig = q.expect_many( EventPattern('dbus-return', method='RequestChannel'), - EventPattern('dbus-signal', signal='NewChannel'), EventPattern('dbus-signal', signal='NewChannels'), ) @@ -34,18 +33,11 @@ def test(q, bus, conn, stream): destroyable_iface = dbus.Interface(text_chan, 'org.freedesktop.Telepathy.Channel.Interface.Destroyable') - assert old_sig.args[0] == ret.value[0] - assert old_sig.args[1] == u'org.freedesktop.Telepathy.Channel.Type.Text' - # check that handle type == contact handle - assert old_sig.args[2] == 1 - assert old_sig.args[3] == foo_handle - assert old_sig.args[4] == True # suppress handler - - assert len(new_sig.args) == 1 - assert len(new_sig.args[0]) == 1 # one channel - assert len(new_sig.args[0][0]) == 2 # two struct members - assert new_sig.args[0][0][0] == ret.value[0] - emitted_props = new_sig.args[0][0][1] + 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.' diff --git a/tests/twisted/text/ensure.py b/tests/twisted/text/ensure.py index e4b7538..0c4e182 100644 --- a/tests/twisted/text/ensure.py +++ b/tests/twisted/text/ensure.py @@ -7,7 +7,8 @@ import dbus from twisted.words.xish import domish from hazetest import exec_test -from servicetest import call_async, EventPattern, unwrap +from servicetest import (call_async, EventPattern, unwrap, assertEquals, + assertLength, assertContains) import pprint @@ -54,9 +55,8 @@ def test_ensure_ensure(q, conn, self_handle, jid, handle): # Check that Ensuring a channel that doesn't exist succeeds call_async(q, conn.Requests, 'EnsureChannel', request_props (handle)) - ret, old_sig, new_sig = q.expect_many( + ret, sig = q.expect_many( EventPattern('dbus-return', method='EnsureChannel'), - EventPattern('dbus-signal', signal='NewChannel'), EventPattern('dbus-signal', signal='NewChannels'), ) @@ -69,29 +69,17 @@ def test_ensure_ensure(q, conn, self_handle, jid, handle): check_props(emitted_props, self_handle, handle, jid) - assert len(old_sig.args) == 5 - old_path, old_ct, old_ht, old_h, old_sh = old_sig.args - - assert old_path == path - assert old_ct == u'org.freedesktop.Telepathy.Channel.Type.Text' - # check that handle type == contact handle - assert old_ht == 1 - assert old_h == handle - assert old_sh == True # suppress handler - - assert len(new_sig.args) == 1 - assert len(new_sig.args[0]) == 1 # one channel - assert len(new_sig.args[0][0]) == 2 # two struct members - assert new_sig.args[0][0][0] == path - assert new_sig.args[0][0][1] == emitted_props + assertLength(1, sig.args) + assertLength(1, sig.args[0]) # one channel + assertLength(2, sig.args[0][0]) # two struct members + assertEquals(path, sig.args[0][0][0]) + assertEquals(emitted_props, sig.args[0][0][1]) properties = conn.GetAll( 'org.freedesktop.Telepathy.Connection.Interface.Requests', dbus_interface=dbus.PROPERTIES_IFACE) - assert new_sig.args[0][0] in properties['Channels'], \ - (new_sig.args[0][0], properties['Channels']) - + assertContains(sig.args[0][0], properties['Channels']) # Now try Ensuring a channel which already exists call_async(q, conn.Requests, 'EnsureChannel', request_props (handle)) @@ -115,9 +103,8 @@ def test_request_ensure(q, conn, self_handle, jid, handle): call_async(q, conn.Requests, 'CreateChannel', request_props (handle)) - ret, old_sig, new_sig = q.expect_many( + ret, sig = q.expect_many( EventPattern('dbus-return', method='CreateChannel'), - EventPattern('dbus-signal', signal='NewChannel'), EventPattern('dbus-signal', signal='NewChannels'), ) @@ -126,29 +113,17 @@ def test_request_ensure(q, conn, self_handle, jid, handle): check_props(emitted_props, self_handle, handle, jid) - assert len(old_sig.args) == 5 - old_path, old_ct, old_ht, old_h, old_sh = old_sig.args - - assert old_path == path - assert old_ct == u'org.freedesktop.Telepathy.Channel.Type.Text' - # check that handle type == contact handle - assert old_ht == 1 - assert old_h == handle - assert old_sh == True # suppress handler - - assert len(new_sig.args) == 1 - assert len(new_sig.args[0]) == 1 # one channel - assert len(new_sig.args[0][0]) == 2 # two struct members - assert new_sig.args[0][0][0] == path - assert new_sig.args[0][0][1] == emitted_props + assertLength(1, sig.args) + assertLength(1, sig.args[0]) # one channel + assertLength(2, sig.args[0][0]) # two struct members + assertEquals(path, sig.args[0][0][0]) + assertEquals(emitted_props, sig.args[0][0][1]) properties = conn.GetAll( 'org.freedesktop.Telepathy.Connection.Interface.Requests', dbus_interface=dbus.PROPERTIES_IFACE) - assert new_sig.args[0][0] in properties['Channels'], \ - (new_sig.args[0][0], properties['Channels']) - + assertContains(sig.args[0][0], properties['Channels']) # Now try Ensuring that same channel. call_async(q, conn.Requests, 'EnsureChannel', request_props (handle)) diff --git a/tests/twisted/text/initiate-requestotron.py b/tests/twisted/text/initiate-requestotron.py index c543b69..41b9407 100644 --- a/tests/twisted/text/initiate-requestotron.py +++ b/tests/twisted/text/initiate-requestotron.py @@ -7,7 +7,10 @@ import dbus from twisted.words.xish import domish from hazetest import exec_test -from servicetest import call_async, EventPattern +from servicetest import (call_async, EventPattern, unwrap, assertEquals, + assertLength, assertContains) + +import pprint import constants as cs @@ -43,9 +46,8 @@ def test(q, bus, conn, stream): 'org.freedesktop.Telepathy.Channel.TargetHandle': foo_handle, }) - ret, old_sig, new_sig = q.expect_many( + ret, sig = q.expect_many( EventPattern('dbus-return', method='CreateChannel'), - EventPattern('dbus-signal', signal='NewChannel'), EventPattern('dbus-signal', signal='NewChannels'), ) @@ -65,25 +67,17 @@ def test(q, bus, conn, stream): assert emitted_props['org.freedesktop.Telepathy.Channel.' 'InitiatorID'] == 'test@localhost' - assert old_sig.args[0] == ret.value[0] - assert old_sig.args[1] == u'org.freedesktop.Telepathy.Channel.Type.Text' - # check that handle type == contact handle - assert old_sig.args[2] == 1 - assert old_sig.args[3] == foo_handle - assert old_sig.args[4] == True # suppress handler - - assert len(new_sig.args) == 1 - assert len(new_sig.args[0]) == 1 # one channel - assert len(new_sig.args[0][0]) == 2 # two struct members - assert new_sig.args[0][0][0] == ret.value[0] - assert new_sig.args[0][0][1] == ret.value[1] + 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]) + assertEquals(ret.value[1], sig.args[0][0][1]) properties = conn.GetAll( 'org.freedesktop.Telepathy.Connection.Interface.Requests', dbus_interface=dbus.PROPERTIES_IFACE) - assert new_sig.args[0][0] in properties['Channels'], \ - (new_sig.args[0][0], properties['Channels']) + assertContains(sig.args[0][0], properties['Channels']) conn.Disconnect() q.expect('dbus-signal', signal='StatusChanged', args=[2, 1]) diff --git a/tests/twisted/text/initiate.py b/tests/twisted/text/initiate.py deleted file mode 100644 index 70fcc92..0000000 --- a/tests/twisted/text/initiate.py +++ /dev/null @@ -1,98 +0,0 @@ -""" -Test text channel initiated by me. -""" - -import dbus - -from twisted.words.xish import domish - -from hazetest import exec_test -from servicetest import call_async, EventPattern -import constants as cs - -def test(q, bus, conn, stream): - self_handle = conn.Properties.Get(cs.CONN, "SelfHandle") - - 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) - - ret, sig = q.expect_many( - EventPattern('dbus-return', method='RequestChannel'), - EventPattern('dbus-signal', signal='NewChannel'), - ) - - text_chan = bus.get_object(conn.bus_name, ret.value[0]) - - assert sig.args[0] == ret.value[0], \ - (sig.args[0], ret.value[0]) - assert sig.args[1] == u'org.freedesktop.Telepathy.Channel.Type.Text',\ - sig.args[1] - # check that handle type == contact handle - assert sig.args[2] == 1, sig.args[1] - assert sig.args[3] == foo_handle, (sig.args[3], foo_handle) - assert sig.args[4] == True # suppress handler - - # Exercise basic Channel Properties from spec 0.17.7 - channel_props = text_chan.GetAll( - 'org.freedesktop.Telepathy.Channel', - dbus_interface=dbus.PROPERTIES_IFACE) - assert channel_props.get('TargetHandle') == foo_handle,\ - (channel_props.get('TargetHandle'), foo_handle) - 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 channel_props['TargetID'] == jid,\ - (channel_props['TargetID'], jid) - assert channel_props['Requested'] == True - assert channel_props['InitiatorHandle'] == self_handle,\ - (channel_props['InitiatorHandle'], self_handle) - assert channel_props['InitiatorID'] == 'test@localhost',\ - channel_props['InitiatorID'] - - dbus.Interface(text_chan, - u'org.freedesktop.Telepathy.Channel.Type.Text').Send(0, 'hey') - - event = q.expect('stream-message') - - elem = event.stanza - assert elem.name == 'message' - assert elem['type'] == 'chat' - - found = False - for e in elem.elements(): - if e.name == 'body': - found = True - e.children[0] == u'hey' - break - assert found, elem.toXml() - - # hello - m = domish.Element((None, 'message')) - m['from'] = 'foo@bar.com/Pidgin' - m['type'] = 'chat' - m.addElement('body', content='hello') - stream.send(m) - - event = q.expect('dbus-signal', signal='Received') - - # message type: normal - assert event.args[3] == 0 - # flags: none - assert event.args[4] == 0 - # body - assert event.args[5] == 'hello' - - conn.Disconnect() - q.expect('dbus-signal', signal='StatusChanged', args=[2, 1]) - -if __name__ == '__main__': - exec_test(test) - diff --git a/tests/twisted/text/respawn.py b/tests/twisted/text/respawn.py index 6cc0266..5510a92 100644 --- a/tests/twisted/text/respawn.py +++ b/tests/twisted/text/respawn.py @@ -7,7 +7,7 @@ import dbus from twisted.words.xish import domish from hazetest import exec_test -from servicetest import call_async, EventPattern, assertEquals +from servicetest import call_async, EventPattern, assertEquals, assertLength import constants as cs def test(q, bus, conn, stream): @@ -19,9 +19,8 @@ def test(q, bus, conn, stream): call_async(q, conn, 'RequestChannel', 'org.freedesktop.Telepathy.Channel.Type.Text', 1, foo_handle, True) - ret, old_sig, new_sig = q.expect_many( + ret, sig = q.expect_many( EventPattern('dbus-return', method='RequestChannel'), - EventPattern('dbus-signal', signal='NewChannel'), EventPattern('dbus-signal', signal='NewChannels'), ) @@ -31,18 +30,11 @@ def test(q, bus, conn, stream): text_iface = dbus.Interface(text_chan, 'org.freedesktop.Telepathy.Channel.Type.Text') - assert old_sig.args[0] == ret.value[0] - assert old_sig.args[1] == u'org.freedesktop.Telepathy.Channel.Type.Text' - # check that handle type == contact handle - assert old_sig.args[2] == 1 - assert old_sig.args[3] == foo_handle - assert old_sig.args[4] == True # suppress handler - - assert len(new_sig.args) == 1 - assert len(new_sig.args[0]) == 1 # one channel - assert len(new_sig.args[0][0]) == 2 # two struct members - assert new_sig.args[0][0][0] == ret.value[0] - emitted_props = new_sig.args[0][0][1] + 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.' @@ -120,16 +112,19 @@ def test(q, bus, conn, stream): assertEquals(text_chan.object_path, old.path) assertEquals(text_chan.object_path, new.args[0]) - event = q.expect('dbus-signal', signal='NewChannel') - assert event.args[0] == text_chan.object_path - assert event.args[1] == u'org.freedesktop.Telepathy.Channel.Type.Text' - assert event.args[2] == 1 # CONTACT - assert event.args[3] == foo_handle - assert event.args[4] == False # suppress handler + # it now behaves as if the message had initiated it + new_props = {} + for k in emitted_props: + new_props[k] = emitted_props[k] + new_props[cs.INITIATOR_HANDLE] = foo_handle + new_props[cs.INITIATOR_ID] = 'foo@bar.com' + new_props[cs.REQUESTED] = False - event = q.expect('dbus-return', method='Close') + event = q.expect('dbus-signal', signal='NewChannels') + assertEquals(text_chan.object_path, event.args[0][0][0]) + assertEquals(new_props, event.args[0][0][1]) - # it now behaves as if the message had initiated it + event = q.expect('dbus-return', method='Close') channel_props = text_chan.GetAll( 'org.freedesktop.Telepathy.Channel', diff --git a/tests/twisted/text/test-text-delayed.py b/tests/twisted/text/test-text-delayed.py index 90f9f98..c7e5740 100644 --- a/tests/twisted/text/test-text-delayed.py +++ b/tests/twisted/text/test-text-delayed.py @@ -8,7 +8,8 @@ import datetime from twisted.words.xish import domish from hazetest import exec_test -from servicetest import EventPattern +from servicetest import EventPattern, assertEquals +import constants as cs def test(q, bus, conn, stream): m = domish.Element((None, 'message')) @@ -22,12 +23,10 @@ def test(q, bus, conn, stream): 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='NewChannels') + assertEquals(cs.CHANNEL_TYPE_TEXT, event.args[0][0][1][cs.CHANNEL_TYPE]) + assertEquals(cs.HT_CONTACT, event.args[0][0][1][cs.TARGET_HANDLE_TYPE]) + assertEquals('foo@bar.com', event.args[0][0][1][cs.TARGET_ID]) received, message_received = q.expect_many( EventPattern('dbus-signal', signal='Received'), diff --git a/tests/twisted/text/test-text-no-body.py b/tests/twisted/text/test-text-no-body.py index a3c8ad5..c8aacd7 100644 --- a/tests/twisted/text/test-text-no-body.py +++ b/tests/twisted/text/test-text-no-body.py @@ -7,6 +7,8 @@ new text channel. from twisted.words.xish import domish from hazetest import exec_test +from servicetest import assertEquals +import constants as cs import ns @@ -27,10 +29,11 @@ def test(q, bus, conn, stream): stream.send(m) # first message should be from Bob, not Alice - event = q.expect('dbus-signal', signal='NewChannel') - assert event.args[1] == u'org.freedesktop.Telepathy.Channel.Type.Text' - jid = conn.InspectHandles(1, [event.args[3]])[0] - assert jid == 'bob@foo.com' + event = q.expect('dbus-signal', signal='NewChannels') + assertEquals(cs.CHANNEL_TYPE_TEXT, event.args[0][0][1][cs.CHANNEL_TYPE]) + assertEquals(cs.HT_CONTACT, event.args[0][0][1][cs.TARGET_HANDLE_TYPE]) + assertEquals('bob@foo.com', event.args[0][0][1][cs.TARGET_ID]) + conn.Disconnect() q.expect('dbus-signal', signal='StatusChanged', args=[2, 1]) diff --git a/tests/twisted/text/test-text.py b/tests/twisted/text/test-text.py index 728491f..aef4abf 100644 --- a/tests/twisted/text/test-text.py +++ b/tests/twisted/text/test-text.py @@ -8,9 +8,12 @@ import dbus from twisted.words.xish import domish from hazetest import exec_test -from servicetest import EventPattern +from servicetest import EventPattern, assertEquals +import constants as cs def test(q, bus, conn, stream): + jid = 'foo@bar.com' + # hello m = domish.Element((None, 'message')) m['from'] = 'foo@bar.com/Pidgin' @@ -18,22 +21,19 @@ def test(q, bus, conn, stream): m.addElement('body', content='hello') stream.send(m) - event = q.expect('dbus-signal', signal='NewChannel') - text_chan = bus.get_object(conn.bus_name, event.args[0]) - assert event.args[1] == u'org.freedesktop.Telepathy.Channel.Type.Text' - # check that handle type == contact handle - assert event.args[2] == 1 - foo_at_bar_dot_com_handle = event.args[3] - jid = conn.InspectHandles(1, [foo_at_bar_dot_com_handle])[0] - assert jid == 'foo@bar.com' - assert event.args[4] == False # suppress handler + event = q.expect('dbus-signal', signal='NewChannels') + assertEquals(cs.CHANNEL_TYPE_TEXT, event.args[0][0][1][cs.CHANNEL_TYPE]) + assertEquals(cs.HT_CONTACT, event.args[0][0][1][cs.TARGET_HANDLE_TYPE]) + assertEquals(jid, event.args[0][0][1][cs.TARGET_ID]) + foo_at_bar_dot_com_handle = event.args[0][0][1][cs.TARGET_HANDLE] + + 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', dbus_interface=dbus.PROPERTIES_IFACE) - assert channel_props.get('TargetHandle') == event.args[3],\ - (channel_props.get('TargetHandle'), event.args[3]) + 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') == \ @@ -48,8 +48,7 @@ def test(q, bus, conn, stream): assert channel_props['TargetID'] == jid,\ (channel_props['TargetID'], jid) assert channel_props['Requested'] == False - assert channel_props['InitiatorHandle'] == event.args[3],\ - (channel_props['InitiatorHandle'], event.args[3]) + assertEquals(foo_at_bar_dot_com_handle, channel_props['InitiatorHandle']) assert channel_props['InitiatorID'] == jid,\ (channel_props['InitiatorID'], jid) -- 1.8.4.rc3