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/CMakeLists.txt | 53 ++ tests/Makefile.am | 51 -- tests/Makefile.am.strings | 38 - 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 ++++++++++ tests/backend-mock-actions.c | 229 ++++++ tests/backend-mock-actions.h | 61 ++ tests/backend-mock-guest.c | 129 ++++ tests/backend-mock-guest.h | 61 ++ tests/backend-mock-users.c | 189 +++++ tests/backend-mock-users.h | 70 ++ tests/backend-mock.c | 44 ++ tests/backend-mock.h | 38 + ...nical.indicator.session.backendmock.gschema.xml | 41 + tests/com.canonical.indicator.session.gschema.xml | 32 + tests/gtest-dbus-fixture.h | 134 ++++ tests/indicator-session.service.in | 4 +- tests/test-service.cc | 831 ++++++++++++++++++--- 45 files changed, 5220 insertions(+), 187 deletions(-) create mode 100644 tests/CMakeLists.txt delete mode 100644 tests/Makefile.am delete mode 100644 tests/Makefile.am.strings 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 create mode 100644 tests/backend-mock-actions.c create mode 100644 tests/backend-mock-actions.h create mode 100644 tests/backend-mock-guest.c create mode 100644 tests/backend-mock-guest.h create mode 100644 tests/backend-mock-users.c create mode 100644 tests/backend-mock-users.h create mode 100644 tests/backend-mock.c create mode 100644 tests/backend-mock.h create mode 100644 tests/com.canonical.indicator.session.backendmock.gschema.xml create mode 100644 tests/com.canonical.indicator.session.gschema.xml create mode 100644 tests/gtest-dbus-fixture.h (limited to 'tests') diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt new file mode 100644 index 0000000..45ab69f --- /dev/null +++ b/tests/CMakeLists.txt @@ -0,0 +1,53 @@ +# build the necessary schemas +set_directory_properties (PROPERTIES + ADDITIONAL_MAKE_CLEAN_FILES gschemas.compiled) +set_source_files_properties (gschemas.compiled GENERATED) + +# GSettings: +# compile the schemas our tests use (indicator-session's, lockdown, media-keys) +# into a gschemas.compiled file in this directory, and help the tests to find +# that file by setting -DSCHEMA_DIR +set (SCHEMA_DIR ${CMAKE_CURRENT_BINARY_DIR}) +add_definitions(-DSCHEMA_DIR="${SCHEMA_DIR}") +execute_process (COMMAND ${PKG_CONFIG_EXECUTABLE} gio-2.0 --variable glib_compile_schemas + OUTPUT_VARIABLE COMPILE_SCHEMA_EXECUTABLE + OUTPUT_STRIP_TRAILING_WHITESPACE) +add_custom_command (OUTPUT gschemas.compiled + DEPENDS ${CMAKE_BINARY_DIR}/data/com.canonical.indicator.session.gschema.xml + ${CMAKE_SOURCE_DIR}/tests/com.canonical.indicator.session.backendmock.gschema.xml + ${CMAKE_SOURCE_DIR}/tests/org.gnome.desktop.lockdown.gschema.xml + ${CMAKE_SOURCE_DIR}/tests/org.gnome.settings-daemon.plugins.media-keys.gschema.xml + COMMAND cp -f ${CMAKE_BINARY_DIR}/data/*gschema.xml ${SCHEMA_DIR} + COMMAND cp -f ${CMAKE_SOURCE_DIR}/tests/*gschema.xml ${SCHEMA_DIR} + COMMAND ${COMPILE_SCHEMA_EXECUTABLE} ${SCHEMA_DIR}) + +# DBus Activation +configure_file (indicator-session.service.in indicator-session.service) +add_definitions(-DINDICATOR_SERVICE_DIR="${CMAKE_CURRENT_BINARY_DIR}") + +# look for hearder in our src dir, and also in the directories where we autogenerate files... +include_directories (${CMAKE_SOURCE_DIR}/src) +include_directories (${CMAKE_CURRENT_BINARY_DIR} ${SERVICE_INCLUDE_DIRS}) + +# backendmock +add_library (backendmock STATIC + backend-mock-actions.c + backend-mock-actions.h + backend-mock.c + backend-mock.h + backend-mock-guest.c + backend-mock-guest.h + backend-mock-users.c + backend-mock-users.h) +set_target_properties (backendmock PROPERTIES COMPILE_FLAGS " ${CC_WARNING_ARGS}") + +# test-service +add_executable (test-service + test-service.cc + gschemas.compiled) +set_target_properties (test-service PROPERTIES COMPILE_FLAGS " ${CC_WARNING_ARGS}") +add_test (test-service test-service) +add_dependencies (test-service libindicatorsessionservice backendmock) +target_link_libraries (test-service libindicatorsessionservice backendmock gtest ${SERVICE_LIBRARIES} ${GTEST_LIBS}) + +add_subdirectory (backend-dbus) diff --git a/tests/Makefile.am b/tests/Makefile.am deleted file mode 100644 index 071f684..0000000 --- a/tests/Makefile.am +++ /dev/null @@ -1,51 +0,0 @@ -TESTS = -CLEANFILES = -BUILT_SOURCES = -check_PROGRAMS = - -integrationcheckdir = . -integrationcheck_PROGRAMS = - -### -### -### - -# stock UMB tests on user-visible strings -include $(srcdir)/Makefile.am.strings - -check_LIBRARIES = libgtest.a -nodist_libgtest_a_SOURCES = \ - $(GTEST_SOURCE)/gtest-all.cc \ - $(GTEST_SOURCE)/gtest_main.cc - -AM_CPPFLAGS = $(GTEST_CPPFLAGS) -I${top_srcdir}/src -Wall -Werror -AM_CXXFLAGS = $(GTEST_CXXFLAGS) - -### -### -### - -BUILT_SOURCES += gschemas.compiled -CLEANFILES += gschemas.compiled -gschemas.compiled: Makefile - $(AM_V_at) cp -f $(top_builddir)/data/*gschema.xml . - $(AM_V_GEN) $(GLIB_COMPILE_SCHEMAS) --targetdir=. . - - -integrationcheck: - ./test-service - -integrationcheck_PROGRAMS += test-service -test_service_SOURCES = test-service.cc -test_service_LDADD = \ - $(TEST_SERVICE_LIBS) \ - $(XORG_GTEST_LDFLAGS) \ - libgtest.a -test_service_CPPFLAGS = \ - -DSCHEMA_DIR="\"$(top_builddir)/tests/\"" \ - -DINDICATOR_SERVICE_DIR="\"$(abs_builddir)\"" \ - -DINDICATOR_SERVICE_PATH="\"$(top_builddir)/src/indicator-session-service\"" \ - $(TEST_SERVICE_CFLAGS) \ - $(AM_CPPFLAGS) - - diff --git a/tests/Makefile.am.strings b/tests/Makefile.am.strings deleted file mode 100644 index 26a23a8..0000000 --- a/tests/Makefile.am.strings +++ /dev/null @@ -1,38 +0,0 @@ -TESTS += \ - test-ellipsis \ - test-space-ellipsis \ - test-ascii-quotes - -##### -# Tests for there being proper ellipsis instead of three periods in a row -##### -test-ellipsis: $(top_srcdir)/po - @echo "#!/bin/bash" > $@ - @echo "(cd $(top_srcdir)/po && make $(GETTEXT_PACKAGE).pot)" >> $@ - @echo "grep -c -e \"^msgid.*\.\.\.\\\"\" $(top_srcdir)/po/$(GETTEXT_PACKAGE).pot > /dev/null && echo \"Ellipsis found in user visible strings\" >&2 && exit 1" >> $@ - @echo "exit 0" >> $@ - @chmod +x $@ - -##### -# Tests for there being a space before an ellipsis -##### -test-space-ellipsis: $(top_srcdir)/po - @echo "#!/bin/bash" > $@ - @echo "(cd $(top_srcdir)/po && make $(GETTEXT_PACKAGE).pot)" >> $@ - @echo "grep -c -e \"^msgid.* …\\\"\" $(top_srcdir)/po/$(GETTEXT_PACKAGE).pot > /dev/null && echo \"Space before ellipsis found in user visible strings\" >&2 && exit 1" >> $@ - @echo "exit 0" >> $@ - @chmod +x $@ - -##### -# Tests for ASCII quote types -##### -test-ascii-quotes: $(top_srcdir)/po - @echo "#!/bin/bash" > $@ - @echo "(cd $(top_srcdir)/po && make $(GETTEXT_PACKAGE).pot)" >> $@ - @echo "grep -c -e \"^msgid \\\".*'.*\\\"\" $(top_srcdir)/po/$(GETTEXT_PACKAGE).pot > /dev/null && echo \"ASCII apostrophy found in user visible strings\" >&2 && exit 1" >> $@ - @echo "grep -c -e \"^msgid \\\".*\\\".*\\\"\" $(top_srcdir)/po/$(GETTEXT_PACKAGE).pot > /dev/null && echo \"ASCII quote found in user visible strings\" >&2 && exit 1" >> $@ - @echo "grep -c -e \"^msgid \\\".*\\\`.*\\\"\" $(top_srcdir)/po/$(GETTEXT_PACKAGE).pot > /dev/null && echo \"ASCII backtick found in user visible strings\" >&2 && exit 1" >> $@ - @echo "exit 0" >> $@ - @chmod +x $@ - -CLEANFILES += $(TESTS) 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); +} diff --git a/tests/backend-mock-actions.c b/tests/backend-mock-actions.c new file mode 100644 index 0000000..121c7ba --- /dev/null +++ b/tests/backend-mock-actions.c @@ -0,0 +1,229 @@ +/* + * 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 "backend-mock.h" +#include "backend-mock-actions.h" + +G_DEFINE_TYPE (IndicatorSessionActionsMock, + indicator_session_actions_mock, + INDICATOR_TYPE_SESSION_ACTIONS) + +/*** +**** Virtual Functions +***/ + +static gboolean +my_can_lock (IndicatorSessionActions * self G_GNUC_UNUSED) +{ + return g_settings_get_boolean (mock_settings, "can-lock"); +} + +static gboolean +my_can_logout (IndicatorSessionActions * self G_GNUC_UNUSED) +{ + return g_settings_get_boolean (mock_settings, "can-logout"); +} + +static gboolean +my_can_switch (IndicatorSessionActions * self G_GNUC_UNUSED) +{ + return g_settings_get_boolean (mock_settings, "can-switch-sessions"); +} + +static gboolean +my_can_suspend (IndicatorSessionActions * self G_GNUC_UNUSED) +{ + return g_settings_get_boolean (mock_settings, "can-suspend"); +} + +static gboolean +my_can_hibernate (IndicatorSessionActions * self G_GNUC_UNUSED) +{ + return g_settings_get_boolean (mock_settings, "can-hibernate"); +} + +static void +my_logout (IndicatorSessionActions * self G_GNUC_UNUSED) +{ + g_settings_set_string (mock_settings, "last-command", "logout"); +} + +static void +my_suspend (IndicatorSessionActions * self G_GNUC_UNUSED) +{ + g_settings_set_string (mock_settings, "last-command", "suspend"); +} + +static void +my_hibernate (IndicatorSessionActions * self G_GNUC_UNUSED) +{ + g_settings_set_string (mock_settings, "last-command", "hibernate"); +} + +static void +my_restart (IndicatorSessionActions * self G_GNUC_UNUSED) +{ + g_settings_set_string (mock_settings, "last-command", "restart"); +} + +static void +my_shutdown (IndicatorSessionActions * self G_GNUC_UNUSED) +{ + g_settings_set_string (mock_settings, "last-command", "shutdown"); +} + +static void +my_switch_to_screensaver (IndicatorSessionActions * self G_GNUC_UNUSED) +{ + g_settings_set_string (mock_settings, "last-command", "switch-to-screensaver"); +} + +static void +my_switch_to_greeter (IndicatorSessionActions * self G_GNUC_UNUSED) +{ + g_settings_set_string (mock_settings, "last-command", "switch-to-greeter"); +} + +static void +my_switch_to_guest (IndicatorSessionActions * self G_GNUC_UNUSED) +{ + g_settings_set_string (mock_settings, "last-command", "switch-to-guest"); +} + +static void +my_switch_to_username (IndicatorSessionActions * self G_GNUC_UNUSED, + const char * username) +{ + gchar * str = g_strdup_printf ("switch-to-user::%s", username); + g_settings_set_string (mock_settings, "last-command", str); +} + +static void +my_help (IndicatorSessionActions * self G_GNUC_UNUSED) +{ + g_settings_set_string (mock_settings, "last-command", "help"); +} + +static void +my_about (IndicatorSessionActions * self G_GNUC_UNUSED) +{ + g_settings_set_string (mock_settings, "last-command", "about"); +} + +static void +my_settings (IndicatorSessionActions * self G_GNUC_UNUSED) +{ + g_settings_set_string (mock_settings, "last-command", "settings"); +} + +static gboolean +my_can_prompt (IndicatorSessionActions * self G_GNUC_UNUSED) +{ + return g_settings_get_boolean (mock_settings, "can-prompt"); +} + +static gboolean +my_has_online_account_error (IndicatorSessionActions * self G_GNUC_UNUSED) +{ + return g_settings_get_boolean (mock_settings, "has-online-account-error"); +} + +static void +my_dispose (GObject * o) +{ + G_OBJECT_CLASS (indicator_session_actions_mock_parent_class)->dispose (o); +} + +static void +my_finalize (GObject * o) +{ + G_OBJECT_CLASS (indicator_session_actions_mock_parent_class)->finalize (o); +} + +/*** +**** GObject Boilerplate +***/ + +static void +/* cppcheck-suppress unusedFunction */ +indicator_session_actions_mock_class_init (IndicatorSessionActionsMockClass * klass) +{ + GObjectClass * object_class; + IndicatorSessionActionsClass * actions_class; + + object_class = G_OBJECT_CLASS (klass); + object_class->dispose = my_dispose; + object_class->finalize = my_finalize; + + actions_class = INDICATOR_SESSION_ACTIONS_CLASS (klass); + actions_class->can_lock = my_can_lock; + actions_class->can_logout = my_can_logout; + actions_class->can_switch = my_can_switch; + actions_class->can_suspend = my_can_suspend; + actions_class->can_hibernate = my_can_hibernate; + actions_class->can_prompt = my_can_prompt; + actions_class->has_online_account_error = my_has_online_account_error; + actions_class->logout = my_logout; + actions_class->suspend = my_suspend; + actions_class->hibernate = my_hibernate; + actions_class->restart = my_restart; + actions_class->shutdown = my_shutdown; + actions_class->settings = my_settings; + actions_class->help = my_help; + actions_class->about = my_about; + actions_class->switch_to_screensaver = my_switch_to_screensaver; + actions_class->switch_to_greeter = my_switch_to_greeter; + actions_class->switch_to_guest = my_switch_to_guest; + actions_class->switch_to_username = my_switch_to_username; +} + +static void +/* cppcheck-suppress unusedFunction */ +indicator_session_actions_mock_init (IndicatorSessionActionsMock * self) +{ + g_signal_connect_swapped (mock_settings, "changed::can-lock", + G_CALLBACK(indicator_session_actions_notify_can_lock), self); + g_signal_connect_swapped (mock_settings, "changed::can-logout", + G_CALLBACK(indicator_session_actions_notify_can_logout), self); + g_signal_connect_swapped (mock_settings, "changed::can-switch-sessions", + G_CALLBACK(indicator_session_actions_notify_can_switch), self); + g_signal_connect_swapped (mock_settings, "changed::can-suspend", + G_CALLBACK(indicator_session_actions_notify_can_suspend), self); + g_signal_connect_swapped (mock_settings, "changed::can-hibernate", + G_CALLBACK(indicator_session_actions_notify_can_hibernate), self); + g_signal_connect_swapped (mock_settings, "changed::can-prompt", + G_CALLBACK(indicator_session_actions_notify_can_prompt), self); + g_signal_connect_swapped (mock_settings, "changed::has-online-account-error", + G_CALLBACK(indicator_session_actions_notify_has_online_account_error), self); +} + +/*** +**** Public +***/ + +IndicatorSessionActions * +indicator_session_actions_mock_new (void) +{ + gpointer o = g_object_new (INDICATOR_TYPE_SESSION_ACTIONS_MOCK, NULL); + + return INDICATOR_SESSION_ACTIONS (o); +} diff --git a/tests/backend-mock-actions.h b/tests/backend-mock-actions.h new file mode 100644 index 0000000..bd9ed47 --- /dev/null +++ b/tests/backend-mock-actions.h @@ -0,0 +1,61 @@ +/* + * 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 __INDICATOR_SESSION_ACTIONS_MOCK_H__ +#define __INDICATOR_SESSION_ACTIONS_MOCK_H__ + +#include +#include + +#include "actions.h" /* parent class */ + +G_BEGIN_DECLS + +#define INDICATOR_TYPE_SESSION_ACTIONS_MOCK (indicator_session_actions_mock_get_type()) +#define INDICATOR_SESSION_ACTIONS_MOCK(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), INDICATOR_TYPE_SESSION_ACTIONS_MOCK, IndicatorSessionActionsMock)) +#define INDICATOR_SESSION_ACTIONS_MOCK_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), INDICATOR_TYPE_SESSION_ACTIONS_MOCK, IndicatorSessionActionsMockClass)) +#define INDICATOR_IS_SESSION_ACTIONS_MOCK(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), INDICATOR_TYPE_SESSION_ACTIONS_MOCK)) + +typedef struct _IndicatorSessionActionsMock IndicatorSessionActionsMock; +typedef struct _IndicatorSessionActionsMockPriv IndicatorSessionActionsMockPriv; +typedef struct _IndicatorSessionActionsMockClass IndicatorSessionActionsMockClass; + +/** + * An implementation of IndicatorSessionActions that gets its user information + * from org.freedesktop.ConsoleKit and org.freedesktop.Accounts over DBus. + */ +struct _IndicatorSessionActionsMock +{ + /*< private >*/ + IndicatorSessionActions parent; + IndicatorSessionActionsMockPriv * priv; +}; + +struct _IndicatorSessionActionsMockClass +{ + IndicatorSessionActionsClass parent_class; +}; + +GType indicator_session_actions_mock_get_type (void); + +IndicatorSessionActions * indicator_session_actions_mock_new (void); + +G_END_DECLS + +#endif /* __INDICATOR_SESSION_ACTIONS_MOCK_H__ */ diff --git a/tests/backend-mock-guest.c b/tests/backend-mock-guest.c new file mode 100644 index 0000000..0428783 --- /dev/null +++ b/tests/backend-mock-guest.c @@ -0,0 +1,129 @@ +/* + * 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 "backend-mock-guest.h" + +struct _IndicatorSessionGuestMockPriv +{ + gboolean guest_is_active; + gboolean guest_is_logged_in; + gboolean guest_is_allowed; +}; + +typedef IndicatorSessionGuestMockPriv priv_t; + +G_DEFINE_TYPE (IndicatorSessionGuestMock, + indicator_session_guest_mock, + INDICATOR_TYPE_SESSION_GUEST) + +/*** +**** Virtual Functions +***/ + +static void +my_dispose (GObject * o) +{ + G_OBJECT_CLASS (indicator_session_guest_mock_parent_class)->dispose (o); +} + +static void +my_finalize (GObject * o) +{ + G_OBJECT_CLASS (indicator_session_guest_mock_parent_class)->finalize (o); +} + +static gboolean +my_is_allowed (IndicatorSessionGuest * self) +{ + return INDICATOR_SESSION_GUEST_MOCK(self)->priv->guest_is_allowed; +} + +static gboolean +my_is_logged_in (IndicatorSessionGuest * self) +{ + g_return_val_if_fail (INDICATOR_IS_SESSION_GUEST_MOCK(self), FALSE); + + return INDICATOR_SESSION_GUEST_MOCK(self)->priv->guest_is_logged_in; +} + +static gboolean +my_is_active (IndicatorSessionGuest * self) +{ + return INDICATOR_SESSION_GUEST_MOCK(self)->priv->guest_is_active; +} + +static void +my_switch_to_guest (IndicatorSessionGuest * self) +{ + g_message ("%s %s FIXME", G_STRLOC, G_STRFUNC); +} + +/*** +**** GObject Boilerplate +***/ + +static void +/* cppcheck-suppress unusedFunction */ +indicator_session_guest_mock_class_init (IndicatorSessionGuestMockClass * klass) +{ + GObjectClass * object_class; + IndicatorSessionGuestClass * guest_class; + + object_class = G_OBJECT_CLASS (klass); + object_class->dispose = my_dispose; + object_class->finalize = my_finalize; + + guest_class = INDICATOR_SESSION_GUEST_CLASS (klass); + guest_class->is_allowed = my_is_allowed; + guest_class->is_logged_in = my_is_logged_in; + guest_class->is_active = my_is_active; + guest_class->switch_to_guest = my_switch_to_guest; + + g_type_class_add_private (klass, sizeof (IndicatorSessionGuestMockPriv)); +} + +static void +/* cppcheck-suppress unusedFunction */ +indicator_session_guest_mock_init (IndicatorSessionGuestMock * self) +{ + priv_t * p; + + p = G_TYPE_INSTANCE_GET_PRIVATE (self, + INDICATOR_TYPE_SESSION_GUEST_MOCK, + IndicatorSessionGuestMockPriv); + self->priv = p; + + p->guest_is_allowed = TRUE; + p->guest_is_active = FALSE; + p->guest_is_logged_in = FALSE; +} + +/*** +**** Public +***/ + +IndicatorSessionGuest * +indicator_session_guest_mock_new (void) +{ + gpointer o = g_object_new (INDICATOR_TYPE_SESSION_GUEST_MOCK, NULL); + + return INDICATOR_SESSION_GUEST (o); +} diff --git a/tests/backend-mock-guest.h b/tests/backend-mock-guest.h new file mode 100644 index 0000000..db6ce86 --- /dev/null +++ b/tests/backend-mock-guest.h @@ -0,0 +1,61 @@ +/* + * 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 __GUEST_MOCK_H__ +#define __GUEST_MOCK_H__ + +#include +#include + +#include "guest.h" /* parent class */ + +G_BEGIN_DECLS + +#define INDICATOR_TYPE_SESSION_GUEST_MOCK (indicator_session_guest_mock_get_type()) +#define INDICATOR_SESSION_GUEST_MOCK(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), INDICATOR_TYPE_SESSION_GUEST_MOCK, IndicatorSessionGuestMock)) +#define INDICATOR_SESSION_GUEST_MOCK_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), INDICATOR_TYPE_SESSION_GUEST_MOCK, IndicatorSessionGuestMockClass)) +#define INDICATOR_IS_SESSION_GUEST_MOCK(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), INDICATOR_TYPE_SESSION_GUEST_MOCK)) + +typedef struct _IndicatorSessionGuestMock IndicatorSessionGuestMock; +typedef struct _IndicatorSessionGuestMockPriv IndicatorSessionGuestMockPriv; +typedef struct _IndicatorSessionGuestMockClass IndicatorSessionGuestMockClass; + +/** + * An implementation of IndicatorSessionGuest that gets its user information + * from org.freedesktop.ConsoleKit and org.freedesktop.Accounts over DBus. + */ +struct _IndicatorSessionGuestMock +{ + /*< private >*/ + IndicatorSessionGuest parent; + IndicatorSessionGuestMockPriv * priv; +}; + +struct _IndicatorSessionGuestMockClass +{ + IndicatorSessionGuestClass parent_class; +}; + +GType indicator_session_guest_mock_get_type (void); + +IndicatorSessionGuest * indicator_session_guest_mock_new (void); + +G_END_DECLS + +#endif diff --git a/tests/backend-mock-users.c b/tests/backend-mock-users.c new file mode 100644 index 0000000..d9ab5a8 --- /dev/null +++ b/tests/backend-mock-users.c @@ -0,0 +1,189 @@ +/* + * 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 "backend-mock.h" +#include "backend-mock-users.h" + +struct _IndicatorSessionUsersMockPriv +{ + GHashTable * users; +}; + +typedef IndicatorSessionUsersMockPriv priv_t; + +G_DEFINE_TYPE (IndicatorSessionUsersMock, + indicator_session_users_mock, + INDICATOR_TYPE_SESSION_USERS) + +/*** +**** +***/ + +static void +my_dispose (GObject * o) +{ + G_OBJECT_CLASS (indicator_session_users_mock_parent_class)->dispose (o); +} + +static void +my_finalize (GObject * o) +{ + priv_t * p = INDICATOR_SESSION_USERS_MOCK (o)->priv; + + g_hash_table_destroy (p->users); + + G_OBJECT_CLASS (indicator_session_users_mock_parent_class)->finalize (o); +} + +static gboolean +my_is_live_session (IndicatorSessionUsers * users G_GNUC_UNUSED) +{ + return g_settings_get_boolean (mock_settings, "is-live-session"); +} + +static void +my_activate_user (IndicatorSessionUsers * users, const char * key) +{ + g_message ("%s %s users %p key %s FIXME", G_STRLOC, G_STRFUNC, (void*)users, key); +} + +static GStrv +my_get_keys (IndicatorSessionUsers * users) +{ + int i; + priv_t * p; + gchar ** keys; + GHashTableIter iter; + gpointer key; + + g_return_val_if_fail (INDICATOR_IS_SESSION_USERS_MOCK(users), NULL); + p = INDICATOR_SESSION_USERS_MOCK (users)->priv; + + i = 0; + keys = g_new (gchar*, g_hash_table_size(p->users)+1); + g_hash_table_iter_init (&iter, p->users); + while (g_hash_table_iter_next (&iter, &key, NULL)) + keys[i++] = g_strdup (key); + keys[i] = NULL; + + return keys; +} + +static IndicatorSessionUser * +my_get_user (IndicatorSessionUsers * self, const gchar * key) +{ + priv_t * p; + const IndicatorSessionUser * src; + IndicatorSessionUser * ret = NULL; + + g_return_val_if_fail (INDICATOR_IS_SESSION_USERS_MOCK(self), NULL); + p = INDICATOR_SESSION_USERS_MOCK (self)->priv; + + if ((src = g_hash_table_lookup (p->users, key))) + { + ret = g_new0 (IndicatorSessionUser, 1); + ret->is_current_user = src->is_current_user; + ret->is_logged_in = src->is_logged_in; + ret->uid = src->uid; + ret->login_frequency = src->login_frequency; + ret->user_name = g_strdup (src->user_name); + ret->real_name = g_strdup (src->real_name); + ret->icon_file = g_strdup (src->icon_file); + } + + return ret; +} + +static void +/* cppcheck-suppress unusedFunction */ +indicator_session_users_mock_class_init (IndicatorSessionUsersMockClass * klass) +{ + GObjectClass * object_class; + IndicatorSessionUsersClass * users_class; + + object_class = G_OBJECT_CLASS (klass); + object_class->dispose = my_dispose; + object_class->finalize = my_finalize; + + users_class = INDICATOR_SESSION_USERS_CLASS (klass); + users_class->is_live_session = my_is_live_session; + users_class->get_keys = my_get_keys; + users_class->get_user = my_get_user; + users_class->activate_user = my_activate_user; + + g_type_class_add_private (klass, sizeof (IndicatorSessionUsersMockPriv)); +} + +static void +/* cppcheck-suppress unusedFunction */ +indicator_session_users_mock_init (IndicatorSessionUsersMock * self) +{ + priv_t * p; + + p = G_TYPE_INSTANCE_GET_PRIVATE (self, + INDICATOR_TYPE_SESSION_USERS_MOCK, + IndicatorSessionUsersMockPriv); + self->priv = p; + + p->users = g_hash_table_new_full (g_str_hash, + g_str_equal, + g_free, + (GDestroyNotify)indicator_session_user_free); + + g_signal_connect_swapped (mock_settings, "changed::is-live-session", + G_CALLBACK(indicator_session_users_notify_is_live_session), self); +} + +/*** +**** Public +***/ + +IndicatorSessionUsers * +indicator_session_users_mock_new (void) +{ + gpointer o = g_object_new (INDICATOR_TYPE_SESSION_USERS_MOCK, NULL); + + return INDICATOR_SESSION_USERS (o); +} + + +void +indicator_session_users_mock_add_user (IndicatorSessionUsersMock * self, + const char * key, + IndicatorSessionUser * user) +{ + g_return_if_fail (INDICATOR_IS_SESSION_USERS_MOCK (self)); + g_return_if_fail (key && *key); + g_return_if_fail (user != NULL); + + g_hash_table_insert (self->priv->users, g_strdup(key), user); + indicator_session_users_added (INDICATOR_SESSION_USERS (self), key); +} + +void +indicator_session_users_mock_remove_user (IndicatorSessionUsersMock * self, + const char * key) +{ + g_return_if_fail (INDICATOR_IS_SESSION_USERS_MOCK (self)); + g_return_if_fail (key && *key); + + g_hash_table_remove (self->priv->users, key); + indicator_session_users_removed (INDICATOR_SESSION_USERS (self), key); +} + diff --git a/tests/backend-mock-users.h b/tests/backend-mock-users.h new file mode 100644 index 0000000..c7e11d3 --- /dev/null +++ b/tests/backend-mock-users.h @@ -0,0 +1,70 @@ +/* + * 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 __USERS_MOCK_H__ +#define __USERS_MOCK_H__ + +#include +#include + +#include "users.h" /* parent class */ + +G_BEGIN_DECLS + +#define INDICATOR_TYPE_SESSION_USERS_MOCK (indicator_session_users_mock_get_type()) +#define INDICATOR_SESSION_USERS_MOCK(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), INDICATOR_TYPE_SESSION_USERS_MOCK, IndicatorSessionUsersMock)) +#define INDICATOR_SESSION_USERS_MOCK_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), INDICATOR_TYPE_SESSION_USERS_MOCK, IndicatorSessionUsersMockClass)) +#define INDICATOR_IS_SESSION_USERS_MOCK(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), INDICATOR_TYPE_SESSION_USERS_MOCK)) + +typedef struct _IndicatorSessionUsersMock IndicatorSessionUsersMock; +typedef struct _IndicatorSessionUsersMockPriv IndicatorSessionUsersMockPriv; +typedef struct _IndicatorSessionUsersMockClass IndicatorSessionUsersMockClass; + +/** + * An implementation of IndicatorSessionUsers that gets its user information + * from org.freedesktop.ConsoleKit and org.freedesktop.Accounts over DBus. + */ +struct _IndicatorSessionUsersMock +{ + /*< private >*/ + IndicatorSessionUsers parent; + IndicatorSessionUsersMockPriv * priv; +}; + +struct _IndicatorSessionUsersMockClass +{ + IndicatorSessionUsersClass parent_class; +}; + +GType indicator_session_users_mock_get_type (void); + +IndicatorSessionUsers * indicator_session_users_mock_new (void); + +void indicator_session_users_mock_add_user (IndicatorSessionUsersMock * self, + const char * key, + IndicatorSessionUser * user); + +void indicator_session_users_mock_remove_user (IndicatorSessionUsersMock * self, + const char * key); + + + +G_END_DECLS + +#endif diff --git a/tests/backend-mock.c b/tests/backend-mock.c new file mode 100644 index 0000000..48d7de4 --- /dev/null +++ b/tests/backend-mock.c @@ -0,0 +1,44 @@ +/* + * 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 "backend-mock.h" +#include "backend-mock-actions.h" +#include "backend-mock-guest.h" +#include "backend-mock-users.h" + +GSettings * mock_settings = NULL; +IndicatorSessionActions * mock_actions = NULL; +IndicatorSessionUsers * mock_users = NULL; +IndicatorSessionGuest * mock_guest = NULL; + +void +backend_get (GCancellable * cancellable G_GNUC_UNUSED, + IndicatorSessionActions ** setme_actions, + IndicatorSessionUsers ** setme_users, + IndicatorSessionGuest ** setme_guest) +{ + if (setme_actions != NULL) + *setme_actions = g_object_ref (mock_actions); + + if (setme_users != NULL) + *setme_users = g_object_ref (mock_users); + + if (setme_guest != NULL) + *setme_guest = g_object_ref (mock_guest); +} diff --git a/tests/backend-mock.h b/tests/backend-mock.h new file mode 100644 index 0000000..d80a185 --- /dev/null +++ b/tests/backend-mock.h @@ -0,0 +1,38 @@ +/* + * 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 __BACKEND_MOCK_H__ +#define __BACKEND_MOCK_H__ + +#include /* GCancellable */ + +#include "actions.h" +#include "guest.h" +#include "users.h" + +G_BEGIN_DECLS + +extern GSettings * mock_settings; +extern IndicatorSessionActions * mock_actions; +extern IndicatorSessionUsers * mock_users; +extern IndicatorSessionGuest * mock_guest; + +G_END_DECLS + +#endif diff --git a/tests/com.canonical.indicator.session.backendmock.gschema.xml b/tests/com.canonical.indicator.session.backendmock.gschema.xml new file mode 100644 index 0000000..34479df --- /dev/null +++ b/tests/com.canonical.indicator.session.backendmock.gschema.xml @@ -0,0 +1,41 @@ + + + + + '' + The last command activated + + + false + Has online account error + + + true + Is hibernation allowed? + + + true + Is suspending allowed? + + + true + Is logging out allowed? + + + true + Is locking the session allowed? + + + true + Is switching sessions allowed? + + + true + Do we have a way of prompting for confirmation? + + + false + Is this a session running on a live CD? + + + diff --git a/tests/com.canonical.indicator.session.gschema.xml b/tests/com.canonical.indicator.session.gschema.xml new file mode 100644 index 0000000..76b2be3 --- /dev/null +++ b/tests/com.canonical.indicator.session.gschema.xml @@ -0,0 +1,32 @@ + + + + + false + Suppress the dialog to confirm logout, restart and shutdown action + Whether or not to show confirmation dialogs for logout, restart and shutdown actions. + + + false + Remove the Log Out item from the session menu + Makes it so that the logout button doesn’t show in the session menu. + + + false + Remove the Restart item from the session menu + Makes it so that the restart button doesn’t show in the session menu. + + + false + Remove the shutdown item from the session menu + Makes it so that the shutdown button doesn’t show in the session menu. + + + false + Determine the visibility of the User's real name on the panel + Allow for the Removal of the users name from the panel + + + + + \ No newline at end of file diff --git a/tests/gtest-dbus-fixture.h b/tests/gtest-dbus-fixture.h new file mode 100644 index 0000000..e6cd9c7 --- /dev/null +++ b/tests/gtest-dbus-fixture.h @@ -0,0 +1,134 @@ +/* + * 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 + +/*** +**** +***/ + +class GTestDBusFixture : public ::testing::Test +{ + private: + + static void + on_bus_opened (GObject * o G_GNUC_UNUSED, GAsyncResult * res, gpointer gself) + { + GTestDBusFixture * self = static_cast(gself); + + GError * err = 0; + self->conn = g_bus_get_finish (res, &err); + g_assert_no_error (err); + + g_main_loop_quit (self->loop); + } + + static void + on_bus_closed (GObject * o G_GNUC_UNUSED, GAsyncResult * res, gpointer gself) + { + GTestDBusFixture * self = static_cast(gself); + + GError * err = 0; + g_dbus_connection_close_finish (self->conn, res, &err); + g_assert_no_error (err); + + g_main_loop_quit (self->loop); + } + + static gboolean + wait_for_signal__timeout (gpointer name) + { + g_error ("%s: timed out waiting for signal '%s'", G_STRLOC, (char*)name); + return G_SOURCE_REMOVE; + } + + protected: + + virtual void SetUp () + { + conn = 0; + test_dbus = 0; + loop = 0; + + g_setenv ("GSETTINGS_SCHEMA_DIR", SCHEMA_DIR, TRUE); + g_setenv ("GSETTINGS_BACKEND", "memory", TRUE); + g_debug ("SCHEMA_DIR is %s", SCHEMA_DIR); + + // pull up a test dbus + loop = g_main_loop_new (NULL, FALSE); + test_dbus = g_test_dbus_new (G_TEST_DBUS_NONE); + g_test_dbus_add_service_dir (test_dbus, INDICATOR_SERVICE_DIR); + g_debug ("INDICATOR_SERVICE_DIR is %s", INDICATOR_SERVICE_DIR); + g_test_dbus_up (test_dbus); + const char * address; + address = g_test_dbus_get_bus_address (test_dbus); + g_setenv ("DBUS_SYSTEM_BUS_ADDRESS", address, TRUE); + g_debug ("test_dbus's address is %s", address); + + // wait for the GDBusConnection before returning + g_bus_get (G_BUS_TYPE_SYSTEM, NULL, on_bus_opened, this); + g_main_loop_run (loop); + } + + virtual void TearDown() + { + // close the bus connection + g_dbus_connection_close (conn, NULL, on_bus_closed, this); + g_main_loop_run (loop); + g_clear_object (&conn); + + // tear down the test dbus + g_test_dbus_down (test_dbus); + g_clear_object (&test_dbus); + + g_clear_pointer (&loop, g_main_loop_unref); + } + + /* convenience func to loop while waiting for a GObject's signal */ + void wait_for_signal (gpointer o, const gchar * signal) + { + const int timeout_seconds = 5; // arbitrary + + // wait for the signal or for timeout, whichever comes first + guint handler_id = g_signal_connect_swapped (o, signal, + G_CALLBACK(g_main_loop_quit), + loop); + gulong timeout_id = g_timeout_add_seconds (timeout_seconds, + wait_for_signal__timeout, + loop); + g_main_loop_run (loop); + g_source_remove (timeout_id); + g_signal_handler_disconnect (o, handler_id); + } + + /* convenience func to loop for N msec */ + void wait_msec (int msec) + { + guint id = g_timeout_add (msec, (GSourceFunc)g_main_loop_quit, loop); + g_main_loop_run (loop); + g_source_remove (id); + } + + GMainLoop * loop; + GTestDBus * test_dbus; + GDBusConnection * conn; +}; diff --git a/tests/indicator-session.service.in b/tests/indicator-session.service.in index 80aab0d..fb3798a 100644 --- a/tests/indicator-session.service.in +++ b/tests/indicator-session.service.in @@ -1,3 +1,3 @@ [D-BUS Service] -Name=com.canonical.indicator.session -Exec=@abs_top_builddir@/src/indicator-session-service +Name=com.canonical.indicator.session-test +Exec=${CMAKE_BINARY_DIR}/src/indicator-session-service --mock diff --git a/tests/test-service.cc b/tests/test-service.cc index b1ca5bc..058fc3d 100644 --- a/tests/test-service.cc +++ b/tests/test-service.cc @@ -17,70 +17,329 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . */ -#include -#include -#include - -#include - -#include "shared-names.h" +#include "gtest-dbus-fixture.h" +#include "service.h" +#include "backend-mock.h" +#include "backend-mock-users.h" +#include "backend-mock-guest.h" +#include "backend-mock-actions.h" /*** **** ***/ -#define INDICATOR_SERVICE_OBJECT_PATH "/org/ayatana/indicator/service" -#define INDICATOR_SERVICE_INTERFACE_NAME "org.ayatana.indicator.service" +#if 0 +namespace +{ + void + dump_menu_model (GMenuModel * model, int depth) + { + GString * indent = g_string_new_len (" ", (depth*4)); + const int n = g_menu_model_get_n_items (model); + g_message ("%s depth[%d] menu model[%p] has %d items", indent->str, depth, (void*)model, n); + + for (int i=0; istr, depth, (void*)model, i, name, str); + g_free (str); + g_variant_unref (attribute_value); + } + g_clear_object (&attribute_iter); + + GMenuLinkIter * link_iter = g_menu_model_iterate_item_links (model, i); + while (g_menu_link_iter_get_next (link_iter, &name, &link_value)) + { + g_message ("%s depth[%d] menu model[%p] item[%d] attribute key[%s] model[%p]", indent->str, depth, (void*)model, i, name, (void*)link_value); + dump_menu_model (link_value, depth+1); + g_object_unref (link_value); + } + g_clear_object (&link_iter); + } + g_string_free (indent, TRUE); + } +} +#endif -class ClientTest : public ::testing::Test + +/* cppcheck-suppress noConstructor */ +class ServiceTest: public GTestDBusFixture { + typedef GTestDBusFixture super; + + enum { TIME_LIMIT_SEC = 10 }; + + private: + + static void on_name_appeared (GDBusConnection * connection G_GNUC_UNUSED, + const gchar * name G_GNUC_UNUSED, + const gchar * name_owner G_GNUC_UNUSED, + gpointer gself) + { + g_main_loop_quit (static_cast(gself)->loop); + } + + GSList * menu_references; + + bool any_item_changed; + + static void on_items_changed (GMenuModel * model G_GNUC_UNUSED, + gint position G_GNUC_UNUSED, + gint removed G_GNUC_UNUSED, + gint added G_GNUC_UNUSED, + gpointer any_item_changed) + { + *((gboolean*)any_item_changed) = true; + } + + protected: + + void activate_subtree (GMenuModel * model) + { + // query the GDBusMenuModel for information to activate it + int n = g_menu_model_get_n_items (model); + if (!n) + { + // give the model a moment to populate its info + wait_msec (100); + n = g_menu_model_get_n_items (model); + } + + // keep a ref so that it stays activated + menu_references = g_slist_prepend (menu_references, g_object_ref(model)); + + g_signal_connect (model, "items-changed", G_CALLBACK(on_items_changed), &any_item_changed); + + // recurse + for (int i=0; i= TIME_LIMIT_SEC; + } + + void wait_for_has_action (const char * name) + { + while (!g_action_group_has_action (G_ACTION_GROUP(action_group), name) && !times_up()) + wait_msec (50); + + ASSERT_FALSE (times_up()); + ASSERT_TRUE (g_action_group_has_action (G_ACTION_GROUP(action_group), name)); + } - virtual void SetUp() + void wait_for_menu_resync (void) { - test_dbus = NULL; - session_bus = NULL; - main_loop = NULL; + any_item_changed = false; + while (!times_up() && !any_item_changed) + wait_msec (50); + sync_menu (); + } + + protected: + + void check_last_command_is (const char * expected) + { + char * str = g_settings_get_string (mock_settings, "last-command"); + ASSERT_STREQ (expected, str); + g_free (str); + } - static bool first_run = true; - if (first_run) + void test_simple_action (const char * action_name) + { + wait_for_has_action (action_name); + + g_action_group_activate_action (G_ACTION_GROUP (action_group), action_name, NULL); + wait_for_signal (mock_settings, "changed::last-command"); + check_last_command_is (action_name); + } + + protected: + + bool find_menu_item_for_action (const char * action_key, GMenuModel ** setme, int * item_index) + { + bool success = false; + + for (GSList * l=menu_references; !success && (l!=NULL); l=l->next) { - g_setenv ("INDICATOR_SERVICE_SHUTDOWN_TIMEOUT", "1000", TRUE); - g_unsetenv ("INDICATOR_ALLOW_NO_WATCHERS"); - g_unsetenv ("INDICATOR_SERVICE_REPLACE_MODE"); - g_setenv ("GSETTINGS_SCHEMA_DIR", SCHEMA_DIR, TRUE); - g_setenv ("GSETTINGS_BACKEND", "memory", TRUE); - first_run = false; + GMenuModel * mm = G_MENU_MODEL (l->data); + const int n = g_menu_model_get_n_items (mm); + + for (int i=0; !success && iref_count); + if (expected_a11y != NULL) + ASSERT_STREQ (expected_a11y, a11y); + + // the session menu is always visible... + ASSERT_TRUE (visible); + + g_variant_unref (variant); } - // undo SetUp - virtual void TearDown() + void check_label (const char * expected_label, GMenuModel * model, int pos) { - g_clear_object (&session_bus); - g_debug (G_STRLOC" tearing down the bus"); - g_test_dbus_down (test_dbus); - g_clear_object (&test_dbus); - g_clear_pointer (&main_loop, g_main_loop_unref); + char * label = NULL; + ASSERT_TRUE (g_menu_model_get_item_attribute (model, pos, G_MENU_ATTRIBUTE_LABEL, "s", &label)); + ASSERT_STREQ (expected_label, label); + g_free (label); } }; @@ -88,58 +347,438 @@ class ClientTest : public ::testing::Test **** ***/ +#if 0 +TEST_F (ServiceTest, HelloWorld) +{ + ASSERT_TRUE (true); +} +#endif + +TEST_F (ServiceTest, About) +{ + test_simple_action ("about"); +} + +TEST_F (ServiceTest, Help) +{ + test_simple_action ("help"); +} + +TEST_F (ServiceTest, Hibernate) +{ + test_simple_action ("hibernate"); +} + +TEST_F (ServiceTest, Settings) +{ + test_simple_action ("settings"); +} + +TEST_F (ServiceTest, Logout) +{ + test_simple_action ("logout"); +} + +TEST_F (ServiceTest, Shutdown) +{ + test_simple_action ("shutdown"); +} + +TEST_F (ServiceTest, Restart) +{ + test_simple_action ("restart"); +} + +TEST_F (ServiceTest, SwitchToScreensaver) +{ + test_simple_action ("switch-to-screensaver"); +} + +TEST_F (ServiceTest, SwitchToGuest) +{ + test_simple_action ("switch-to-guest"); +} + +TEST_F (ServiceTest, SwitchToGreeter) +{ + test_simple_action ("switch-to-greeter"); +} + +TEST_F (ServiceTest, Suspend) +{ + test_simple_action ("suspend"); +} + +#if 0 +namespace +{ + gboolean + find_menu_item_for_action (GMenuModel * top, const char * action_key, GMenuModel ** setme, int * item_index) + { + gboolean success = FALSE; + const int n = g_menu_model_get_n_items (top); + + for (int i=0; !success && imessage); - g_clear_error (&error); - } - - name = NULL; - g_variant_get (result, "(&s)", &name); - ASSERT_STREQ (g_get_real_name(), name); - g_clear_pointer (&result, g_variant_unref); - - // call IndicatorService's Shutdown() method for a clean exit - result = g_dbus_connection_call_sync (session_bus, - INDICATOR_SESSION_DBUS_NAME, - "/org/ayatana/indicator/service", - "org.ayatana.indicator.service", - "Shutdown", NULL, - NULL, - G_DBUS_CALL_FLAGS_NONE, - -1, NULL, NULL); - g_clear_pointer (&result, g_variant_unref); +TEST_F (ServiceTest, DefaultMenuItems) +{ + ASSERT_TRUE (find_menu_item_for_action ("indicator.about", NULL, NULL)); + ASSERT_TRUE (find_menu_item_for_action ("indicator.help", NULL, NULL)); + ASSERT_TRUE (find_menu_item_for_action ("indicator.settings", NULL, NULL)); + ASSERT_TRUE (find_menu_item_for_action ("indicator.switch-to-greeter", NULL, NULL)); + ASSERT_TRUE (find_menu_item_for_action ("indicator.switch-to-guest", NULL, NULL)); + ASSERT_TRUE (find_menu_item_for_action ("indicator.logout", NULL, NULL)); + ASSERT_TRUE (find_menu_item_for_action ("indicator.suspend", NULL, NULL)); + ASSERT_TRUE (find_menu_item_for_action ("indicator.hibernate", NULL, NULL)); + ASSERT_TRUE (find_menu_item_for_action ("indicator.restart", NULL, NULL)); + ASSERT_TRUE (find_menu_item_for_action ("indicator.shutdown", NULL, NULL)); +} + +TEST_F (ServiceTest, OnlineAccountError) +{ + bool err; + int pos = -1; + GMenuModel * model = 0; + const char * const error_key = "has-online-account-error"; + + // check the initial default header state + check_header ("", "system-devices-panel", "System"); + + // check that the menuitems' existence matches the error flag + err = g_settings_get_boolean (mock_settings, error_key); + ASSERT_FALSE (err); + ASSERT_EQ (err, find_menu_item_for_action ("indicator.online-accounts", &model, &pos)); + g_clear_object (&model); + + // now toggle the error flag + err = !err; + g_settings_set_boolean (mock_settings, error_key, err); + + // wait for the _header action and error menuitem to update + wait_for_menu_resync (); + + // check that the menuitems' existence matches the error flag + ASSERT_TRUE (g_settings_get_boolean (mock_settings, error_key)); + ASSERT_TRUE (find_menu_item_for_action ("indicator.online-accounts", &model, &pos)); + g_clear_object (&model); + + // check that the header's icon and a11y adjusted to the error state + check_header ("", "system-devices-panel-alert", "System (Attention Required)"); + + // cleanup + g_settings_reset (mock_settings, error_key); +} + +namespace +{ + gboolean set_live_session_to_true (gpointer unused G_GNUC_UNUSED) + { + const char * const live_session_key = "is-live-session"; + g_settings_set_boolean (mock_settings, live_session_key, true); + return G_SOURCE_REMOVE; + } +} + +TEST_F (ServiceTest, LiveSession) +{ + gboolean b; + const char * const live_session_key = "is-live-session"; + + // default BackendMock is not a live session + ASSERT_FALSE (g_settings_get_boolean (mock_settings, live_session_key)); + g_object_get (mock_users, INDICATOR_SESSION_USERS_PROP_IS_LIVE_SESSION, &b, NULL); + ASSERT_FALSE (b); + + // confirm that we can see live sessions + g_idle_add (set_live_session_to_true, NULL); + wait_for_signal (mock_users, "notify::" INDICATOR_SESSION_USERS_PROP_IS_LIVE_SESSION); + ASSERT_TRUE (g_settings_get_boolean (mock_settings, live_session_key)); + wait_msec (50); + g_object_get (mock_users, INDICATOR_SESSION_USERS_PROP_IS_LIVE_SESSION, &b, NULL); + ASSERT_TRUE (b); + + // cleanup + g_settings_reset (mock_settings, live_session_key); +} + + +TEST_F (ServiceTest, User) +{ + const char * const error_key = "has-online-account-error"; + const char * const show_name_key = "show-real-name-on-panel"; + + const struct { + guint64 login_frequency; + const gchar * user_name; + const gchar * real_name; + } account_info[] = { + { 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" } + }; + + // Find the switcher menu model. + // In BackendMock's default setup, it will only two menuitems: greeter & guest + int pos = 0; + GMenuModel * switch_menu = 0; + ASSERT_TRUE (find_menu_item_for_action ("indicator.switch-to-greeter", &switch_menu, &pos)); + ASSERT_EQ (0, pos); + ASSERT_EQ (2, g_menu_model_get_n_items (switch_menu)); + g_clear_object (&switch_menu); + + // now add some users + IndicatorSessionUser * users[12]; + for (int i=0; i<12; ++i) + users[i] = 0; + for (int i=0; i<5; ++i) + { + IndicatorSessionUser * u = g_new0 (IndicatorSessionUser, 1); + u->is_current_user = false; + u->is_logged_in = false; + u->uid = 101 + i; + u->login_frequency = account_info[i].login_frequency; + u->user_name = g_strdup (account_info[i].user_name); + u->real_name = g_strdup (account_info[i].real_name); + indicator_session_users_mock_add_user (INDICATOR_SESSION_USERS_MOCK(mock_users), u->user_name, u); + users[i] = u; + } + + wait_for_menu_resync (); + + // now there should be 7 menuitems: greeter + guest + the five doctors + ASSERT_TRUE (find_menu_item_for_action ("indicator.switch-to-greeter", &switch_menu, &pos)); + ASSERT_EQ (0, pos); + ASSERT_EQ (7, g_menu_model_get_n_items (switch_menu)); + // confirm that the doctor names are sorted + check_label ("Fifth Doctor", switch_menu, 2); + check_label ("First Doctor", switch_menu, 3); + check_label ("Fourth Doctor", switch_menu, 4); + check_label ("Second Doctor", switch_menu, 5); + check_label ("Third Doctor", switch_menu, 6); + g_clear_object (&switch_menu); + + // now remove a couple of 'em + indicator_session_users_mock_remove_user (INDICATOR_SESSION_USERS_MOCK(mock_users), account_info[3].user_name); + indicator_session_users_mock_remove_user (INDICATOR_SESSION_USERS_MOCK(mock_users), account_info[4].user_name); + + wait_for_menu_resync (); + + // now there should be 5 menuitems: greeter + guest + the three doctors + ASSERT_TRUE (find_menu_item_for_action ("indicator.switch-to-greeter", &switch_menu, &pos)); + ASSERT_EQ (0, pos); + ASSERT_EQ (5, g_menu_model_get_n_items (switch_menu)); + // confirm that the doctor names are sorted + check_label ("First Doctor", switch_menu, 2); + check_label ("Second Doctor", switch_menu, 3); + check_label ("Third Doctor", switch_menu, 4); + g_clear_object (&switch_menu); + + // now let's have the third one be the current user + users[2]->is_current_user = true; + users[2]->is_logged_in = true; + indicator_session_users_changed (mock_users, users[2]->user_name); + + wait_for_menu_resync (); + + // now there should be 5 menuitems: greeter + guest + the three doctors + ASSERT_TRUE (find_menu_item_for_action ("indicator.switch-to-greeter", &switch_menu, &pos)); + ASSERT_EQ (0, pos); + ASSERT_EQ (5, g_menu_model_get_n_items (switch_menu)); + g_clear_object (&switch_menu); + + // oh hey, while we've got an active user let's check the header + ASSERT_FALSE (g_settings_get_boolean (indicator_settings, show_name_key)); + ASSERT_FALSE (g_settings_get_boolean (mock_settings, error_key)); + check_header ("", "system-devices-panel", "System"); + g_settings_set_boolean (indicator_settings, show_name_key, true); + wait_for_signal (action_group, "action-state-changed"); + check_header ("Third Doctor", "system-devices-panel", "System, Third Doctor"); + g_settings_set_boolean (mock_settings, error_key, true); + wait_for_signal (action_group, "action-state-changed"); + check_header ("Third Doctor", "system-devices-panel-alert", "System, Third Doctor (Attention Required)"); + g_settings_reset (mock_settings, error_key); + g_settings_reset (indicator_settings, show_name_key); + wait_for_menu_resync (); + + // try setting the max user count to 2... + // since troughton has the fewest logins, he should get culled + g_object_set (service, "max-users", 2, NULL); + guint max_users; + g_object_get (service, "max-users", &max_users, NULL); + ASSERT_EQ (2, max_users); + wait_for_menu_resync (); + ASSERT_TRUE (find_menu_item_for_action ("indicator.switch-to-greeter", &switch_menu, &pos)); + ASSERT_EQ (0, pos); + ASSERT_EQ (4, g_menu_model_get_n_items (switch_menu)); + check_label ("First Doctor", switch_menu, 2); + check_label ("Third Doctor", switch_menu, 3); + g_clear_object (&switch_menu); + + // add some more, test sorting and culling. + // add in all the doctors, but only show 7, and make msmith the current session + g_object_set (service, "max-users", 7, NULL); + g_object_get (service, "max-users", &max_users, NULL); + ASSERT_EQ (7, max_users); + for (int i=3; i<12; ++i) + { + IndicatorSessionUser * u = g_new0 (IndicatorSessionUser, 1); + u->is_current_user = false; + u->is_logged_in = false; + u->uid = 101 + i; + u->login_frequency = account_info[i].login_frequency; + u->user_name = g_strdup (account_info[i].user_name); + u->real_name = g_strdup (account_info[i].real_name); + indicator_session_users_mock_add_user (INDICATOR_SESSION_USERS_MOCK(mock_users), u->user_name, u); + users[i] = u; + } + users[2]->is_current_user = false; + indicator_session_users_changed (mock_users, users[2]->user_name); + users[10]->is_current_user = true; + users[10]->is_logged_in = true; + indicator_session_users_changed (mock_users, users[10]->user_name); + wait_for_menu_resync (); + ASSERT_TRUE (find_menu_item_for_action ("indicator.switch-to-greeter", &switch_menu, &pos)); + ASSERT_EQ (0, pos); + ASSERT_EQ (9, g_menu_model_get_n_items (switch_menu)); + check_label ("Eleventh Doctor", switch_menu, 2); + check_label ("Fifth Doctor", switch_menu, 3); + check_label ("First Doctor", switch_menu, 4); + check_label ("Fourth Doctor", switch_menu, 5); + check_label ("Second Doctor", switch_menu, 6); + check_label ("Tenth Doctor", switch_menu, 7); + check_label ("Third Doctor", switch_menu, 8); + g_clear_object (&switch_menu); + + // now switch to one of the doctors + g_action_group_activate_action (G_ACTION_GROUP(action_group), + "switch-to-user", + g_variant_new_string("tbaker")); + wait_for_signal (mock_settings, "changed::last-command"); + check_last_command_is ("switch-to-user::tbaker"); } -- cgit v1.2.3 From 0c5a240c6fb66d4b2dd620c81d48a065bedeb7f7 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Wed, 19 Jun 2013 18:22:32 -0500 Subject: silence unused variable warning --- tests/backend-mock-guest.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'tests') diff --git a/tests/backend-mock-guest.c b/tests/backend-mock-guest.c index 0428783..b04c805 100644 --- a/tests/backend-mock-guest.c +++ b/tests/backend-mock-guest.c @@ -71,7 +71,7 @@ my_is_active (IndicatorSessionGuest * self) } static void -my_switch_to_guest (IndicatorSessionGuest * self) +my_switch_to_guest (IndicatorSessionGuest * self G_GNUC_UNUSED) { g_message ("%s %s FIXME", G_STRLOC, G_STRFUNC); } -- cgit v1.2.3 From 4df0aceb48e9b3d6c77f6e06c7acde89747ff878 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Wed, 19 Jun 2013 18:27:08 -0500 Subject: use the production dbus name --- tests/test-service.cc | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'tests') diff --git a/tests/test-service.cc b/tests/test-service.cc index 058fc3d..76dcd75 100644 --- a/tests/test-service.cc +++ b/tests/test-service.cc @@ -170,7 +170,7 @@ class ServiceTest: public GTestDBusFixture // wait for the service to show up on the bus const guint watch_id = g_bus_watch_name_on_connection (conn, - "com.canonical.indicator.session-test", + "com.canonical.indicator.session", G_BUS_NAME_WATCHER_FLAGS_NONE, on_name_appeared, // quits the loop NULL, this, NULL); @@ -182,10 +182,10 @@ class ServiceTest: public GTestDBusFixture // get the actions & menus that the service exported. action_group = g_dbus_action_group_get (conn, - "com.canonical.indicator.session-test", + "com.canonical.indicator.session", "/com/canonical/indicator/session"); menu_model = g_dbus_menu_model_get (conn, - "com.canonical.indicator.session-test", + "com.canonical.indicator.session", "/com/canonical/indicator/session/desktop"); // the actions are added asynchronously, so wait for the actions if (!g_action_group_has_action (G_ACTION_GROUP(action_group), "about")) -- cgit v1.2.3 From fab2853f35eb108988b5ddb16cd9f6e43de79773 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Thu, 20 Jun 2013 13:54:41 -0500 Subject: remove the --replace command-line argument and property as we're using upstart for that --- tests/test-service.cc | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) (limited to 'tests') diff --git a/tests/test-service.cc b/tests/test-service.cc index 76dcd75..57b4221 100644 --- a/tests/test-service.cc +++ b/tests/test-service.cc @@ -161,12 +161,7 @@ class ServiceTest: public GTestDBusFixture // Start an IndicatorSessionService and wait for it to appear on the bus. // This way our calls to g_dbus_*_get() in the next paragraph won't activate // a second copy of the service... - service = indicator_session_service_new (true); - - // confirm that the property got set - gboolean replace = FALSE; - g_object_get (service, "replace", &replace, NULL); - ASSERT_TRUE (replace); + service = indicator_session_service_new (); // wait for the service to show up on the bus const guint watch_id = g_bus_watch_name_on_connection (conn, -- cgit v1.2.3 From 889f876ae7263bde2fb54b69930d1d31cbad8423 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Mon, 24 Jun 2013 17:08:53 -0500 Subject: in the tests, use login1's terminology 'power off', 'reboot' --- tests/backend-mock-actions.c | 12 ++++++------ tests/test-service.cc | 28 ++++++++++++++-------------- 2 files changed, 20 insertions(+), 20 deletions(-) (limited to 'tests') diff --git a/tests/backend-mock-actions.c b/tests/backend-mock-actions.c index 121c7ba..d5a506b 100644 --- a/tests/backend-mock-actions.c +++ b/tests/backend-mock-actions.c @@ -80,15 +80,15 @@ my_hibernate (IndicatorSessionActions * self G_GNUC_UNUSED) } static void -my_restart (IndicatorSessionActions * self G_GNUC_UNUSED) +my_reboot (IndicatorSessionActions * self G_GNUC_UNUSED) { - g_settings_set_string (mock_settings, "last-command", "restart"); + g_settings_set_string (mock_settings, "last-command", "reboot"); } static void -my_shutdown (IndicatorSessionActions * self G_GNUC_UNUSED) +my_power_off (IndicatorSessionActions * self G_GNUC_UNUSED) { - g_settings_set_string (mock_settings, "last-command", "shutdown"); + g_settings_set_string (mock_settings, "last-command", "power-off"); } static void @@ -185,8 +185,8 @@ indicator_session_actions_mock_class_init (IndicatorSessionActionsMockClass * kl actions_class->logout = my_logout; actions_class->suspend = my_suspend; actions_class->hibernate = my_hibernate; - actions_class->restart = my_restart; - actions_class->shutdown = my_shutdown; + actions_class->reboot = my_reboot; + actions_class->power_off = my_power_off; actions_class->settings = my_settings; actions_class->help = my_help; actions_class->about = my_about; diff --git a/tests/test-service.cc b/tests/test-service.cc index 57b4221..2d81441 100644 --- a/tests/test-service.cc +++ b/tests/test-service.cc @@ -374,14 +374,14 @@ TEST_F (ServiceTest, Logout) test_simple_action ("logout"); } -TEST_F (ServiceTest, Shutdown) +TEST_F (ServiceTest, PowerOff) { - test_simple_action ("shutdown"); + test_simple_action ("power-off"); } -TEST_F (ServiceTest, Restart) +TEST_F (ServiceTest, Reboot) { - test_simple_action ("restart"); + test_simple_action ("reboot"); } TEST_F (ServiceTest, SwitchToScreensaver) @@ -477,8 +477,8 @@ TEST_F (ServiceTest, ConfirmationDisabledByBackend) // confirm that the ellipsis are correct ASSERT_EQ (confirm, action_menuitem_label_is_ellipsized ("indicator.switch-to-greeter")); ASSERT_EQ (confirm, action_menuitem_label_is_ellipsized ("indicator.logout")); - ASSERT_EQ (confirm, action_menuitem_label_is_ellipsized ("indicator.restart")); - ASSERT_EQ (confirm, action_menuitem_label_is_ellipsized ("indicator.shutdown")); + ASSERT_EQ (confirm, action_menuitem_label_is_ellipsized ("indicator.reboot")); + ASSERT_EQ (confirm, action_menuitem_label_is_ellipsized ("indicator.power-off")); // now toggle the can-prompt flag confirm_supported = !confirm_supported; @@ -490,8 +490,8 @@ TEST_F (ServiceTest, ConfirmationDisabledByBackend) // confirm that the ellipsis are correct ASSERT_EQ (confirm, action_menuitem_label_is_ellipsized ("indicator.switch-to-greeter")); ASSERT_EQ (confirm, action_menuitem_label_is_ellipsized ("indicator.logout")); - ASSERT_EQ (confirm, action_menuitem_label_is_ellipsized ("indicator.restart")); - ASSERT_EQ (confirm, action_menuitem_label_is_ellipsized ("indicator.shutdown")); + ASSERT_EQ (confirm, action_menuitem_label_is_ellipsized ("indicator.reboot")); + ASSERT_EQ (confirm, action_menuitem_label_is_ellipsized ("indicator.power-off")); // cleanup g_settings_reset (mock_settings, confirm_supported_key); @@ -509,8 +509,8 @@ TEST_F (ServiceTest, ConfirmationDisabledByUser) // confirm that the ellipsis are correct ASSERT_EQ (confirm, action_menuitem_label_is_ellipsized ("indicator.switch-to-greeter")); ASSERT_EQ (confirm, action_menuitem_label_is_ellipsized ("indicator.logout")); - ASSERT_EQ (confirm, action_menuitem_label_is_ellipsized ("indicator.restart")); - ASSERT_EQ (confirm, action_menuitem_label_is_ellipsized ("indicator.shutdown")); + ASSERT_EQ (confirm, action_menuitem_label_is_ellipsized ("indicator.reboot")); + ASSERT_EQ (confirm, action_menuitem_label_is_ellipsized ("indicator.power-off")); // now toggle the can-prompt flag confirm_disabled = !confirm_disabled; @@ -522,8 +522,8 @@ TEST_F (ServiceTest, ConfirmationDisabledByUser) // confirm that the ellipsis are correct ASSERT_EQ (confirm, action_menuitem_label_is_ellipsized ("indicator.switch-to-greeter")); ASSERT_EQ (confirm, action_menuitem_label_is_ellipsized ("indicator.logout")); - ASSERT_EQ (confirm, action_menuitem_label_is_ellipsized ("indicator.restart")); - ASSERT_EQ (confirm, action_menuitem_label_is_ellipsized ("indicator.shutdown")); + ASSERT_EQ (confirm, action_menuitem_label_is_ellipsized ("indicator.reboot")); + ASSERT_EQ (confirm, action_menuitem_label_is_ellipsized ("indicator.power-off")); // cleanup g_settings_reset (indicator_settings, confirm_disabled_key); @@ -542,8 +542,8 @@ TEST_F (ServiceTest, DefaultMenuItems) ASSERT_TRUE (find_menu_item_for_action ("indicator.logout", NULL, NULL)); ASSERT_TRUE (find_menu_item_for_action ("indicator.suspend", NULL, NULL)); ASSERT_TRUE (find_menu_item_for_action ("indicator.hibernate", NULL, NULL)); - ASSERT_TRUE (find_menu_item_for_action ("indicator.restart", NULL, NULL)); - ASSERT_TRUE (find_menu_item_for_action ("indicator.shutdown", NULL, NULL)); + ASSERT_TRUE (find_menu_item_for_action ("indicator.reboot", NULL, NULL)); + ASSERT_TRUE (find_menu_item_for_action ("indicator.power-off", NULL, NULL)); } TEST_F (ServiceTest, OnlineAccountError) -- cgit v1.2.3 From 9cfc9e9da79cabacf1f81e96511d10895992c47d Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Mon, 24 Jun 2013 17:13:07 -0500 Subject: get all the tests in test-actions passing again. --- tests/backend-dbus/CMakeLists.txt | 14 +- tests/backend-dbus/gtest-mock-dbus-fixture.h | 28 +-- 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 | 17 +- tests/backend-dbus/mock-display-manager-seat.h | 6 +- tests/backend-dbus/mock-login1-manager.cc | 275 ++++++++++++++++++++++++ tests/backend-dbus/mock-login1-manager.h | 68 ++++++ tests/backend-dbus/mock-login1-seat.cc | 230 ++++++++++++++++++++ tests/backend-dbus/mock-login1-seat.h | 77 +++++++ tests/backend-dbus/mock-upower.cc | 103 --------- tests/backend-dbus/mock-upower.h | 72 ------- tests/backend-dbus/test-actions.cc | 137 +++++------- 17 files changed, 726 insertions(+), 1007 deletions(-) delete mode 100644 tests/backend-dbus/mock-consolekit-manager.cc delete mode 100644 tests/backend-dbus/mock-consolekit-manager.h delete mode 100644 tests/backend-dbus/mock-consolekit-seat.cc delete mode 100644 tests/backend-dbus/mock-consolekit-seat.h delete mode 100644 tests/backend-dbus/mock-consolekit-session.cc delete mode 100644 tests/backend-dbus/mock-consolekit-session.h create mode 100644 tests/backend-dbus/mock-login1-manager.cc create mode 100644 tests/backend-dbus/mock-login1-manager.h create mode 100644 tests/backend-dbus/mock-login1-seat.cc create mode 100644 tests/backend-dbus/mock-login1-seat.h delete mode 100644 tests/backend-dbus/mock-upower.cc delete mode 100644 tests/backend-dbus/mock-upower.h (limited to 'tests') diff --git a/tests/backend-dbus/CMakeLists.txt b/tests/backend-dbus/CMakeLists.txt index 7cd7030..ed9c93a 100644 --- a/tests/backend-dbus/CMakeLists.txt +++ b/tests/backend-dbus/CMakeLists.txt @@ -5,16 +5,16 @@ add_library (gtest STATIC set_target_properties (gtest PROPERTIES INCLUDE_DIRECTORIES ${GTEST_INCLUDE_DIR}) +SET (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x") + # 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-login1-manager.cc + mock-login1-manager.h + mock-login1-seat.cc + mock-login1-seat.h mock-display-manager-seat.cc mock-display-manager-seat.h mock-end-session-dialog.cc @@ -25,8 +25,6 @@ add_library (desktopmock STATIC 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 diff --git a/tests/backend-dbus/gtest-mock-dbus-fixture.h b/tests/backend-dbus/gtest-mock-dbus-fixture.h index 5c4e312..1b1b895 100644 --- a/tests/backend-dbus/gtest-mock-dbus-fixture.h +++ b/tests/backend-dbus/gtest-mock-dbus-fixture.h @@ -20,14 +20,12 @@ #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-login1-manager.h" +#include "mock-login1-seat.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" @@ -47,10 +45,8 @@ class GTestMockDBusFixture: public GTestDBusFixture MockSessionManager * session_manager; MockDisplayManagerSeat * dm_seat; MockAccounts * accounts; - MockConsoleKitSession * ck_session; - MockConsoleKitSeat * ck_seat; - MockConsoleKitManager * ck_manager; - MockUPower * upower; + MockLogin1Manager * login1_manager; + MockLogin1Seat * login1_seat; MockEndSessionDialog * end_session_dialog; MockWebcredentials * webcredentials; @@ -64,19 +60,18 @@ class GTestMockDBusFixture: public GTestDBusFixture 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); + login1_manager = new MockLogin1Manager (loop, conn); + login1_seat = new MockLogin1Seat (loop, conn, true); + login1_manager->add_seat (login1_seat); 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); + const int session_tag = login1_manager->add_session (login1_seat, user); + dm_seat->set_login1_seat (login1_seat); dm_seat->switch_to_user (user->username()); - ASSERT_EQ (ck_session, ck_manager->current_session()); + ASSERT_EQ (session_tag, login1_seat->active_session()); } protected: @@ -84,9 +79,8 @@ class GTestMockDBusFixture: public GTestDBusFixture virtual void TearDown () { delete accounts; - delete ck_manager; + delete login1_manager; delete dm_seat; - delete upower; delete screen_saver; delete session_manager; delete end_session_dialog; diff --git a/tests/backend-dbus/mock-consolekit-manager.cc b/tests/backend-dbus/mock-consolekit-manager.cc deleted file mode 100644 index 40f9bf9..0000000 --- a/tests/backend-dbus/mock-consolekit-manager.cc +++ /dev/null @@ -1,156 +0,0 @@ -/* - * 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 deleted file mode 100644 index c5942b2..0000000 --- a/tests/backend-dbus/mock-consolekit-manager.h +++ /dev/null @@ -1,77 +0,0 @@ -/* - * 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 deleted file mode 100644 index ce1fbad..0000000 --- a/tests/backend-dbus/mock-consolekit-seat.cc +++ /dev/null @@ -1,220 +0,0 @@ -/* - * 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 deleted file mode 100644 index aa52276..0000000 --- a/tests/backend-dbus/mock-consolekit-seat.h +++ /dev/null @@ -1,75 +0,0 @@ -/* - * 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 deleted file mode 100644 index 2f39411..0000000 --- a/tests/backend-dbus/mock-consolekit-session.cc +++ /dev/null @@ -1,113 +0,0 @@ -/* - * 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 deleted file mode 100644 index 7759f72..0000000 --- a/tests/backend-dbus/mock-consolekit-session.h +++ /dev/null @@ -1,65 +0,0 @@ -/* - * 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 index 9f30ae1..c8a4857 100644 --- a/tests/backend-dbus/mock-display-manager-seat.cc +++ b/tests/backend-dbus/mock-display-manager-seat.cc @@ -18,7 +18,7 @@ */ #include "mock-display-manager-seat.h" -#include "mock-consolekit-seat.h" +#include "mock-login1-seat.h" namespace { @@ -79,10 +79,10 @@ MockDisplayManagerSeat :: handle_switch_to_guest (DisplayManagerSeat * o, void MockDisplayManagerSeat :: switch_to_guest () { - g_assert (my_ck_seat != 0); + g_assert (my_login1_seat != 0); my_last_action = GUEST; - my_ck_seat->switch_to_guest (); + my_login1_seat->switch_to_guest (); } gboolean @@ -100,16 +100,16 @@ MockDisplayManagerSeat :: handle_switch_to_user (DisplayManagerSeat * o, void MockDisplayManagerSeat :: switch_to_user (const char * username) { - g_assert (my_ck_seat != 0); + g_assert (my_login1_seat != 0); my_last_action = USER; - my_ck_seat->switch_to_user (username); + my_login1_seat->switch_to_user (username); } void -MockDisplayManagerSeat :: set_consolekit_seat (MockConsoleKitSeat * seat) +MockDisplayManagerSeat :: set_login1_seat (MockLogin1Seat * seat) { - my_ck_seat = seat; + my_login1_seat = seat; } /*** @@ -120,7 +120,6 @@ 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", @@ -135,6 +134,6 @@ MockDisplayManagerSeat :: MockDisplayManagerSeat (GMainLoop * loop, MockDisplayManagerSeat :: ~MockDisplayManagerSeat () { - g_signal_handlers_disconnect_by_data (my_skeleton, this); + //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 index b0ea415..fcdd17a 100644 --- a/tests/backend-dbus/mock-display-manager-seat.h +++ b/tests/backend-dbus/mock-display-manager-seat.h @@ -23,7 +23,7 @@ #include "mock-object.h" // parent class #include "backend-dbus/dbus-display-manager.h" -class MockConsoleKitSeat; +class MockLogin1Seat; class MockDisplayManagerSeat: public MockObject { @@ -35,7 +35,7 @@ class MockDisplayManagerSeat: public MockObject void set_guest_allowed (bool b); - void set_consolekit_seat (MockConsoleKitSeat * ck_seat); + void set_login1_seat (MockLogin1Seat * login1_seat); void switch_to_guest (); @@ -65,7 +65,7 @@ class MockDisplayManagerSeat: public MockObject gpointer gself); DisplayManagerSeat * my_skeleton; - MockConsoleKitSeat * my_ck_seat; + MockLogin1Seat * my_login1_seat; Action my_last_action; }; diff --git a/tests/backend-dbus/mock-login1-manager.cc b/tests/backend-dbus/mock-login1-manager.cc new file mode 100644 index 0000000..2d341df --- /dev/null +++ b/tests/backend-dbus/mock-login1-manager.cc @@ -0,0 +1,275 @@ +/* + * 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-login1-manager.h" +#include "mock-login1-seat.h" +#include "mock-user.h" + +namespace +{ + const char * const BUS_NAME = "org.freedesktop.login1"; + + const char * const BUS_PATH = "/org/freedesktop/login1"; + +#if 0 + void on_active_session_changed (Login1Seat * o G_GNUC_UNUSED, + const gchar * new_ssid, + gpointer ssid) + { + *static_cast(ssid) = new_ssid; + } +#endif +} + +/*** +**** +***/ + +#if 0 +gboolean +MockLogin1Manager :: on_get_current_session (Login1Manager * m, + GDBusMethodInvocation * inv, + gpointer gself) +{ + MockLogin1Manager * 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 +MockLogin1Manager :: on_get_seats (Login1Manager * 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 +MockLogin1Manager :: handle_restart (Login1Manager * ckm, + GDBusMethodInvocation * inv, + gpointer gself) +{ + static_cast(gself)->my_last_action = Restart; + console_kit_manager_complete_restart (ckm, inv); + return true; +} + +gboolean +MockLogin1Manager :: handle_stop (Login1Manager * ckm, + GDBusMethodInvocation * inv, + gpointer gself) +{ + static_cast(gself)->my_last_action = Shutdown; + console_kit_manager_complete_stop (ckm, inv); + return true; +} +#endif + +/*** +**** +***/ + +#if 0 +MockLogin1Session * +MockLogin1Manager :: current_session () +{ + MockLogin1Session * 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; +} +#endif + +void +MockLogin1Manager :: emit_session_new (MockLogin1Seat * seat, int tag) const +{ + std::string id; + std::string path; + + seat->get_session_id_and_path_for_tag (tag, id, path); + +g_message ("%s %s emitting session-new [%s][%s]", G_STRLOC, G_STRFUNC, id.c_str(), path.c_str()); + login1_manager_emit_session_new (my_skeleton, id.c_str(), path.c_str()); +} + +int +MockLogin1Manager :: add_session (MockLogin1Seat * seat, MockUser * user) +{ + g_assert (my_seats.count(seat) == 1); + + const int tag = seat->add_session (user); + emit_session_new (seat, tag); + return tag; +} + +void +MockLogin1Manager :: add_seat (MockLogin1Seat * seat) +{ + g_assert (my_seats.count(seat) == 0); + + my_seats.insert (seat); + std::set sessions = seat->sessions (); + for (auto tag : sessions) + emit_session_new (seat, tag); +} + +/*** +**** Skeleton Handlers +***/ + +gboolean +MockLogin1Manager :: handle_can_suspend (Login1Manager * m, + GDBusMethodInvocation * inv, + gpointer gself) +{ + const std::string& s = static_cast(gself)->can_suspend(); + login1_manager_complete_can_suspend (m, inv, s.c_str()); + return true; +} + +gboolean +MockLogin1Manager :: handle_can_hibernate (Login1Manager * m, + GDBusMethodInvocation * inv, + gpointer gself) +{ + const std::string& s = static_cast(gself)->can_hibernate(); + login1_manager_complete_can_hibernate (m, inv, s.c_str()); + return true; +} + +gboolean +MockLogin1Manager :: handle_reboot (Login1Manager * m, + GDBusMethodInvocation * inv, + gboolean interactive, + gpointer gself) +{ + static_cast(gself)->my_last_action = "reboot"; + login1_manager_complete_reboot (m, inv); + return true; +} + +gboolean +MockLogin1Manager :: handle_power_off (Login1Manager * m, + GDBusMethodInvocation * inv, + gboolean interactive, + gpointer gself) +{ + static_cast(gself)->my_last_action = "power-off"; + login1_manager_complete_power_off (m, inv); + return true; +} + +gboolean +MockLogin1Manager :: handle_suspend (Login1Manager * m, + GDBusMethodInvocation * inv, + gboolean interactive, + gpointer gself) +{ + static_cast(gself)->my_last_action = "suspend"; + login1_manager_complete_suspend (m, inv); + return true; +} + +gboolean +MockLogin1Manager :: handle_hibernate (Login1Manager * m, + GDBusMethodInvocation * inv, + gboolean interactive, + gpointer gself) +{ + static_cast(gself)->my_last_action = "hibernate"; + login1_manager_complete_hibernate (m, inv); + return true; +} + +/*** +**** +***/ + +const std::string& +MockLogin1Manager :: can_suspend () const +{ + return my_can_suspend; +} + +const std::string& +MockLogin1Manager :: can_hibernate () const +{ + return my_can_hibernate; +} + +/*** +**** +***/ + +MockLogin1Manager :: MockLogin1Manager (GMainLoop * loop, + GDBusConnection * conn): + MockObject (loop, conn, BUS_NAME, BUS_PATH), + my_skeleton (login1_manager_skeleton_new ()), + my_can_suspend ("yes"), + my_can_hibernate ("yes") +{ + g_signal_connect (my_skeleton, "handle-can-suspend", + G_CALLBACK(handle_can_suspend), this); + g_signal_connect (my_skeleton, "handle-can-hibernate", + G_CALLBACK(handle_can_hibernate), this); + g_signal_connect (my_skeleton, "handle_reboot", + G_CALLBACK(handle_reboot), this); + g_signal_connect (my_skeleton, "handle-power-off", + G_CALLBACK(handle_power_off), this); + g_signal_connect (my_skeleton, "handle-suspend", + G_CALLBACK(handle_suspend), this); + g_signal_connect (my_skeleton, "handle-hibernate", + G_CALLBACK(handle_hibernate), this); +#if 0 + 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); +#endif + + set_skeleton (G_DBUS_INTERFACE_SKELETON(my_skeleton)); +} + +MockLogin1Manager :: ~MockLogin1Manager () +{ + for (auto seat : my_seats) + delete seat; + + g_signal_handlers_disconnect_by_data (my_skeleton, this); + g_clear_object (&my_skeleton); +} diff --git a/tests/backend-dbus/mock-login1-manager.h b/tests/backend-dbus/mock-login1-manager.h new file mode 100644 index 0000000..da05660 --- /dev/null +++ b/tests/backend-dbus/mock-login1-manager.h @@ -0,0 +1,68 @@ +/* + * 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_LOGIN1_MANAGER_H +#define MOCK_LOGIN1_MANAGER_H + +#include +#include +#include "mock-object.h" +#include "backend-dbus/dbus-login1-manager.h" + +class MockLogin1Seat; +class MockUser; + +class MockLogin1Manager: public MockObject +{ + public: + + MockLogin1Manager (GMainLoop * loop, + GDBusConnection * bus_connection); + virtual ~MockLogin1Manager (); + + int add_session (MockLogin1Seat * seat, MockUser * user); + + void add_seat (MockLogin1Seat * seat); + + const std::string& can_suspend () const; + const std::string& can_hibernate () const; + + const std::string& last_action () const { return my_last_action; } + void clear_last_action () { my_last_action.clear(); } + + private: + void emit_session_new (MockLogin1Seat * seat, int tag) const; + + static gboolean handle_can_suspend (Login1Manager *, GDBusMethodInvocation *, gpointer); + static gboolean handle_can_hibernate (Login1Manager *, GDBusMethodInvocation *, gpointer); + static gboolean handle_reboot (Login1Manager *, GDBusMethodInvocation *, gboolean, gpointer); + static gboolean handle_power_off (Login1Manager *, GDBusMethodInvocation *, gboolean, gpointer); + static gboolean handle_suspend (Login1Manager *, GDBusMethodInvocation *, gboolean, gpointer); + static gboolean handle_hibernate (Login1Manager *, GDBusMethodInvocation *, gboolean, gpointer); + + private: + + Login1Manager * my_skeleton; + std::set my_seats; + std::string my_can_suspend; + std::string my_can_hibernate; + std::string my_last_action; +}; + +#endif // #ifndef MOCK_LOGIN1_MANAGER_H diff --git a/tests/backend-dbus/mock-login1-seat.cc b/tests/backend-dbus/mock-login1-seat.cc new file mode 100644 index 0000000..a95b9e1 --- /dev/null +++ b/tests/backend-dbus/mock-login1-seat.cc @@ -0,0 +1,230 @@ +/* + * 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-login1-seat.h" + +#include "mock-object.h" +#include "mock-user.h" + +namespace +{ + const char * BUS_NAME = "org.freedesktop.login1"; + + std::string next_unique_sid () + { + static int id = 1; + + char * tmp; + std::string ret; + + tmp = g_strdup_printf ("/org/freedesktop/login1/seat/seat%d", id++); + ret = tmp; + g_free (tmp); + return ret; + } + + static int next_session_tag = 1; +} + +void +MockLogin1Seat :: get_session_id_and_path_for_tag (int tag, std::string& id, std::string& path) +{ + if (tag) + { + char tmp[80]; + + g_snprintf (tmp, sizeof(tmp), "%d", tag); + id = tmp; + + g_snprintf (tmp, sizeof(tmp), "/org/freedesktop/login1/session/%d", tag); + path = tmp; + } + else + { + id = ""; + path = ""; + } +} + + +/*** +**** +***/ + +void +MockLogin1Seat :: update_sessions_property () +{ + GVariantBuilder b; + + g_variant_builder_init (&b, G_VARIANT_TYPE("a(so)")); + for (const auto& it : my_sessions) + { + std::string id, path; + get_session_id_and_path_for_tag (it.first, id, path); + g_variant_builder_add (&b, "(so)", id.c_str(), path.c_str()); + } + + GVariant * v = g_variant_builder_end (&b); + g_message ("%s %s setting session property to %s", G_STRLOC, G_STRFUNC, g_variant_print (v, true)); + g_object_set (my_skeleton, "sessions", v, NULL); +} + +void +MockLogin1Seat :: update_active_session_property () +{ + std::string id; + std::string path; + get_session_id_and_path_for_tag (my_active_session, id, path); + + GVariant * v = g_variant_new ("(so)", id.c_str(), path.c_str()); + g_message ("%s %s setting active session property to %s", G_STRLOC, G_STRFUNC, g_variant_print (v, true)); + g_object_set (my_skeleton, "active-session", v, NULL); +} + +void +MockLogin1Seat :: update_can_multi_session_property () +{ + g_object_set (my_skeleton, "can-multi-session", my_can_multi_session, NULL); +} + +/*** +**** +***/ + +/* lists this seat's sessions in the format of Login1Manager::ListSessions() */ +GVariant * +MockLogin1Seat :: list_sessions () +{ + GVariantBuilder b; + g_variant_builder_init (&b, G_VARIANT_TYPE("a(susso)")); + for (auto it : my_sessions) + { + std::string id, path; + get_session_id_and_path_for_tag (it.first, id, path); + g_variant_builder_add (&b, "(susso)", + id.c_str(), + uint32_t(it.second->uid()), + it.second->username(), + seat_id(), + path.c_str()); + } + + return g_variant_builder_end (&b); +} + +/*** +**** +***/ + +std::set +MockLogin1Seat :: sessions () const +{ + std::set ret; + + for (auto it : my_sessions) + ret.insert (it.first); + + return ret; +} + +MockLogin1Seat :: session_tag_t +MockLogin1Seat :: add_session (MockUser * user) +{ + const session_tag_t tag = next_session_tag++; + + my_sessions[tag] = user; + update_sessions_property (); + + return tag; +} + +void +MockLogin1Seat :: remove_session (session_tag_t tag) +{ + my_sessions.erase (tag); + update_sessions_property (); +} + +/*** +**** +***/ + +void +MockLogin1Seat :: activate_session (session_tag_t tag) +{ + g_assert (my_sessions.count(tag) == 1); + + if (my_active_session != tag) + { + my_active_session = tag; + update_active_session_property (); + } +} + +void +MockLogin1Seat :: switch_to_guest () +{ + for (const auto& it : my_sessions) + { + if (it.second->is_guest()) + { + activate_session (it.first); + return; + } + } + + g_warn_if_reached (); +} + +void +MockLogin1Seat :: switch_to_user (const char * username) +{ + for (const auto& it : my_sessions) + { + if (!g_strcmp0 (username, it.second->username())) + { + activate_session (it.first); + return; + } + } + + g_warn_if_reached (); +} + +/*** +**** Life Cycle +***/ + +MockLogin1Seat :: MockLogin1Seat (GMainLoop * loop, + GDBusConnection * bus_connection, + bool can_activate_sessions): + MockObject (loop, bus_connection, BUS_NAME, next_unique_sid()), + my_skeleton (login1_seat_skeleton_new ()), + my_can_multi_session (can_activate_sessions), + my_active_session (0) + +{ + set_skeleton (G_DBUS_INTERFACE_SKELETON(my_skeleton)); + update_can_multi_session_property (); +} + +MockLogin1Seat :: ~MockLogin1Seat () +{ + g_clear_object (&my_skeleton); +} diff --git a/tests/backend-dbus/mock-login1-seat.h b/tests/backend-dbus/mock-login1-seat.h new file mode 100644 index 0000000..96ff19c --- /dev/null +++ b/tests/backend-dbus/mock-login1-seat.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_LOGIN1_SEAT_H +#define MOCK_LOGIN1_SEAT_H + +#include /* strrchr */ +#include +#include +#include +#include "backend-dbus/dbus-login1-seat.h" +#include "mock-object.h" + +class MockUser; +class MockLogin1Session; + +class MockLogin1Seat: public MockObject +{ + public: + + typedef int session_tag_t; + + MockLogin1Seat (GMainLoop * loop, + GDBusConnection * bus_connection, + bool can_activate_sessions); + + virtual ~MockLogin1Seat (); + + const char * seat_id() const { return strrchr(path(),'/')+1; } + + session_tag_t add_session (MockUser * user); + void remove_session (session_tag_t session); + std::set sessions () const; + int active_session () const { return my_active_session; } + + bool can_activate_sessions () const { return my_can_multi_session; } + void activate_session (session_tag_t session); + void switch_to_guest (); + void switch_to_user (const char * username); + + //const char * sid() { return path(); } + //MockLogin1Session * find (const char * ssid); + + GVariant * list_sessions (); + + static void get_session_id_and_path_for_tag (int tag, std::string& id, std::string& path); + + private: + void update_sessions_property (); + void update_active_session_property (); + void update_can_multi_session_property (); + + + private: + Login1Seat * my_skeleton; + std::map my_sessions; + session_tag_t my_active_session; + bool my_can_multi_session; +}; + +#endif // #ifndef MOCK_LOGIN1_SEAT_H diff --git a/tests/backend-dbus/mock-upower.cc b/tests/backend-dbus/mock-upower.cc deleted file mode 100644 index 65757b3..0000000 --- a/tests/backend-dbus/mock-upower.cc +++ /dev/null @@ -1,103 +0,0 @@ -/* - * 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 deleted file mode 100644 index 351d0f7..0000000 --- a/tests/backend-dbus/mock-upower.h +++ /dev/null @@ -1,72 +0,0 @@ -/* - * 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/test-actions.cc b/tests/backend-dbus/test-actions.cc index f79c913..410a02a 100644 --- a/tests/backend-dbus/test-actions.cc +++ b/tests/backend-dbus/test-actions.cc @@ -90,7 +90,7 @@ TEST_F (Actions, CanSwitch) bool b; gboolean b2; - b = ck_seat->can_activate_sessions() && !g_settings_get_boolean (s, settings_key); + b = login1_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); @@ -172,130 +172,88 @@ TEST_F (Actions, CanLogout) 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); - } + const std::string can_suspend = login1_manager->can_suspend (); + gboolean b = indicator_session_actions_can_suspend (actions); + ASSERT_EQ (b, can_suspend=="yes" || can_suspend=="challenge"); } 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 + const std::string can_hibernate = login1_manager->can_hibernate (); + gboolean b = indicator_session_actions_can_hibernate (actions); + ASSERT_EQ (b, can_hibernate=="yes" || can_hibernate=="challenge"); } -TEST_F (Actions, Restart) +TEST_F (Actions, Reboot) { - ASSERT_EQ (MockConsoleKitManager::None, ck_manager->last_action()); + ASSERT_TRUE (login1_manager->last_action().empty()); // confirm that user is prompted // and that no action is taken when the user cancels the dialog - indicator_session_actions_restart (actions); + indicator_session_actions_reboot (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()); + ASSERT_TRUE (login1_manager->last_action().empty()); // confirm that user is prompted // and that no action is taken when the user cancels the dialog - indicator_session_actions_restart (actions); + indicator_session_actions_reboot (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()); + ASSERT_EQ (login1_manager->last_action(), "reboot"); - // confirm that we try to restart w/o prompting + // confirm that we try to reboot 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()); + login1_manager->clear_last_action (); + ASSERT_TRUE (login1_manager->last_action().empty()); wait_msec (50); - indicator_session_actions_restart (actions); + indicator_session_actions_reboot (actions); wait_msec (50); - ASSERT_EQ (MockConsoleKitManager::Restart, ck_manager->last_action()); + ASSERT_EQ (login1_manager->last_action(), "reboot"); } -TEST_F (Actions, Shutdown) +TEST_F (Actions, PowerOff) { - ASSERT_EQ (MockConsoleKitManager::None, ck_manager->last_action()); + ASSERT_TRUE (login1_manager->last_action().empty()); // confirm that user is prompted // and that no action is taken when the user cancels the dialog - indicator_session_actions_shutdown (actions); + indicator_session_actions_power_off (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()); + ASSERT_TRUE (login1_manager->last_action().empty()); // confirm that user is prompted // and that no action is taken when the user cancels the dialog - indicator_session_actions_shutdown (actions); + indicator_session_actions_power_off (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()); + ASSERT_EQ (login1_manager->last_action(), "power-off"); // 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 (); + login1_manager->clear_last_action (); wait_msec (50); - indicator_session_actions_shutdown (actions); + indicator_session_actions_power_off (actions); wait_msec (50); - ASSERT_EQ (MockConsoleKitManager::Shutdown, ck_manager->last_action()); + ASSERT_EQ (login1_manager->last_action(), "power-off"); } TEST_F (Actions, Logout) { - ASSERT_EQ (MockConsoleKitManager::None, ck_manager->last_action()); + 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 @@ -308,7 +266,7 @@ TEST_F (Actions, Logout) // confirm that user is prompted // and that no action is taken when the user cancels the dialog - indicator_session_actions_shutdown (actions); + indicator_session_actions_logout (actions); wait_msec (50); ASSERT_TRUE (end_session_dialog->is_open ()); end_session_dialog->confirm_logout (); @@ -327,18 +285,18 @@ TEST_F (Actions, Logout) TEST_F (Actions, Suspend) { - ASSERT_EQ (MockUPower::None, upower->last_action()); + ASSERT_TRUE (login1_manager->last_action().empty()); indicator_session_actions_suspend (actions); wait_msec (50); - ASSERT_EQ (MockUPower::Suspend, upower->last_action()); + ASSERT_EQ (login1_manager->last_action(), "suspend"); } TEST_F (Actions, Hibernate) { - ASSERT_EQ (MockUPower::None, upower->last_action()); + ASSERT_TRUE (login1_manager->last_action().empty()); indicator_session_actions_hibernate (actions); wait_msec (50); - ASSERT_EQ (MockUPower::Hibernate, upower->last_action()); + ASSERT_EQ (login1_manager->last_action(), "hibernate"); } TEST_F (Actions, SwitchToScreensaver) @@ -362,18 +320,19 @@ TEST_F (Actions, SwitchToGuest) // allow guests dm_seat->set_guest_allowed (true); MockUser * guest_user; - MockConsoleKitSession * guest_ck_session; + //int guest_session_tag; +// 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); + int guest_session_tag = login1_manager->add_session (login1_seat, 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_for_signal (login1_seat->skeleton(), "notify::active-session"); + ASSERT_EQ (guest_session_tag, login1_seat->active_session()); wait_msec (50); } @@ -383,28 +342,28 @@ TEST_F (Actions, SwitchToUsername) const char * const dr2_username = "ptroughton"; MockUser * dr1_user; MockUser * dr2_user; - MockConsoleKitSession * dr1_session; - MockConsoleKitSession * dr2_session; + int dr1_session; + int dr2_session; dr1_user = accounts->find_by_username (dr1_username); - dr1_session = ck_seat->add_session_by_user (dr1_user); + dr1_session = login1_manager->add_session (login1_seat, dr1_user); dr2_user = accounts->find_by_username (dr2_username); - dr2_session = ck_seat->add_session_by_user (dr2_user); + dr2_session = login1_manager->add_session (login1_seat, 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_for_signal (login1_seat->skeleton(), "notify::active-session"); + ASSERT_EQ (dr1_session, login1_seat->active_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_for_signal (login1_seat->skeleton(), "notify::active-session"); + ASSERT_EQ (dr2_session, login1_seat->active_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_for_signal (login1_seat->skeleton(), "notify::active-session"); + ASSERT_EQ (dr1_session, login1_seat->active_session()); wait_msec (50); } -- cgit v1.2.3 From 3882e60f88c19b4522a08145230b7101c1a127d3 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Mon, 24 Jun 2013 23:12:03 -0500 Subject: In tests/, enable debuginfo and warnings for C++ file. Fix a couple of minor compile warnings. --- tests/backend-dbus/mock-end-session-dialog.cc | 12 ++++++------ tests/backend-dbus/mock-webcredentials.h | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) (limited to 'tests') diff --git a/tests/backend-dbus/mock-end-session-dialog.cc b/tests/backend-dbus/mock-end-session-dialog.cc index e289a83..903962b 100644 --- a/tests/backend-dbus/mock-end-session-dialog.cc +++ b/tests/backend-dbus/mock-end-session-dialog.cc @@ -44,10 +44,10 @@ MockEndSessionDialog :: handle_simulate_user_activity (GnomeScreenSaver * s 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, + guint arg_type G_GNUC_UNUSED, + guint arg_timestamp G_GNUC_UNUSED, + guint arg_seconds_to_stay_open G_GNUC_UNUSED, + const gchar * const * inhibitor_paths G_GNUC_UNUSED, gpointer gself) { static_cast(gself)->my_isOpen = true; @@ -68,8 +68,8 @@ namespace 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 ()) + my_skeleton (end_session_dialog_skeleton_new ()), + my_isOpen (false) { g_signal_connect (my_skeleton, "handle-open", G_CALLBACK(handle_open), this); diff --git a/tests/backend-dbus/mock-webcredentials.h b/tests/backend-dbus/mock-webcredentials.h index 212ca76..fd1b10c 100644 --- a/tests/backend-dbus/mock-webcredentials.h +++ b/tests/backend-dbus/mock-webcredentials.h @@ -32,7 +32,7 @@ class MockWebcredentials: public MockObject 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); } + void set_error (bool b) const { webcredentials_set_error_status (my_skeleton, b); } private: -- cgit v1.2.3 From 084f6cdc08117e26c34a8c6afa00735bd3d75b4a Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Mon, 24 Jun 2013 23:13:37 -0500 Subject: remove unused method MockAccounts::find_by_path() --- tests/backend-dbus/mock-accounts.cc | 12 ------------ tests/backend-dbus/mock-accounts.h | 1 - 2 files changed, 13 deletions(-) (limited to 'tests') diff --git a/tests/backend-dbus/mock-accounts.cc b/tests/backend-dbus/mock-accounts.cc index 2a0e7e7..c753c79 100644 --- a/tests/backend-dbus/mock-accounts.cc +++ b/tests/backend-dbus/mock-accounts.cc @@ -69,18 +69,6 @@ MockAccounts :: find_by_uid (guint64 uid) 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) { diff --git a/tests/backend-dbus/mock-accounts.h b/tests/backend-dbus/mock-accounts.h index 95da102..8032441 100644 --- a/tests/backend-dbus/mock-accounts.h +++ b/tests/backend-dbus/mock-accounts.h @@ -40,7 +40,6 @@ class MockAccounts: public MockObject 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: -- cgit v1.2.3 From 0f8caa36ee3efac6ccf0861deb9af9e1c186885d Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Mon, 24 Jun 2013 23:14:57 -0500 Subject: fix the first four tests in test-users: HelloWorld, InitialUsers, UserAdded, and UserRemoved. --- tests/backend-dbus/CMakeLists.txt | 2 +- tests/backend-dbus/gtest-mock-dbus-fixture.h | 1 + tests/backend-dbus/mock-login1-manager.cc | 51 +++++++++++++++++++++++--- tests/backend-dbus/mock-login1-manager.h | 4 +++ tests/backend-dbus/mock-login1-seat.cc | 52 ++++++++++++++++++++------- tests/backend-dbus/mock-login1-seat.h | 16 ++++----- tests/backend-dbus/test-users.cc | 54 ++++++++++++++++++++-------- 7 files changed, 141 insertions(+), 39 deletions(-) (limited to 'tests') diff --git a/tests/backend-dbus/CMakeLists.txt b/tests/backend-dbus/CMakeLists.txt index ed9c93a..e28d8e6 100644 --- a/tests/backend-dbus/CMakeLists.txt +++ b/tests/backend-dbus/CMakeLists.txt @@ -5,7 +5,7 @@ add_library (gtest STATIC set_target_properties (gtest PROPERTIES INCLUDE_DIRECTORIES ${GTEST_INCLUDE_DIR}) -SET (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x") +SET (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x -g ${CC_WARNING_ARGS}") # build desktopmock add_library (desktopmock STATIC diff --git a/tests/backend-dbus/gtest-mock-dbus-fixture.h b/tests/backend-dbus/gtest-mock-dbus-fixture.h index 1b1b895..bab82bf 100644 --- a/tests/backend-dbus/gtest-mock-dbus-fixture.h +++ b/tests/backend-dbus/gtest-mock-dbus-fixture.h @@ -65,6 +65,7 @@ class GTestMockDBusFixture: public GTestDBusFixture dm_seat->set_guest_allowed (false); login1_manager = new MockLogin1Manager (loop, conn); login1_seat = new MockLogin1Seat (loop, conn, true); + g_setenv ("XDG_SEAT", login1_seat->seat_id(), TRUE); login1_manager->add_seat (login1_seat); accounts = build_accounts_mock (); MockUser * user = accounts->find_by_username ("msmith"); diff --git a/tests/backend-dbus/mock-login1-manager.cc b/tests/backend-dbus/mock-login1-manager.cc index 2d341df..0c1acc9 100644 --- a/tests/backend-dbus/mock-login1-manager.cc +++ b/tests/backend-dbus/mock-login1-manager.cc @@ -146,10 +146,50 @@ MockLogin1Manager :: add_seat (MockLogin1Seat * seat) emit_session_new (seat, tag); } +/*** +**** +***/ + +GVariant * +MockLogin1Manager :: list_sessions () const +{ + GVariantBuilder b; + + g_variant_builder_init (&b, G_VARIANT_TYPE("a(susso)")); + + for (auto seat : my_seats) + { + GVariant * seat_sessions = seat->list_sessions (); + + GVariantIter iter; + g_variant_iter_init (&iter, seat_sessions); + GVariant * child; + while ((child = g_variant_iter_next_value (&iter))) + { + g_variant_builder_add_value (&b, child); + g_variant_unref (child); + } + } + + GVariant * v = g_variant_builder_end (&b); + g_message ("%s %s returning %s", G_STRLOC, G_STRFUNC, g_variant_print (v, true)); + return v; +} + /*** **** Skeleton Handlers ***/ +gboolean +MockLogin1Manager :: handle_list_sessions (Login1Manager * m, + GDBusMethodInvocation * inv, + gpointer gself) +{ + GVariant * sessions = static_cast(gself)->list_sessions(); + login1_manager_complete_list_sessions (m, inv, sessions); + return true; +} + gboolean MockLogin1Manager :: handle_can_suspend (Login1Manager * m, GDBusMethodInvocation * inv, @@ -173,7 +213,7 @@ MockLogin1Manager :: handle_can_hibernate (Login1Manager * m, gboolean MockLogin1Manager :: handle_reboot (Login1Manager * m, GDBusMethodInvocation * inv, - gboolean interactive, + gboolean interactive G_GNUC_UNUSED, gpointer gself) { static_cast(gself)->my_last_action = "reboot"; @@ -184,7 +224,7 @@ MockLogin1Manager :: handle_reboot (Login1Manager * m, gboolean MockLogin1Manager :: handle_power_off (Login1Manager * m, GDBusMethodInvocation * inv, - gboolean interactive, + gboolean interactive G_GNUC_UNUSED, gpointer gself) { static_cast(gself)->my_last_action = "power-off"; @@ -195,7 +235,7 @@ MockLogin1Manager :: handle_power_off (Login1Manager * m, gboolean MockLogin1Manager :: handle_suspend (Login1Manager * m, GDBusMethodInvocation * inv, - gboolean interactive, + gboolean interactive G_GNUC_UNUSED, gpointer gself) { static_cast(gself)->my_last_action = "suspend"; @@ -206,7 +246,7 @@ MockLogin1Manager :: handle_suspend (Login1Manager * m, gboolean MockLogin1Manager :: handle_hibernate (Login1Manager * m, GDBusMethodInvocation * inv, - gboolean interactive, + gboolean interactive G_GNUC_UNUSED, gpointer gself) { static_cast(gself)->my_last_action = "hibernate"; @@ -253,6 +293,9 @@ MockLogin1Manager :: MockLogin1Manager (GMainLoop * loop, G_CALLBACK(handle_suspend), this); g_signal_connect (my_skeleton, "handle-hibernate", G_CALLBACK(handle_hibernate), this); + g_signal_connect (my_skeleton, "handle-list-sessions", + G_CALLBACK(handle_list_sessions), this); + #if 0 g_signal_connect (my_skeleton, "handle-get-seats", G_CALLBACK(on_get_seats), this); diff --git a/tests/backend-dbus/mock-login1-manager.h b/tests/backend-dbus/mock-login1-manager.h index da05660..a093f7f 100644 --- a/tests/backend-dbus/mock-login1-manager.h +++ b/tests/backend-dbus/mock-login1-manager.h @@ -47,8 +47,12 @@ class MockLogin1Manager: public MockObject void clear_last_action () { my_last_action.clear(); } private: + void emit_session_new (MockLogin1Seat * seat, int tag) const; + GVariant * list_sessions () const; + + static gboolean handle_list_sessions (Login1Manager *, GDBusMethodInvocation *, gpointer); static gboolean handle_can_suspend (Login1Manager *, GDBusMethodInvocation *, gpointer); static gboolean handle_can_hibernate (Login1Manager *, GDBusMethodInvocation *, gpointer); static gboolean handle_reboot (Login1Manager *, GDBusMethodInvocation *, gboolean, gpointer); diff --git a/tests/backend-dbus/mock-login1-seat.cc b/tests/backend-dbus/mock-login1-seat.cc index a95b9e1..08a18dc 100644 --- a/tests/backend-dbus/mock-login1-seat.cc +++ b/tests/backend-dbus/mock-login1-seat.cc @@ -49,10 +49,10 @@ MockLogin1Seat :: get_session_id_and_path_for_tag (int tag, std::string& id, std { char tmp[80]; - g_snprintf (tmp, sizeof(tmp), "%d", tag); + g_snprintf (tmp, sizeof(tmp), "c%d", tag); id = tmp; - g_snprintf (tmp, sizeof(tmp), "/org/freedesktop/login1/session/%d", tag); + g_snprintf (tmp, sizeof(tmp), "/org/freedesktop/login1/session/%s", id.c_str()); path = tmp; } else @@ -143,10 +143,10 @@ MockLogin1Seat :: sessions () const return ret; } -MockLogin1Seat :: session_tag_t +int MockLogin1Seat :: add_session (MockUser * user) { - const session_tag_t tag = next_session_tag++; + const int tag = next_session_tag++; my_sessions[tag] = user; update_sessions_property (); @@ -155,9 +155,9 @@ MockLogin1Seat :: add_session (MockUser * user) } void -MockLogin1Seat :: remove_session (session_tag_t tag) +MockLogin1Seat :: remove_session (int session_tag) { - my_sessions.erase (tag); + my_sessions.erase (session_tag); update_sessions_property (); } @@ -165,15 +165,43 @@ MockLogin1Seat :: remove_session (session_tag_t tag) **** ***/ +MockUser * +MockLogin1Seat :: active_user () +{ + auto it = my_sessions.find (active_session()); + return it == my_sessions.end() ? NULL : it->second; +} + +const MockUser * +MockLogin1Seat :: active_user () const +{ + auto it = my_sessions.find (active_session()); + return it == my_sessions.end() ? NULL : it->second; +} + +int +MockLogin1Seat :: find_session_for_user (guint uid) const +{ + for (auto it : my_sessions) + if (it.second->uid() == uid) + return it.first; + + return 0; +} + void -MockLogin1Seat :: activate_session (session_tag_t tag) +MockLogin1Seat :: activate_session (int session_tag) { - g_assert (my_sessions.count(tag) == 1); + g_assert (my_sessions.count(session_tag) == 1); - if (my_active_session != tag) + if (my_active_session != session_tag) { - my_active_session = tag; + std::string id, path; + my_active_session = session_tag; + get_session_id_and_path_for_tag (session_tag, id, path); + g_setenv ("XDG_SESSION_ID", id.c_str(), true); update_active_session_property (); + } } @@ -216,8 +244,8 @@ MockLogin1Seat :: MockLogin1Seat (GMainLoop * loop, bool can_activate_sessions): MockObject (loop, bus_connection, BUS_NAME, next_unique_sid()), my_skeleton (login1_seat_skeleton_new ()), - my_can_multi_session (can_activate_sessions), - my_active_session (0) + my_active_session (0), + my_can_multi_session (can_activate_sessions) { set_skeleton (G_DBUS_INTERFACE_SKELETON(my_skeleton)); diff --git a/tests/backend-dbus/mock-login1-seat.h b/tests/backend-dbus/mock-login1-seat.h index 96ff19c..473d21a 100644 --- a/tests/backend-dbus/mock-login1-seat.h +++ b/tests/backend-dbus/mock-login1-seat.h @@ -34,8 +34,6 @@ class MockLogin1Seat: public MockObject { public: - typedef int session_tag_t; - MockLogin1Seat (GMainLoop * loop, GDBusConnection * bus_connection, bool can_activate_sessions); @@ -44,13 +42,16 @@ class MockLogin1Seat: public MockObject const char * seat_id() const { return strrchr(path(),'/')+1; } - session_tag_t add_session (MockUser * user); - void remove_session (session_tag_t session); + int add_session (MockUser * user); + void remove_session (int session_tag); std::set sessions () const; int active_session () const { return my_active_session; } + MockUser * active_user (); + const MockUser * active_user () const; + int find_session_for_user (guint uid) const; bool can_activate_sessions () const { return my_can_multi_session; } - void activate_session (session_tag_t session); + void activate_session (int session_tag); void switch_to_guest (); void switch_to_user (const char * username); @@ -66,11 +67,10 @@ class MockLogin1Seat: public MockObject void update_active_session_property (); void update_can_multi_session_property (); - private: Login1Seat * my_skeleton; - std::map my_sessions; - session_tag_t my_active_session; + std::map my_sessions; + int my_active_session; bool my_can_multi_session; }; diff --git a/tests/backend-dbus/test-users.cc b/tests/backend-dbus/test-users.cc index bd0547d..fa52df3 100644 --- a/tests/backend-dbus/test-users.cc +++ b/tests/backend-dbus/test-users.cc @@ -153,23 +153,25 @@ 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); + const MockUser * active_user = login1_seat->active_user (); 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); + IndicatorSessionUser * isu = indicator_session_users_get_user (users, keys[i]); + MockUser * mu = accounts->find_by_uid (isu->uid); + const bool is_logged_in = login1_seat->find_session_for_user (isu->uid) != 0; + const bool is_active = active_user && (active_user->uid() == isu->uid); + compare_user (isu, mu, is_logged_in, is_active); + + indicator_session_user_free (isu); } g_strfreev (keys); @@ -187,7 +189,9 @@ TEST_F (Users, UserAdded) 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()); + IndicatorSessionUser * isu = indicator_session_users_get_user (users, event_keys[0].c_str()); + ASSERT_EQ (mu->uid(), isu->uid); + indicator_session_user_free (isu); compare_user (event_keys[0], mu, false, false); } @@ -197,24 +201,45 @@ TEST_F (Users, UserAdded) */ TEST_F (Users, UserRemoved) { - MockUser * mu; + MockUser * mu = accounts->find_by_username ("pdavison"); - mu = accounts->find_by_username ("pdavison"); + /* confirm that users knows about pdavison */ + bool found = false; + GStrv keys = indicator_session_users_get_keys (users); + ASSERT_EQ (12, g_strv_length (keys)); + for (int i=0; !found && keys && keys[i]; i++) + { + IndicatorSessionUser * isu = indicator_session_users_get_user (users, keys[i]); + found = isu->uid == mu->uid(); + indicator_session_user_free (isu); + } + g_strfreev (keys); + ASSERT_TRUE (found); + + /* on the bus, remove pdavison. */ accounts->remove_user (mu); + + /* now, users should emit a 'user removed' signal... */ 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); + /* confirm that users doesn't know about pdavison */ + keys = indicator_session_users_get_keys (users); ASSERT_EQ (11, g_strv_length (keys)); + for (int i=0; keys && keys[i]; i++) + { + IndicatorSessionUser * isu = indicator_session_users_get_user (users, keys[i]); + ASSERT_NE (event_keys[0], keys[i]); + ASSERT_NE (mu->uid(), isu->uid); + indicator_session_user_free (isu); + } g_strfreev (keys); - ASSERT_TRUE (indicator_session_users_get_user (users, mu->path()) == NULL); - delete mu; } +#if 0 /** * Confirm that 'users' notices when a user's real name changes */ @@ -368,10 +393,11 @@ TEST_F (Users, LiveSession) 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); + 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); } +#endif -- cgit v1.2.3 From c572f9940e9c0081e281d13e8f8038dcb1b92c3c Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Tue, 25 Jun 2013 01:07:36 -0500 Subject: in tests-users, fix 3 more tests: RealnameChanged, LogInLogOut, ActivateSession --- tests/backend-dbus/mock-accounts.cc | 5 +- tests/backend-dbus/mock-login1-manager.cc | 25 ++++++-- tests/backend-dbus/mock-login1-manager.h | 2 + tests/backend-dbus/mock-login1-seat.cc | 22 ++----- tests/backend-dbus/mock-login1-seat.h | 5 +- tests/backend-dbus/test-users.cc | 95 +++++++++++++------------------ 6 files changed, 69 insertions(+), 85 deletions(-) (limited to 'tests') diff --git a/tests/backend-dbus/mock-accounts.cc b/tests/backend-dbus/mock-accounts.cc index c753c79..16e37d2 100644 --- a/tests/backend-dbus/mock-accounts.cc +++ b/tests/backend-dbus/mock-accounts.cc @@ -107,9 +107,8 @@ MockAccounts :: on_list_cached_users_static (Accounts * a, 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(); + for (auto it : users) + paths[i++] = it->path(); accounts_complete_list_cached_users (a, invocation, paths); g_free (paths); diff --git a/tests/backend-dbus/mock-login1-manager.cc b/tests/backend-dbus/mock-login1-manager.cc index 0c1acc9..3bd652b 100644 --- a/tests/backend-dbus/mock-login1-manager.cc +++ b/tests/backend-dbus/mock-login1-manager.cc @@ -121,7 +121,6 @@ MockLogin1Manager :: emit_session_new (MockLogin1Seat * seat, int tag) const seat->get_session_id_and_path_for_tag (tag, id, path); -g_message ("%s %s emitting session-new [%s][%s]", G_STRLOC, G_STRFUNC, id.c_str(), path.c_str()); login1_manager_emit_session_new (my_skeleton, id.c_str(), path.c_str()); } @@ -130,9 +129,27 @@ MockLogin1Manager :: add_session (MockLogin1Seat * seat, MockUser * user) { g_assert (my_seats.count(seat) == 1); - const int tag = seat->add_session (user); - emit_session_new (seat, tag); - return tag; + const int session_tag = seat->add_session (user); + emit_session_new (seat, session_tag); + return session_tag; +} + +void +MockLogin1Manager :: emit_session_removed (MockLogin1Seat * seat, int tag) const +{ + std::string id; + std::string path; + + seat->get_session_id_and_path_for_tag (tag, id, path); + + login1_manager_emit_session_removed (my_skeleton, id.c_str(), path.c_str()); +} + +void +MockLogin1Manager :: remove_session (MockLogin1Seat * seat, int session_tag) +{ + seat->remove_session (session_tag); + emit_session_removed (seat, session_tag); } void diff --git a/tests/backend-dbus/mock-login1-manager.h b/tests/backend-dbus/mock-login1-manager.h index a093f7f..f630329 100644 --- a/tests/backend-dbus/mock-login1-manager.h +++ b/tests/backend-dbus/mock-login1-manager.h @@ -37,6 +37,7 @@ class MockLogin1Manager: public MockObject virtual ~MockLogin1Manager (); int add_session (MockLogin1Seat * seat, MockUser * user); + void remove_session (MockLogin1Seat * seat, int session_tag); void add_seat (MockLogin1Seat * seat); @@ -49,6 +50,7 @@ class MockLogin1Manager: public MockObject private: void emit_session_new (MockLogin1Seat * seat, int tag) const; + void emit_session_removed (MockLogin1Seat * seat, int tag) const; GVariant * list_sessions () const; diff --git a/tests/backend-dbus/mock-login1-seat.cc b/tests/backend-dbus/mock-login1-seat.cc index 08a18dc..46dc5f9 100644 --- a/tests/backend-dbus/mock-login1-seat.cc +++ b/tests/backend-dbus/mock-login1-seat.cc @@ -165,28 +165,14 @@ MockLogin1Seat :: remove_session (int session_tag) **** ***/ -MockUser * -MockLogin1Seat :: active_user () -{ - auto it = my_sessions.find (active_session()); - return it == my_sessions.end() ? NULL : it->second; -} - -const MockUser * -MockLogin1Seat :: active_user () const -{ - auto it = my_sessions.find (active_session()); - return it == my_sessions.end() ? NULL : it->second; -} - -int -MockLogin1Seat :: find_session_for_user (guint uid) const +std::string +MockLogin1Seat :: user_state (unsigned int uid) const { for (auto it : my_sessions) if (it.second->uid() == uid) - return it.first; + return it.first == my_active_session ? "active" : "online"; - return 0; + return "offline"; // no matching session } void diff --git a/tests/backend-dbus/mock-login1-seat.h b/tests/backend-dbus/mock-login1-seat.h index 473d21a..254ebe9 100644 --- a/tests/backend-dbus/mock-login1-seat.h +++ b/tests/backend-dbus/mock-login1-seat.h @@ -46,9 +46,8 @@ class MockLogin1Seat: public MockObject void remove_session (int session_tag); std::set sessions () const; int active_session () const { return my_active_session; } - MockUser * active_user (); - const MockUser * active_user () const; - int find_session_for_user (guint uid) const; + + std::string user_state (unsigned int uid) const; bool can_activate_sessions () const { return my_can_multi_session; } void activate_session (int session_tag); diff --git a/tests/backend-dbus/test-users.cc b/tests/backend-dbus/test-users.cc index fa52df3..d477a4f 100644 --- a/tests/backend-dbus/test-users.cc +++ b/tests/backend-dbus/test-users.cc @@ -62,31 +62,23 @@ class Users: public GTestMockDBusFixture protected: - void compare_user (const IndicatorSessionUser * isu, - MockUser * mu, - bool is_logged_in, - bool is_current_user) + void compare_user (const MockUser * mu, const IndicatorSessionUser * isu, const std::string& user_state) { - ASSERT_TRUE (isu != 0); - ASSERT_TRUE (mu != 0); - + ASSERT_EQ (user_state, login1_seat->user_state (mu->uid())); 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? + ASSERT_EQ (mu->uid(), isu->uid); + ASSERT_EQ (user_state!="offline", isu->is_logged_in); + ASSERT_EQ (user_state=="active", isu->is_current_user); } - void compare_user (const std::string & key, - MockUser * mu, - bool is_logged_in, - bool is_current_user) + void compare_user (const MockUser * mu, const std::string& key, const std::string& user_state) { IndicatorSessionUser * isu; isu = indicator_session_users_get_user (users, key.c_str()); - compare_user (isu, mu, is_logged_in, is_current_user); + compare_user (mu, isu, user_state); indicator_session_user_free (isu); } @@ -159,7 +151,6 @@ TEST_F (Users, HelloWorld) TEST_F (Users, InitialUsers) { GStrv keys = indicator_session_users_get_keys (users); - const MockUser * active_user = login1_seat->active_user (); ASSERT_EQ (12, g_strv_length (keys)); @@ -167,10 +158,7 @@ TEST_F (Users, InitialUsers) { IndicatorSessionUser * isu = indicator_session_users_get_user (users, keys[i]); MockUser * mu = accounts->find_by_uid (isu->uid); - const bool is_logged_in = login1_seat->find_session_for_user (isu->uid) != 0; - const bool is_active = active_user && (active_user->uid() == isu->uid); - compare_user (isu, mu, is_logged_in, is_active); - + compare_user (mu, isu, login1_seat->user_state (isu->uid)); indicator_session_user_free (isu); } @@ -189,11 +177,7 @@ TEST_F (Users, UserAdded) ASSERT_EQ (0, event_keys.size()); wait_for_signals (users, INDICATOR_SESSION_USERS_SIGNAL_USER_ADDED, 1); ASSERT_EQ (1, event_keys.size()); - IndicatorSessionUser * isu = indicator_session_users_get_user (users, event_keys[0].c_str()); - ASSERT_EQ (mu->uid(), isu->uid); - indicator_session_user_free (isu); - - compare_user (event_keys[0], mu, false, false); + compare_user (mu, event_keys[0], "offline"); } /** @@ -239,7 +223,6 @@ TEST_F (Users, UserRemoved) delete mu; } -#if 0 /** * Confirm that 'users' notices when a user's real name changes */ @@ -254,8 +237,7 @@ TEST_F (Users, RealnameChanged) 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); + compare_user (mu, event_keys[0], "offline"); } /** @@ -266,21 +248,18 @@ 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); + ASSERT_EQ (login1_seat->user_state (mu->uid()), "offline"); + const int session_tag = login1_manager->add_session (login1_seat, 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); + ASSERT_EQ (1, event_keys.size()); + compare_user (mu, event_keys[0], "online"); - // The fist doctor logs out. + // The first doctor logs out. // Confirm that 'users' notices. - ck_seat->remove_session (session); + login1_manager->remove_session (login1_seat, session_tag); 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; + compare_user (mu, event_keys[0], "offline"); } /** @@ -288,34 +267,36 @@ TEST_F (Users, LogInLogOut) */ 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); + // confirm preconditions: msmith is active, msmith is offline + MockUser * const whartnell = accounts->find_by_username ("whartnell"); + ASSERT_EQ (login1_seat->user_state (whartnell->uid()), "offline"); + MockUser * const msmith = accounts->find_by_username ("msmith"); + ASSERT_EQ (login1_seat->user_state (msmith->uid()), "active"); + + // whartnell logs in... confirm that 'users' notices + login1_manager->add_session (login1_seat, whartnell); 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); + ASSERT_EQ (1, event_keys.size()); + compare_user (whartnell, event_keys[0], "online"); - // 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); + // activate whartnell's session... confirm that 'users' sees: + // 1. msmith changes from 'active' to 'online' + // 2. whartnell changes from 'online' to 'active' + login1_seat->switch_to_user (whartnell->username()); 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); + compare_user (msmith, event_keys[0], "online"); + compare_user (whartnell, event_keys[1], "active"); - // 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); + // reverse the test + login1_seat->switch_to_user (msmith->username()); 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); + compare_user (whartnell, event_keys[0], "online"); + compare_user (msmith, event_keys[1], "active"); } +#if 0 /** * Confirm that we can change the active session via users' API. * This is nearly the same as ActivateSession but uses users' API -- cgit v1.2.3 From b3938a1f14b687d62ad1d6e4e27bac47f58722de Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Tue, 25 Jun 2013 11:16:34 -0500 Subject: in IndicatorSessionUsers, use the uid as the user's key. Users.ActivateUser is now green. --- tests/CMakeLists.txt | 4 +- tests/backend-dbus/test-users.cc | 114 +++++++++++++++++++-------------------- tests/backend-mock-users.c | 52 +++++++----------- tests/backend-mock-users.h | 3 +- tests/test-service.cc | 43 +++++++-------- 5 files changed, 99 insertions(+), 117 deletions(-) (limited to 'tests') diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 45ab69f..f67008e 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -39,13 +39,13 @@ add_library (backendmock STATIC backend-mock-guest.h backend-mock-users.c backend-mock-users.h) -set_target_properties (backendmock PROPERTIES COMPILE_FLAGS " ${CC_WARNING_ARGS}") +set_target_properties (backendmock PROPERTIES COMPILE_FLAGS " ${CC_WARNING_ARGS} -std=c++0x -g") # test-service add_executable (test-service test-service.cc gschemas.compiled) -set_target_properties (test-service PROPERTIES COMPILE_FLAGS " ${CC_WARNING_ARGS}") +set_target_properties (test-service PROPERTIES COMPILE_FLAGS " ${CC_WARNING_ARGS} -std=c++0x -g") add_test (test-service test-service) add_dependencies (test-service libindicatorsessionservice backendmock) target_link_libraries (test-service libindicatorsessionservice backendmock gtest ${SERVICE_LIBRARIES} ${GTEST_LIBS}) diff --git a/tests/backend-dbus/test-users.cc b/tests/backend-dbus/test-users.cc index d477a4f..279796e 100644 --- a/tests/backend-dbus/test-users.cc +++ b/tests/backend-dbus/test-users.cc @@ -74,10 +74,18 @@ class Users: public GTestMockDBusFixture ASSERT_EQ (user_state=="active", isu->is_current_user); } - void compare_user (const MockUser * mu, const std::string& key, const std::string& user_state) + void compare_user (const MockUser * mu, guint uid, const std::string& user_state) { IndicatorSessionUser * isu; - isu = indicator_session_users_get_user (users, key.c_str()); + isu = indicator_session_users_get_user (users, uid); + compare_user (mu, isu, user_state); + indicator_session_user_free (isu); + } + + void compare_user (guint uid, const std::string& user_state) + { + IndicatorSessionUser * isu = indicator_session_users_get_user (users, uid); + MockUser * mu = accounts->find_by_uid (uid); compare_user (mu, isu, user_state); indicator_session_user_free (isu); } @@ -99,12 +107,12 @@ class Users: public GTestMockDBusFixture static void wait_for_signals__event (IndicatorSessionUser * u G_GNUC_UNUSED, - const char * key, - gpointer gself) + guint uid, + gpointer gself) { Users * self = static_cast(gself); - self->event_keys.push_back (key); + self->event_keys.push_back (uid); if (self->event_keys.size() == self->expected_event_count) g_main_loop_quit (self->loop); @@ -112,7 +120,7 @@ class Users: public GTestMockDBusFixture protected: - std::vector event_keys; + std::vector event_keys; size_t expected_event_count; void wait_for_signals (gpointer o, const gchar * name, size_t n) @@ -150,19 +158,18 @@ TEST_F (Users, HelloWorld) */ TEST_F (Users, InitialUsers) { - GStrv keys = indicator_session_users_get_keys (users); + GList * l; + GList * uids = indicator_session_users_get_uids (users); - ASSERT_EQ (12, g_strv_length (keys)); + ASSERT_EQ (12, g_list_length (uids)); - for (int i=0; keys && keys[i]; ++i) + for (l=uids; l!=NULL; l=l->next) { - IndicatorSessionUser * isu = indicator_session_users_get_user (users, keys[i]); - MockUser * mu = accounts->find_by_uid (isu->uid); - compare_user (mu, isu, login1_seat->user_state (isu->uid)); - indicator_session_user_free (isu); + const guint uid = GPOINTER_TO_UINT (l->data); + compare_user (uid, login1_seat->user_state (uid)); } - g_strfreev (keys); + g_list_free (uids); } /** @@ -188,17 +195,10 @@ TEST_F (Users, UserRemoved) MockUser * mu = accounts->find_by_username ("pdavison"); /* confirm that users knows about pdavison */ - bool found = false; - GStrv keys = indicator_session_users_get_keys (users); - ASSERT_EQ (12, g_strv_length (keys)); - for (int i=0; !found && keys && keys[i]; i++) - { - IndicatorSessionUser * isu = indicator_session_users_get_user (users, keys[i]); - found = isu->uid == mu->uid(); - indicator_session_user_free (isu); - } - g_strfreev (keys); - ASSERT_TRUE (found); + IndicatorSessionUser * isu = indicator_session_users_get_user (users, mu->uid()); + ASSERT_TRUE (isu != NULL); + compare_user (mu, isu, "offline"); + g_clear_pointer (&isu, indicator_session_user_free); /* on the bus, remove pdavison. */ accounts->remove_user (mu); @@ -208,17 +208,14 @@ TEST_F (Users, UserRemoved) wait_for_signals (users, INDICATOR_SESSION_USERS_SIGNAL_USER_REMOVED, 1); ASSERT_EQ (1, event_keys.size()); - /* confirm that users doesn't know about pdavison */ - keys = indicator_session_users_get_keys (users); - ASSERT_EQ (11, g_strv_length (keys)); - for (int i=0; keys && keys[i]; i++) - { - IndicatorSessionUser * isu = indicator_session_users_get_user (users, keys[i]); - ASSERT_NE (event_keys[0], keys[i]); - ASSERT_NE (mu->uid(), isu->uid); - indicator_session_user_free (isu); - } - g_strfreev (keys); + /* confirm that users won't give us pdavison's info */ + isu = indicator_session_users_get_user (users, mu->uid()); + ASSERT_TRUE (isu == NULL); + + /* confirm that users won't give us pdavison's uid */ + GList * uids = indicator_session_users_get_uids (users); + ASSERT_TRUE (g_list_find (uids, GUINT_TO_POINTER(mu->uid())) == NULL); + g_list_free (uids); delete mu; } @@ -296,43 +293,42 @@ TEST_F (Users, ActivateSession) compare_user (msmith, event_keys[1], "active"); } -#if 0 /** * 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); + // confirm preconditions: msmith is active, msmith is offline + MockUser * const whartnell = accounts->find_by_username ("whartnell"); + ASSERT_EQ (login1_seat->user_state (whartnell->uid()), "offline"); + MockUser * const msmith = accounts->find_by_username ("msmith"); + ASSERT_EQ (login1_seat->user_state (msmith->uid()), "active"); + + // whartnell logs in... confirm that 'users' notices + login1_manager->add_session (login1_seat, whartnell); 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); + ASSERT_EQ (1, event_keys.size()); + compare_user (whartnell, event_keys[0], "online"); - // 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); + // activate whartnell's session... confirm that 'users' sees: + // 1. msmith changes from 'active' to 'online' + // 2. whartnell changes from 'online' to 'active' + indicator_session_users_activate_user (users, whartnell->uid()); 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); + compare_user (msmith, event_keys[0], "online"); + compare_user (whartnell, event_keys[1], "active"); + + // reverse the test + indicator_session_users_activate_user (users, msmith->uid()); 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); + compare_user (whartnell, event_keys[0], "online"); + compare_user (msmith, event_keys[1], "active"); } +#if 0 /** * Confirm that adding a Guest doesn't show up in the users list */ diff --git a/tests/backend-mock-users.c b/tests/backend-mock-users.c index d9ab5a8..bae40b6 100644 --- a/tests/backend-mock-users.c +++ b/tests/backend-mock-users.c @@ -58,35 +58,21 @@ my_is_live_session (IndicatorSessionUsers * users G_GNUC_UNUSED) } static void -my_activate_user (IndicatorSessionUsers * users, const char * key) +my_activate_user (IndicatorSessionUsers * users, guint uid) { - g_message ("%s %s users %p key %s FIXME", G_STRLOC, G_STRFUNC, (void*)users, key); + g_message ("%s %s users %p uid %u FIXME", G_STRLOC, G_STRFUNC, (void*)users, uid); } -static GStrv -my_get_keys (IndicatorSessionUsers * users) +static GList * +my_get_uids (IndicatorSessionUsers * users) { - int i; - priv_t * p; - gchar ** keys; - GHashTableIter iter; - gpointer key; - g_return_val_if_fail (INDICATOR_IS_SESSION_USERS_MOCK(users), NULL); - p = INDICATOR_SESSION_USERS_MOCK (users)->priv; - - i = 0; - keys = g_new (gchar*, g_hash_table_size(p->users)+1); - g_hash_table_iter_init (&iter, p->users); - while (g_hash_table_iter_next (&iter, &key, NULL)) - keys[i++] = g_strdup (key); - keys[i] = NULL; - return keys; + return g_hash_table_get_keys (INDICATOR_SESSION_USERS_MOCK(users)->priv->users); } static IndicatorSessionUser * -my_get_user (IndicatorSessionUsers * self, const gchar * key) +my_get_user (IndicatorSessionUsers * self, guint uid) { priv_t * p; const IndicatorSessionUser * src; @@ -95,7 +81,7 @@ my_get_user (IndicatorSessionUsers * self, const gchar * key) g_return_val_if_fail (INDICATOR_IS_SESSION_USERS_MOCK(self), NULL); p = INDICATOR_SESSION_USERS_MOCK (self)->priv; - if ((src = g_hash_table_lookup (p->users, key))) + if ((src = g_hash_table_lookup (p->users, GUINT_TO_POINTER(uid)))) { ret = g_new0 (IndicatorSessionUser, 1); ret->is_current_user = src->is_current_user; @@ -123,7 +109,7 @@ indicator_session_users_mock_class_init (IndicatorSessionUsersMockClass * klass) users_class = INDICATOR_SESSION_USERS_CLASS (klass); users_class->is_live_session = my_is_live_session; - users_class->get_keys = my_get_keys; + users_class->get_uids = my_get_uids; users_class->get_user = my_get_user; users_class->activate_user = my_activate_user; @@ -141,9 +127,9 @@ indicator_session_users_mock_init (IndicatorSessionUsersMock * self) IndicatorSessionUsersMockPriv); self->priv = p; - p->users = g_hash_table_new_full (g_str_hash, - g_str_equal, - g_free, + p->users = g_hash_table_new_full (g_direct_hash, + g_direct_equal, + NULL, (GDestroyNotify)indicator_session_user_free); g_signal_connect_swapped (mock_settings, "changed::is-live-session", @@ -165,25 +151,25 @@ indicator_session_users_mock_new (void) void indicator_session_users_mock_add_user (IndicatorSessionUsersMock * self, - const char * key, IndicatorSessionUser * user) { g_return_if_fail (INDICATOR_IS_SESSION_USERS_MOCK (self)); - g_return_if_fail (key && *key); g_return_if_fail (user != NULL); + g_return_if_fail (user->uid > 0); + g_return_if_fail (!g_hash_table_contains (self->priv->users, GUINT_TO_POINTER(user->uid))); - g_hash_table_insert (self->priv->users, g_strdup(key), user); - indicator_session_users_added (INDICATOR_SESSION_USERS (self), key); + g_hash_table_insert (self->priv->users, GUINT_TO_POINTER(user->uid), user); + indicator_session_users_added (INDICATOR_SESSION_USERS (self), user->uid); } void indicator_session_users_mock_remove_user (IndicatorSessionUsersMock * self, - const char * key) + guint uid) { g_return_if_fail (INDICATOR_IS_SESSION_USERS_MOCK (self)); - g_return_if_fail (key && *key); + g_return_if_fail (uid > 0); - g_hash_table_remove (self->priv->users, key); - indicator_session_users_removed (INDICATOR_SESSION_USERS (self), key); + g_hash_table_remove (self->priv->users, GUINT_TO_POINTER(uid)); + indicator_session_users_removed (INDICATOR_SESSION_USERS (self), uid); } diff --git a/tests/backend-mock-users.h b/tests/backend-mock-users.h index c7e11d3..7470ec9 100644 --- a/tests/backend-mock-users.h +++ b/tests/backend-mock-users.h @@ -57,11 +57,10 @@ GType indicator_session_users_mock_get_type (void); IndicatorSessionUsers * indicator_session_users_mock_new (void); void indicator_session_users_mock_add_user (IndicatorSessionUsersMock * self, - const char * key, IndicatorSessionUser * user); void indicator_session_users_mock_remove_user (IndicatorSessionUsersMock * self, - const char * key); + guint uid); diff --git a/tests/test-service.cc b/tests/test-service.cc index 2d81441..996332d 100644 --- a/tests/test-service.cc +++ b/tests/test-service.cc @@ -619,23 +619,24 @@ TEST_F (ServiceTest, User) const char * const error_key = "has-online-account-error"; const char * const show_name_key = "show-real-name-on-panel"; - const struct { + struct { + guint uid; guint64 login_frequency; const gchar * user_name; const gchar * real_name; } account_info[] = { - { 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" } + { 101, 134, "whartnell", "First Doctor" }, + { 102, 119, "ptroughton", "Second Doctor" }, + { 103, 128, "jpertwee", "Third Doctor" }, + { 104, 172, "tbaker", "Fourth Doctor" }, + { 105, 69, "pdavison", "Fifth Doctor" }, + { 106, 31, "cbaker", "Sixth Doctor" }, + { 107, 42, "smccoy", "Seventh Doctor" }, + { 108, 1, "pmcgann", "Eigth Doctor" }, + { 109, 13, "ceccleston", "Ninth Doctor" }, + { 110, 47, "dtennant", "Tenth Doctor" }, + { 111, 34, "msmith", "Eleventh Doctor" }, + { 201, 1, "rhurndall", "First Doctor" } }; // Find the switcher menu model. @@ -656,11 +657,11 @@ TEST_F (ServiceTest, User) IndicatorSessionUser * u = g_new0 (IndicatorSessionUser, 1); u->is_current_user = false; u->is_logged_in = false; - u->uid = 101 + i; + u->uid = account_info[i].uid; u->login_frequency = account_info[i].login_frequency; u->user_name = g_strdup (account_info[i].user_name); u->real_name = g_strdup (account_info[i].real_name); - indicator_session_users_mock_add_user (INDICATOR_SESSION_USERS_MOCK(mock_users), u->user_name, u); + indicator_session_users_mock_add_user (INDICATOR_SESSION_USERS_MOCK(mock_users), u); users[i] = u; } @@ -679,8 +680,8 @@ TEST_F (ServiceTest, User) g_clear_object (&switch_menu); // now remove a couple of 'em - indicator_session_users_mock_remove_user (INDICATOR_SESSION_USERS_MOCK(mock_users), account_info[3].user_name); - indicator_session_users_mock_remove_user (INDICATOR_SESSION_USERS_MOCK(mock_users), account_info[4].user_name); + indicator_session_users_mock_remove_user (INDICATOR_SESSION_USERS_MOCK(mock_users), account_info[3].uid); + indicator_session_users_mock_remove_user (INDICATOR_SESSION_USERS_MOCK(mock_users), account_info[4].uid); wait_for_menu_resync (); @@ -697,7 +698,7 @@ TEST_F (ServiceTest, User) // now let's have the third one be the current user users[2]->is_current_user = true; users[2]->is_logged_in = true; - indicator_session_users_changed (mock_users, users[2]->user_name); + indicator_session_users_changed (mock_users, users[2]->uid); wait_for_menu_resync (); @@ -749,14 +750,14 @@ TEST_F (ServiceTest, User) u->login_frequency = account_info[i].login_frequency; u->user_name = g_strdup (account_info[i].user_name); u->real_name = g_strdup (account_info[i].real_name); - indicator_session_users_mock_add_user (INDICATOR_SESSION_USERS_MOCK(mock_users), u->user_name, u); + indicator_session_users_mock_add_user (INDICATOR_SESSION_USERS_MOCK(mock_users), u); users[i] = u; } users[2]->is_current_user = false; - indicator_session_users_changed (mock_users, users[2]->user_name); + indicator_session_users_changed (mock_users, users[2]->uid); users[10]->is_current_user = true; users[10]->is_logged_in = true; - indicator_session_users_changed (mock_users, users[10]->user_name); + indicator_session_users_changed (mock_users, users[10]->uid); wait_for_menu_resync (); ASSERT_TRUE (find_menu_item_for_action ("indicator.switch-to-greeter", &switch_menu, &pos)); ASSERT_EQ (0, pos); -- cgit v1.2.3 From d16aaaed46f3381ea7ac4cb4c2cb491d4c5d2e03 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Tue, 25 Jun 2013 11:39:46 -0500 Subject: all the tests in test-users pass --- tests/backend-dbus/test-users.cc | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) (limited to 'tests') diff --git a/tests/backend-dbus/test-users.cc b/tests/backend-dbus/test-users.cc index 279796e..b022984 100644 --- a/tests/backend-dbus/test-users.cc +++ b/tests/backend-dbus/test-users.cc @@ -328,26 +328,25 @@ TEST_F (Users, ActivateUser) compare_user (msmith, event_keys[1], "active"); } -#if 0 /** * Confirm that adding a Guest doesn't show up in the users list */ TEST_F (Users, UnwantedGuest) { - GStrv keys; + GList * uids; - keys = indicator_session_users_get_keys (users); - const size_t n = g_strv_length (keys); - g_strfreev (keys); + uids = indicator_session_users_get_uids (users); + const size_t n = g_list_length (uids); + g_list_free (uids); 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); + uids = indicator_session_users_get_uids (users); + ASSERT_EQ (n, g_list_length (uids)); + g_list_free (uids); } @@ -367,9 +366,9 @@ TEST_F (Users, LiveSession) 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); + const int session_tag = login1_manager->add_session (login1_seat, live_user); wait_msec (100); - ck_seat->activate_session (session); + login1_seat->activate_session (session_tag); wait_for_signal (users, "notify::" INDICATOR_SESSION_USERS_PROP_IS_LIVE_SESSION); // confirm the backend thinks it's a live session @@ -377,4 +376,3 @@ TEST_F (Users, LiveSession) g_object_get (users, INDICATOR_SESSION_USERS_PROP_IS_LIVE_SESSION, &b, NULL); ASSERT_TRUE (b); } -#endif -- cgit v1.2.3 From 7a995d0b4466b12be7b7f297d1f964aae37eae63 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Tue, 25 Jun 2013 12:31:53 -0500 Subject: don't pass -std=c++0x to the compiler when building .c files --- tests/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'tests') diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index f67008e..33009a3 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -39,7 +39,7 @@ add_library (backendmock STATIC backend-mock-guest.h backend-mock-users.c backend-mock-users.h) -set_target_properties (backendmock PROPERTIES COMPILE_FLAGS " ${CC_WARNING_ARGS} -std=c++0x -g") +set_target_properties (backendmock PROPERTIES COMPILE_FLAGS " ${CC_WARNING_ARGS} -g") # test-service add_executable (test-service -- cgit v1.2.3 From 9ed837fbafae3021d597508ba9272a54d3623534 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Tue, 25 Jun 2013 12:32:16 -0500 Subject: test-guest's HelloWorld and Allowed tests now pass --- tests/backend-dbus/test-guest.cc | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) (limited to 'tests') diff --git a/tests/backend-dbus/test-guest.cc b/tests/backend-dbus/test-guest.cc index db55dd1..c5f948a 100644 --- a/tests/backend-dbus/test-guest.cc +++ b/tests/backend-dbus/test-guest.cc @@ -66,14 +66,11 @@ class Guest: public GTestMockDBusFixture protected: void add_mock_guest (MockUser *& guest_user, - MockConsoleKitSession *& guest_session) + int & guest_session_tag) { 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); + guest_session_tag = login1_manager->add_session (login1_seat, guest_user); } }; @@ -104,6 +101,7 @@ TEST_F (Guest, Allowed) ASSERT_FALSE (indicator_session_guest_is_active (guest)); } +#if 0 /** * Have a guest user log in & out. * Confirm that "guest" reflects the changes. @@ -190,3 +188,4 @@ TEST_F (Guest, Activate) ASSERT_EQ (guest_session, ck_manager->current_session()); wait_msec (50); } +#endif -- cgit v1.2.3 From 95e99abc20dc4e0c5de390e11f467d44dd05cba0 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Tue, 25 Jun 2013 12:59:32 -0500 Subject: TestGuest::Login now passes --- tests/backend-dbus/test-guest.cc | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'tests') diff --git a/tests/backend-dbus/test-guest.cc b/tests/backend-dbus/test-guest.cc index c5f948a..ea04f3c 100644 --- a/tests/backend-dbus/test-guest.cc +++ b/tests/backend-dbus/test-guest.cc @@ -68,7 +68,8 @@ class Guest: public GTestMockDBusFixture void add_mock_guest (MockUser *& guest_user, int & guest_session_tag) { - guest_user = new MockUser (loop, conn, "guest-jjbEVV", "Guest", 10); + guest_user = new MockUser (loop, conn, "guest-jjbEVV", "Guest", 10, 100); + accounts->add_user (guest_user); guest_user->set_system_account (true); guest_session_tag = login1_manager->add_session (login1_seat, guest_user); } @@ -101,7 +102,6 @@ TEST_F (Guest, Allowed) ASSERT_FALSE (indicator_session_guest_is_active (guest)); } -#if 0 /** * Have a guest user log in & out. * Confirm that "guest" reflects the changes. @@ -115,8 +115,8 @@ TEST_F (Guest, Login) // 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); + int session_tag; + add_mock_guest (guest_user, session_tag); 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)); @@ -126,10 +126,9 @@ TEST_F (Guest, Login) // Log the Guest User out // and confirm that guest's is_login changes to false - ck_seat->remove_session (guest_session); + login1_manager->remove_session (login1_seat, session_tag); 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)); @@ -138,6 +137,7 @@ TEST_F (Guest, Login) ASSERT_FALSE (indicator_session_guest_is_active (guest)); } +#if 0 /** * Activate a Guest session, then activate a different session. * Confirm that "guest" reflects the changes. -- cgit v1.2.3 From 3cabab005963648247d8af74645c8e245efe13e8 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Tue, 25 Jun 2013 13:36:02 -0500 Subject: everything in test-guest now passes --- tests/backend-dbus/test-guest.cc | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) (limited to 'tests') diff --git a/tests/backend-dbus/test-guest.cc b/tests/backend-dbus/test-guest.cc index ea04f3c..f71d445 100644 --- a/tests/backend-dbus/test-guest.cc +++ b/tests/backend-dbus/test-guest.cc @@ -137,7 +137,6 @@ TEST_F (Guest, Login) ASSERT_FALSE (indicator_session_guest_is_active (guest)); } -#if 0 /** * Activate a Guest session, then activate a different session. * Confirm that "guest" reflects the changes. @@ -145,15 +144,16 @@ TEST_F (Guest, Login) TEST_F (Guest, Active) { gboolean b; + const int user_session_tag = login1_seat->active_session(); dm_seat->set_guest_allowed (true); MockUser * guest_user; - MockConsoleKitSession * guest_session; - add_mock_guest (guest_user, guest_session); + int guest_session_tag; + add_mock_guest (guest_user, guest_session_tag); // Activate the guest session // and confirm that guest's is_active changes to true - ck_seat->activate_session (guest_session); + login1_seat->activate_session (guest_session_tag); 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)); @@ -163,7 +163,7 @@ TEST_F (Guest, Active) // Activate a non-guest session // and confirm that guest's is_active changes to false - ck_seat->activate_session (ck_session); + login1_seat->activate_session (user_session_tag); 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)); @@ -179,13 +179,15 @@ TEST_F (Guest, Active) TEST_F (Guest, Activate) { dm_seat->set_guest_allowed (true); + wait_for_signal (guest, "notify::guest-is-allowed"); + MockUser * guest_user; - MockConsoleKitSession * guest_session; - add_mock_guest (guest_user, guest_session); + int guest_session_tag; + add_mock_guest (guest_user, guest_session_tag); 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_for_signal (login1_seat->skeleton(), "notify::active-session"); + ASSERT_EQ (guest_session_tag, login1_seat->active_session()); wait_msec (50); } -#endif -- cgit v1.2.3 From 660d7bcbbddf3ea10146b47d3bf5c64899b8b2e0 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Tue, 25 Jun 2013 13:53:25 -0500 Subject: copyediting: remove g_messages() added while fixing the tests, remove dead code --- tests/backend-dbus/mock-end-session-dialog.cc | 28 -------- tests/backend-dbus/mock-login1-manager.cc | 96 +-------------------------- tests/backend-dbus/mock-login1-seat.cc | 6 +- tests/backend-dbus/mock-user.cc | 8 --- tests/backend-dbus/mock-webcredentials.cc | 15 ----- 5 files changed, 4 insertions(+), 149 deletions(-) (limited to 'tests') diff --git a/tests/backend-dbus/mock-end-session-dialog.cc b/tests/backend-dbus/mock-end-session-dialog.cc index 903962b..2772423 100644 --- a/tests/backend-dbus/mock-end-session-dialog.cc +++ b/tests/backend-dbus/mock-end-session-dialog.cc @@ -19,28 +19,6 @@ #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, @@ -73,12 +51,6 @@ MockEndSessionDialog :: MockEndSessionDialog (GMainLoop * loop, { 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)); } diff --git a/tests/backend-dbus/mock-login1-manager.cc b/tests/backend-dbus/mock-login1-manager.cc index 3bd652b..4461125 100644 --- a/tests/backend-dbus/mock-login1-manager.cc +++ b/tests/backend-dbus/mock-login1-manager.cc @@ -26,99 +26,17 @@ namespace const char * const BUS_NAME = "org.freedesktop.login1"; const char * const BUS_PATH = "/org/freedesktop/login1"; - -#if 0 - void on_active_session_changed (Login1Seat * o G_GNUC_UNUSED, - const gchar * new_ssid, - gpointer ssid) - { - *static_cast(ssid) = new_ssid; - } -#endif -} - -/*** -**** -***/ - -#if 0 -gboolean -MockLogin1Manager :: on_get_current_session (Login1Manager * m, - GDBusMethodInvocation * inv, - gpointer gself) -{ - MockLogin1Manager * 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 -MockLogin1Manager :: on_get_seats (Login1Manager * 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 -MockLogin1Manager :: handle_restart (Login1Manager * ckm, - GDBusMethodInvocation * inv, - gpointer gself) -{ - static_cast(gself)->my_last_action = Restart; - console_kit_manager_complete_restart (ckm, inv); - return true; } -gboolean -MockLogin1Manager :: handle_stop (Login1Manager * ckm, - GDBusMethodInvocation * inv, - gpointer gself) -{ - static_cast(gself)->my_last_action = Shutdown; - console_kit_manager_complete_stop (ckm, inv); - return true; -} -#endif - /*** **** ***/ -#if 0 -MockLogin1Session * -MockLogin1Manager :: current_session () -{ - MockLogin1Session * 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; -} -#endif - void MockLogin1Manager :: emit_session_new (MockLogin1Seat * seat, int tag) const { std::string id; std::string path; - seat->get_session_id_and_path_for_tag (tag, id, path); login1_manager_emit_session_new (my_skeleton, id.c_str(), path.c_str()); @@ -139,7 +57,6 @@ MockLogin1Manager :: emit_session_removed (MockLogin1Seat * seat, int tag) const { std::string id; std::string path; - seat->get_session_id_and_path_for_tag (tag, id, path); login1_manager_emit_session_removed (my_skeleton, id.c_str(), path.c_str()); @@ -188,9 +105,7 @@ MockLogin1Manager :: list_sessions () const } } - GVariant * v = g_variant_builder_end (&b); - g_message ("%s %s returning %s", G_STRLOC, G_STRFUNC, g_variant_print (v, true)); - return v; + return g_variant_builder_end (&b); } /*** @@ -313,15 +228,6 @@ MockLogin1Manager :: MockLogin1Manager (GMainLoop * loop, g_signal_connect (my_skeleton, "handle-list-sessions", G_CALLBACK(handle_list_sessions), this); -#if 0 - 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); -#endif - set_skeleton (G_DBUS_INTERFACE_SKELETON(my_skeleton)); } diff --git a/tests/backend-dbus/mock-login1-seat.cc b/tests/backend-dbus/mock-login1-seat.cc index 46dc5f9..49d7fb6 100644 --- a/tests/backend-dbus/mock-login1-seat.cc +++ b/tests/backend-dbus/mock-login1-seat.cc @@ -43,7 +43,9 @@ namespace } void -MockLogin1Seat :: get_session_id_and_path_for_tag (int tag, std::string& id, std::string& path) +MockLogin1Seat :: get_session_id_and_path_for_tag (int tag, + std::string & id, + std::string & path) { if (tag) { @@ -81,7 +83,6 @@ MockLogin1Seat :: update_sessions_property () } GVariant * v = g_variant_builder_end (&b); - g_message ("%s %s setting session property to %s", G_STRLOC, G_STRFUNC, g_variant_print (v, true)); g_object_set (my_skeleton, "sessions", v, NULL); } @@ -93,7 +94,6 @@ MockLogin1Seat :: update_active_session_property () get_session_id_and_path_for_tag (my_active_session, id, path); GVariant * v = g_variant_new ("(so)", id.c_str(), path.c_str()); - g_message ("%s %s setting active session property to %s", G_STRLOC, G_STRFUNC, g_variant_print (v, true)); g_object_set (my_skeleton, "active-session", v, NULL); } diff --git a/tests/backend-dbus/mock-user.cc b/tests/backend-dbus/mock-user.cc index abf2e21..f74b5a4 100644 --- a/tests/backend-dbus/mock-user.cc +++ b/tests/backend-dbus/mock-user.cc @@ -54,14 +54,6 @@ 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) { diff --git a/tests/backend-dbus/mock-webcredentials.cc b/tests/backend-dbus/mock-webcredentials.cc index 44fa8ac..22e10b7 100644 --- a/tests/backend-dbus/mock-webcredentials.cc +++ b/tests/backend-dbus/mock-webcredentials.cc @@ -30,21 +30,6 @@ MockWebcredentials :: MockWebcredentials (GMainLoop * loop, 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)); } -- cgit v1.2.3 From 4850b1b6ba8a1438d8c0903592108fcefc35235e Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Fri, 28 Jun 2013 10:31:51 -0500 Subject: update comments & documentation to reflect that we've replaced consolekit and login1 --- tests/backend-dbus/test-actions.cc | 5 +---- tests/backend-mock-actions.h | 3 +-- tests/backend-mock-guest.h | 3 +-- tests/backend-mock-users.h | 3 +-- 4 files changed, 4 insertions(+), 10 deletions(-) (limited to 'tests') diff --git a/tests/backend-dbus/test-actions.cc b/tests/backend-dbus/test-actions.cc index 410a02a..20e2521 100644 --- a/tests/backend-dbus/test-actions.cc +++ b/tests/backend-dbus/test-actions.cc @@ -319,12 +319,9 @@ TEST_F (Actions, SwitchToGuest) { // allow guests dm_seat->set_guest_allowed (true); - MockUser * guest_user; - //int guest_session_tag; -// MockConsoleKitSession * guest_ck_session; // set up a guest - guest_user = new MockUser (loop, conn, "guest-zzbEVV", "Guest", 10); + MockUser * guest_user = new MockUser (loop, conn, "guest-zzbEVV", "Guest", 10); guest_user->set_system_account (true); accounts->add_user (guest_user); int guest_session_tag = login1_manager->add_session (login1_seat, guest_user); diff --git a/tests/backend-mock-actions.h b/tests/backend-mock-actions.h index bd9ed47..96c51be 100644 --- a/tests/backend-mock-actions.h +++ b/tests/backend-mock-actions.h @@ -37,8 +37,7 @@ typedef struct _IndicatorSessionActionsMockPriv IndicatorSessionActionsMockPr typedef struct _IndicatorSessionActionsMockClass IndicatorSessionActionsMockClass; /** - * An implementation of IndicatorSessionActions that gets its user information - * from org.freedesktop.ConsoleKit and org.freedesktop.Accounts over DBus. + * An implementation of IndicatorSessionActions that lies about everything. */ struct _IndicatorSessionActionsMock { diff --git a/tests/backend-mock-guest.h b/tests/backend-mock-guest.h index db6ce86..4a15c70 100644 --- a/tests/backend-mock-guest.h +++ b/tests/backend-mock-guest.h @@ -37,8 +37,7 @@ typedef struct _IndicatorSessionGuestMockPriv IndicatorSessionGuestMockPriv; typedef struct _IndicatorSessionGuestMockClass IndicatorSessionGuestMockClass; /** - * An implementation of IndicatorSessionGuest that gets its user information - * from org.freedesktop.ConsoleKit and org.freedesktop.Accounts over DBus. + * An implementation of IndicatorSessionGuest that lies about everything. */ struct _IndicatorSessionGuestMock { diff --git a/tests/backend-mock-users.h b/tests/backend-mock-users.h index 7470ec9..513e799 100644 --- a/tests/backend-mock-users.h +++ b/tests/backend-mock-users.h @@ -37,8 +37,7 @@ typedef struct _IndicatorSessionUsersMockPriv IndicatorSessionUsersMockPriv; typedef struct _IndicatorSessionUsersMockClass IndicatorSessionUsersMockClass; /** - * An implementation of IndicatorSessionUsers that gets its user information - * from org.freedesktop.ConsoleKit and org.freedesktop.Accounts over DBus. + * An implementation of IndicatorSessionUsers that lies about everything. */ struct _IndicatorSessionUsersMock { -- cgit v1.2.3 From 3222a77d7159826d89743685d35710597f269e00 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Fri, 28 Jun 2013 21:15:15 -0500 Subject: fix test breakage introduced with the recent commit that hides the reboot menuitem when running in unity shell --- tests/backend-mock-guest.c | 34 +++++++++++++++++++--------------- tests/backend-mock-users.c | 42 +++++++++++++++++++++++++----------------- tests/test-service.cc | 25 ++++++++++++++++++++----- 3 files changed, 64 insertions(+), 37 deletions(-) (limited to 'tests') diff --git a/tests/backend-mock-guest.c b/tests/backend-mock-guest.c index b04c805..8bc188f 100644 --- a/tests/backend-mock-guest.c +++ b/tests/backend-mock-guest.c @@ -34,21 +34,9 @@ G_DEFINE_TYPE (IndicatorSessionGuestMock, indicator_session_guest_mock, INDICATOR_TYPE_SESSION_GUEST) -/*** -**** Virtual Functions -***/ - -static void -my_dispose (GObject * o) -{ - G_OBJECT_CLASS (indicator_session_guest_mock_parent_class)->dispose (o); -} - -static void -my_finalize (GObject * o) -{ - G_OBJECT_CLASS (indicator_session_guest_mock_parent_class)->finalize (o); -} +/** +*** IndicatorSessionGuest virtual functions +**/ static gboolean my_is_allowed (IndicatorSessionGuest * self) @@ -76,6 +64,22 @@ my_switch_to_guest (IndicatorSessionGuest * self G_GNUC_UNUSED) g_message ("%s %s FIXME", G_STRLOC, G_STRFUNC); } +/*** +**** GObject virtual Functions +***/ + +static void +my_dispose (GObject * o) +{ + G_OBJECT_CLASS (indicator_session_guest_mock_parent_class)->dispose (o); +} + +static void +my_finalize (GObject * o) +{ + G_OBJECT_CLASS (indicator_session_guest_mock_parent_class)->finalize (o); +} + /*** **** GObject Boilerplate ***/ diff --git a/tests/backend-mock-users.c b/tests/backend-mock-users.c index bae40b6..f2291cd 100644 --- a/tests/backend-mock-users.c +++ b/tests/backend-mock-users.c @@ -32,25 +32,9 @@ G_DEFINE_TYPE (IndicatorSessionUsersMock, INDICATOR_TYPE_SESSION_USERS) /*** -**** +**** IndicatorSessionUsers virtual functions ***/ -static void -my_dispose (GObject * o) -{ - G_OBJECT_CLASS (indicator_session_users_mock_parent_class)->dispose (o); -} - -static void -my_finalize (GObject * o) -{ - priv_t * p = INDICATOR_SESSION_USERS_MOCK (o)->priv; - - g_hash_table_destroy (p->users); - - G_OBJECT_CLASS (indicator_session_users_mock_parent_class)->finalize (o); -} - static gboolean my_is_live_session (IndicatorSessionUsers * users G_GNUC_UNUSED) { @@ -96,6 +80,30 @@ my_get_user (IndicatorSessionUsers * self, guint uid) return ret; } +/*** +**** GObject virtual functions +***/ + +static void +my_dispose (GObject * o) +{ + G_OBJECT_CLASS (indicator_session_users_mock_parent_class)->dispose (o); +} + +static void +my_finalize (GObject * o) +{ + priv_t * p = INDICATOR_SESSION_USERS_MOCK (o)->priv; + + g_hash_table_destroy (p->users); + + G_OBJECT_CLASS (indicator_session_users_mock_parent_class)->finalize (o); +} + +/*** +**** GObject boilerplate +***/ + static void /* cppcheck-suppress unusedFunction */ indicator_session_users_mock_class_init (IndicatorSessionUsersMockClass * klass) diff --git a/tests/test-service.cc b/tests/test-service.cc index 996332d..86f49e6 100644 --- a/tests/test-service.cc +++ b/tests/test-service.cc @@ -285,6 +285,18 @@ class ServiceTest: public GTestDBusFixture return success; } + bool action_menuitem_exists (const char * action_name) + { + bool found; + GMenuModel * model = 0; + int pos = -1; + + if ((found = find_menu_item_for_action (action_name, &model, &pos))) + g_object_unref (G_OBJECT(model)); + + return found; + } + bool action_menuitem_label_is_ellipsized (const char * action_name) { int pos = -1; @@ -477,7 +489,8 @@ TEST_F (ServiceTest, ConfirmationDisabledByBackend) // confirm that the ellipsis are correct ASSERT_EQ (confirm, action_menuitem_label_is_ellipsized ("indicator.switch-to-greeter")); ASSERT_EQ (confirm, action_menuitem_label_is_ellipsized ("indicator.logout")); - ASSERT_EQ (confirm, action_menuitem_label_is_ellipsized ("indicator.reboot")); + if (action_menuitem_exists ("indicator.reboot")) + ASSERT_EQ (confirm, action_menuitem_label_is_ellipsized ("indicator.reboot")); ASSERT_EQ (confirm, action_menuitem_label_is_ellipsized ("indicator.power-off")); // now toggle the can-prompt flag @@ -490,7 +503,8 @@ TEST_F (ServiceTest, ConfirmationDisabledByBackend) // confirm that the ellipsis are correct ASSERT_EQ (confirm, action_menuitem_label_is_ellipsized ("indicator.switch-to-greeter")); ASSERT_EQ (confirm, action_menuitem_label_is_ellipsized ("indicator.logout")); - ASSERT_EQ (confirm, action_menuitem_label_is_ellipsized ("indicator.reboot")); + if (action_menuitem_exists ("indicator.reboot")) + ASSERT_EQ (confirm, action_menuitem_label_is_ellipsized ("indicator.reboot")); ASSERT_EQ (confirm, action_menuitem_label_is_ellipsized ("indicator.power-off")); // cleanup @@ -509,7 +523,8 @@ TEST_F (ServiceTest, ConfirmationDisabledByUser) // confirm that the ellipsis are correct ASSERT_EQ (confirm, action_menuitem_label_is_ellipsized ("indicator.switch-to-greeter")); ASSERT_EQ (confirm, action_menuitem_label_is_ellipsized ("indicator.logout")); - ASSERT_EQ (confirm, action_menuitem_label_is_ellipsized ("indicator.reboot")); + if (action_menuitem_exists ("indicator.reboot")) + ASSERT_EQ (confirm, action_menuitem_label_is_ellipsized ("indicator.reboot")); ASSERT_EQ (confirm, action_menuitem_label_is_ellipsized ("indicator.power-off")); // now toggle the can-prompt flag @@ -522,7 +537,8 @@ TEST_F (ServiceTest, ConfirmationDisabledByUser) // confirm that the ellipsis are correct ASSERT_EQ (confirm, action_menuitem_label_is_ellipsized ("indicator.switch-to-greeter")); ASSERT_EQ (confirm, action_menuitem_label_is_ellipsized ("indicator.logout")); - ASSERT_EQ (confirm, action_menuitem_label_is_ellipsized ("indicator.reboot")); + if (action_menuitem_exists ("indicator.reboot")) + ASSERT_EQ (confirm, action_menuitem_label_is_ellipsized ("indicator.reboot")); ASSERT_EQ (confirm, action_menuitem_label_is_ellipsized ("indicator.power-off")); // cleanup @@ -542,7 +558,6 @@ TEST_F (ServiceTest, DefaultMenuItems) ASSERT_TRUE (find_menu_item_for_action ("indicator.logout", NULL, NULL)); ASSERT_TRUE (find_menu_item_for_action ("indicator.suspend", NULL, NULL)); ASSERT_TRUE (find_menu_item_for_action ("indicator.hibernate", NULL, NULL)); - ASSERT_TRUE (find_menu_item_for_action ("indicator.reboot", NULL, NULL)); ASSERT_TRUE (find_menu_item_for_action ("indicator.power-off", NULL, NULL)); } -- cgit v1.2.3 From 0c8526d6686623f1d77231dd43a61775e3b6aadf Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Fri, 28 Jun 2013 23:00:27 -0500 Subject: in test-users, fix timing issue in the fixture's SetUp --- tests/backend-dbus/test-users.cc | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'tests') diff --git a/tests/backend-dbus/test-users.cc b/tests/backend-dbus/test-users.cc index b022984..dccacbb 100644 --- a/tests/backend-dbus/test-users.cc +++ b/tests/backend-dbus/test-users.cc @@ -48,7 +48,10 @@ class Users: public GTestMockDBusFixture users = 0; backend_get (cancellable, NULL, &users, NULL); g_assert (users != 0); - wait_msec (100); + + // wait for the users added by GTestMockDBusFixture::SetUp() to show up + wait_for_signals (users, INDICATOR_SESSION_USERS_SIGNAL_USER_ADDED, 12); + init_event_keys (0); } virtual void TearDown () -- cgit v1.2.3 From 34b4d992f042293af1db641e66bef5620576d1ba Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Fri, 28 Jun 2013 23:17:22 -0500 Subject: in test-users, possible fix to async dbus action timing issue in SetUp --- tests/backend-dbus/test-actions.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'tests') diff --git a/tests/backend-dbus/test-actions.cc b/tests/backend-dbus/test-actions.cc index 20e2521..a7e7a5f 100644 --- a/tests/backend-dbus/test-actions.cc +++ b/tests/backend-dbus/test-actions.cc @@ -46,7 +46,7 @@ class Actions: public GTestMockDBusFixture actions = 0; backend_get (cancellable, &actions, NULL, NULL); g_assert (actions != 0); - wait_msec (100); + wait_msec (300); } virtual void TearDown () -- cgit v1.2.3 From 98d1379784757b63e317bcd9ac7af69836e473d9 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Fri, 12 Jul 2013 02:03:13 -0500 Subject: Add a 'can-reboot' property to the Actions class. This is used for handling a couple of pathological cases where features and states mix and match: 1. unity has the same dialog for 'reboot' and 'power off', so remove the duplicate menuitem, EXCEPT: 2. if the unity prompt isn't available (such as in the greeter), show both menuitems, OR 3. if the user has prompting disabled we need both, OR 4. if the user has the 'power off' button disabled, don't treat 'reboot' as redundant. --- tests/backend-mock-actions.c | 7 +++++++ tests/com.canonical.indicator.session.backendmock.gschema.xml | 4 ++++ 2 files changed, 11 insertions(+) (limited to 'tests') diff --git a/tests/backend-mock-actions.c b/tests/backend-mock-actions.c index d5a506b..af4afd9 100644 --- a/tests/backend-mock-actions.c +++ b/tests/backend-mock-actions.c @@ -43,6 +43,12 @@ my_can_logout (IndicatorSessionActions * self G_GNUC_UNUSED) return g_settings_get_boolean (mock_settings, "can-logout"); } +static gboolean +my_can_reboot (IndicatorSessionActions * self G_GNUC_UNUSED) +{ + return g_settings_get_boolean (mock_settings, "can-reboot"); +} + static gboolean my_can_switch (IndicatorSessionActions * self G_GNUC_UNUSED) { @@ -177,6 +183,7 @@ indicator_session_actions_mock_class_init (IndicatorSessionActionsMockClass * kl actions_class = INDICATOR_SESSION_ACTIONS_CLASS (klass); actions_class->can_lock = my_can_lock; actions_class->can_logout = my_can_logout; + actions_class->can_reboot = my_can_reboot; actions_class->can_switch = my_can_switch; actions_class->can_suspend = my_can_suspend; actions_class->can_hibernate = my_can_hibernate; diff --git a/tests/com.canonical.indicator.session.backendmock.gschema.xml b/tests/com.canonical.indicator.session.backendmock.gschema.xml index 34479df..79d0c02 100644 --- a/tests/com.canonical.indicator.session.backendmock.gschema.xml +++ b/tests/com.canonical.indicator.session.backendmock.gschema.xml @@ -21,6 +21,10 @@ true Is logging out allowed? + + true + Is rebooting allowed? + true Is locking the session allowed? -- cgit v1.2.3 From c9352cd0a8363e4508d19e66ccf373b2abb2b7d3 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Fri, 12 Jul 2013 02:05:35 -0500 Subject: If the Unity dialogs aren't available (such as in the greeter) but zenity is, use it to for confirmation dialogs: - add the implementation in actions.c - add actions.c to the i18n files list - sync the unit tests - suggest zenity in debian/control --- tests/backend-dbus/test-actions.cc | 74 +++++++++++++++++++++++++------------- 1 file changed, 49 insertions(+), 25 deletions(-) (limited to 'tests') diff --git a/tests/backend-dbus/test-actions.cc b/tests/backend-dbus/test-actions.cc index a7e7a5f..c0f8517 100644 --- a/tests/backend-dbus/test-actions.cc +++ b/tests/backend-dbus/test-actions.cc @@ -22,6 +22,8 @@ #include "backend.h" #include "backend-dbus/backend-dbus.h" +#define SUPPRESS_KEY "suppress-logout-restart-shutdown" + /*** **** ***/ @@ -36,12 +38,14 @@ class Actions: public GTestMockDBusFixture GCancellable * cancellable; IndicatorSessionActions * actions; + GSettings * indicator_settings; virtual void SetUp () { super :: SetUp (); // init 'actions' + indicator_settings = g_settings_new ("com.canonical.indicator.session"); cancellable = g_cancellable_new (); actions = 0; backend_get (cancellable, &actions, NULL, NULL); @@ -52,6 +56,7 @@ class Actions: public GTestMockDBusFixture virtual void TearDown () { g_cancellable_cancel (cancellable); + g_clear_object (&indicator_settings); g_clear_object (&cancellable); g_clear_object (&actions); @@ -187,6 +192,7 @@ TEST_F (Actions, CanHibernate) TEST_F (Actions, Reboot) { ASSERT_TRUE (login1_manager->last_action().empty()); + ASSERT_FALSE (g_settings_get_boolean (indicator_settings, SUPPRESS_KEY)); // confirm that user is prompted // and that no action is taken when the user cancels the dialog @@ -207,15 +213,18 @@ TEST_F (Actions, Reboot) ASSERT_EQ (login1_manager->last_action(), "reboot"); // confirm that we try to reboot w/o prompting - // if the EndSessionDialog isn't available - delete end_session_dialog; - end_session_dialog = 0; + // if prompting is disabled login1_manager->clear_last_action (); + ASSERT_EQ ("", login1_manager->last_action()); + g_settings_set_boolean (indicator_settings, SUPPRESS_KEY, TRUE); + wait_msec (50); ASSERT_TRUE (login1_manager->last_action().empty()); wait_msec (50); indicator_session_actions_reboot (actions); wait_msec (50); - ASSERT_EQ (login1_manager->last_action(), "reboot"); + ASSERT_EQ ("reboot", login1_manager->last_action()); + + g_settings_reset (indicator_settings, SUPPRESS_KEY); } TEST_F (Actions, PowerOff) @@ -242,13 +251,16 @@ TEST_F (Actions, PowerOff) // confirm that we try to shutdown w/o prompting // if the EndSessionDialog isn't available - delete end_session_dialog; - end_session_dialog = 0; + // if prompting is disabled login1_manager->clear_last_action (); + ASSERT_EQ ("", login1_manager->last_action()); + g_settings_set_boolean (indicator_settings, SUPPRESS_KEY, TRUE); wait_msec (50); indicator_session_actions_power_off (actions); wait_msec (50); ASSERT_EQ (login1_manager->last_action(), "power-off"); + + g_settings_reset (indicator_settings, SUPPRESS_KEY); } TEST_F (Actions, Logout) @@ -273,14 +285,17 @@ TEST_F (Actions, 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; + // confirm that we try to call SessionManager::LogoutQuet + // when prompts are disabled + login1_manager->clear_last_action (); + ASSERT_EQ ("", login1_manager->last_action()); + g_settings_set_boolean (indicator_settings, SUPPRESS_KEY, TRUE); wait_msec (50); indicator_session_actions_logout (actions); wait_msec (50); - ASSERT_EQ (MockSessionManager::LogoutNormal, session_manager->last_action ()); + ASSERT_EQ (MockSessionManager::LogoutQuiet, session_manager->last_action ()); + + g_settings_reset (indicator_settings, SUPPRESS_KEY); } TEST_F (Actions, Suspend) @@ -389,22 +404,31 @@ TEST_F (Actions, HasOnlineAccountError) ASSERT_EQ (b, gb); } -TEST_F (Actions, CanPrompt) +namespace { - gboolean b; + static gboolean toggle_suppress (gpointer settings) + { + const char * key = SUPPRESS_KEY; + gboolean b = g_settings_get_boolean (G_SETTINGS(settings), key); + g_settings_set_boolean (G_SETTINGS(settings), key, !b); + return G_SOURCE_REMOVE; + } +} - ASSERT_TRUE (indicator_session_actions_can_prompt (actions)); +TEST_F (Actions, SuppressPrompts) +{ + for (int i=0; i<3; ++i) + { + bool b; + gboolean b2; - 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); + b = indicator_session_actions_can_prompt (actions); + b2 = !g_settings_get_boolean (indicator_settings, SUPPRESS_KEY); + ASSERT_EQ (b, b2); + + g_idle_add (toggle_suppress, indicator_settings); + wait_for_signal (actions, "notify::" INDICATOR_SESSION_ACTIONS_PROP_CAN_PROMPT); + } - 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); + g_settings_reset (indicator_settings, SUPPRESS_KEY); } -- cgit v1.2.3 From a19955f3f81b1a5eb8fc928bc5dcf8a24bb6833f Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Fri, 12 Jul 2013 07:42:07 -0500 Subject: add the online-accounts action and unit tests for it --- tests/backend-mock-actions.c | 7 +++++++ tests/test-service.cc | 9 +++++++++ 2 files changed, 16 insertions(+) (limited to 'tests') diff --git a/tests/backend-mock-actions.c b/tests/backend-mock-actions.c index af4afd9..25a606f 100644 --- a/tests/backend-mock-actions.c +++ b/tests/backend-mock-actions.c @@ -141,6 +141,12 @@ my_settings (IndicatorSessionActions * self G_GNUC_UNUSED) g_settings_set_string (mock_settings, "last-command", "settings"); } +static void +my_online_accounts (IndicatorSessionActions * self G_GNUC_UNUSED) +{ + g_settings_set_string (mock_settings, "last-command", "online-accounts"); +} + static gboolean my_can_prompt (IndicatorSessionActions * self G_GNUC_UNUSED) { @@ -195,6 +201,7 @@ indicator_session_actions_mock_class_init (IndicatorSessionActionsMockClass * kl actions_class->reboot = my_reboot; actions_class->power_off = my_power_off; actions_class->settings = my_settings; + actions_class->online_accounts = my_online_accounts; actions_class->help = my_help; actions_class->about = my_about; actions_class->switch_to_screensaver = my_switch_to_screensaver; diff --git a/tests/test-service.cc b/tests/test-service.cc index 86f49e6..2027ec0 100644 --- a/tests/test-service.cc +++ b/tests/test-service.cc @@ -589,6 +589,15 @@ TEST_F (ServiceTest, OnlineAccountError) ASSERT_TRUE (find_menu_item_for_action ("indicator.online-accounts", &model, &pos)); g_clear_object (&model); + // check that the service has a corresponding action + ASSERT_TRUE (g_action_group_has_action (G_ACTION_GROUP(action_group), "online-accounts")); + ASSERT_TRUE (g_action_group_get_action_enabled (G_ACTION_GROUP(action_group), "online-accounts")); + + // confirm that activating the action is handled by the service + g_action_group_activate_action (G_ACTION_GROUP(action_group), "online-accounts", NULL); + wait_for_signal (mock_settings, "changed::last-command"); + check_last_command_is ("online-accounts"); + // check that the header's icon and a11y adjusted to the error state check_header ("", "system-devices-panel-alert", "System (Attention Required)"); -- cgit v1.2.3