From ae39f7001e5603010afc02de29787ade6d48ef14 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Fri, 22 Mar 2013 16:34:34 -0500 Subject: port indicator-session to GMenu/cmake. Code coverage increased from 0% to 95.4%. --- tests/backend-dbus/mock-accounts.cc | 156 ++++++++++++++++++++++++++++++++++++ 1 file changed, 156 insertions(+) create mode 100644 tests/backend-dbus/mock-accounts.cc (limited to 'tests/backend-dbus/mock-accounts.cc') diff --git a/tests/backend-dbus/mock-accounts.cc b/tests/backend-dbus/mock-accounts.cc new file mode 100644 index 0000000..2a0e7e7 --- /dev/null +++ b/tests/backend-dbus/mock-accounts.cc @@ -0,0 +1,156 @@ +/* + * Copyright 2013 Canonical Ltd. + * + * Authors: + * Charles Kerr + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 3, as published + * by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranties of + * MERCHANTABILITY, SATISFACTORY QUALITY, 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 . + */ + +#include "mock-accounts.h" +#include "mock-user.h" + +namespace +{ + const char * const DBUS_ACCOUNTS_NAME = "org.freedesktop.Accounts"; + + const char * const DBUS_ACCOUNTS_PATH = "/org/freedesktop/Accounts"; +} + +/*** +**** +***/ + +void +MockAccounts :: add_user (MockUser * user) +{ + g_assert (my_users.count(user) == 0); + + my_users.insert (user); + my_uid_to_user[user->uid()] = user; + my_path_to_user[user->path()] = user; + my_username_to_user[user->username()] = user; + + accounts_emit_user_added (my_skeleton, user->path()); +} + +void +MockAccounts :: remove_user (MockUser * user) +{ + g_assert (my_users.count(user) == 1); + + my_users.erase (user); + my_uid_to_user.erase (user->uid()); + my_path_to_user.erase (user->path()); + my_username_to_user.erase (user->username()); + + accounts_emit_user_deleted (my_skeleton, user->path()); +} + +MockUser * +MockAccounts :: find_by_uid (guint64 uid) +{ + const uid_to_user_t::iterator it (my_uid_to_user.find(uid)); + + if (it != my_uid_to_user.end()) + return it->second; + + g_warn_if_reached (); + return 0; +} + +MockUser * +MockAccounts :: find_by_path (const char * path) +{ + const path_to_user_t::iterator it (my_path_to_user.find(path)); + + if (it != my_path_to_user.end()) + return it->second; + + g_warn_if_reached (); + return 0; +} + +MockUser * +MockAccounts :: find_by_username (const char * username) +{ + const username_to_user_t::iterator it (my_username_to_user.find(username)); + + if (it != my_path_to_user.end()) + return it->second; + + g_warn_if_reached (); + return 0; +} + +/*** +**** +***/ + +gboolean +MockAccounts :: on_find_user_by_id_static (Accounts * a, + GDBusMethodInvocation * invocation, + guint64 uid, + gpointer gself) +{ + MockUser * user = static_cast(gself)->find_by_uid (uid); + accounts_complete_find_user_by_id (a, invocation, user ? user->path() : ""); + return true; +} + +gboolean +MockAccounts :: on_list_cached_users_static (Accounts * a, + GDBusMethodInvocation * invocation, + gpointer gself) +{ + int i; + const char ** paths; + const users_t& users = static_cast(gself)->my_users; + + i = 0; + paths = g_new0 (const char*, users.size() + 1); + for (users_t::iterator it(users.begin()), + end(users.end()); it!=end; ++it) + paths[i++] = (*it)->path(); + accounts_complete_list_cached_users (a, invocation, paths); + g_free (paths); + + return true; +} + +/*** +**** +***/ + +MockAccounts :: MockAccounts (GMainLoop * loop, + GDBusConnection * bus_connection): + MockObject (loop, bus_connection, DBUS_ACCOUNTS_NAME, DBUS_ACCOUNTS_PATH), + my_skeleton (accounts_skeleton_new ()) +{ + g_signal_connect (my_skeleton, "handle-list-cached-users", + G_CALLBACK(on_list_cached_users_static), this); + g_signal_connect (my_skeleton, "handle-find-user-by-id", + G_CALLBACK(on_find_user_by_id_static), this); + + set_skeleton (G_DBUS_INTERFACE_SKELETON(my_skeleton)); +} + +MockAccounts :: ~MockAccounts () +{ + for (users_t::iterator it(my_users.begin()), + end(my_users.end()); it!=end; ++it) + delete *it; + + g_signal_handlers_disconnect_by_data (my_skeleton, this); + g_clear_object (&my_skeleton); +} -- cgit v1.2.3