aboutsummaryrefslogtreecommitdiff
path: root/tests/accounts-service-mock.h
blob: d057f07a125d11dce88d6309fa3d20c9b3222ab9 (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
/*
 * Copyright © 2014 Canonical Ltd.
 * Copyright © 2021 Robert Tari
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 3.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Authors:
 *      Ted Gould <ted@canonical.com>
 *      Robert Tari <robert@tari.in>
 */

#include <memory>
#include <libdbustest/dbus-test.h>

class AccountsServiceMock
{
        DbusTestDbusMock * mock = nullptr;
        DbusTestDbusMockObject * soundobj = nullptr;
        DbusTestDbusMockObject * userobj = nullptr;
        DbusTestDbusMockObject * syssoundobj = nullptr;
        DbusTestDbusMockObject * privacyobj = nullptr;

    public:
        AccountsServiceMock () {
            mock = dbus_test_dbus_mock_new("org.freedesktop.Accounts");

            dbus_test_task_set_bus(DBUS_TEST_TASK(mock), DBUS_TEST_SERVICE_BUS_SYSTEM);

            DbusTestDbusMockObject * baseobj = dbus_test_dbus_mock_get_object(mock, "/org/freedesktop/Accounts", "org.freedesktop.Accounts", NULL);

            dbus_test_dbus_mock_object_add_method(mock, baseobj,
                "CacheUser", G_VARIANT_TYPE_STRING, G_VARIANT_TYPE_OBJECT_PATH,
                "ret = dbus.ObjectPath('/user')\n", NULL);
            dbus_test_dbus_mock_object_add_method(mock, baseobj,
                "FindUserById", G_VARIANT_TYPE_INT64, G_VARIANT_TYPE_OBJECT_PATH,
                "ret = dbus.ObjectPath('/user')\n", NULL);
            dbus_test_dbus_mock_object_add_method(mock, baseobj,
                "FindUserByName", G_VARIANT_TYPE_STRING, G_VARIANT_TYPE_OBJECT_PATH,
                "ret = dbus.ObjectPath('/user')\n", NULL);
            dbus_test_dbus_mock_object_add_method(mock, baseobj,
                "ListCachedUsers", NULL, G_VARIANT_TYPE_OBJECT_PATH_ARRAY,
                "ret = [ dbus.ObjectPath('/user') ]\n", NULL);
            dbus_test_dbus_mock_object_add_method(mock, baseobj,
                "UncacheUser", G_VARIANT_TYPE_STRING, NULL,
                "", NULL);

            userobj = dbus_test_dbus_mock_get_object(mock, "/user", "org.freedesktop.Accounts.User", NULL);
            dbus_test_dbus_mock_object_add_property(mock, userobj,
                "UserName", G_VARIANT_TYPE_STRING,
                g_variant_new_string(g_get_user_name()), NULL);
            dbus_test_dbus_mock_object_add_method(mock, baseobj,
                "SetXHasMessages", G_VARIANT_TYPE_BOOLEAN, nullptr,
                "", NULL);

            soundobj = dbus_test_dbus_mock_get_object(mock, "/user", "org.ayatana.indicator.sound.AccountsService", NULL);
            dbus_test_dbus_mock_object_add_property(mock, soundobj,
                "Timestamp", G_VARIANT_TYPE_UINT64,
                g_variant_new_uint64(0), NULL);
            dbus_test_dbus_mock_object_add_property(mock, soundobj,
                "PlayerName", G_VARIANT_TYPE_STRING,
                g_variant_new_string(""), NULL);
            dbus_test_dbus_mock_object_add_property(mock, soundobj,
                "PlayerIcon", G_VARIANT_TYPE_VARIANT,
                g_variant_new_variant(g_variant_new_string("")), NULL);
            dbus_test_dbus_mock_object_add_property(mock, soundobj,
                "Running", G_VARIANT_TYPE_BOOLEAN,
                g_variant_new_boolean(FALSE), NULL);
            dbus_test_dbus_mock_object_add_property(mock, soundobj,
                "State", G_VARIANT_TYPE_STRING,
                g_variant_new_string(""), NULL);
            dbus_test_dbus_mock_object_add_property(mock, soundobj,
                "Title", G_VARIANT_TYPE_STRING,
                g_variant_new_string(""), NULL);
            dbus_test_dbus_mock_object_add_property(mock, soundobj,
                "Artist", G_VARIANT_TYPE_STRING,
                g_variant_new_string(""), NULL);
            dbus_test_dbus_mock_object_add_property(mock, soundobj,
                "Album", G_VARIANT_TYPE_STRING,
                g_variant_new_string(""), NULL);
            dbus_test_dbus_mock_object_add_property(mock, soundobj,
                "ArtUrl", G_VARIANT_TYPE_STRING,
                g_variant_new_string(""), NULL);

            syssoundobj = dbus_test_dbus_mock_get_object(mock, "/user", "com.lomiri.touch.AccountsService.Sound", NULL);
            dbus_test_dbus_mock_object_add_property(mock, syssoundobj,
                "SilentMode", G_VARIANT_TYPE_BOOLEAN,
                g_variant_new_boolean(FALSE), NULL);

            privacyobj = dbus_test_dbus_mock_get_object(mock, "/user", "com.ubuntu.touch.AccountsService.SecurityPrivacy", NULL);
            dbus_test_dbus_mock_object_add_property(mock, privacyobj,
                "MessagesWelcomeScreen", G_VARIANT_TYPE_BOOLEAN,
                g_variant_new_boolean(true), NULL);
            dbus_test_dbus_mock_object_add_property(mock, privacyobj,
                "StatsWelcomeScreen", G_VARIANT_TYPE_BOOLEAN,
                g_variant_new_boolean(true), NULL);
        }

        ~AccountsServiceMock () {
            g_debug("Destroying the Accounts Service Mock");
            g_clear_object(&mock);
        }

        void setSilentMode (bool modeValue) {
            dbus_test_dbus_mock_object_update_property(mock, syssoundobj,
                "SilentMode", g_variant_new_boolean(modeValue ? TRUE : FALSE),
                NULL);
        }

        operator std::shared_ptr<DbusTestTask> () {
            return std::shared_ptr<DbusTestTask>(
                DBUS_TEST_TASK(g_object_ref(mock)),
                [](DbusTestTask * task) { g_clear_object(&task); });
        }

        operator DbusTestTask* () {
            return DBUS_TEST_TASK(mock);
        }

        operator DbusTestDbusMock* () {
            return mock;
        }

        DbusTestDbusMockObject * get_sound () {
            return soundobj;
        }
};