summaryrefslogtreecommitdiff
path: root/tests/twisted/power-save.py
blob: 862c709c9990011a6f74a2bbf2dd0d1a8ab47d86 (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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
"""
Test entering and leaving power saving mode.
"""

import config

import constants as cs

from gabbletest import exec_test, GoogleXmlStream, make_result_iq, \
    send_error_reply, disconnect_conn, make_presence, sync_stream, elem, \
    acknowledge_iq
from servicetest import call_async, Event, assertEquals, EventPattern, \
    assertContains, assertDoesNotContain, sync_dbus
import ns

import dbus
import dbus.service

from twisted.internet import reactor
from twisted.words.xish import domish

def expect_command(q, name):
    event = q.expect('stream-iq', query_name='query', query_ns=ns.GOOGLE_QUEUE)

    # Regression test: when we split MCE out of Gabble and moved Gabble's code
    # to conn-power-saving, this erroneously became a 'get'.
    assertEquals('set', event.iq_type)

    command = event.query.firstChildElement()
    assertEquals(name, command.name)
    return event.stanza

def test_error(q, bus, conn, stream):
    assertContains(cs.CONN_IFACE_POWER_SAVING,
                  conn.Get(cs.CONN, "Interfaces",
                           dbus_interface=cs.PROPERTIES_IFACE))

    assertEquals (False, conn.Get(cs.CONN_IFACE_POWER_SAVING,
                                  "PowerSavingActive",
                                  dbus_interface=cs.PROPERTIES_IFACE))

    call_async(q, conn.PowerSaving, 'SetPowerSaving', True)

    stanza = expect_command(q, 'enable')

    error = domish.Element((None, 'error'))

    error.addElement((ns.STANZA, 'service-unavailable'))

    send_error_reply(stream, stanza, error)

    q.expect('dbus-error', method='SetPowerSaving', name=cs.NOT_AVAILABLE)

    # Power saving state should remain false
    assertEquals (False, conn.Get(cs.CONN_IFACE_POWER_SAVING,
                                  "PowerSavingActive",
                                  dbus_interface=cs.PROPERTIES_IFACE))


def test_local_queueing(q, bus, conn, stream):
    assertContains(cs.CONN_IFACE_POWER_SAVING,
                  conn.Get(cs.CONN, "Interfaces",
                           dbus_interface=cs.PROPERTIES_IFACE))

    assertEquals (False, conn.Get(cs.CONN_IFACE_POWER_SAVING,
                                  "PowerSavingActive",
                                  dbus_interface=cs.PROPERTIES_IFACE))

    event = q.expect('stream-iq', to=None, query_ns='vcard-temp',
            query_name='vCard')
    acknowledge_iq(stream, event.stanza)

    presence_update = [EventPattern('dbus-signal', signal='PresencesChanged')]
    q.forbid_events(presence_update)

    call_async(q, conn.PowerSaving, 'SetPowerSaving', True)

    q.expect_many(EventPattern('dbus-return', method='SetPowerSaving'),
                  EventPattern('dbus-signal', signal='PowerSavingChanged',
                               args=[True]))

    assertEquals (True, conn.Get(cs.CONN_IFACE_POWER_SAVING,
                                  "PowerSavingActive",
                                  dbus_interface=cs.PROPERTIES_IFACE))

    # These presence stanzas should be queued
    stream.send(make_presence('amy@foo.com', show='away',
                              status='At the pub'))
    stream.send(make_presence('bob@foo.com', show='xa',
                              status='Somewhere over the rainbow'))

    # Pep notifications too
    message = elem('message', from_='bob@foo.com')(
        elem((ns.PUBSUB_EVENT), 'event')(
            elem('items', node=ns.NICK)(
                elem('item')(
                    elem(ns.NICK, 'nick')(u'Robert')
                )
            )
        )
    )
    stream.send(message.toXml())

    sync_dbus(bus, q, conn)
    q.unforbid_events(presence_update)

    # Incoming important stanza will flush the queue
    m = domish.Element((None, 'message'))
    m['from'] = 'foo@bar.com/Pidgin'
    m['id'] = '123'
    m['type'] = 'chat'
    m.addElement('body', content='important message')
    stream.send(m)

    # Presence updates should come in the original order ...
    p1 = q.expect('dbus-signal', signal='PresencesChanged')
    p2 = q.expect('dbus-signal', signal='PresencesChanged')

    assertEquals('away', p1.args[0].values()[0][1])
    assertEquals('xa', p2.args[0].values()[0][1])

    # .. followed by the result of PEP notification ..
    event = q.expect('dbus-signal', signal='AliasesChanged')

    # .. and finally the message that flushed the stanza queue
    q.expect('dbus-signal', signal='NewChannel')

    sync_stream(q, stream)

    q.forbid_events(presence_update)

    stream.send(make_presence('carl@foo.com', show='away',
                              status='Home'))

    # Carl's presence update is queued
    sync_dbus(bus, q, conn)
    q.unforbid_events(presence_update)

    # Disable powersaving, flushing the queue
    conn.PowerSaving.SetPowerSaving(False)

    q.expect('dbus-signal', signal='PresencesChanged')


def test(q, bus, conn, stream):
    assertContains(cs.CONN_IFACE_POWER_SAVING,
                  conn.Get(cs.CONN, "Interfaces",
                           dbus_interface=cs.PROPERTIES_IFACE))

    assertEquals (False, conn.Get(cs.CONN_IFACE_POWER_SAVING,
                                  "PowerSavingActive",
                                  dbus_interface=cs.PROPERTIES_IFACE))

    call_async(q, conn.PowerSaving, 'SetPowerSaving', True)

    stanza = expect_command(q, 'enable')

    stream.send(make_result_iq(stream, stanza, False))

    q.expect_many(EventPattern('dbus-return', method='SetPowerSaving'),
                  EventPattern('dbus-signal', signal='PowerSavingChanged',
                               args=[True]))

    assertEquals (True, conn.Get(cs.CONN_IFACE_POWER_SAVING,
                                  "PowerSavingActive",
                                  dbus_interface=cs.PROPERTIES_IFACE))

    pattern = [EventPattern('stream-iq', query_name='query',
                            query_ns=ns.GOOGLE_QUEUE),
               EventPattern('dbus-signal', signal='PowerSavingChanged',
                            args=[True])]

    q.forbid_events(pattern)

    # Enabling power saving again should be a no-op.
    call_async(q, conn.PowerSaving, 'SetPowerSaving', True)

    q.expect('dbus-return', method='SetPowerSaving')

    q.unforbid_events(pattern)

    call_async(q, conn.PowerSaving, 'SetPowerSaving', False)

    stanza = expect_command(q, 'disable')

    stream.send(make_result_iq(stream, stanza, False))

    event, _, _ = q.expect_many(
        EventPattern('stream-iq', query_name='query', query_ns=ns.GOOGLE_QUEUE),
        EventPattern('dbus-return', method='SetPowerSaving'),
        EventPattern('dbus-signal', signal='PowerSavingChanged',
                     args=[False]))

    command = event.query.firstChildElement()
    assertEquals("flush", command.name)

    assertEquals (False, conn.Get(cs.CONN_IFACE_POWER_SAVING,
                                  "PowerSavingActive",
                                  dbus_interface=cs.PROPERTIES_IFACE))


def test_disconnect(q, bus, conn, stream):
    assertContains(cs.CONN_IFACE_POWER_SAVING,
                  conn.Get(cs.CONN, "Interfaces",
                           dbus_interface=cs.PROPERTIES_IFACE))

    assertEquals (False, conn.Get(cs.CONN_IFACE_POWER_SAVING,
                                  "PowerSavingActive",
                                  dbus_interface=cs.PROPERTIES_IFACE))

    call_async(q, conn.PowerSaving, 'SetPowerSaving', True)

    stanza = expect_command(q, 'enable')

    disconnect_conn(q, conn, stream)

if __name__ == '__main__':
    exec_test(test, protocol=GoogleXmlStream)
    exec_test(test_local_queueing)
    exec_test(test_error, protocol=GoogleXmlStream)
    exec_test(test_disconnect, protocol=GoogleXmlStream)