summaryrefslogtreecommitdiff
path: root/tests/twisted/account-manager/device-idle.py
blob: 196b6a0413ecd5affe8ef9544c9e4049f83c6a42 (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
# Copyright (C) 2009 Nokia Corporation
# Copyright (C) 2009 Collabora Ltd.
# Copyright (C) 2013 Intel Corporation
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
# 02110-1301 USA

import config

import dbus.service

from servicetest import EventPattern, tp_name_prefix, tp_path_prefix, \
        call_async, unwrap, sync_dbus
from mctest import exec_test, create_fakecm_account, SimulatedConnection, \
    enable_fakecm_account
import constants as cs

# Fake SessionManager constants, cloned from mcd-slacker.c
STATUS_AVAILABLE = 0
STATUS_INVISIBLE = 1
STATUS_BUSY = 2
STATUS_IDLE = 3

SERVICE_NAME = "org.gnome.SessionManager"
SERVICE_OBJECT_PATH = "/org/gnome/SessionManager/Presence"
SERVICE_INTERFACE = "org.gnome.SessionManager.Presence"
SERVICE_PROP_NAME = "status"
SERVICE_SIG_NAME = "StatusChanged"

class SimulatedSession(object):
    def __init__(self, q, bus, status=STATUS_AVAILABLE):
        self.bus = bus
        self.q = q
        self.status = status
        self.object_path = SERVICE_OBJECT_PATH
        self._name_ref = dbus.service.BusName(SERVICE_NAME, bus)

        self.q.add_dbus_method_impl(self.GetAll,
                path=self.object_path,
                interface=cs.PROPERTIES_IFACE, method='GetAll')
        self.q.add_dbus_method_impl(self.Get,
                path=self.object_path,
                interface=cs.PROPERTIES_IFACE, method='Get')

    def GetAll(self, e):
        ret = dbus.Dictionary({}, signature='sv')
        ret[SERVICE_PROP_NAME] = dbus.UInt32(self.status)
        self.q.dbus_return(e.message, ret, signature='a{sv}')

    def Get(self, e):
        if e.args[0] == SERVICE_INTERFACE and e.args[1] == SERVICE_PROP_NAME:
            self.q.dbus_return(e.message, dbus.UInt32(self.status), signature='v')
            return

        self.q.dbus_raise(e.message, cs.NOT_IMPLEMENTED, \
            "Unknown property %s on interface %s" % (e.args[0], e.args[1]))

    def StatusChanged(self, new_value):
        self.status = new_value
        self.q.dbus_emit(self.object_path, SERVICE_INTERFACE, SERVICE_SIG_NAME,
                         dbus.UInt32(self.status), signature="u")

    def release_name(self):
        del self._name_ref

def _create_and_enable(q, bus, mc, account_name, power_saving_supported,
                       expect_after_connect=[]):
    extra_interfaces = []
    if power_saving_supported:
        extra_interfaces = [cs.CONN_IFACE_POWER_SAVING]
    params = dbus.Dictionary({"account": account_name, "password": "secrecy"},
                              signature='sv')
    cm_name_ref, account = create_fakecm_account(q, bus, mc, params)
    conn = enable_fakecm_account(q, bus, mc, account, params, has_requests=False,
                                 extra_interfaces=extra_interfaces,
                                 expect_after_connect=expect_after_connect)

    if isinstance(conn, tuple):
        conn = conn[0]

    return account, conn

def _disable_account(q, bus, mc, account, conn):
    account.Set(cs.ACCOUNT, 'Enabled', False,
                dbus_interface=cs.PROPERTIES_IFACE)

    q.expect('dbus-method-call', method='Disconnect',
            path=conn.object_path, handled=True)

def test(q, bus, mc):
    service = SimulatedSession(q, bus, STATUS_IDLE)

    account1, conn1 = _create_and_enable(
        q, bus, mc, "first@example.com", True,
        [EventPattern('dbus-method-call', method='SetPowerSaving', args=[True])])
    account2, conn2 = _create_and_enable(q, bus, mc, "second@example.com", False)

    # Second account does not support PowerSaving interface, don't call SetPowerSaving
    forbid_no_iface =[EventPattern('dbus-method-call', method='SetPowerSaving',
                                   path=conn2.object_path)]

    q.forbid_events(forbid_no_iface)

    for status in [STATUS_AVAILABLE, STATUS_IDLE, STATUS_BUSY]:
        service.StatusChanged(status)
        q.expect('dbus-method-call', method='SetPowerSaving',
                 args=[status == STATUS_IDLE], interface=cs.CONN_IFACE_POWER_SAVING,
                 path=conn1.object_path)

    _disable_account(q, bus, mc, account1, conn1)
    _disable_account(q, bus, mc, account2, conn2)

    q.unforbid_events(forbid_no_iface)

    # Make sure we don't call SetPowerSaving on a disconnected connection.

    forbid_when_disconnected =[EventPattern('dbus-method-call', method='SetPowerSaving')]

    q.forbid_events(forbid_when_disconnected)

    service.StatusChanged(STATUS_IDLE)

    sync_dbus(bus, q, account1)

    q.unforbid_events(forbid_when_disconnected)

    service.release_name()

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