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/CMakeLists.txt | 62 ++++ tests/backend-dbus/gtest-mock-dbus-fixture.h | 130 +++++++ tests/backend-dbus/mock-accounts.cc | 156 ++++++++ tests/backend-dbus/mock-accounts.h | 74 ++++ tests/backend-dbus/mock-consolekit-manager.cc | 156 ++++++++ tests/backend-dbus/mock-consolekit-manager.h | 77 ++++ tests/backend-dbus/mock-consolekit-seat.cc | 220 ++++++++++++ tests/backend-dbus/mock-consolekit-seat.h | 75 ++++ tests/backend-dbus/mock-consolekit-session.cc | 113 ++++++ tests/backend-dbus/mock-consolekit-session.h | 65 ++++ tests/backend-dbus/mock-display-manager-seat.cc | 140 ++++++++ tests/backend-dbus/mock-display-manager-seat.h | 72 ++++ tests/backend-dbus/mock-end-session-dialog.cc | 89 +++++ tests/backend-dbus/mock-end-session-dialog.h | 67 ++++ tests/backend-dbus/mock-object.cc | 122 +++++++ tests/backend-dbus/mock-object.h | 62 ++++ tests/backend-dbus/mock-screen-saver.cc | 71 ++++ tests/backend-dbus/mock-screen-saver.h | 53 +++ tests/backend-dbus/mock-session-manager.cc | 66 ++++ tests/backend-dbus/mock-session-manager.h | 50 +++ tests/backend-dbus/mock-upower.cc | 103 ++++++ tests/backend-dbus/mock-upower.h | 72 ++++ tests/backend-dbus/mock-user.cc | 131 +++++++ tests/backend-dbus/mock-user.h | 57 +++ tests/backend-dbus/mock-webcredentials.cc | 54 +++ tests/backend-dbus/mock-webcredentials.h | 42 +++ tests/backend-dbus/test-actions.cc | 454 ++++++++++++++++++++++++ tests/backend-dbus/test-guest.cc | 192 ++++++++++ tests/backend-dbus/test-users.cc | 377 ++++++++++++++++++++ 29 files changed, 3402 insertions(+) create mode 100644 tests/backend-dbus/CMakeLists.txt create mode 100644 tests/backend-dbus/gtest-mock-dbus-fixture.h create mode 100644 tests/backend-dbus/mock-accounts.cc create mode 100644 tests/backend-dbus/mock-accounts.h create mode 100644 tests/backend-dbus/mock-consolekit-manager.cc create mode 100644 tests/backend-dbus/mock-consolekit-manager.h create mode 100644 tests/backend-dbus/mock-consolekit-seat.cc create mode 100644 tests/backend-dbus/mock-consolekit-seat.h create mode 100644 tests/backend-dbus/mock-consolekit-session.cc create mode 100644 tests/backend-dbus/mock-consolekit-session.h create mode 100644 tests/backend-dbus/mock-display-manager-seat.cc create mode 100644 tests/backend-dbus/mock-display-manager-seat.h create mode 100644 tests/backend-dbus/mock-end-session-dialog.cc create mode 100644 tests/backend-dbus/mock-end-session-dialog.h create mode 100644 tests/backend-dbus/mock-object.cc create mode 100644 tests/backend-dbus/mock-object.h create mode 100644 tests/backend-dbus/mock-screen-saver.cc create mode 100644 tests/backend-dbus/mock-screen-saver.h create mode 100644 tests/backend-dbus/mock-session-manager.cc create mode 100644 tests/backend-dbus/mock-session-manager.h create mode 100644 tests/backend-dbus/mock-upower.cc create mode 100644 tests/backend-dbus/mock-upower.h create mode 100644 tests/backend-dbus/mock-user.cc create mode 100644 tests/backend-dbus/mock-user.h create mode 100644 tests/backend-dbus/mock-webcredentials.cc create mode 100644 tests/backend-dbus/mock-webcredentials.h create mode 100644 tests/backend-dbus/test-actions.cc create mode 100644 tests/backend-dbus/test-guest.cc create mode 100644 tests/backend-dbus/test-users.cc (limited to 'tests/backend-dbus') diff --git a/tests/backend-dbus/CMakeLists.txt b/tests/backend-dbus/CMakeLists.txt new file mode 100644 index 0000000..7cd7030 --- /dev/null +++ b/tests/backend-dbus/CMakeLists.txt @@ -0,0 +1,62 @@ +# build libgtest +add_library (gtest STATIC + ${GTEST_SOURCE_DIR}/gtest-all.cc + ${GTEST_SOURCE_DIR}/gtest_main.cc) +set_target_properties (gtest PROPERTIES INCLUDE_DIRECTORIES + ${GTEST_INCLUDE_DIR}) + +# build desktopmock +add_library (desktopmock STATIC + mock-accounts.cc + mock-accounts.h + mock-consolekit-manager.cc + mock-consolekit-manager.h + mock-consolekit-seat.cc + mock-consolekit-seat.h + mock-consolekit-session.cc + mock-consolekit-session.h + mock-display-manager-seat.cc + mock-display-manager-seat.h + mock-end-session-dialog.cc + mock-end-session-dialog.h + mock-object.cc + mock-object.h + mock-screen-saver.cc + mock-screen-saver.h + mock-session-manager.cc + mock-session-manager.h + mock-upower.cc + mock-upower.h + mock-user.cc + mock-user.h + mock-webcredentials.cc + mock-webcredentials.h) + +include_directories (${SERVICE_INCLUDE_DIRS}) +include_directories (${CMAKE_SOURCE_DIR}/src) +include_directories (${CMAKE_BINARY_DIR}/src) +include_directories (${CMAKE_SOURCE_DIR}/tests) + +# test the Actions class +add_executable (test-actions + test-actions.cc) +add_test (test-actions test-actions) +set_tests_properties (test-actions PROPERTIES COMPILE_FLAGS "${SERVICE_CFLAGS}") +target_link_libraries (test-actions desktopmock backenddbus libindicatorsessionservice gtest ${SERVICE_LDFLAGS} ${GTEST_LIBS} ${GCOV_LIBS}) + +# test the Guest class +add_executable (test-guest + test-guest.cc) +add_test (test-guest test-guest) +set_tests_properties (test-guest PROPERTIES COMPILE_FLAGS "${SERVICE_CFLAGS}") +target_link_libraries (test-guest desktopmock backenddbus libindicatorsessionservice gtest ${SERVICE_LDFLAGS} ${GTEST_LIBS} ${GCOV_LIBS}) + +# test the Users class +add_executable (test-users + test-users.cc) +add_test (test-users test-users) +set_tests_properties (test-users PROPERTIES COMPILE_FLAGS "${SERVICE_CFLAGS}") +target_link_libraries (test-users desktopmock backenddbus libindicatorsessionservice gtest ${SERVICE_LDFLAGS} ${GTEST_LIBS} ${GCOV_LIBS}) + + + diff --git a/tests/backend-dbus/gtest-mock-dbus-fixture.h b/tests/backend-dbus/gtest-mock-dbus-fixture.h new file mode 100644 index 0000000..5c4e312 --- /dev/null +++ b/tests/backend-dbus/gtest-mock-dbus-fixture.h @@ -0,0 +1,130 @@ +/* + * 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 "gtest-dbus-fixture.h" + +#include "mock-accounts.h" +#include "mock-consolekit-manager.h" +#include "mock-consolekit-seat.h" +#include "mock-consolekit-session.h" +#include "mock-display-manager-seat.h" +#include "mock-end-session-dialog.h" +#include "mock-screen-saver.h" +#include "mock-session-manager.h" +#include "mock-upower.h" +#include "mock-user.h" +#include "mock-webcredentials.h" + +/*** +**** +***/ + +class GTestMockDBusFixture: public GTestDBusFixture +{ + private: + + typedef GTestDBusFixture super; + + protected: + + MockScreenSaver * screen_saver; + MockSessionManager * session_manager; + MockDisplayManagerSeat * dm_seat; + MockAccounts * accounts; + MockConsoleKitSession * ck_session; + MockConsoleKitSeat * ck_seat; + MockConsoleKitManager * ck_manager; + MockUPower * upower; + MockEndSessionDialog * end_session_dialog; + MockWebcredentials * webcredentials; + + protected: + + virtual void SetUp () + { + super :: SetUp (); + + webcredentials = new MockWebcredentials (loop, conn); + end_session_dialog = new MockEndSessionDialog (loop, conn); + session_manager = new MockSessionManager (loop, conn); + screen_saver = new MockScreenSaver (loop, conn); + upower = new MockUPower (loop, conn); + dm_seat = new MockDisplayManagerSeat (loop, conn); + g_setenv ("XDG_SEAT_PATH", dm_seat->path(), TRUE); + dm_seat->set_guest_allowed (false); + accounts = build_accounts_mock (); + ck_manager = new MockConsoleKitManager (loop, conn); + ck_seat = new MockConsoleKitSeat (loop, conn, true); + MockUser * user = accounts->find_by_username ("msmith"); + ck_session = ck_seat->add_session_by_user (user); + ck_manager->add_seat (ck_seat); + dm_seat->set_consolekit_seat (ck_seat); + dm_seat->switch_to_user (user->username()); + ASSERT_EQ (ck_session, ck_manager->current_session()); + } + + protected: + + virtual void TearDown () + { + delete accounts; + delete ck_manager; + delete dm_seat; + delete upower; + delete screen_saver; + delete session_manager; + delete end_session_dialog; + delete webcredentials; + + super :: TearDown (); + } + + private: + + MockAccounts * build_accounts_mock () + { + struct { + guint64 login_frequency; + const gchar * user_name; + const gchar * real_name; + } users[] = { + { 134, "whartnell", "First Doctor" }, + { 119, "ptroughton", "Second Doctor" }, + { 128, "jpertwee", "Third Doctor" }, + { 172, "tbaker", "Fourth Doctor" }, + { 69, "pdavison", "Fifth Doctor" }, + { 31, "cbaker", "Sixth Doctor" }, + { 42, "smccoy", "Seventh Doctor" }, + { 1, "pmcgann", "Eigth Doctor" }, + { 13, "ceccleston", "Ninth Doctor" }, + { 47, "dtennant", "Tenth Doctor" }, + { 34, "msmith", "Eleventh Doctor" }, + { 1, "rhurndall", "First Doctor" } + }; + + MockAccounts * a = new MockAccounts (loop, conn); + for (int i=0, n=G_N_ELEMENTS(users); iadd_user (new MockUser (loop, conn, + users[i].user_name, + users[i].real_name, + users[i].login_frequency)); + return a; + } +}; + 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); +} diff --git a/tests/backend-dbus/mock-accounts.h b/tests/backend-dbus/mock-accounts.h new file mode 100644 index 0000000..95da102 --- /dev/null +++ b/tests/backend-dbus/mock-accounts.h @@ -0,0 +1,74 @@ +/* + * 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 . + */ + +#ifndef MOCK_ACCOUNTS_H +#define MOCK_ACCOUNTS_H + +#include +#include +#include +#include "mock-object.h" +#include "backend-dbus/dbus-accounts.h" // struct Accounts + +class MockUser; + +class MockAccounts: public MockObject +{ + public: + + MockAccounts (GMainLoop * loop, + GDBusConnection * bus_connection); + virtual ~MockAccounts (); + + void add_user (MockUser * user); + void remove_user (MockUser * user); + size_t size() const { return my_users.size(); } + MockUser * find_by_uid (guint64 uid); + MockUser * find_by_path (const char * path); + MockUser * find_by_username (const char * username); + + private: + + Accounts * my_skeleton; + + typedef std::set users_t; + users_t my_users; + + typedef std::map uid_to_user_t; + uid_to_user_t my_uid_to_user; + + typedef std::map path_to_user_t; + path_to_user_t my_path_to_user; + + typedef std::map username_to_user_t; + username_to_user_t my_username_to_user; + + private: + + static gboolean on_find_user_by_id_static (Accounts *, + GDBusMethodInvocation *, + guint64, + gpointer); + + static gboolean on_list_cached_users_static (Accounts *, + GDBusMethodInvocation *, + gpointer); +}; + +#endif // #ifndef MOCK_ACCOUNTS_H diff --git a/tests/backend-dbus/mock-consolekit-manager.cc b/tests/backend-dbus/mock-consolekit-manager.cc new file mode 100644 index 0000000..40f9bf9 --- /dev/null +++ b/tests/backend-dbus/mock-consolekit-manager.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-consolekit-manager.h" +#include "mock-consolekit-seat.h" +#include "mock-consolekit-session.h" + +namespace +{ + const char * CONSOLEKIT_MANAGER_NAME = "org.freedesktop.ConsoleKit"; + + const char * CONSOLEKIT_MANAGER_PATH = "/org/freedesktop/ConsoleKit/Manager"; + + void on_active_session_changed (ConsoleKitSeat * o G_GNUC_UNUSED, + const gchar * new_ssid, + gpointer ssid) + { + *static_cast(ssid) = new_ssid; + } +} + +/*** +**** +***/ + +gboolean +MockConsoleKitManager :: on_get_current_session (ConsoleKitManager * m, + GDBusMethodInvocation * inv, + gpointer gself) +{ + MockConsoleKitManager * self = static_cast(gself); + const std::string& ssid = self->my_current_ssid; + console_kit_manager_complete_get_current_session (m, inv, ssid.c_str()); + return true; +} + +gboolean +MockConsoleKitManager :: on_get_seats (ConsoleKitManager * m, + GDBusMethodInvocation * inv, + gpointer gself) +{ + int i; + char ** sids; + const seats_t& seats = static_cast(gself)->my_seats; + + i = 0; + sids = g_new0 (char*, seats.size()+1); + for (seats_t::const_iterator it(seats.begin()), + end(seats.end()); it!=end; ++it) + sids[i++] = (char*) (*it)->path(); + console_kit_manager_complete_get_seats (m, inv, sids); + g_strfreev (sids); + + return true; +} + +gboolean +MockConsoleKitManager :: handle_restart (ConsoleKitManager * ckm, + GDBusMethodInvocation * inv, + gpointer gself) +{ + static_cast(gself)->my_last_action = Restart; + console_kit_manager_complete_restart (ckm, inv); + return true; +} + +gboolean +MockConsoleKitManager :: handle_stop (ConsoleKitManager * ckm, + GDBusMethodInvocation * inv, + gpointer gself) +{ + static_cast(gself)->my_last_action = Shutdown; + console_kit_manager_complete_stop (ckm, inv); + return true; +} + +/*** +**** +***/ + +MockConsoleKitSession * +MockConsoleKitManager :: current_session () +{ + MockConsoleKitSession * ret = 0; + + for (seats_t::iterator it(my_seats.begin()), + end(my_seats.end()); it!=end; ++it) + if ((ret = (*it)->find (my_current_ssid.c_str()))) + break; + + return ret; +} + +void +MockConsoleKitManager :: add_seat (MockConsoleKitSeat * seat) +{ + g_assert (my_seats.count(seat) == 0); + + my_seats.insert (seat); + + console_kit_manager_emit_seat_added (my_skeleton, seat->sid()); + + g_signal_connect (seat->skeleton(), "active-session-changed", + G_CALLBACK(on_active_session_changed), &my_current_ssid); +} + +/*** +**** +***/ + +MockConsoleKitManager :: MockConsoleKitManager (GMainLoop * loop, + GDBusConnection * conn): + MockObject (loop, conn, CONSOLEKIT_MANAGER_NAME, CONSOLEKIT_MANAGER_PATH), + my_skeleton (console_kit_manager_skeleton_new ()), + my_last_action (None) +{ + g_signal_connect (my_skeleton, "handle-get-current-session", + G_CALLBACK(on_get_current_session), this); + g_signal_connect (my_skeleton, "handle-get-seats", + G_CALLBACK(on_get_seats), this); + g_signal_connect (my_skeleton, "handle-stop", + G_CALLBACK(handle_stop), this); + g_signal_connect (my_skeleton, "handle-restart", + G_CALLBACK(handle_restart), this); + + set_skeleton (G_DBUS_INTERFACE_SKELETON(my_skeleton)); +} + +MockConsoleKitManager :: ~MockConsoleKitManager () +{ + for (seats_t::iterator it(my_seats.begin()); it!=my_seats.end(); ++it) + { + MockConsoleKitSeat * seat = *it; + g_signal_handlers_disconnect_by_data (seat->skeleton(), &my_current_ssid); + delete seat; + } + + g_signal_handlers_disconnect_by_data (my_skeleton, this); + g_clear_object (&my_skeleton); +} diff --git a/tests/backend-dbus/mock-consolekit-manager.h b/tests/backend-dbus/mock-consolekit-manager.h new file mode 100644 index 0000000..c5942b2 --- /dev/null +++ b/tests/backend-dbus/mock-consolekit-manager.h @@ -0,0 +1,77 @@ +/* + * 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 . + */ + +#ifndef MOCK_CONSOLEKIT_MANAGER_H +#define MOCK_CONSOLEKIT_MANAGER_H + +#include +#include +#include "mock-object.h" +#include "backend-dbus/dbus-consolekit-manager.h" + +class MockConsoleKitSession; +class MockConsoleKitSeat; + +class MockConsoleKitManager: public MockObject +{ + public: + + MockConsoleKitManager (GMainLoop * loop, + GDBusConnection * bus_connection); + virtual ~MockConsoleKitManager (); + + void add_seat (MockConsoleKitSeat * seat); + + MockConsoleKitSession * current_session (); + + public: + + enum Action { None, Shutdown, Restart }; + + Action last_action () const { return my_last_action; } + + void clear_last_action () { my_last_action = None; } + + private: + + typedef std::set seats_t; + seats_t my_seats; + + ConsoleKitManager * my_skeleton; + + std::string my_current_ssid; + + Action my_last_action; + + static gboolean on_get_current_session (ConsoleKitManager *, + GDBusMethodInvocation *, + gpointer ); + static gboolean on_get_seats (ConsoleKitManager *, + GDBusMethodInvocation *, + gpointer ); + static gboolean handle_restart (ConsoleKitManager *, + GDBusMethodInvocation *, + gpointer); + static gboolean handle_stop (ConsoleKitManager *, + GDBusMethodInvocation *, + gpointer); + +}; + +#endif // #ifndef MOCK_CONSOLEKIT_MANAGER_H diff --git a/tests/backend-dbus/mock-consolekit-seat.cc b/tests/backend-dbus/mock-consolekit-seat.cc new file mode 100644 index 0000000..ce1fbad --- /dev/null +++ b/tests/backend-dbus/mock-consolekit-seat.cc @@ -0,0 +1,220 @@ +/* + * 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-consolekit-seat.h" + +#include "mock-object.h" +#include "mock-consolekit-session.h" +#include "mock-user.h" + +namespace +{ + const char * CONSOLEKIT_BUS_NAME = "org.freedesktop.ConsoleKit"; + + std::string next_unique_sid () + { + static int id = 1; + + char * tmp; + std::string ret; + + tmp = g_strdup_printf ("/org/freedesktop/ConsoleKit/Seat%d", id++); + ret = tmp; + g_free (tmp); + return ret; + } +} + +/*** +**** +***/ + +void +MockConsoleKitSeat :: activate_session (MockConsoleKitSession * session) +{ + g_assert (my_sessions.count(session) == 1); + + const char * ssid = session->ssid (); + if (my_active_ssid != ssid) + { + my_active_ssid = ssid; + console_kit_seat_emit_active_session_changed (my_skeleton, ssid); + } +} + +void +MockConsoleKitSeat :: switch_to_guest () +{ + for (sessions_t::iterator it(my_sessions.begin()), + end(my_sessions.end()); it!=end; ++it) + { + MockConsoleKitSession * session (*it); + + if (session->user()->is_guest()) + { + activate_session (*it); + return; + } + } + + g_warn_if_reached (); +} + +void +MockConsoleKitSeat :: switch_to_user (const char * username) +{ + for (sessions_t::iterator it(my_sessions.begin()), + end(my_sessions.end()); it!=end; ++it) + { + MockConsoleKitSession * session (*it); + + if (!g_strcmp0 (username, session->user()->username())) + { + activate_session (*it); + return; + } + } + + g_warn_if_reached (); +} + +/*** +**** +***/ + +MockConsoleKitSession * +MockConsoleKitSeat :: add_session_by_user (MockUser * mu) +{ + g_assert (mu != 0); + + MockConsoleKitSession * session; + + session = new MockConsoleKitSession (my_loop, my_bus_connection); + session->set_user (mu); + add_session (session); + return session; +} + +void +MockConsoleKitSeat :: add_session (MockConsoleKitSession * session) +{ + g_assert (my_sessions.count(session) == 0); + + my_sessions.insert (session); + session->set_sid (path()); + console_kit_seat_emit_session_added (my_skeleton, session->ssid()); +} + +void +MockConsoleKitSeat :: remove_session (MockConsoleKitSession * session) +{ + g_assert (my_sessions.count(session) == 1); + + my_sessions.erase (session); + session->set_sid (""); + console_kit_seat_emit_session_removed (my_skeleton, session->ssid()); +} + +/*** +**** Handlers +***/ + +gboolean +MockConsoleKitSeat :: on_can_activate_sessions (ConsoleKitSeat * cks, + GDBusMethodInvocation * inv, + gpointer gself) +{ + bool b = static_cast(gself)->my_can_activate_sessions; + console_kit_seat_complete_can_activate_sessions (cks, inv, b); + return true; +} + +gboolean +MockConsoleKitSeat :: on_get_active_session (ConsoleKitSeat * cks, + GDBusMethodInvocation * invoke, + gpointer gself) +{ + std::string ssid = static_cast(gself)->my_active_ssid; + console_kit_seat_complete_get_active_session (cks, invoke, ssid.c_str()); + return true; +} + +gboolean +MockConsoleKitSeat :: on_get_sessions (ConsoleKitSeat * cks, + GDBusMethodInvocation * inv, + gpointer gself) +{ + int i; + const char ** paths; + sessions_t& sessions = static_cast(gself)->my_sessions; + + i = 0; + paths = g_new0 (const char*, sessions.size() + 1); + for (sessions_t::iterator it(sessions.begin()), + end(sessions.end()); it!=end; ++it) + paths[i++] = (*it)->path(); + + g_debug ("returning a list of %d sessions", i); + console_kit_seat_complete_get_sessions (cks, inv, paths); + g_free (paths); + + return true; +} + +MockConsoleKitSession * +MockConsoleKitSeat :: find (const char * ssid) +{ + for (sessions_t::iterator it(my_sessions.begin()), + end(my_sessions.end()); it!=end; ++it) + if (!g_strcmp0 ((*it)->path(), ssid)) + return *it; + + return 0; +} + +/*** +**** Life Cycle +***/ + +MockConsoleKitSeat :: MockConsoleKitSeat (GMainLoop * loop, + GDBusConnection * bus_connection, + bool can_activate_sessions): + MockObject (loop, bus_connection, CONSOLEKIT_BUS_NAME, next_unique_sid()), + my_skeleton (console_kit_seat_skeleton_new ()), + my_can_activate_sessions (can_activate_sessions) +{ + g_signal_connect (my_skeleton, "handle-get-active-session", + G_CALLBACK(on_get_active_session), this); + g_signal_connect (my_skeleton, "handle-get-sessions", + G_CALLBACK(on_get_sessions), this); + g_signal_connect (my_skeleton, "handle-can-activate-sessions", + G_CALLBACK(on_can_activate_sessions), this); + + set_skeleton (G_DBUS_INTERFACE_SKELETON(my_skeleton)); +} + +MockConsoleKitSeat :: ~MockConsoleKitSeat () +{ + for (sessions_t::iterator it(my_sessions.begin()), + end(my_sessions.end()); it!=end; ++it) + delete *it; + + g_signal_handlers_disconnect_by_data (my_skeleton, this); + g_clear_object (&my_skeleton); +} diff --git a/tests/backend-dbus/mock-consolekit-seat.h b/tests/backend-dbus/mock-consolekit-seat.h new file mode 100644 index 0000000..aa52276 --- /dev/null +++ b/tests/backend-dbus/mock-consolekit-seat.h @@ -0,0 +1,75 @@ +/* + * 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 . + */ + +#ifndef MOCK_CONSOLEKIT_SEAT_H +#define MOCK_CONSOLEKIT_SEAT_H + +#include +#include +#include "backend-dbus/dbus-consolekit-seat.h" +#include "mock-object.h" + +class MockUser; +class MockConsoleKitSession; + +class MockConsoleKitSeat: public MockObject +{ + public: + + MockConsoleKitSeat (GMainLoop * loop, + GDBusConnection * bus_connection, + bool can_activate_sessions); + virtual ~MockConsoleKitSeat (); + + const char * sid() { return path(); } + MockConsoleKitSession * add_session_by_user (MockUser * user); + void add_session (MockConsoleKitSession * session); + void remove_session (MockConsoleKitSession * session); + void activate_session (MockConsoleKitSession * session); + void switch_to_guest (); + void switch_to_user (const char * username); + bool can_activate_sessions () const { return my_can_activate_sessions; } + MockConsoleKitSession * find (const char * ssid); + + private: + + static gboolean on_get_active_session (ConsoleKitSeat * cks, + GDBusMethodInvocation * inv, + gpointer gself); + static gboolean on_get_sessions (ConsoleKitSeat * cks, + GDBusMethodInvocation * inv, + gpointer gself); + static gboolean on_can_activate_sessions (ConsoleKitSeat * cks, + GDBusMethodInvocation * inv, + gpointer gself); + + + private: + + ConsoleKitSeat * my_skeleton; + + std::string my_active_ssid; + + typedef std::set sessions_t; + sessions_t my_sessions; + bool my_can_activate_sessions; + +}; + +#endif // #ifndef MOCK_CONSOLEKIT_SEAT_H diff --git a/tests/backend-dbus/mock-consolekit-session.cc b/tests/backend-dbus/mock-consolekit-session.cc new file mode 100644 index 0000000..2f39411 --- /dev/null +++ b/tests/backend-dbus/mock-consolekit-session.cc @@ -0,0 +1,113 @@ +/* + * 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-consolekit-session.h" +#include "mock-user.h" + +namespace +{ + const char * const DEFAULT_X11_DISPLAY = ":0:0"; + + const char * const CONSOLEKIT_NAME = "org.freedesktop.ConsoleKit"; + + std::string next_unique_ssid () + { + static int id = 333; // arbitrary + + char * tmp; + std::string ret; + + tmp = g_strdup_printf ("/org/freedesktop/ConsoleKit/Session%d", id++); + ret = tmp; + g_free (tmp); + return ret; + } +} + +void +MockConsoleKitSession :: set_user (MockUser * user) +{ + my_user = user; +} + +/*** +**** +***/ + +gboolean +MockConsoleKitSession :: on_get_seat_id_static (ConsoleKitSession * cks, + GDBusMethodInvocation * inv, + gpointer gself) +{ + const std::string& sid = static_cast(gself)->my_sid; + g_debug ("%s %s returning seat id of %s", G_STRLOC, G_STRFUNC, sid.c_str()); + console_kit_session_complete_get_seat_id (cks, inv, sid.c_str()); + return true; +} + +gboolean +MockConsoleKitSession :: on_get_unix_user_static (ConsoleKitSession * cks, + GDBusMethodInvocation * inv, + gpointer gself) +{ + MockUser * user = static_cast(gself)->my_user; + g_debug ("%s %s returning uid of %u", G_STRLOC, G_STRFUNC, user->uid()); + console_kit_session_complete_get_unix_user (cks, inv, user->uid()); + return true; +} + +gboolean +MockConsoleKitSession :: on_get_x11_display (ConsoleKitSession * cks, + GDBusMethodInvocation * inv, + gpointer gself) +{ + MockConsoleKitSession * self = static_cast(gself); + const char * x11 = self->x11_display(); + g_debug ("%s %s returning x11 display '%s'", G_STRLOC, G_STRFUNC, x11); + console_kit_session_complete_get_x11_display (cks, inv, x11); + return true; +} + +/*** +**** +***/ + +MockConsoleKitSession :: MockConsoleKitSession (GMainLoop * loop, + GDBusConnection * conn): + MockObject (loop, conn, CONSOLEKIT_NAME, next_unique_ssid ()), + my_skeleton (console_kit_session_skeleton_new ()), + my_x11_display (DEFAULT_X11_DISPLAY), + my_user (0) +{ + g_signal_connect (my_skeleton, "handle-get-seat-id", + G_CALLBACK(on_get_seat_id_static), this); + g_signal_connect (my_skeleton, "handle-get-unix-user", + G_CALLBACK(on_get_unix_user_static), this); + g_signal_connect (my_skeleton, "handle-get-x11-display", + G_CALLBACK(on_get_x11_display), this); + + set_skeleton (G_DBUS_INTERFACE_SKELETON(my_skeleton)); +} + +MockConsoleKitSession :: ~MockConsoleKitSession () +{ + const int n = g_signal_handlers_disconnect_by_data (my_skeleton, this); + g_assert (n == 3); + g_clear_object (&my_skeleton); +} diff --git a/tests/backend-dbus/mock-consolekit-session.h b/tests/backend-dbus/mock-consolekit-session.h new file mode 100644 index 0000000..7759f72 --- /dev/null +++ b/tests/backend-dbus/mock-consolekit-session.h @@ -0,0 +1,65 @@ +/* + * 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 . + */ + +#ifndef MOCK_CONSOLEKIT_SESSION_H +#define MOCK_CONSOLEKIT_SESSION_H + +#include +#include "mock-object.h" +#include "backend-dbus/dbus-consolekit-session.h" + +class MockUser; + +class MockConsoleKitSession: public MockObject +{ + public: + + MockConsoleKitSession (GMainLoop * loop, + GDBusConnection * bus_connection); + virtual ~MockConsoleKitSession (); + + MockUser * user () { return my_user; } + void set_user (MockUser * user); + const char * ssid () { return path(); } + void set_sid (const std::string& sid) { my_sid = sid; } + const char * x11_display() { return my_x11_display.c_str(); } + void set_x11_display (const std::string& x) { my_x11_display = x; } + + private: + + static gboolean on_get_seat_id_static (ConsoleKitSession *, + GDBusMethodInvocation *, + gpointer); + static gboolean on_get_unix_user_static (ConsoleKitSession *, + GDBusMethodInvocation *, + gpointer); + static gboolean on_get_x11_display (ConsoleKitSession *, + GDBusMethodInvocation *, + gpointer); + + + private: + + ConsoleKitSession * my_skeleton; + std::string my_sid; + std::string my_x11_display; + MockUser * my_user; +}; + +#endif // #ifndef MOCK_CONSOLEKIT_SESSION_H diff --git a/tests/backend-dbus/mock-display-manager-seat.cc b/tests/backend-dbus/mock-display-manager-seat.cc new file mode 100644 index 0000000..9f30ae1 --- /dev/null +++ b/tests/backend-dbus/mock-display-manager-seat.cc @@ -0,0 +1,140 @@ +/* + * 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-display-manager-seat.h" +#include "mock-consolekit-seat.h" + +namespace +{ + const char * const DISPLAY_MANAGER_NAME = "org.freedesktop.DisplayManager"; + + std::string + next_unique_path () + { + static int id = 12; // arbitrary; doesn't matter + + char * tmp; + std::string ret; + + tmp = g_strdup_printf ("/org/freedesktop/DisplayManager/Seat%d", id++); + ret = tmp; + g_free (tmp); + return ret; + } +} + +/*** +**** +***/ + +void +MockDisplayManagerSeat :: switch_to_greeter () +{ + my_last_action = GREETER; +} + +gboolean +MockDisplayManagerSeat :: handle_switch_to_greeter (DisplayManagerSeat * o, + GDBusMethodInvocation * inv, + gpointer gself) +{ + static_cast(gself)->switch_to_greeter (); + display_manager_seat_complete_switch_to_greeter (o, inv); + return true; +} + +void +MockDisplayManagerSeat :: set_guest_allowed (bool b) +{ + display_manager_seat_set_has_guest_account (my_skeleton, b); +} + +gboolean +MockDisplayManagerSeat :: handle_switch_to_guest (DisplayManagerSeat * o, + GDBusMethodInvocation * inv, + const gchar * session_name G_GNUC_UNUSED, + gpointer gself) +{ + static_cast(gself)->switch_to_guest (); + display_manager_seat_complete_switch_to_guest (o, inv); + return true; +} + +void +MockDisplayManagerSeat :: switch_to_guest () +{ + g_assert (my_ck_seat != 0); + + my_last_action = GUEST; + my_ck_seat->switch_to_guest (); +} + +gboolean +MockDisplayManagerSeat :: handle_switch_to_user (DisplayManagerSeat * o, + GDBusMethodInvocation * inv, + const gchar * username, + const gchar * session_name G_GNUC_UNUSED, + gpointer gself) +{ + static_cast(gself)->switch_to_user (username); + display_manager_seat_complete_switch_to_user (o, inv); + return true; +} + +void +MockDisplayManagerSeat :: switch_to_user (const char * username) +{ + g_assert (my_ck_seat != 0); + + my_last_action = USER; + my_ck_seat->switch_to_user (username); +} + +void +MockDisplayManagerSeat :: set_consolekit_seat (MockConsoleKitSeat * seat) +{ + my_ck_seat = seat; +} + +/*** +**** +***/ + +MockDisplayManagerSeat :: MockDisplayManagerSeat (GMainLoop * loop, + GDBusConnection * connection): + MockObject (loop, connection, DISPLAY_MANAGER_NAME, next_unique_path()), + my_skeleton (display_manager_seat_skeleton_new ()), + my_ck_seat (0), + my_last_action (NONE) +{ + g_signal_connect (my_skeleton, "handle-switch-to-guest", + G_CALLBACK(handle_switch_to_guest), this); + g_signal_connect (my_skeleton, "handle-switch-to-user", + G_CALLBACK(handle_switch_to_user), this); + g_signal_connect (my_skeleton, "handle-switch-to-greeter", + G_CALLBACK(handle_switch_to_greeter), this); + + set_skeleton (G_DBUS_INTERFACE_SKELETON(my_skeleton)); +} + +MockDisplayManagerSeat :: ~MockDisplayManagerSeat () +{ + g_signal_handlers_disconnect_by_data (my_skeleton, this); + g_clear_object (&my_skeleton); +} diff --git a/tests/backend-dbus/mock-display-manager-seat.h b/tests/backend-dbus/mock-display-manager-seat.h new file mode 100644 index 0000000..b0ea415 --- /dev/null +++ b/tests/backend-dbus/mock-display-manager-seat.h @@ -0,0 +1,72 @@ +/* + * 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 . + */ + +#ifndef MOCK_DISPLAY_MANAGER_SEAT_H +#define MOCK_DISPLAY_MANAGER_SEAT_H + +#include "mock-object.h" // parent class +#include "backend-dbus/dbus-display-manager.h" + +class MockConsoleKitSeat; + +class MockDisplayManagerSeat: public MockObject +{ + public: + + MockDisplayManagerSeat (GMainLoop * loop, + GDBusConnection * bus_connection); + virtual ~MockDisplayManagerSeat (); + + void set_guest_allowed (bool b); + + void set_consolekit_seat (MockConsoleKitSeat * ck_seat); + + void switch_to_guest (); + + void switch_to_greeter (); + + void switch_to_user (const char * username); + + public: + + enum Action { NONE, GUEST, GREETER, USER }; + + Action last_action () const { return my_last_action; } + + private: + + static gboolean handle_switch_to_greeter (DisplayManagerSeat *o, + GDBusMethodInvocation *inv, + gpointer gself); + static gboolean handle_switch_to_guest (DisplayManagerSeat *o, + GDBusMethodInvocation *inv, + const gchar *session_name, + gpointer gself); + static gboolean handle_switch_to_user (DisplayManagerSeat * o, + GDBusMethodInvocation * inv, + const gchar * username, + const gchar * session_name, + gpointer gself); + + DisplayManagerSeat * my_skeleton; + MockConsoleKitSeat * my_ck_seat; + Action my_last_action; +}; + +#endif // #ifndef MOCK_DISPLAY_MANAGER_SEAT_H diff --git a/tests/backend-dbus/mock-end-session-dialog.cc b/tests/backend-dbus/mock-end-session-dialog.cc new file mode 100644 index 0000000..e289a83 --- /dev/null +++ b/tests/backend-dbus/mock-end-session-dialog.cc @@ -0,0 +1,89 @@ +/* + * 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-end-session-dialog.h" + +#if 0 +gboolean +MockEndSessionDialog :: handle_lock (GnomeScreenSaver * ss, + GDBusMethodInvocation * inv, + gpointer gself) +{ + static_cast(gself)->my_last_action = Lock; + gnome_screen_saver_complete_lock (ss, inv); + return true; +} + +gboolean +MockEndSessionDialog :: handle_simulate_user_activity (GnomeScreenSaver * ss, + GDBusMethodInvocation * inv, + gpointer gself) +{ + static_cast(gself)->my_last_action = UserActivity; + gnome_screen_saver_complete_simulate_user_activity (ss, inv); + return true; +} +#endif + +gboolean +MockEndSessionDialog :: handle_open (EndSessionDialog * object, + GDBusMethodInvocation * invocation, + guint arg_type, + guint arg_timestamp, + guint arg_seconds_to_stay_open, + const gchar * const * inhibitor_paths, + gpointer gself) +{ + static_cast(gself)->my_isOpen = true; + end_session_dialog_complete_open (object, invocation); + return true; +} + +/*** +**** +***/ + +namespace +{ + const char * const MY_NAME = "com.canonical.Unity"; + const char * const MY_PATH = "/org/gnome/SessionManager/EndSessionDialog"; +} + +MockEndSessionDialog :: MockEndSessionDialog (GMainLoop * loop, + GDBusConnection * bus_connection): + MockObject (loop, bus_connection, MY_NAME, MY_PATH), + my_isOpen (false), + my_skeleton (end_session_dialog_skeleton_new ()) +{ + g_signal_connect (my_skeleton, "handle-open", + G_CALLBACK(handle_open), this); +#if 0 + g_signal_connect (my_skeleton, "handle-lock", + G_CALLBACK(handle_lock), this); + g_signal_connect (my_skeleton, "handle-simulate-user-activity", + G_CALLBACK(handle_simulate_user_activity), this); +#endif + + set_skeleton (G_DBUS_INTERFACE_SKELETON(my_skeleton)); +} + +MockEndSessionDialog :: ~MockEndSessionDialog () +{ + g_clear_object (&my_skeleton); +} diff --git a/tests/backend-dbus/mock-end-session-dialog.h b/tests/backend-dbus/mock-end-session-dialog.h new file mode 100644 index 0000000..468715c --- /dev/null +++ b/tests/backend-dbus/mock-end-session-dialog.h @@ -0,0 +1,67 @@ +/* + * 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 . + */ + +#ifndef MOCK_END_SESSION_DIALOG_H +#define MOCK_END_SESSION_DIALOG_H + +#include "mock-object.h" // parent class +#include "backend-dbus/dbus-end-session-dialog.h" // EndSessionDialog + +class MockEndSessionDialog: public MockObject +{ + public: + + MockEndSessionDialog (GMainLoop * loop, + GDBusConnection * bus_connection); + virtual ~MockEndSessionDialog (); + + bool is_open () const { return my_isOpen; } + + void cancel () { my_isOpen = false; end_session_dialog_emit_canceled (my_skeleton); } + void confirm_logout () { my_isOpen = false; end_session_dialog_emit_confirmed_logout (my_skeleton); } + void confirm_reboot () { my_isOpen = false; end_session_dialog_emit_confirmed_reboot (my_skeleton); } + void confirm_shutdown () { my_isOpen = false; end_session_dialog_emit_confirmed_shutdown (my_skeleton); } + void close () { my_isOpen = false; end_session_dialog_emit_closed (my_skeleton); } + + private: + + EndSessionDialog * my_skeleton; + + bool my_isOpen; + + static gboolean handle_open (EndSessionDialog *, + GDBusMethodInvocation *, + guint, + guint, + guint, + const gchar * const *, + gpointer); + + +#if 0 + static gboolean handle_lock (GnomeScreenSaver *, + GDBusMethodInvocation *, + gpointer); + static gboolean handle_simulate_user_activity (GnomeScreenSaver *, + GDBusMethodInvocation *, + gpointer); +#endif +}; + +#endif diff --git a/tests/backend-dbus/mock-object.cc b/tests/backend-dbus/mock-object.cc new file mode 100644 index 0000000..af9330b --- /dev/null +++ b/tests/backend-dbus/mock-object.cc @@ -0,0 +1,122 @@ +/* + * 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 +#include + +#include "mock-object.h" + +namespace +{ + const int TIMEOUT_SECONDS = 5; + + gboolean on_timeout_reached (gpointer loop) + { + g_main_loop_quit (static_cast(loop)); + return G_SOURCE_REMOVE; + } + + void on_name_acquired (GDBusConnection * connection G_GNUC_UNUSED, + const char * name G_GNUC_UNUSED, + gpointer loop) + { + //g_debug ("name '%s' acquired", name); + g_main_loop_quit (static_cast(loop)); + } + + void on_name_lost (GDBusConnection * connection G_GNUC_UNUSED, + const char * name G_GNUC_UNUSED, + gpointer loop) + { + //g_debug ("name '%s' lost", name); + g_main_loop_quit (static_cast(loop)); + } +} + +void +MockObject :: set_skeleton (GDBusInterfaceSkeleton * skeleton) +{ + g_assert (skeleton != NULL); + g_assert (my_skeleton == NULL); + g_assert (g_variant_is_object_path (my_object_path.c_str())); + g_assert (my_owner_id == 0); + + my_skeleton = G_DBUS_INTERFACE_SKELETON (g_object_ref (skeleton)); + + GError * err = NULL; + g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON(my_skeleton), + my_bus_connection, + my_object_path.c_str(), + &err); + g_assert_no_error (err); + + my_owner_id = g_bus_own_name_on_connection (my_bus_connection, + my_object_name.c_str(), + G_BUS_NAME_OWNER_FLAGS_NONE, + on_name_acquired, + on_name_lost, + my_loop, + NULL); + + // wait for the name to be acquired or timeout, whichever comes first + const guint timeout_id = g_timeout_add_seconds (TIMEOUT_SECONDS, + on_timeout_reached, + my_loop); + g_main_loop_run (my_loop); + g_assert (g_main_context_find_source_by_id (NULL, timeout_id) != NULL); + g_source_remove (timeout_id); +} + +/*** +**** +***/ + +MockObject :: MockObject (GMainLoop * loop, + GDBusConnection * bus_connection, + const std::string & object_name, + const std::string & object_path): + my_owner_id (0), + my_loop (g_main_loop_ref (loop)), + my_bus_connection (G_DBUS_CONNECTION (g_object_ref (bus_connection))), + my_object_name (object_name), + my_object_path (object_path), + my_skeleton (0) +{ +} + +MockObject :: ~MockObject () +{ + g_main_loop_unref (my_loop); + + if (my_owner_id != 0) + { + g_bus_unown_name (my_owner_id); + + my_owner_id = 0; + } + + if (my_skeleton) + { + g_dbus_interface_skeleton_unexport (my_skeleton); + + g_clear_object (&my_skeleton); + } + + g_clear_object (&my_bus_connection); +} diff --git a/tests/backend-dbus/mock-object.h b/tests/backend-dbus/mock-object.h new file mode 100644 index 0000000..8dc7070 --- /dev/null +++ b/tests/backend-dbus/mock-object.h @@ -0,0 +1,62 @@ +/* + * 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 . + */ + +#ifndef MOCK_OBJECT_H +#define MOCK_OBJECT_H + +#include + +#include +#include + +class MockObject +{ + public: + + MockObject (GMainLoop * loop, + GDBusConnection * bus_connection, + const std::string & object_name, + const std::string & object_path); + + virtual ~MockObject (); + + const char * name() const { return my_object_name.c_str(); } + const char * path() const { return my_object_path.c_str(); } + + GDBusInterfaceSkeleton * skeleton() { return my_skeleton; } + + protected: + + guint my_owner_id; + GMainLoop * my_loop; + GDBusConnection * my_bus_connection; + const std::string my_object_name; + const std::string my_object_path; + GDBusInterfaceSkeleton * my_skeleton; + + void set_skeleton (GDBusInterfaceSkeleton * skeleton); + + private: + // safeguard to make sure we don't copy-by-value... + // this object's holding a handful of pointers + MockObject (const MockObject& rhs); + MockObject& operator= (const MockObject& rhs); +}; + +#endif diff --git a/tests/backend-dbus/mock-screen-saver.cc b/tests/backend-dbus/mock-screen-saver.cc new file mode 100644 index 0000000..1d3bb11 --- /dev/null +++ b/tests/backend-dbus/mock-screen-saver.cc @@ -0,0 +1,71 @@ +/* + * 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-screen-saver.h" + + +gboolean +MockScreenSaver :: handle_lock (GnomeScreenSaver * ss, + GDBusMethodInvocation * inv, + gpointer gself) +{ + static_cast(gself)->my_last_action = Lock; + gnome_screen_saver_complete_lock (ss, inv); + return true; +} + +gboolean +MockScreenSaver :: handle_simulate_user_activity (GnomeScreenSaver * ss, + GDBusMethodInvocation * inv, + gpointer gself) +{ + static_cast(gself)->my_last_action = UserActivity; + gnome_screen_saver_complete_simulate_user_activity (ss, inv); + return true; +} + +/*** +**** +***/ + +namespace +{ + const char * const SCREENSAVER_NAME = "org.gnome.ScreenSaver"; + const char * const SCREENSAVER_PATH = "/org/gnome/ScreenSaver"; + +} + +MockScreenSaver :: MockScreenSaver (GMainLoop * loop, + GDBusConnection * bus_connection): + MockObject (loop, bus_connection, SCREENSAVER_NAME, SCREENSAVER_PATH), + my_skeleton (gnome_screen_saver_skeleton_new ()), + my_last_action (None) +{ + g_signal_connect (my_skeleton, "handle-lock", + G_CALLBACK(handle_lock), this); + g_signal_connect (my_skeleton, "handle-simulate-user-activity", + G_CALLBACK(handle_simulate_user_activity), this); + + set_skeleton (G_DBUS_INTERFACE_SKELETON(my_skeleton)); +} + +MockScreenSaver :: ~MockScreenSaver () +{ + g_clear_object (&my_skeleton); +} diff --git a/tests/backend-dbus/mock-screen-saver.h b/tests/backend-dbus/mock-screen-saver.h new file mode 100644 index 0000000..c57a4c6 --- /dev/null +++ b/tests/backend-dbus/mock-screen-saver.h @@ -0,0 +1,53 @@ +/* + * 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 . + */ + +#ifndef MOCK_SCREENSAVER_H +#define MOCK_SCREENSAVER_H + +#include "mock-object.h" // parent class +#include "backend-dbus/gnome-screen-saver.h" // GnomeScreenSaver + +class MockScreenSaver: public MockObject +{ + public: + + MockScreenSaver (GMainLoop * loop, + GDBusConnection * bus_connection); + virtual ~MockScreenSaver (); + + public: + + enum Action { None, Lock, UserActivity }; + Action last_action () { return my_last_action; } + + private: + + GnomeScreenSaver * my_skeleton; + Action my_last_action; + + static gboolean handle_lock (GnomeScreenSaver *, + GDBusMethodInvocation *, + gpointer); + static gboolean handle_simulate_user_activity (GnomeScreenSaver *, + GDBusMethodInvocation *, + gpointer); + +}; + +#endif diff --git a/tests/backend-dbus/mock-session-manager.cc b/tests/backend-dbus/mock-session-manager.cc new file mode 100644 index 0000000..7a4ce87 --- /dev/null +++ b/tests/backend-dbus/mock-session-manager.cc @@ -0,0 +1,66 @@ +/* + * 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-session-manager.h" + +gboolean +MockSessionManager :: handle_logout (GnomeSessionManager * gsm, + GDBusMethodInvocation * inv, + guint arg, + gpointer gself) +{ + Action action; + switch (arg) { + case 0: action = LogoutNormal; break; + case 1: action = LogoutQuiet; break; + case 2: action = LogoutForce; break; + default: action = None; break; + } + static_cast(gself)->my_last_action = action; + gnome_session_manager_complete_logout (gsm, inv); + return true; +} + + /*** +**** +***/ + +namespace +{ + const char * const SESSION_MANAGER_NAME = "org.gnome.SessionManager"; + const char * const SESSION_MANAGER_PATH = "/org/gnome/SessionManager"; + +} + +MockSessionManager :: MockSessionManager (GMainLoop * loop, + GDBusConnection * bus_connection): + MockObject (loop, bus_connection, SESSION_MANAGER_NAME, SESSION_MANAGER_PATH), + my_skeleton (gnome_session_manager_skeleton_new ()), + my_last_action (None) +{ + g_signal_connect (my_skeleton, "handle-logout", + G_CALLBACK(handle_logout), this); + + set_skeleton (G_DBUS_INTERFACE_SKELETON(my_skeleton)); +} + +MockSessionManager :: ~MockSessionManager () +{ + g_clear_object (&my_skeleton); +} diff --git a/tests/backend-dbus/mock-session-manager.h b/tests/backend-dbus/mock-session-manager.h new file mode 100644 index 0000000..6a21277 --- /dev/null +++ b/tests/backend-dbus/mock-session-manager.h @@ -0,0 +1,50 @@ +/* + * 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 . + */ + +#ifndef MOCK_SESSION_MANAGER_H +#define MOCK_SESSION_MANAGER_H + +#include "mock-object.h" // parent class +#include "backend-dbus/gnome-session-manager.h" // GnomeSessionManager + +class MockSessionManager: public MockObject +{ + public: + + MockSessionManager (GMainLoop * loop, + GDBusConnection * bus_connection); + virtual ~MockSessionManager (); + + public: + + enum Action { None, LogoutNormal, LogoutQuiet, LogoutForce }; + Action last_action () { return my_last_action; } + + private: + + GnomeSessionManager * my_skeleton; + Action my_last_action; + + static gboolean handle_logout (GnomeSessionManager *, + GDBusMethodInvocation *, + guint, + gpointer); +}; + +#endif diff --git a/tests/backend-dbus/mock-upower.cc b/tests/backend-dbus/mock-upower.cc new file mode 100644 index 0000000..65757b3 --- /dev/null +++ b/tests/backend-dbus/mock-upower.cc @@ -0,0 +1,103 @@ +/* + * 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-upower.h" + + +gboolean +MockUPower :: handle_suspend (UPower * upower, + GDBusMethodInvocation * inv, + gpointer gself) +{ + static_cast(gself)->my_last_action = Suspend; + upower_complete_suspend (upower, inv); + return true; +} + +gboolean +MockUPower :: handle_hibernate (UPower * upower, + GDBusMethodInvocation * inv, + gpointer gself) +{ + static_cast(gself)->my_last_action = Hibernate; + upower_complete_hibernate (upower, inv); + return true; +} + +gboolean +MockUPower :: handle_suspend_allowed (UPower * upower, + GDBusMethodInvocation * inv, + gpointer gself) +{ + const bool allowed = static_cast(gself)->my_can_suspend; + upower_complete_suspend_allowed (upower, inv, allowed); + return true; +} + +gboolean +MockUPower :: handle_hibernate_allowed (UPower * upower, + GDBusMethodInvocation * inv, + gpointer gself) +{ + const bool allowed = static_cast(gself)->my_can_hibernate; + upower_complete_hibernate_allowed (upower, inv, allowed); + return true; +} + +/*** +**** +***/ + +namespace +{ + const char * const UPOWER_NAME = "org.freedesktop.UPower"; + const char * const UPOWER_PATH = "/org/freedesktop/UPower"; + +} + +MockUPower :: MockUPower (GMainLoop * loop, + GDBusConnection * bus_connection): + MockObject (loop, bus_connection, UPOWER_NAME, UPOWER_PATH), + my_skeleton (upower_skeleton_new ()), + my_can_suspend (true), + my_can_hibernate (true), + my_suspend_allowed (true), + my_hibernate_allowed (true), + my_last_action (None) +{ + //set_can_hibernate (false); + //set_can_suspend (true); + + g_signal_connect (my_skeleton, "handle-suspend", + G_CALLBACK(handle_suspend), this); + g_signal_connect (my_skeleton, "handle-suspend-allowed", + G_CALLBACK(handle_suspend_allowed), this); + + g_signal_connect (my_skeleton, "handle-hibernate", + G_CALLBACK(handle_hibernate), this); + g_signal_connect (my_skeleton, "handle-hibernate-allowed", + G_CALLBACK(handle_hibernate_allowed), this); + + set_skeleton (G_DBUS_INTERFACE_SKELETON(my_skeleton)); +} + +MockUPower :: ~MockUPower () +{ + g_clear_object (&my_skeleton); +} diff --git a/tests/backend-dbus/mock-upower.h b/tests/backend-dbus/mock-upower.h new file mode 100644 index 0000000..351d0f7 --- /dev/null +++ b/tests/backend-dbus/mock-upower.h @@ -0,0 +1,72 @@ +/* + * 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 . + */ + +#ifndef MOCK_UPOWER_H +#define MOCK_UPOWER_H + +#include "mock-object.h" // parent class +#include "backend-dbus/dbus-upower.h" // UPower + +class MockUPower: public MockObject +{ + public: + + MockUPower (GMainLoop * loop, + GDBusConnection * bus_connection); + virtual ~MockUPower (); + + void set_can_suspend (bool b) { upower_set_can_suspend (my_skeleton, b); } + void set_can_hibernate (bool b) { upower_set_can_hibernate (my_skeleton, b); } + + bool suspend_allowed () const { return my_suspend_allowed; } + bool hibernate_allowed () const { return my_suspend_allowed; } + bool can_suspend () const { return upower_get_can_suspend (my_skeleton); } + bool can_hibernate () const { return upower_get_can_hibernate (my_skeleton); } + + public: + + enum Action { None, Suspend, Hibernate }; + Action last_action () { return my_last_action; } + + private: + + UPower * my_skeleton; + bool my_can_suspend; + bool my_can_hibernate; + bool my_suspend_allowed; + bool my_hibernate_allowed; + Action my_last_action; + + static gboolean handle_suspend_allowed (UPower *, + GDBusMethodInvocation *, + gpointer); + static gboolean handle_suspend (UPower *, + GDBusMethodInvocation *, + gpointer); + + static gboolean handle_hibernate_allowed (UPower *, + GDBusMethodInvocation *, + gpointer); + static gboolean handle_hibernate (UPower *, + GDBusMethodInvocation *, + gpointer); + +}; + +#endif diff --git a/tests/backend-dbus/mock-user.cc b/tests/backend-dbus/mock-user.cc new file mode 100644 index 0000000..abf2e21 --- /dev/null +++ b/tests/backend-dbus/mock-user.cc @@ -0,0 +1,131 @@ +/* + * 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-user.h" + +/*** +**** +***/ + +const char * +MockUser :: username () const +{ + return accounts_user_get_user_name (my_skeleton); +} + +const char * +MockUser :: realname () const +{ + return accounts_user_get_real_name (my_skeleton); +} + +void +MockUser :: set_realname (const char * realname) +{ + accounts_user_set_real_name (my_skeleton, realname); + accounts_user_emit_changed (my_skeleton); +} + +guint +MockUser :: uid () const +{ + return accounts_user_get_uid (my_skeleton); +} + +guint64 +MockUser :: login_frequency () const +{ + return accounts_user_get_login_frequency (my_skeleton); +} + +#if 0 +bool +MockUser :: system_account() const +{ + return accounts_user_get_system_account (my_skeleton); +} +#endif + +void +MockUser :: set_system_account (gboolean b) +{ + accounts_user_set_system_account (my_skeleton, b); +} + +bool +MockUser :: is_guest () const +{ + // a guest will look like this: + // username:[guest-jjbEVV] realname:[Guest] system:[1] + return accounts_user_get_system_account (my_skeleton) + && !g_ascii_strcasecmp (accounts_user_get_real_name(my_skeleton), "Guest"); +} + +/*** +**** +***/ + +namespace +{ + const char * const DBUS_ACCOUNTS_NAME = "org.freedesktop.Accounts"; + + static guint next_uid = 1000; + + std::string path_for_uid (guint uid) + { + char * tmp; + std::string ret; + const char * const DBUS_ACCOUNTS_PATH = "/org/freedesktop/Accounts"; + tmp = g_strdup_printf ("%s/User%u", DBUS_ACCOUNTS_PATH, uid); + ret = tmp; + g_free (tmp); + return ret; + } +} + +guint +MockUser :: get_next_uid () +{ + return next_uid++; +} + + +MockUser :: MockUser (GMainLoop * loop, + GDBusConnection * bus_connection, + const char * userName, + const char * realName, + guint64 login_frequency, + guint uid_): + MockObject (loop, bus_connection, DBUS_ACCOUNTS_NAME, path_for_uid(uid_)), + my_skeleton (accounts_user_skeleton_new ()) +{ + accounts_user_set_uid (my_skeleton, uid_); + accounts_user_set_user_name (my_skeleton, userName); + accounts_user_set_real_name (my_skeleton, realName); + accounts_user_set_login_frequency (my_skeleton, login_frequency); + accounts_user_set_system_account (my_skeleton, false); + + set_skeleton (G_DBUS_INTERFACE_SKELETON(my_skeleton)); +} + +MockUser :: ~MockUser () +{ + g_signal_handlers_disconnect_by_data (my_skeleton, this); + g_clear_object (&my_skeleton); +} diff --git a/tests/backend-dbus/mock-user.h b/tests/backend-dbus/mock-user.h new file mode 100644 index 0000000..c1d3d0f --- /dev/null +++ b/tests/backend-dbus/mock-user.h @@ -0,0 +1,57 @@ +/* + * 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 . + */ + +#ifndef MOCK_USER_H +#define MOCK_USER_H + +#include "mock-object.h" // parent class +#include "backend-dbus/dbus-user.h" // AccountsUser + +class MockUser: public MockObject +{ + protected: + + static guint get_next_uid (); + + public: + + MockUser (GMainLoop * loop, + GDBusConnection * bus_connection, + const char * userName, + const char * realName, + guint64 login_frequency, + guint uid = get_next_uid()); + virtual ~MockUser (); + + const char * username () const; + const char * realname () const; + void set_realname (const char *); + guint uid () const; + guint64 login_frequency () const; + //bool system_account() const; + + bool is_guest() const; + void set_system_account (gboolean b); + + private: + + AccountsUser * my_skeleton; +}; + +#endif diff --git a/tests/backend-dbus/mock-webcredentials.cc b/tests/backend-dbus/mock-webcredentials.cc new file mode 100644 index 0000000..44fa8ac --- /dev/null +++ b/tests/backend-dbus/mock-webcredentials.cc @@ -0,0 +1,54 @@ +/* + * 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-webcredentials.h" + +namespace +{ + const char * const MY_NAME = "com.canonical.indicators.webcredentials"; + const char * const MY_PATH = "/com/canonical/indicators/webcredentials"; +} + +MockWebcredentials :: MockWebcredentials (GMainLoop * loop, + GDBusConnection * bus_connection): + MockObject (loop, bus_connection, MY_NAME, MY_PATH), + my_skeleton (webcredentials_skeleton_new ()) +{ + //set_can_hibernate (false); + //set_can_suspend (true); + +#if 0 + g_signal_connect (my_skeleton, "handle-suspend", + G_CALLBACK(handle_suspend), this); + g_signal_connect (my_skeleton, "handle-suspend-allowed", + G_CALLBACK(handle_suspend_allowed), this); + + g_signal_connect (my_skeleton, "handle-hibernate", + G_CALLBACK(handle_hibernate), this); + g_signal_connect (my_skeleton, "handle-hibernate-allowed", + G_CALLBACK(handle_hibernate_allowed), this); +#endif + + set_skeleton (G_DBUS_INTERFACE_SKELETON(my_skeleton)); +} + +MockWebcredentials :: ~MockWebcredentials () +{ + g_clear_object (&my_skeleton); +} diff --git a/tests/backend-dbus/mock-webcredentials.h b/tests/backend-dbus/mock-webcredentials.h new file mode 100644 index 0000000..212ca76 --- /dev/null +++ b/tests/backend-dbus/mock-webcredentials.h @@ -0,0 +1,42 @@ +/* + * 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 . + */ + +#ifndef MOCK_WEBCREDENTIALS_H +#define MOCK_WEBCREDENTIALS_H + +#include "mock-object.h" // parent class +#include "backend-dbus/dbus-webcredentials.h" // Webcredentials + +class MockWebcredentials: public MockObject +{ + public: + + MockWebcredentials (GMainLoop * loop, + GDBusConnection * bus_connection); + virtual ~MockWebcredentials (); + + bool has_error () const { return webcredentials_get_error_status (my_skeleton); } + bool set_error (bool b) const { webcredentials_set_error_status (my_skeleton, b); } + + private: + + Webcredentials * my_skeleton; +}; + +#endif diff --git a/tests/backend-dbus/test-actions.cc b/tests/backend-dbus/test-actions.cc new file mode 100644 index 0000000..f79c913 --- /dev/null +++ b/tests/backend-dbus/test-actions.cc @@ -0,0 +1,454 @@ +/* + * 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 "gtest-mock-dbus-fixture.h" + +#include "backend.h" +#include "backend-dbus/backend-dbus.h" + +/*** +**** +***/ + +class Actions: public GTestMockDBusFixture +{ + private: + + typedef GTestMockDBusFixture super; + + protected: + + GCancellable * cancellable; + IndicatorSessionActions * actions; + + virtual void SetUp () + { + super :: SetUp (); + + // init 'actions' + cancellable = g_cancellable_new (); + actions = 0; + backend_get (cancellable, &actions, NULL, NULL); + g_assert (actions != 0); + wait_msec (100); + } + + virtual void TearDown () + { + g_cancellable_cancel (cancellable); + g_clear_object (&cancellable); + g_clear_object (&actions); + + super :: TearDown (); + } +}; + +/*** +**** +***/ + +TEST_F (Actions, HelloWorld) +{ + ASSERT_TRUE (true); +} + +namespace +{ + static gboolean toggle_can_switch (gpointer settings) + { + const char * key = "disable-user-switching"; + gboolean b = g_settings_get_boolean (G_SETTINGS(settings), key); + g_settings_set_boolean (G_SETTINGS(settings), key, !b); + return G_SOURCE_REMOVE; + } +} + +TEST_F (Actions, CanSwitch) +{ + const char * schema_id = "org.gnome.desktop.lockdown"; + const char * settings_key = "disable-user-switching"; + GSettings * s = g_settings_new (schema_id); + + for (int i=0; i<3; ++i) + { + bool b; + gboolean b2; + + b = ck_seat->can_activate_sessions() && !g_settings_get_boolean (s, settings_key); + ASSERT_EQ (b, indicator_session_actions_can_switch (actions)); + g_object_get (actions, INDICATOR_SESSION_ACTIONS_PROP_CAN_SWITCH, &b2, NULL); + ASSERT_EQ (b, b2); + + g_idle_add (toggle_can_switch, s); + wait_for_signal (actions, "notify::" INDICATOR_SESSION_ACTIONS_PROP_CAN_SWITCH); + } + + g_object_unref (s); +} + +namespace +{ + static gboolean toggle_can_lock (gpointer settings) + { + const char * key = "disable-lock-screen"; + gboolean b = g_settings_get_boolean (G_SETTINGS(settings), key); + g_settings_set_boolean (G_SETTINGS(settings), key, !b); + return G_SOURCE_REMOVE; + } +} + +TEST_F (Actions, CanLock) +{ + const char * schema_id = "org.gnome.desktop.lockdown"; + const char * settings_key = "disable-lock-screen"; + GSettings * s = g_settings_new (schema_id); + + for (int i=0; i<3; ++i) + { + bool b; + gboolean b2; + + b = g_settings_get_boolean (s, settings_key); + ASSERT_EQ (b, !indicator_session_actions_can_lock (actions)); + g_object_get (actions, INDICATOR_SESSION_ACTIONS_PROP_CAN_LOCK, &b2, NULL); + ASSERT_EQ (b, !b2); + + g_idle_add (toggle_can_lock, s); + wait_for_signal (actions, "notify::" INDICATOR_SESSION_ACTIONS_PROP_CAN_LOCK); + } + + g_object_unref (s); +} + +namespace +{ + static gboolean toggle_can_logout (gpointer settings) + { + const char * key = "disable-log-out"; + gboolean b = g_settings_get_boolean (G_SETTINGS(settings), key); + g_settings_set_boolean (G_SETTINGS(settings), key, !b); + return G_SOURCE_REMOVE; + } +} + +TEST_F (Actions, CanLogout) +{ + const char * schema_id = "org.gnome.desktop.lockdown"; + const char * settings_key = "disable-log-out"; + GSettings * s = g_settings_new (schema_id); + + for (int i=0; i<3; ++i) + { + bool b; + gboolean b2; + + b = g_settings_get_boolean (s, settings_key); + ASSERT_EQ (b, !indicator_session_actions_can_logout (actions)); + g_object_get (actions, INDICATOR_SESSION_ACTIONS_PROP_CAN_LOGOUT, &b2, NULL); + ASSERT_EQ (b, !b2); + + g_idle_add (toggle_can_logout, s); + wait_for_signal (actions, "notify::" INDICATOR_SESSION_ACTIONS_PROP_CAN_LOGOUT); + } + + g_object_unref (s); +} + +TEST_F (Actions, CanSuspend) +{ + bool b; + bool can; + bool allowed; + gboolean b2; + + can = upower->can_suspend (); + allowed = upower->suspend_allowed (); + b = can && allowed; + + ASSERT_EQ (b, indicator_session_actions_can_suspend (actions)); + g_object_get (actions, INDICATOR_SESSION_ACTIONS_PROP_CAN_SUSPEND, &b2, NULL); + ASSERT_EQ (b, b2); + + for (int i=0; i<2; ++i) + { + can = !can; + b = can && allowed; + + upower->set_can_suspend (can); + wait_for_signal (actions, "notify::" INDICATOR_SESSION_ACTIONS_PROP_CAN_SUSPEND); + ASSERT_EQ (b, indicator_session_actions_can_suspend (actions)); + g_object_get (actions, INDICATOR_SESSION_ACTIONS_PROP_CAN_SUSPEND, &b2, NULL); + ASSERT_EQ (b, b2); + } +} + +TEST_F (Actions, CanHibernate) +{ + bool b; + bool can; + bool allowed; + gboolean b2; + + can = upower->can_hibernate (); + allowed = upower->hibernate_allowed (); + b = can && allowed; + + ASSERT_EQ (b, indicator_session_actions_can_hibernate (actions)); + g_object_get (actions, INDICATOR_SESSION_ACTIONS_PROP_CAN_HIBERNATE, &b2, NULL); + ASSERT_EQ (b, b2); + +#if 0 + for (int i=0; i<2; ++i) + { + b = !b; + upower->set_can_hibernate (b); + wait_for_signal (actions, "notify::" INDICATOR_SESSION_ACTIONS_PROP_CAN_HIBERNATE); + ASSERT_EQ (b, indicator_session_actions_can_hibernate (actions)); + g_object_get (actions, INDICATOR_SESSION_ACTIONS_PROP_CAN_HIBERNATE, &b2, NULL); + ASSERT_EQ (b, b2); + } +#endif +} + +TEST_F (Actions, Restart) +{ + ASSERT_EQ (MockConsoleKitManager::None, ck_manager->last_action()); + + // confirm that user is prompted + // and that no action is taken when the user cancels the dialog + indicator_session_actions_restart (actions); + wait_msec (50); + ASSERT_TRUE (end_session_dialog->is_open()); + end_session_dialog->cancel(); + wait_msec (50); + ASSERT_EQ (MockConsoleKitManager::None, ck_manager->last_action()); + + // confirm that user is prompted + // and that no action is taken when the user cancels the dialog + indicator_session_actions_restart (actions); + wait_msec (50); + ASSERT_TRUE (end_session_dialog->is_open ()); + end_session_dialog->confirm_reboot (); + wait_msec (100); + ASSERT_EQ (MockConsoleKitManager::Restart, ck_manager->last_action()); + + // confirm that we try to restart w/o prompting + // if the EndSessionDialog isn't available + delete end_session_dialog; + end_session_dialog = 0; + ck_manager->clear_last_action (); + ASSERT_EQ (MockConsoleKitManager::None, ck_manager->last_action()); + wait_msec (50); + indicator_session_actions_restart (actions); + wait_msec (50); + ASSERT_EQ (MockConsoleKitManager::Restart, ck_manager->last_action()); +} + +TEST_F (Actions, Shutdown) +{ + ASSERT_EQ (MockConsoleKitManager::None, ck_manager->last_action()); + + // confirm that user is prompted + // and that no action is taken when the user cancels the dialog + indicator_session_actions_shutdown (actions); + wait_msec (50); + ASSERT_TRUE (end_session_dialog->is_open()); + end_session_dialog->cancel(); + wait_msec (50); + ASSERT_EQ (MockConsoleKitManager::None, ck_manager->last_action()); + + // confirm that user is prompted + // and that no action is taken when the user cancels the dialog + indicator_session_actions_shutdown (actions); + wait_msec (50); + ASSERT_TRUE (end_session_dialog->is_open ()); + end_session_dialog->confirm_shutdown (); + wait_msec (100); + ASSERT_EQ (MockConsoleKitManager::Shutdown, ck_manager->last_action()); + + // confirm that we try to shutdown w/o prompting + // if the EndSessionDialog isn't available + delete end_session_dialog; + end_session_dialog = 0; + ck_manager->clear_last_action (); + wait_msec (50); + indicator_session_actions_shutdown (actions); + wait_msec (50); + ASSERT_EQ (MockConsoleKitManager::Shutdown, ck_manager->last_action()); +} + +TEST_F (Actions, Logout) +{ + ASSERT_EQ (MockConsoleKitManager::None, ck_manager->last_action()); + + // confirm that user is prompted + // and that no action is taken when the user cancels the dialog + indicator_session_actions_logout (actions); + wait_msec (50); + ASSERT_TRUE (end_session_dialog->is_open()); + end_session_dialog->cancel(); + wait_msec (50); + ASSERT_EQ (MockSessionManager::None, session_manager->last_action ()); + + // confirm that user is prompted + // and that no action is taken when the user cancels the dialog + indicator_session_actions_shutdown (actions); + wait_msec (50); + ASSERT_TRUE (end_session_dialog->is_open ()); + end_session_dialog->confirm_logout (); + wait_msec (100); + ASSERT_EQ (MockSessionManager::LogoutQuiet, session_manager->last_action ()); + + // confirm that we try to call SessionManager::LogoutNormal + // if the EndSessionDialog isn't available + delete end_session_dialog; + end_session_dialog = 0; + wait_msec (50); + indicator_session_actions_logout (actions); + wait_msec (50); + ASSERT_EQ (MockSessionManager::LogoutNormal, session_manager->last_action ()); +} + +TEST_F (Actions, Suspend) +{ + ASSERT_EQ (MockUPower::None, upower->last_action()); + indicator_session_actions_suspend (actions); + wait_msec (50); + ASSERT_EQ (MockUPower::Suspend, upower->last_action()); +} + +TEST_F (Actions, Hibernate) +{ + ASSERT_EQ (MockUPower::None, upower->last_action()); + indicator_session_actions_hibernate (actions); + wait_msec (50); + ASSERT_EQ (MockUPower::Hibernate, upower->last_action()); +} + +TEST_F (Actions, SwitchToScreensaver) +{ + ASSERT_EQ (MockScreenSaver::None, screen_saver->last_action()); + indicator_session_actions_switch_to_screensaver (actions); + wait_msec (50); + ASSERT_EQ (MockScreenSaver::Lock, screen_saver->last_action()); +} + +TEST_F (Actions, SwitchToGreeter) +{ + ASSERT_NE (MockDisplayManagerSeat::GREETER, dm_seat->last_action()); + indicator_session_actions_switch_to_greeter (actions); + wait_msec (50); + ASSERT_EQ (MockDisplayManagerSeat::GREETER, dm_seat->last_action()); +} + +TEST_F (Actions, SwitchToGuest) +{ + // allow guests + dm_seat->set_guest_allowed (true); + MockUser * guest_user; + MockConsoleKitSession * guest_ck_session; + + // set up a guest + guest_user = new MockUser (loop, conn, "guest-zzbEVV", "Guest", 10); + guest_user->set_system_account (true); + accounts->add_user (guest_user); + guest_ck_session = ck_seat->add_session_by_user (guest_user); + + // try to switch to guest + indicator_session_actions_switch_to_guest (actions); + wait_for_signal (ck_seat->skeleton(), "active-session-changed"); + ASSERT_EQ (guest_ck_session, ck_manager->current_session()); + wait_msec (50); +} + +TEST_F (Actions, SwitchToUsername) +{ + const char * const dr1_username = "whartnell"; + const char * const dr2_username = "ptroughton"; + MockUser * dr1_user; + MockUser * dr2_user; + MockConsoleKitSession * dr1_session; + MockConsoleKitSession * dr2_session; + + dr1_user = accounts->find_by_username (dr1_username); + dr1_session = ck_seat->add_session_by_user (dr1_user); + + dr2_user = accounts->find_by_username (dr2_username); + dr2_session = ck_seat->add_session_by_user (dr2_user); + + indicator_session_actions_switch_to_username (actions, dr1_username); + wait_for_signal (ck_seat->skeleton(), "active-session-changed"); + ASSERT_EQ (dr1_session, ck_manager->current_session()); + wait_msec (50); + + indicator_session_actions_switch_to_username (actions, dr2_username); + wait_for_signal (ck_seat->skeleton(), "active-session-changed"); + ASSERT_EQ (dr2_session, ck_manager->current_session()); + wait_msec (50); + + indicator_session_actions_switch_to_username (actions, dr1_username); + wait_for_signal (ck_seat->skeleton(), "active-session-changed"); + ASSERT_EQ (dr1_session, ck_manager->current_session()); + wait_msec (50); +} + +TEST_F (Actions, HasOnlineAccountError) +{ + bool b; + gboolean gb; + + b = webcredentials->has_error (); + ASSERT_EQ (b, indicator_session_actions_has_online_account_error (actions)); + g_object_get (actions, INDICATOR_SESSION_ACTIONS_PROP_HAS_ONLINE_ACCOUNT_ERROR, &gb, NULL); + ASSERT_EQ (b, gb); + + b = !b; + webcredentials->set_error (b); + wait_msec (50); + ASSERT_EQ (b, indicator_session_actions_has_online_account_error (actions)); + g_object_get (actions, INDICATOR_SESSION_ACTIONS_PROP_HAS_ONLINE_ACCOUNT_ERROR, &gb, NULL); + ASSERT_EQ (b, gb); + + b = !b; + webcredentials->set_error (b); + wait_msec (50); + ASSERT_EQ (b, indicator_session_actions_has_online_account_error (actions)); + g_object_get (actions, INDICATOR_SESSION_ACTIONS_PROP_HAS_ONLINE_ACCOUNT_ERROR, &gb, NULL); + ASSERT_EQ (b, gb); +} + +TEST_F (Actions, CanPrompt) +{ + gboolean b; + + ASSERT_TRUE (indicator_session_actions_can_prompt (actions)); + + delete end_session_dialog; + end_session_dialog = 0; + wait_msec (50); + ASSERT_FALSE (indicator_session_actions_can_prompt (actions)); + g_object_get (actions, INDICATOR_SESSION_ACTIONS_PROP_CAN_PROMPT, &b, NULL); + ASSERT_FALSE (b); + + end_session_dialog = new MockEndSessionDialog (loop, conn); + wait_msec (50); + ASSERT_TRUE (indicator_session_actions_can_prompt (actions)); + g_object_get (actions, INDICATOR_SESSION_ACTIONS_PROP_CAN_PROMPT, &b, NULL); + ASSERT_TRUE (b); +} diff --git a/tests/backend-dbus/test-guest.cc b/tests/backend-dbus/test-guest.cc new file mode 100644 index 0000000..db55dd1 --- /dev/null +++ b/tests/backend-dbus/test-guest.cc @@ -0,0 +1,192 @@ +/* + * 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 "gtest-mock-dbus-fixture.h" + +#include "backend.h" +#include "backend-dbus/backend-dbus.h" + +/*** +**** +***/ + +class Guest: public GTestMockDBusFixture +{ + private: + + typedef GTestMockDBusFixture super; + + protected: + + GCancellable * cancellable; + IndicatorSessionGuest * guest; + + virtual void SetUp () + { + super :: SetUp (); + + // get the guest-dbus + cancellable = g_cancellable_new (); + guest = 0; + backend_get (cancellable, NULL, NULL, &guest); + wait_msec (100); + + // test the default state + ASSERT_TRUE (guest != 0); + ASSERT_FALSE (indicator_session_guest_is_allowed (guest)); + ASSERT_FALSE (indicator_session_guest_is_logged_in (guest)); + ASSERT_FALSE (indicator_session_guest_is_active (guest)); + } + + virtual void TearDown () + { + g_cancellable_cancel (cancellable); + g_clear_object (&cancellable); + g_clear_object (&guest); + + super :: TearDown (); + } + + protected: + + void add_mock_guest (MockUser *& guest_user, + MockConsoleKitSession *& guest_session) + { + guest_user = new MockUser (loop, conn, "guest-jjbEVV", "Guest", 10); + guest_user->set_system_account (true); + accounts->add_user (guest_user); + guest_session = new MockConsoleKitSession (loop, conn); + guest_session->set_user (guest_user); + ck_seat->add_session (guest_session); + } +}; + +/** + * Confirms that the Fixture's SetUp() and TearDown() work + */ +TEST_F (Guest, HelloWorld) +{ + ASSERT_TRUE (true); +} + +/** + * Toggle in the DM whether or not guests are allowed. + * Confirm that "guest" reflects the changes. + */ +TEST_F (Guest, Allowed) +{ + dm_seat->set_guest_allowed (true); + wait_for_signal (guest, "notify::guest-is-allowed"); + ASSERT_TRUE (indicator_session_guest_is_allowed (guest)); + ASSERT_FALSE (indicator_session_guest_is_logged_in (guest)); + ASSERT_FALSE (indicator_session_guest_is_active (guest)); + + dm_seat->set_guest_allowed (false); + wait_for_signal (guest, "notify::guest-is-allowed"); + ASSERT_FALSE (indicator_session_guest_is_allowed (guest)); + ASSERT_FALSE (indicator_session_guest_is_logged_in (guest)); + ASSERT_FALSE (indicator_session_guest_is_active (guest)); +} + +/** + * Have a guest user log in & out. + * Confirm that "guest" reflects the changes. + */ +TEST_F (Guest, Login) +{ + gboolean b; + + dm_seat->set_guest_allowed (true); + + // Log a Guest in + // And confirm that guest's is_login changes to true + MockUser * guest_user; + MockConsoleKitSession * guest_session; + add_mock_guest (guest_user, guest_session); + wait_for_signal (guest, "notify::guest-is-logged-in"); + ASSERT_TRUE (indicator_session_guest_is_allowed (guest)); + ASSERT_TRUE (indicator_session_guest_is_logged_in (guest)); + g_object_get (guest, INDICATOR_SESSION_GUEST_PROPERTY_LOGGED_IN, &b,NULL); + ASSERT_TRUE (b); + ASSERT_FALSE (indicator_session_guest_is_active (guest)); + + // Log the Guest User out + // and confirm that guest's is_login changes to false + ck_seat->remove_session (guest_session); + accounts->remove_user (guest_user); + delete guest_user; + delete guest_session; + wait_for_signal (guest, "notify::guest-is-logged-in"); + ASSERT_TRUE (indicator_session_guest_is_allowed (guest)); + ASSERT_FALSE (indicator_session_guest_is_logged_in (guest)); + g_object_get (guest, INDICATOR_SESSION_GUEST_PROPERTY_LOGGED_IN, &b,NULL); + ASSERT_FALSE (b); + ASSERT_FALSE (indicator_session_guest_is_active (guest)); +} + +/** + * Activate a Guest session, then activate a different session. + * Confirm that "guest" reflects the changes. + */ +TEST_F (Guest, Active) +{ + gboolean b; + + dm_seat->set_guest_allowed (true); + MockUser * guest_user; + MockConsoleKitSession * guest_session; + add_mock_guest (guest_user, guest_session); + + // Activate the guest session + // and confirm that guest's is_active changes to true + ck_seat->activate_session (guest_session); + wait_for_signal (guest, "notify::guest-is-active-session"); + ASSERT_TRUE (indicator_session_guest_is_allowed (guest)); + ASSERT_TRUE (indicator_session_guest_is_logged_in (guest)); + ASSERT_TRUE (indicator_session_guest_is_active (guest)); + g_object_get (guest, INDICATOR_SESSION_GUEST_PROPERTY_ACTIVE, &b,NULL); + ASSERT_TRUE (b); + + // Activate a non-guest session + // and confirm that guest's is_active changes to false + ck_seat->activate_session (ck_session); + wait_for_signal (guest, "notify::guest-is-active-session"); + ASSERT_TRUE (indicator_session_guest_is_allowed (guest)); + ASSERT_TRUE (indicator_session_guest_is_logged_in (guest)); + ASSERT_FALSE (indicator_session_guest_is_active (guest)); + g_object_get (guest, INDICATOR_SESSION_GUEST_PROPERTY_ACTIVE, &b,NULL); + ASSERT_FALSE (b); +} + +/** + * Activate a guest session using the "guest" API. + * Confirm that the guest session gets activated on the bus. + */ +TEST_F (Guest, Activate) +{ + dm_seat->set_guest_allowed (true); + MockUser * guest_user; + MockConsoleKitSession * guest_session; + add_mock_guest (guest_user, guest_session); + + indicator_session_guest_switch_to_guest (guest); + wait_for_signal (ck_seat->skeleton(), "active-session-changed"); + ASSERT_EQ (guest_session, ck_manager->current_session()); + wait_msec (50); +} diff --git a/tests/backend-dbus/test-users.cc b/tests/backend-dbus/test-users.cc new file mode 100644 index 0000000..bd0547d --- /dev/null +++ b/tests/backend-dbus/test-users.cc @@ -0,0 +1,377 @@ +/* + * 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 "gtest-mock-dbus-fixture.h" + +#include "backend.h" +#include "backend-dbus/backend-dbus.h" + +/*** +**** +***/ + +class Users: public GTestMockDBusFixture +{ + private: + + typedef GTestMockDBusFixture super; + + protected: + + GCancellable * cancellable; + IndicatorSessionUsers * users; + + virtual void SetUp () + { + super :: SetUp (); + + init_event_keys (0); + + // init 'users' + cancellable = g_cancellable_new (); + users = 0; + backend_get (cancellable, NULL, &users, NULL); + g_assert (users != 0); + wait_msec (100); + } + + virtual void TearDown () + { + g_cancellable_cancel (cancellable); + g_clear_object (&cancellable); + g_clear_object (&users); + + super :: TearDown (); + } + + protected: + + void compare_user (const IndicatorSessionUser * isu, + MockUser * mu, + bool is_logged_in, + bool is_current_user) + { + ASSERT_TRUE (isu != 0); + ASSERT_TRUE (mu != 0); + + ASSERT_EQ (mu->uid(), isu->uid); + ASSERT_EQ (mu->login_frequency(), isu->login_frequency); + ASSERT_STREQ (mu->username(), isu->user_name); + ASSERT_STREQ (mu->realname(), isu->real_name); + ASSERT_EQ (is_logged_in, isu->is_logged_in); + ASSERT_EQ (is_current_user, isu->is_current_user); + // FIXME: test icon file? + } + + void compare_user (const std::string & key, + MockUser * mu, + bool is_logged_in, + bool is_current_user) + { + IndicatorSessionUser * isu; + isu = indicator_session_users_get_user (users, key.c_str()); + compare_user (isu, mu, is_logged_in, is_current_user); + indicator_session_user_free (isu); + } + + private: + + void init_event_keys (size_t n) + { + expected_event_count = n; + event_keys.clear(); + } + + static gboolean + wait_for_signals__timeout (gpointer name) + { + g_error ("%s: timed out waiting for signal '%s'", G_STRLOC, (char*)name); + return G_SOURCE_REMOVE; + } + + static void + wait_for_signals__event (IndicatorSessionUser * u G_GNUC_UNUSED, + const char * key, + gpointer gself) + { + Users * self = static_cast(gself); + + self->event_keys.push_back (key); + + if (self->event_keys.size() == self->expected_event_count) + g_main_loop_quit (self->loop); + } + + protected: + + std::vector event_keys; + size_t expected_event_count; + + void wait_for_signals (gpointer o, const gchar * name, size_t n) + { + const int timeout_seconds = 5; // arbitrary + + init_event_keys (n); + + guint handler_id = g_signal_connect (o, name, + G_CALLBACK(wait_for_signals__event), + this); + gulong timeout_id = g_timeout_add_seconds (timeout_seconds, + wait_for_signals__timeout, + (gpointer)name); + g_main_loop_run (loop); + g_source_remove (timeout_id); + g_signal_handler_disconnect (o, handler_id); + } +}; + +/*** +**** +***/ + +/** + * Confirm that the fixture's SetUp() and TearDown() work + */ +TEST_F (Users, HelloWorld) +{ + ASSERT_TRUE (true); +} + + +/** + * Confirm that 'users' can get the cached users from our Mock Accounts + */ +TEST_F (Users, InitialUsers) +{ + const guint logged_in_uid = ck_session->user()->uid(); + GStrv keys = indicator_session_users_get_keys (users); + + ASSERT_EQ (12, g_strv_length (keys)); + + for (int i=0; keys && keys[i]; ++i) + { + MockUser * mu = accounts->find_by_path (keys[i]); + const bool is_logged_in = mu->uid() == logged_in_uid; + const bool is_current_user = mu->uid() == logged_in_uid; + compare_user (keys[i], mu, is_logged_in, is_current_user); + } + + g_strfreev (keys); +} + +/** + * Confirm that 'users' can tell when a new user is added + */ +TEST_F (Users, UserAdded) +{ + MockUser * mu; + + mu = new MockUser (loop, conn, "pcushing", "Peter Cushing", 2); + accounts->add_user (mu); + ASSERT_EQ (0, event_keys.size()); + wait_for_signals (users, INDICATOR_SESSION_USERS_SIGNAL_USER_ADDED, 1); + ASSERT_EQ (1, event_keys.size()); + ASSERT_STREQ (mu->path(), event_keys[0].c_str()); + + compare_user (event_keys[0], mu, false, false); +} + +/** + * Confirm that 'users' can tell when a user is removed + */ +TEST_F (Users, UserRemoved) +{ + MockUser * mu; + + mu = accounts->find_by_username ("pdavison"); + accounts->remove_user (mu); + ASSERT_EQ (0, event_keys.size()); + wait_for_signals (users, INDICATOR_SESSION_USERS_SIGNAL_USER_REMOVED, 1); + ASSERT_EQ (1, event_keys.size()); + ASSERT_STREQ (mu->path(), event_keys[0].c_str()); + + GStrv keys = indicator_session_users_get_keys (users); + ASSERT_EQ (11, g_strv_length (keys)); + g_strfreev (keys); + + ASSERT_TRUE (indicator_session_users_get_user (users, mu->path()) == NULL); + + delete mu; +} + +/** + * Confirm that 'users' notices when a user's real name changes + */ +TEST_F (Users, RealnameChanged) +{ + MockUser * mu; + + mu = accounts->find_by_username ("pdavison"); + const char * const realname = "Peter M. G. Moffett"; + mu->set_realname (realname); + ASSERT_NE (mu->realname(), realname); + ASSERT_STREQ (mu->realname(), realname); + wait_for_signals (users, INDICATOR_SESSION_USERS_SIGNAL_USER_CHANGED, 1); + ASSERT_EQ (1, event_keys.size()); + ASSERT_STREQ (mu->path(), event_keys[0].c_str()); + compare_user (mu->path(), mu, false, false); +} + +/** + * Confirm that 'users' notices when users log in and out + */ +TEST_F (Users, LogInLogOut) +{ + // The fist doctor logs in. + // Confirm that 'users' notices. + MockUser * mu = accounts->find_by_username ("whartnell"); + compare_user (mu->path(), mu, false, false); + MockConsoleKitSession * session = ck_seat->add_session_by_user (mu); + wait_for_signals (users, INDICATOR_SESSION_USERS_SIGNAL_USER_CHANGED, 1); + ASSERT_STREQ (mu->path(), event_keys[0].c_str()); + compare_user (mu->path(), mu, true, false); + + // The fist doctor logs out. + // Confirm that 'users' notices. + ck_seat->remove_session (session); + wait_for_signals (users, INDICATOR_SESSION_USERS_SIGNAL_USER_CHANGED, 1); + ASSERT_EQ (1, event_keys.size()); + ASSERT_STREQ (mu->path(), event_keys[0].c_str()); + compare_user (event_keys[0], mu, false, false); + + delete session; +} + +/** + * Confirm that 'users' notices when the active session changes + */ +TEST_F (Users, ActivateSession) +{ + // The fist doctor logs in. + // Confirm that 'users' notices. + MockUser * mu = accounts->find_by_username ("whartnell"); + compare_user (mu->path(), mu, false, false); + MockConsoleKitSession * session = ck_seat->add_session_by_user (mu); + wait_for_signals (users, INDICATOR_SESSION_USERS_SIGNAL_USER_CHANGED, 1); + ASSERT_STREQ (mu->path(), event_keys[0].c_str()); + compare_user (mu->path(), mu, true, false); + + // activate the first doctor's session. + // confirm that 'users' sees he's active and that ck_session isn't. + // this should come in the form of two 'user-changed' events + ck_seat->activate_session (session); + wait_for_signals (users, INDICATOR_SESSION_USERS_SIGNAL_USER_CHANGED, 2); + ASSERT_EQ (2, event_keys.size()); + compare_user (event_keys[0], ck_session->user(), true, false); + compare_user (event_keys[1], mu, true, true); + + // switch back to the previous + // confirm that 'users' sees it's active and the first doctor's session isn't + // this should come in the form of two 'user-changed' events + ck_seat->activate_session (ck_session); + wait_for_signals (users, INDICATOR_SESSION_USERS_SIGNAL_USER_CHANGED, 2); + ASSERT_EQ (2, event_keys.size()); + compare_user (event_keys[0], mu, true, false); + compare_user (event_keys[1], ck_session->user(), true, true); +} + +/** + * Confirm that we can change the active session via users' API. + * This is nearly the same as ActivateSession but uses users' API + */ +TEST_F (Users, ActivateUser) +{ + // The fist doctor logs in. + // Confirm that 'users' notices. + MockUser * mu = accounts->find_by_username ("whartnell"); + compare_user (mu->path(), mu, false, false); + MockConsoleKitSession * session = ck_seat->add_session_by_user (mu); + wait_for_signals (users, INDICATOR_SESSION_USERS_SIGNAL_USER_CHANGED, 1); + ASSERT_STREQ (mu->path(), event_keys[0].c_str()); + compare_user (mu->path(), mu, true, false); + + // activate the first doctor's session. + // confirm that 'users' sees he's active and that ck_session isn't. + // this should come in the form of two 'user-changed' events + indicator_session_users_activate_user (users, mu->path()); + ck_seat->activate_session (session); + wait_for_signals (users, INDICATOR_SESSION_USERS_SIGNAL_USER_CHANGED, 2); + ASSERT_EQ (2, event_keys.size()); + compare_user (event_keys[0], ck_session->user(), true, false); + compare_user (event_keys[1], mu, true, true); + + // switch back to the previous + // confirm that 'users' sees it's active and the first doctor's session isn't + // this should come in the form of two 'user-changed' events + indicator_session_users_activate_user (users, ck_session->user()->path()); + ck_seat->activate_session (ck_session); + wait_for_signals (users, INDICATOR_SESSION_USERS_SIGNAL_USER_CHANGED, 2); + ASSERT_EQ (2, event_keys.size()); + compare_user (event_keys[0], mu, true, false); + compare_user (event_keys[1], ck_session->user(), true, true); +} + +/** + * Confirm that adding a Guest doesn't show up in the users list + */ +TEST_F (Users, UnwantedGuest) +{ + GStrv keys; + + keys = indicator_session_users_get_keys (users); + const size_t n = g_strv_length (keys); + g_strfreev (keys); + + MockUser * mu = new MockUser (loop, conn, "guest-jjbEVV", "Guest", 1); + mu->set_system_account (true); + accounts->add_user (mu); + wait_msec (50); + + keys = indicator_session_users_get_keys (users); + ASSERT_EQ (n, g_strv_length (keys)); + g_strfreev (keys); +} + + +/** + * Confirm that we can detect live sessions + */ +TEST_F (Users, LiveSession) +{ + gboolean b; + + // not initially a live session + ASSERT_FALSE (indicator_session_users_is_live_session (users)); + g_object_get (users, INDICATOR_SESSION_USERS_PROP_IS_LIVE_SESSION, &b, NULL); + ASSERT_FALSE (b); + + // now add the criteria for a live session + MockUser * live_user = new MockUser (loop, conn, "ubuntu", "Ubuntu", 1, 999); + live_user->set_system_account (true); + accounts->add_user (live_user); + MockConsoleKitSession * session = ck_seat->add_session_by_user (live_user); + wait_msec (100); + ck_seat->activate_session (session); + wait_for_signal (users, "notify::"INDICATOR_SESSION_USERS_PROP_IS_LIVE_SESSION); + + // confirm the backend thinks it's a live session + ASSERT_TRUE (indicator_session_users_is_live_session (users)); + g_object_get (users, INDICATOR_SESSION_USERS_PROP_IS_LIVE_SESSION, &b, NULL); + ASSERT_TRUE (b); +} -- cgit v1.2.3