summaryrefslogtreecommitdiff
path: root/test/twisted/test-connect.py
blob: dbb60739362e784f43ad6834d8b58288e2cb193f (plain)
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
import dbus

from servicetest import EventPattern, tp_name_prefix, tp_path_prefix
from fakecm import start_fake_connection_manager
from fakeclient import start_fake_client
from mctest import exec_test

FakeCM_bus_name = "org.freedesktop.Telepathy.ConnectionManager.fakecm"
ConnectionManager_object_path = \
    "/org/freedesktop/Telepathy/ConnectionManager/fakecm"

FakeClient_bus_name = "org.freedesktop.Telepathy.Client.fakeclient"
Client_object_path = \
    "/org/freedesktop/Telepathy/Client/fakeclient"


def test(q, bus, mc):
    start_fake_connection_manager(q, bus, FakeCM_bus_name,
            ConnectionManager_object_path)
    
    http_fixed_properties = dbus.Dictionary({
        'org.freedesktop.Telepathy.Channel.TargetHandleType': 1L,
        'org.freedesktop.Telepathy.Channel.ChannelType':
            'org.freedesktop.Telepathy.Channel.Type.StreamTube.DRAFT',
        'org.freedesktop.Telepathy.Channel.Type.StreamTube.DRAFT.Service':
            'http'
        }, signature='sv')
    caps = dbus.Array([http_fixed_properties], signature='a{sv}')

    fake_client = start_fake_client(q, bus, FakeClient_bus_name,
            Client_object_path, caps)
    
    # Get the AccountManager interface
    account_manager = bus.get_object(
        tp_name_prefix + '.AccountManager',
        tp_path_prefix + '/AccountManager')
    account_manager_iface = dbus.Interface(account_manager,
            'org.freedesktop.Telepathy.AccountManager')

    # Create an account
    params = dbus.Dictionary({"nickname": "fakenick"}, signature='sv')
    account_path = account_manager_iface.CreateAccount(
            'fakecm', # Connection_Manager
            'fakeprotocol', # Protocol
            'fakeaccount', #Display_Name
            params, # Parameters
            )
    assert account_path is not None

    # Get the Account interface
    account = bus.get_object(
        tp_name_prefix + '.AccountManager',
        account_path)
    account_iface = dbus.Interface(account,
            'org.freedesktop.Telepathy.Account')

    # Enable the account
    account.Set('org.freedesktop.Telepathy.Account',
            'Enabled', True,
            dbus_interface='org.freedesktop.DBus.Properties')
    q.expect('dbus-signal',
            path=account_path,
            signal='AccountPropertyChanged',
            interface='org.freedesktop.Telepathy.Account',
            args=[dbus.Dictionary({"Enabled": True}, signature='sv')])

    # Check the requested presence is offline
    properties = account.GetAll(
            'org.freedesktop.Telepathy.Account',
            dbus_interface='org.freedesktop.DBus.Properties')
    assert properties is not None
    # the requested presence is defined by Connection_Presence_Type:
    #  Connection_Presence_Type_Unset = 0
    #  Connection_Presence_Type_Offline = 1
    #  Connection_Presence_Type_Available = 2
    assert properties.get('RequestedPresence') == \
        dbus.Struct((dbus.UInt32(0L), dbus.String(u''), dbus.String(u''))), \
        properties.get('RequestedPresence')  # FIXME: we should expect 1

    # Go online
    requested_presence = dbus.Struct((dbus.UInt32(2L), dbus.String(u'brb'),
                dbus.String(u'Be back soon!')))
    account.Set('org.freedesktop.Telepathy.Account',
            'RequestedPresence', requested_presence,
            dbus_interface='org.freedesktop.DBus.Properties')

    e = q.expect('dbus-method-call', name='RequestConnection',
            protocol='fakeprotocol')
    conn_object_path = e.conn.object_path
    fake_conn = e.conn
    assert e.parameters == params

    e = q.expect('dbus-method-call', name='Connect',
            path=conn_object_path)

    e = q.expect('dbus-method-call', name='SetSelfCapabilities',
            path=conn_object_path)
    assert e.caps == caps, e.caps

    # Check the requested presence is online
    properties = account.GetAll(
            'org.freedesktop.Telepathy.Account',
            dbus_interface='org.freedesktop.DBus.Properties')
    assert properties is not None
    assert properties.get('RequestedPresence') == requested_presence, \
        properties.get('RequestedPresence')

    new_channel = http_fixed_properties
    handle = fake_conn.get_handle("buddy")
    new_channel['org.freedesktop.Telepathy.Channel.TargetID'] = "buddy"
    new_channel['org.freedesktop.Telepathy.Channel.TargetHandle'] = handle

    fake_conn.new_incoming_channel('/foobar', new_channel)

    e = q.expect('dbus-method-call', name='HandleChannels', obj=fake_client,
            connection=conn_object_path)

if __name__ == '__main__':
    exec_test(test, {})