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/mock-login1-seat.cc | 230 +++++++++++++++++++++++++++++++++ 1 file changed, 230 insertions(+) create mode 100644 tests/backend-dbus/mock-login1-seat.cc (limited to 'tests/backend-dbus/mock-login1-seat.cc') 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); +} -- 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/mock-login1-seat.cc | 52 ++++++++++++++++++++++++++-------- 1 file changed, 40 insertions(+), 12 deletions(-) (limited to 'tests/backend-dbus/mock-login1-seat.cc') 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)); -- 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-login1-seat.cc | 22 ++++------------------ 1 file changed, 4 insertions(+), 18 deletions(-) (limited to 'tests/backend-dbus/mock-login1-seat.cc') 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 -- 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-login1-seat.cc | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'tests/backend-dbus/mock-login1-seat.cc') 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); } -- cgit v1.2.3